电科院-电能替代模型工具开发
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.8 KiB

1 year ago
package com.dky.tool;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dky.modelI.DntdModelI;
import com.dky.security.GetCpuInfo;
import com.dky.utils.ConfigReader;
import com.dky.utils.entity.SysDeviceHeatScene;
1 year ago
import com.dky.utils.enums.Code;
import com.dky.utils.result.ResponseUtil;
import org.bouncycastle.math.raw.Mod;
1 year ago
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
1 year ago
public class ModelTool {
public List<SysDeviceHeatScene> specList;
1 year ago
// 私有化构造方法,避免外部实例化
private ModelTool() {
}
public ModelTool(List<SysDeviceHeatScene> specList) {
this.specList = specList;
}
// 创建工具类的静态工厂方法
public static ModelTool create(JSONObject list) {
List<SysDeviceHeatScene> specList = new ArrayList<>();
1 year ago
JSONArray specArray = list.getJSONArray("devSpecList");
for (int i = 0; i < specArray.size(); i++) {
SysDeviceHeatScene devSpec = specArray.getJSONObject(i).toBean(SysDeviceHeatScene.class);
1 year ago
specList.add(devSpec);
}
return new ModelTool(specList);
1 year ago
}
public JSONObject exeModel2Report(JSONObject jsonObject, String key) {
1 year ago
JSONObject jsonObjectResult = new JSONObject();
try {
// 根据给定的类名初始化类 加密不需要反实例化new
Class sm4UtilsClass = Class.forName("com.dky.security.SM4Utils");
// 实例化这个类
Object obj = sm4UtilsClass.newInstance();
// 调用指定方法
Map map = (Map) sm4UtilsClass.getMethod("decrypt", String.class).invoke(obj, key);
String cpuIds = (String) map.get("cpuIds");
String expireTime = (String) map.get("expireTime");
// 定义日期时间格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = formatter.parse(expireTime);
//拿到解析后的cpuid。获取本地的cpuid.
String localCpuId = GetCpuInfo.getCpuId();
if (cpuIds.contains(localCpuId)){
if (new Date().before(date)){
// 判断模型使用权限
1 year ago
// 根据给定的类名初始化类 加密不需要反实例化new
Class buildHeatingScene = Class.forName("com.dky"+"."+ ConfigReader.getProperty(jsonObject.getStr("type")));
1 year ago
// 实例化这个类
DntdModelI buildHeatingSceneModel = (DntdModelI)buildHeatingScene.newInstance();
1 year ago
// 调用指定方法
jsonObjectResult = buildHeatingSceneModel.createReport(jsonObject,this.specList);
1 year ago
}else {
jsonObjectResult = ResponseUtil.createResponse(Code.KEY_EXPIRATION.getCode(), Code.KEY_EXPIRATION.getDesc(), null);
}
}else {
jsonObjectResult = ResponseUtil.createResponse(Code.KEY_UNAUTHORIZED.getCode(), Code.KEY_UNAUTHORIZED.getDesc(), null);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
jsonObjectResult = ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null);
1 year ago
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
}
return jsonObjectResult;
}
}