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; import com.dky.utils.enums.Code; import com.dky.utils.result.ResponseUtil; import org.bouncycastle.math.raw.Mod; 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; public class ModelTool { public List specList; // 私有化构造方法,避免外部实例化 private ModelTool() { } public ModelTool(List specList) { this.specList = specList; } // 创建工具类的静态工厂方法 public static ModelTool create(JSONObject list) { List specList = new ArrayList<>(); JSONArray specArray = list.getJSONArray("devSpecList"); for (int i = 0; i < specArray.size(); i++) { SysDeviceHeatScene devSpec = specArray.getJSONObject(i).toBean(SysDeviceHeatScene.class); specList.add(devSpec); } return new ModelTool(specList); } public JSONObject exeModel2Report(JSONObject jsonObject, String key) { 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)){ // 判断模型使用权限 // 根据给定的类名初始化类 加密不需要反实例化new Class buildHeatingScene = Class.forName("com.dky"+"."+ ConfigReader.getProperty(jsonObject.getStr("type"))); // 实例化这个类 DntdModelI buildHeatingSceneModel = (DntdModelI)buildHeatingScene.newInstance(); // 调用指定方法 jsonObjectResult = buildHeatingSceneModel.createReport(jsonObject,this.specList); }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); e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (Exception e) { throw new RuntimeException(e); } return jsonObjectResult; } }