Ver código fonte

报警定义唯一标识增加匹配规则ID

lgy 4 anos atrás
pai
commit
2474a3078b

+ 2 - 2
src/main/java/com/persagy/cache/AlarmInfoCache.java

@@ -251,7 +251,7 @@ public class AlarmInfoCache {
     }
     /**
      * @description:获取报警定义id,
-     * 中台报警定义ID没有实际意义,根据itemCode-objId拼接作为唯一标识
+     * 中台报警定义ID没有实际意义,根据itemCode-objId-targetId拼接作为唯一标识
      * @exception:
      * @author: LuoGuangyi
      * @company: Persagy Technology Co.,Ltd
@@ -260,7 +260,7 @@ public class AlarmInfoCache {
      * @version: V1.0
      */
     public static String getAlarmDefineId(AlarmDefine alarmDefine) {
-        return alarmDefine.getItemCode()+"-"+alarmDefine.getObjId();
+        return alarmDefine.getItemCode()+"-"+alarmDefine.getObjId()+"-"+alarmDefine.getTargetId();
     }
 
     /**

+ 3 - 2
src/main/java/com/persagy/client/GroupNettyClientHandler.java

@@ -131,7 +131,7 @@ public class GroupNettyClientHandler extends ChannelInboundHandlerAdapter {
                         LockUtil.getInstance().lock.lock();
                         LockUtil.getInstance().setExecute(false);
                         //加个等待,保证正在执行的逻辑执行成功
-                        Thread.sleep(40000);
+                        Thread.sleep(4000);
                         alarmDefineService.listAllAlarmDefine(definesList);
                         LockUtil.getInstance().setExecute(true);
                         LockUtil.getInstance().condition.signalAll();
@@ -161,11 +161,12 @@ public class GroupNettyClientHandler extends ChannelInboundHandlerAdapter {
                 List content = message.getContent();
                 if (CollectionUtil.isNotEmpty(content)) {
                     JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(content.get(0)));
-                    String defineId = parseObject.getString("itemCode") + "-" + parseObject.getString("objId");
+                    String defineId = parseObject.getString("itemCode") + "-" + parseObject.getString("objId") + "-" + parseObject.getString("targetId");
                     ZktAlarmRecordDO zktAlarmRecordDO = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO());
                     zktAlarmRecordDO.setDefinitionId(defineId);
                     zktAlarmRecordDO.setItemCode(parseObject.getString("itemCode"));
                     zktAlarmRecordDO.setObjId(parseObject.getString("objId"));
+                    zktAlarmRecordDO.setTargetId(parseObject.getString("targetId"));
                     zktAlarmRecordDO.setAlarmId(parseObject.getString("id"));
                     alarmRecordRepository.save(zktAlarmRecordDO);
                 }

+ 1 - 1
src/main/java/com/persagy/client/WebSocketClientFactory.java

@@ -120,7 +120,7 @@ public class WebSocketClientFactory {
                         log.error("解密失败!", e);
                     }
                 }
-//                log.info("接收信息为:{}", message);
+                log.info("接收信息为:{}", message);
                 try {
                     while (!LockUtil.getInstance().isExecute()){
                         try {

+ 2 - 1
src/main/java/com/persagy/controller/HelloWorld.java

@@ -82,11 +82,12 @@ public class HelloWorld {
 
 	@PostMapping("/test3")
 	public ArrayList<ZktAlarmRecordDO> test3(@RequestBody JSONObject jsonObject) throws Exception {
-		String defineId = jsonObject.getString("itemCode") + "-" + jsonObject.getString("objId");
+		String defineId = jsonObject.getString("itemCode") + "-" + jsonObject.getString("objId") + "-" + jsonObject.getString("targetId");
 		ZktAlarmRecordDO zktAlarmRecordDO = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO());
 		zktAlarmRecordDO.setDefinitionId(defineId);
 		zktAlarmRecordDO.setItemCode(jsonObject.getString("itemCode"));
 		zktAlarmRecordDO.setObjId(jsonObject.getString("objId"));
+		zktAlarmRecordDO.setTargetId(jsonObject.getString("targetId"));
 		zktAlarmRecordDO.setAlarmId(jsonObject.getString("id"));
 		ZktAlarmRecordDO save = alarmRecordRepository.save(zktAlarmRecordDO);
 		log.info("保存结果为:{}",save);

+ 4 - 0
src/main/java/com/persagy/entity/AlarmDefine.java

@@ -47,6 +47,10 @@ public class AlarmDefine {
      */
     private String objId;
     /**
+     * 报警匹配规则ID
+     */
+    private String targetId;
+    /**
      * 对象类型
      */
     private String classCode;

+ 8 - 0
src/main/java/com/persagy/entity/AlarmRecord.java

@@ -9,6 +9,7 @@ import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
+import javax.persistence.Column;
 import java.io.Serializable;
 import java.util.Date;
 
@@ -46,6 +47,13 @@ public class AlarmRecord implements Serializable {
      */
     @ApiModelProperty(value = "对象id")
     private String objId;
+
+    /**
+     * 报警对象匹配规则ID
+     */
+    @ApiModelProperty(value = "报警对象匹配规则ID")
+    private String targetId;
+
     /**
      * 对象id
      */

+ 1 - 0
src/main/java/com/persagy/entity/Condition.java

@@ -46,6 +46,7 @@ import java.util.List;
 public class Condition {
     List<String> infoCode;
     List<JSONObject> infoCodes;
+    private JSONObject configs;
     private String trigger;
     private String end;
     private int triggerUphold;

+ 6 - 0
src/main/java/com/persagy/entity/ZktAlarmRecordDO.java

@@ -48,6 +48,12 @@ public class ZktAlarmRecordDO implements Serializable {
     @Column(name = "obj_id", nullable = false)
     @ApiModelProperty(value = "报警对象ID")
     private String objId;
+    /**
+     * 报警对象ID
+     */
+    @Column(name = "target_id", nullable = false)
+    @ApiModelProperty(value = "报警对象匹配规则ID")
+    private String targetId;
 
     /**
      * 报警定义对应最新一条报警记录状态

+ 28 - 21
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -2,6 +2,7 @@ package com.persagy.service.impl;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Maps;
 import com.googlecode.aviator.AviatorEvaluator;
@@ -88,6 +89,9 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             String dateTime = split[i * 4];
             String meterId = split[i * 4 + 1];
             String funcId = split[i * 4 + 2];
+            if("1001".equals(meterId) && "102".equals(funcId)){
+                System.out.println("-----"+split[i * 4 + 3]);
+            }
             double value = Double.parseDouble(split[i * 4 + 3]);
 
             if (alarmInfoCache.hasKey(meterId, funcId)) {
@@ -183,6 +187,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                     alarmRecordDO.setDefinitionId(defineId);
                     alarmRecordDO.setObjId(alarmDefine.getObjId());
                     alarmRecordDO.setItemCode(alarmDefine.getItemCode());
+                    alarmRecordDO.setTargetId(alarmDefine.getTargetId());
                     alarmRecordDO.setState("2");
                     alarmRecordDO.setEndTime(DateUtils.parseDate(dateTime));
                     alarmRecordDO.setEndInfo(JSONObject.toJSONString(paramMap));
@@ -257,26 +262,25 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
     private void handlerNowDataAlarm(AlarmDefine alarmDefine, AlarmState alarmState, String dateTime, Condition condition, String defineId, HashMap paramMap) throws SchedulerException, InterruptedException {
         alarmState.setLatestDataNormalstate(false);
         JSONObject effectTime = condition.getEffectTime();
-        boolean inEffectTime = false;
-        boolean hasExpire = true;
-        //生效时间段,有误过期时间判断
-        if (StringUtil.isEmpty(effectTime) || StringUtil.isEmpty(effectTime.getString("type"))) {
-            //无生效时间设置,则任何时间生效
-            inEffectTime = true;
-            //没有设置生效时间,所以也没有设置过期时间
-            hasExpire = false;
-
-        } else {
-            if ("period".equals(effectTime.getJSONObject("period").getString("type")) && "000000".equals(effectTime.getJSONObject("period").getString("startTime").substring(0, 6)) && "235959".equals(effectTime.getJSONObject("period").getString("endTime").substring(0, 6))) {
-                //架构师w确认 00:00-23:59的生效时间为一值生效,23:59分不过期
-                hasExpire = false;
-            }
-
-            if ("period".equals(effectTime.getString("type")) && effectTime.getJSONObject("period").getString("startTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) <= 0 && effectTime.getJSONObject("period").getString("endTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) >= 0) {
-                //在生效时间
-                inEffectTime = true;
-            } else {
-                log.info("[{}]不在设置的生效时间段", dateTime);
+        //无生效时间设置,则任何时间生效
+        boolean inEffectTime = true;
+        //没有设置生效时间,所以也没有设置过期时间
+        boolean hasExpire = false;
+        JSONObject period = effectTime.getJSONObject("period");
+        if(ObjectUtil.isAllNotEmpty(effectTime,effectTime.getString("type"))) {
+            if ("period".equals(effectTime.getString("type"))) {
+                //开始结束时间都不为空
+                String startTime = period.getString("startTime");
+                String endTime = period.getString("endTime");
+                String dateTimeFormat = DateUtils.getSdfTimeNotDate(dateTime);
+                if(ObjectUtil.isAllNotEmpty(startTime, endTime)){
+                    if(!"000000".equals(startTime) || !"235959".equals(endTime)){
+                        hasExpire = true;
+                    }
+                    if(startTime.compareTo(dateTimeFormat) > 0 && endTime.compareTo(DateUtils.getSdfTimeNotDate(dateTime)) < 0){
+                        inEffectTime = false;
+                    }
+                }
             }
         }
         if (inEffectTime) {
@@ -294,7 +298,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 Date expireDate = null;
                 if (hasExpire && "period".equals(effectTime.getString("type"))) {
                     //过期时间
-                    String expireTime = effectTime.getJSONObject("period").getString("endTime");
+                    String expireTime = period.getString("endTime");
                     LocalTime localTime = LocalTime.parse(expireTime, DateTimeFormatter.ofPattern(DateUtils.sdfTimeNotDate));
                     expireDateTime = LocalDateTime.of(LocalDate.now(), localTime);
                     expireDate = DateUtils.localDateTime2Date(expireDateTime);
@@ -316,6 +320,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                             .groupCode(groupCode)
                             .itemCode(alarmDefine.getItemCode())
                             .objId(alarmDefine.getObjId())
+                            .targetId(alarmDefine.getTargetId())
                             .classCode(alarmDefine.getClassCode())
                             .createUser(systemId)
                             .build();
@@ -327,6 +332,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                     zktAlarmRecordDO.setDefinitionId(defineId);
                     zktAlarmRecordDO.setObjId(alarmDefine.getObjId());
                     zktAlarmRecordDO.setItemCode(alarmDefine.getItemCode());
+                    zktAlarmRecordDO.setTargetId(alarmDefine.getTargetId());
                     zktAlarmRecordDO.setState("1");
                     alarmRecordRepository.save(zktAlarmRecordDO);
                     alarmState.setState(AlarmState.State.NOT_DEAL.getType());
@@ -342,6 +348,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                                 .itemCode(alarmDefine.getItemCode())
                                 .name(alarmDefine.getName())
                                 .objId(alarmDefine.getObjId())
+                                .targetId(alarmDefine.getTargetId())
                                 .state("3")  //要变成已过期
                                 .alarmId(alarmId)
                                 .build();

+ 17 - 3
src/main/java/com/persagy/utils/StringUtil.java

@@ -1,11 +1,13 @@
 package com.persagy.utils;
 
+import cn.hutool.core.util.NumberUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.googlecode.aviator.AviatorEvaluator;
 import com.googlecode.aviator.Expression;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
 import org.springframework.util.CollectionUtils;
 
 import java.lang.reflect.Array;
@@ -448,11 +450,23 @@ public class StringUtil {
     }
 
     public static void main(String[] args) {
+        System.out.println(System.getProperty("user.dir"));
+        String str = "{\\\"name\":\"spy\",\"id\":\"123456\"}";
+        str = str.replaceAll("\"", "");
+        System.out.println("原始 str = " + str);
+        String str1 = StringEscapeUtils.unescapeJava(str);
+        System.out.println("目标 str1 = " + str1);
+        Random r = new Random();
+        for (int i = 0; i < 20; i++) {
+            System.out.println(i+"---"+r.nextInt(2));
+        }
         JSONObject paramMap = new JSONObject();
         paramMap.put("hello",6);
-        paramMap.put("X",6.0);
-        paramMap.put("Y",6.0);
-        String exp = "X < seq.min(seq.list(decimal(1), decimal(2.5), decimal(hello), decimal(3.14)))";
+        paramMap.put("X",3);
+        paramMap.put("Y",6);
+//        String exp = "X < seq.min(seq.list(decimal(1), decimal(2.5), decimal(hello), decimal(3.14)))";
+//        String exp = "X < seq.min(seq.list(1, 2.5, 3.14))";
+        String exp = "X == 3.0000";
         Expression expExp = AviatorEvaluator.compile(exp, true);
         Boolean triggerResult = (Boolean) expExp.execute(paramMap);
         System.out.println("triggerResult:"+triggerResult);

+ 6 - 6
src/main/resources/application.yml

@@ -47,23 +47,23 @@ group:
     #需要更改
     host: 39.102.43.179     #netty IP
     #需要更改
-    port: 8826          #netty 端口9986
+    port: 9986          #netty 端口8826
     #需要更改
 terminal:  #边缘端IOT采集程序地址
   alarm:  # 拼接后的地址为ws://host:port/suffix
     #需要更改
-    compress: true    #采用的是压缩方式还是不压缩方式  true-压缩 false-不压缩
+    compress: false    #采用的是压缩方式还是不压缩方式  true-压缩 false-不压缩
     #需要更改
-    host: 39.96.55.242
+    host: 39.102.54.110
     #需要更改
-    port: 9982
+    port: 9989
     #需要更改
     suffix: websocket   #websocker后缀
 project:
   #需要更改
-  iotid: 1101050029  #iot使用项目ID,不包含PJ
+  iotid: 4403070003  #iot使用项目ID,不包含PJ
   #需要更改
-  id: Pj1101050029   #项目ID
+  id: Pj4403070003   #项目ID
 system:
   id: system  #默认用户表示 ,用于报警记录的创建人
 alarm: