Bläddra i källkod

添加堵口预测模型

wangxiaofei 1 dag sedan
förälder
incheckning
ec4df0b155

+ 28 - 0
taphole-iron/src/main/java/com/sckj/iron/constant/ModelConstants.java

@@ -0,0 +1,28 @@
+package com.sckj.iron.constant;
+
+/***
+ * 定时任务延迟和定时常量
+ * 单位:s
+ */
+public class ModelConstants {
+    //hit_mud
+    public static final String hit_mud = "hit_mud";
+
+    //closure_warn1
+    public static final String closure_warn1 = "closure_warn1";
+
+    //closure_warn2
+    public static final String closure_warn2 = "closure_warn2";
+
+    //closure_warn3
+    public static final String closure_warn3 = "closure_warn3";
+
+    //tapping_test
+    public static final String tapping_test = "tapping_test";
+
+    public static final String tapping_warn = "tapping_warn";
+
+    public static final String closure_predict = "closure_predict";
+
+
+}

+ 2 - 23
taphole-iron/src/main/java/com/sckj/iron/constant/ParamsConstants.java

@@ -15,31 +15,10 @@ public class ParamsConstants {
     public static final String server_url = "server_url";
     //server_url
     public static final String ironwater_temp = "ironwater_temp";
-    public static final String open_hour = "open_hour";
-    public static final String iron_weight = "iron_weight";
-
-    //开口预警
-    public static final String open_warn = "open_warn";
-
-    //堵口预警
-    public static final String closure_warn = "closure_warn";
-
-    //出铁预警
-    public static final String tapping_warn = "tapping_warn";
 
-    //出铁超时报警
-    public static final String tapping_timeout_warn = "tapping_timeout_warn";
-
-    //打泥量计算
-    public static final String hit_mud = "hit_mud";
-
-    //出铁计时
-    public static final String tapping_consttime = "tapping_consttime";
+    public static final String open_hour = "open_hour";
 
-    //出铁诊断
-    public static final String tapping_test = "tapping_test";
+    public static final String iron_weight = "iron_weight";
 
-    //OPCDA订阅
-    public static final String opcdasubscribe = "opcdasubscribe";
 
 }

+ 1 - 2
taphole-iron/src/main/java/com/sckj/iron/constant/TaskNameConstants.java

@@ -10,7 +10,7 @@ package com.sckj.iron.constant;
  */
 public class TaskNameConstants {
     //开口预警
-//    public static final String TASKNAME_OPEN_WARN = "open_warn";
+    public static final String TASKNAME_OPEN_WARN = "open_warn";
     //堵口预警,出铁xxx分钟开始,出铁后开始计时
     public static final String TASKNAME_CLOSURE_WARN = "closure_warn";
     //出铁预警
@@ -27,7 +27,6 @@ public class TaskNameConstants {
     //L1订阅
     public static final String TASKNAME_OPCDASUBSCRIBE = "opcdasubscribe";
 
-    //
     public static final String TASKNAME_CLOSETIME = "closetime_suppose";
 
 }

+ 19 - 0
taphole-iron/src/main/java/com/sckj/iron/dto/ClosurePredictInfo.java

@@ -0,0 +1,19 @@
+package com.sckj.iron.dto;
+
+import lombok.Data;
+
+@Data
+public class ClosurePredictInfo {
+
+    private int predMin;
+
+    private int predMax;
+
+    //增加的分钟:
+    private int incrMinute;
+
+    //减少的分钟
+    private int decrMinute;
+
+    private int interval;
+}

+ 3 - 3
taphole-iron/src/main/java/com/sckj/iron/entity/TIronModel.java

@@ -36,13 +36,13 @@ public class TIronModel implements Serializable {
     @ApiModelProperty(value = "高炉编号")
     private String boilerId;
 
-    @ApiModelProperty(value = "参数名称")
+    @ApiModelProperty(value = "模型名称")
     private String modelName;
 
-    @ApiModelProperty(value = "参数值")
+    @ApiModelProperty(value = "模型表达式")
     private String modelExpression;
 
-    @ApiModelProperty(value = "参数说明")
+    @ApiModelProperty(value = "模型说明")
     private String modelDesc;
 
     @ApiModelProperty(value = "模型存储位置")

+ 34 - 1
taphole-iron/src/main/java/com/sckj/iron/service/impl/TIronModelServiceImpl.java

@@ -130,7 +130,7 @@ public class TIronModelServiceImpl extends ServiceImpl<TIronModelMapper, TIronMo
                         .eq("model_name", createValidate.getModelName())
                         .last("limit 1"));
 
-        Assert.notNull(repeatModel, "模型名称不能重复!");
+        Assert.isNull(repeatModel, "模型名称不能重复!");
 
         TIronModel model = new TIronModel();
         model.setCreateBy(String.valueOf(TapholeAdminThreadLocal.getAdminUsername()));
@@ -144,7 +144,40 @@ public class TIronModelServiceImpl extends ServiceImpl<TIronModelMapper, TIronMo
         if (ObjectUtils.isNotEmpty(path)) {
             String relativeUrl = UrlUtils.toRelativeUrl(path);
             model.setModelPath(relativeUrl);
+            String modelFilePath = YmlUtils.get("like.upload-directory") + relativeUrl;
+//            File file = new File(YmlUtils.get("like.upload-directory") + relativeUrl);
+            try {
+                Path filePath = Paths.get(modelFilePath);
+                boolean exists = Files.exists(filePath);
+                if (!exists) {
+                    log.error("{}路径不存在", modelFilePath);
+                    model.setModelPath(null);
+                    return;
+                }
+
+                List<String> lines = Files.readAllLines(filePath);
 
+                StringBuilder content = new StringBuilder();
+                for (String line : lines) {
+                    content.append(line);
+                }
+                log.info("读取的模型文件为:{}", content.toString());
+                //随机生成密码
+//                String generateRandomKey = AES.generateRandomKey();
+                //加密
+//                String encrypt = AES.encrypt(content.toString(), generateRandomKey);
+                //解密
+                TIronParam modelKey = ironParamService.lambdaQuery().eq(TIronParam::getParamName, "model_key").one();
+                String key = "bg@CN_9999_8888";
+                if (ObjectUtils.isNotEmpty(modelKey) && ObjectUtils.isNotEmpty(modelKey.getParamValue())) {
+                    key = modelKey.getParamValue();
+                }
+                String decrypt = AES.decrypt(content.toString(), key);
+                model.setModelExpression(decrypt);
+
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         } else {
             model.setModelPath(null);
         }

+ 74 - 65
taphole-iron/src/main/java/com/sckj/iron/socketio/DeviceEventListener.java

@@ -4,6 +4,7 @@ import com.corundumstudio.socketio.SocketIOClient;
 import com.corundumstudio.socketio.annotation.OnEvent;
 import com.google.common.eventbus.Subscribe;
 import com.google.common.util.concurrent.AtomicDouble;
+import com.google.gson.Gson;
 import com.sckj.common.config.GlobalConfig;
 import com.sckj.common.enums.RefreshItemEnum;
 import com.sckj.common.eventbus.EventListener;
@@ -12,18 +13,15 @@ import com.sckj.common.socketio.SocketUtil;
 import com.sckj.common.util.RedisUtils;
 import com.sckj.common.util.ToolUtils;
 import com.sckj.iron.constant.*;
-import com.sckj.iron.dto.IronStepDTO;
-import com.sckj.iron.dto.IronTimeNoDTO;
-import com.sckj.iron.dto.RealtimeData;
-import com.sckj.iron.dto.WarnData;
+import com.sckj.iron.dto.*;
 import com.sckj.iron.entity.*;
 import com.sckj.iron.service.impl.*;
 import com.sckj.iron.util.LocalDateUtils;
 import com.sckj.iron.vo.IronStepVO;
 import com.sckj.l2.entity.TL2Data;
-import com.sckj.l2.service.impl.L2DataServiceImpl;
 import com.sckj.l2.service.impl.TL2DataServiceImpl;
 import com.sckj.opc.dataservice.HDServiceImpl;
+import com.sckj.opc.dataservice.OPCDAServiceImpl;
 import com.sckj.opc.entity.OPCData;
 import com.sckj.opc.service.OPCDataServiceImpl;
 import com.sckj.opc.service.THdTagServiceImpl;
@@ -53,6 +51,7 @@ import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 
 /**
  * @Author feng
@@ -88,9 +87,6 @@ public class DeviceEventListener extends EventListener { //
     TL2DataServiceImpl tl2DataService;
 
     @Resource
-    L2DataServiceImpl l2DataServiceImpl;
-
-    @Resource
     OPCDataServiceImpl opcDataService;
 
     @Resource
@@ -119,6 +115,8 @@ public class DeviceEventListener extends EventListener { //
     @Resource
     TIronTestServiceImpl iTIronTestService;
 
+    @Resource
+    OPCDAServiceImpl opcuaService;
 
     //铁水成分
     private static final String IRON_ELEMENT = "ironElement";
@@ -258,7 +256,7 @@ public class DeviceEventListener extends EventListener { //
                 //程序启动后隔断时间启动订阅
                 scheduledTaskManager.addTask(scheduleOpcdasubscribe.getName(), scheduleOpcdasubscribe.getDelay(), scheduleOpcdasubscribe.getPeriod(), TimeUnit.SECONDS, () -> {
                     log.info("opcdaService subscribe available");
-//                    opcdaService.subscribeAvailable();
+                    opcuaService.subscribeAvailable();
                     scheduledTaskManager.cancelTask(TaskNameConstants.TASKNAME_OPCDASUBSCRIBE);
                 });
             }
@@ -280,17 +278,53 @@ public class DeviceEventListener extends EventListener { //
     //预警出铁模型
     private TIronModel modelTappingWarn;
 
+    private TIronModel modelClosurePredict;
+
+    private ClosurePredictInfo mClosurePredictInfo;
+
 
     /***
      * 更新模型
      */
     private void getModels() {
-        modelHitMud = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "hit_mud").one();
-        modelClosureWarn1 = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "closure_warn1").one();
-        modelClosureWarn2 = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "closure_warn2").one();
-        modelClosureWarn3 = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "closure_warn3").one();
-        modelTappingTest = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "tapping_test").one();
-        modelTappingWarn = ironModelService.lambdaQuery().eq(TIronModel::getModelName, "tapping_warn").one();
+        Map<String, TIronModel> modelMap = ironModelService.lambdaQuery().list().stream().collect(Collectors.toMap(
+                TIronModel::getModelName, // 键映射函数
+                model -> model, // 值映射函数
+                (existing, replacement) -> existing // 合并函数
+        ));
+        modelHitMud = modelMap.get(ModelConstants.hit_mud);
+        modelClosureWarn1 = modelMap.get(ModelConstants.closure_warn1);
+        modelClosureWarn2 = modelMap.get(ModelConstants.closure_warn2);
+        modelClosureWarn3 = modelMap.get(ModelConstants.closure_warn3);
+        modelTappingTest = modelMap.get(ModelConstants.tapping_test);
+        modelTappingWarn = modelMap.get(ModelConstants.tapping_warn);
+        modelClosurePredict = modelMap.get(ModelConstants.closure_predict);
+
+        if (null != modelClosurePredict && "1".equals(modelClosurePredict.getStatus())) {
+            String modelExpression = modelClosurePredict.getModelExpression();
+            if (null != modelExpression) {
+                mClosurePredictInfo = new Gson().fromJson(modelExpression, ClosurePredictInfo.class);
+                if (null != mClosurePredictInfo) {
+                    totalHitTime = new Random().nextInt(mClosurePredictInfo.getPredMax() - mClosurePredictInfo.getPredMin() + 1) + mClosurePredictInfo.getPredMin();
+                    scheduledTaskManager.cancelTask(modelClosurePredict.getModelName());
+                    scheduledTaskManager.addTask(modelClosurePredict.getModelName(), 0, mClosurePredictInfo.getInterval(), TimeUnit.SECONDS, () -> {
+                        if (mMaxSpeed != 0 && ironLoading1.get() > 0) {
+                            if (this.mLastSpeed == 0) {
+                                this.mLastSpeed = mMaxSpeed;
+                            } else if (mMaxSpeed > this.mLastSpeed) {
+                                mDiffCloseTime.set(mDiffCloseTime.get() + mClosurePredictInfo.getDecrMinute());
+                                this.mLastSpeed = mMaxSpeed;
+                            } else if (mMaxSpeed < this.mLastSpeed) {
+                                mDiffCloseTime.set(mDiffCloseTime.get() - mClosurePredictInfo.getIncrMinute());
+                                this.mLastSpeed = mMaxSpeed;
+                            }
+                            getCloseTime();
+                        }
+                    });
+                }
+            }
+        }
+
     }
 
     private void getAudios() {
@@ -362,37 +396,20 @@ public class DeviceEventListener extends EventListener { //
      * 定时任务
      */
     private void getSchedules() {
-        List<TIronSchedule> list = iTIronScheduleService.lambdaQuery().list();
-        if (ObjectUtils.isNotEmpty(list)) {
-            for (TIronSchedule ironSchedule : list) {
-                switch (ironSchedule.getName()) {
-                    case ParamsConstants.open_warn:
-                        scheduleOpenWarn = ironSchedule;
-                        break;
-                    case ParamsConstants.closure_warn:
-                        scheduleClosureWarn = ironSchedule;
-                        break;
-                    case ParamsConstants.tapping_warn:
-                        scheduleTappingWarn = ironSchedule;
-                        break;
-                    case ParamsConstants.tapping_timeout_warn:
-                        scheduleTappingTimeoutWarn = ironSchedule;
-                        break;
-                    case ParamsConstants.hit_mud:
-                        scheduleHitMud = ironSchedule;
-                        break;
-                    case ParamsConstants.tapping_consttime:
-                        scheduleTappingConsttime = ironSchedule;
-                        break;
-                    case ParamsConstants.tapping_test:
-                        scheduleTappingTest = ironSchedule;
-                        break;
-                    case ParamsConstants.opcdasubscribe:
-                        scheduleOpcdasubscribe = ironSchedule;
-                        break;
-                }
-            }
-        }
+
+        Map<String, TIronSchedule> modelMap = iTIronScheduleService.lambdaQuery().list().stream().collect(Collectors.toMap(
+                TIronSchedule::getName, // 键映射函数
+                schedule -> schedule, // 值映射函数
+                (existing, replacement) -> existing // 合并函数
+        ));
+        scheduleOpenWarn = modelMap.get(TaskNameConstants.TASKNAME_OPEN_WARN);
+        scheduleClosureWarn = modelMap.get(TaskNameConstants.TASKNAME_CLOSURE_WARN);
+        scheduleTappingWarn = modelMap.get(TaskNameConstants.TASKNAME_TAPPING_WARN);
+        scheduleTappingTimeoutWarn = modelMap.get(TaskNameConstants.TASKNAME_TAPPING_TIMEOUT_WARN);
+        scheduleHitMud = modelMap.get(TaskNameConstants.TASKNAME_HIT_MUD);
+        scheduleTappingConsttime = modelMap.get(TaskNameConstants.TASKNAME_TAPPING_CONSTTIME);
+        scheduleTappingTest = modelMap.get(TaskNameConstants.TASKNAME_TAPPING_TEST);
+        scheduleOpcdasubscribe = modelMap.get(TaskNameConstants.TASKNAME_OPCDASUBSCRIBE);
     }
 
 
@@ -428,6 +445,7 @@ public class DeviceEventListener extends EventListener { //
     }
 
     private double mLastSpeed;
+    private double mMaxSpeed;
 
 
     /***
@@ -526,12 +544,13 @@ public class DeviceEventListener extends EventListener { //
 
     }
 
+    private int totalHitTime;
     //代表要减去的时间,非剩余时间
     private AtomicInteger mDiffCloseTime = new AtomicInteger(0);
 
     private void getCloseTime() {
         RealtimeData realtimeData = new RealtimeData();
-        realtimeData.setValue(200 - getIronElapsedMinute() - mDiffCloseTime.get());
+        realtimeData.setValue(Math.max(totalHitTime - getIronElapsedMinute() - mDiffCloseTime.get(), 0));
         realtimeData.setDesc("距离堵口预计还剩");
         realtimeData.setUnit("分钟");
         PushData.send2CloseTime(realtimeData);
@@ -560,8 +579,6 @@ public class DeviceEventListener extends EventListener { //
             scheduledTaskManager.addTask(scheduleTappingConsttime.getName(), scheduleTappingConsttime.getDelay(), scheduleTappingConsttime.getPeriod(), TimeUnit.SECONDS, () -> {
                 getIronTime();
                 mSecondsElapsed.incrementAndGet();
-
-                getCloseTime();
             });
 
         }
@@ -692,6 +709,12 @@ public class DeviceEventListener extends EventListener { //
             ironDataService.saveOrUpdate(mTIronData);
         }
 
+        mLastSpeed = mMaxSpeed = 0;
+        mDiffCloseTime.set(0);
+        if (null != mClosurePredictInfo) {
+            totalHitTime = new Random().nextInt(mClosurePredictInfo.getPredMax() - mClosurePredictInfo.getPredMin() + 1) + mClosurePredictInfo.getPredMin();
+        }
+
         scheduledTaskManager.cancelTask(TaskNameConstants.TASKNAME_CLOSURE_WARN);
         scheduledTaskManager.cancelTask(TaskNameConstants.TASKNAME_HIT_MUD);
         scheduledTaskManager.cancelTask(TaskNameConstants.TASKNAME_TAPPING_CONSTTIME);
@@ -874,22 +897,8 @@ public class DeviceEventListener extends EventListener { //
                     speeds[1] = realtimeData;
                     speed2 = new AtomicDouble(Double.parseDouble(opcData.getData().toString()));
                 }
-                double maxSpeed = Math.max(speed1.get(), speed2.get());
-                mContext.setVariable(ExpressionConstants.rtIronSpeed, maxSpeed);
-
-                if (maxSpeed != 0 && ironLoading1.get() > 0) {
-                    if (this.mLastSpeed == 0) {
-                        this.mLastSpeed = maxSpeed;
-                    } else if (maxSpeed > this.mLastSpeed) {
-                        mDiffCloseTime.set(Math.max(mDiffCloseTime.get() + 1, 0));
-                        this.mLastSpeed = maxSpeed;
-                    } else if (maxSpeed < this.mLastSpeed) {
-                        mDiffCloseTime.set(Math.max(mDiffCloseTime.get() - 1, 0));
-                        this.mLastSpeed = maxSpeed;
-                    }
-                    getCloseTime();
-                }
-
+                mMaxSpeed = Math.max(speed1.get(), speed2.get());
+                mContext.setVariable(ExpressionConstants.rtIronSpeed, mMaxSpeed);
 
                 //只在两个都有数据的时候才添加
                 if (ObjectUtils.isNotEmpty(speeds)