Browse Source

修改一个生效时间判断失败逻辑

lgy 4 years ago
parent
commit
b321c8784c

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

@@ -1,9 +1,11 @@
 package com.persagy.controller;
 package com.persagy.controller;
 
 
 
 
+import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.cache.AlarmInfoCache;
 import com.persagy.cache.AlarmInfoCache;
 import com.persagy.client.GroupNettyClient;
 import com.persagy.client.GroupNettyClient;
+import com.persagy.entity.AlarmDefine;
 import com.persagy.entity.NettyMessage;
 import com.persagy.entity.NettyMessage;
 import com.persagy.entity.ZktAlarmRecordDO;
 import com.persagy.entity.ZktAlarmRecordDO;
 import com.persagy.repository.AlarmRecordRepository;
 import com.persagy.repository.AlarmRecordRepository;
@@ -18,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
 import java.util.*;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicInteger;
 
 
 @Api(tags = "hello")
 @Api(tags = "hello")
@@ -100,7 +103,14 @@ public class HelloWorld {
 		return list;
 		return list;
 	}
 	}
 
 
-
+	@PostMapping("/test4")
+	public Map<String, AlarmDefine> test4(@RequestBody JSONObject jsonObject) throws Exception {
+		ConcurrentHashMap<String, AlarmDefine> alarmDefineMap = alarmInfoCache.getAlarmDefineMap();
+		if(ObjectUtil.isNotEmpty(alarmDefineMap)){
+			return alarmDefineMap;
+		}
+		return new HashMap<>();
+	}
 
 
 }
 }
 
 

+ 30 - 56
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -123,33 +123,35 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 if (triggerResult && endResult) {
                 if (triggerResult && endResult) {
                     log.warn("报警触发条件和报警恢复条件同时满足,请检查,报警定义详情【{}】", alarmDefine.toString());
                     log.warn("报警触发条件和报警恢复条件同时满足,请检查,报警定义详情【{}】", alarmDefine.toString());
                 }
                 }
-                AlarmState alarmState = alarmInfoCache.getAlarmState(defineId);
-                if (Objects.isNull(alarmState)) {
-                    //默认正常报警状态
-                    alarmState = new AlarmState(defineId);
-                    Optional<ZktAlarmRecordDO> recordOptional = alarmRecordRepository.findById(defineId);
-                    if(recordOptional.isPresent()){
-                        ZktAlarmRecordDO alarmRecordDO = recordOptional.get();
-                        //数据库报警状态:1-未处理
-                        if("1".equals(alarmRecordDO.getState())){
-                            alarmState.setState(AlarmState.State.NOT_DEAL.getType());
-                            alarmState.setAlarmStartTime(alarmRecordDO.getAlarmTime());
+                synchronized (defineId.intern()) {
+                    AlarmState alarmState = alarmInfoCache.getAlarmState(defineId);
+                    if (Objects.isNull(alarmState)) {
+                        //默认正常报警状态
+                        alarmState = new AlarmState(defineId);
+                        Optional<ZktAlarmRecordDO> recordOptional = alarmRecordRepository.findById(defineId);
+                        if(recordOptional.isPresent()){
+                            ZktAlarmRecordDO alarmRecordDO = recordOptional.get();
+                            //数据库报警状态:1-未处理
+                            if("1".equals(alarmRecordDO.getState())){
+                                alarmState.setState(AlarmState.State.NOT_DEAL.getType());
+                                alarmState.setAlarmStartTime(alarmRecordDO.getAlarmTime());
+                            }
                         }
                         }
                     }
                     }
-                }
-                //报警产生值满足(这里的满足不考虑报警持续时间)
-                if (triggerResult) {
-                    log.info("有一条满足报警条件");
-                    log.info("--"+alarmDefine.toString());
-                    log.info("--"+JSONObject.toJSONString(paramMap));
-                    //报警的时候不考虑报警恢复,因为同时报警和报警恢复是不应该出现的
-                    handlerNowDataAlarm(alarmDefine, alarmState, dateTime, condition, defineId,paramMap,meterId,funcId,value);
-                } else {
-                    log.info("不满足报警条件");
-                    log.info("--"+alarmDefine.toString());
-                    log.info("--"+JSONObject.toJSONString(paramMap));
-                    //当前数据正常
-                    handlerNowDataNormal(alarmDefine, dateTime, condition, defineId, endResult, alarmState,paramMap,meterId,funcId,value);
+                    //报警产生值满足(这里的满足不考虑报警持续时间)
+                    if (triggerResult) {
+                        log.info("有一条满足报警条件");
+                        log.info("--"+alarmDefine.toString());
+                        log.info("--"+JSONObject.toJSONString(paramMap));
+                        //报警的时候不考虑报警恢复,因为同时报警和报警恢复是不应该出现的
+                        handlerNowDataAlarm(alarmDefine, alarmState, dateTime, condition, defineId,paramMap,meterId,funcId,value);
+                    } else {
+                        log.info("不满足报警条件");
+                        log.info("--"+alarmDefine.toString());
+                        log.info("--"+JSONObject.toJSONString(paramMap));
+                        //当前数据正常
+                        handlerNowDataNormal(alarmDefine, dateTime, condition, defineId, endResult, alarmState,paramMap,meterId,funcId,value);
+                    }
                 }
                 }
             } else {
             } else {
                 log.warn("部分信息点没有数值:[{}]", codeDetail);
                 log.warn("部分信息点没有数值:[{}]", codeDetail);
@@ -182,21 +184,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             alarmState = new AlarmState(defineId);
             alarmState = new AlarmState(defineId);
             alarmInfoCache.setAlarmState(defineId, alarmState);
             alarmInfoCache.setAlarmState(defineId, alarmState);
         } else if (AlarmState.State.NOT_DEAL.getType().equals(alarmState.getState())) {
         } else if (AlarmState.State.NOT_DEAL.getType().equals(alarmState.getState())) {
-            synchronized (defineId.intern()){
-                AlarmState latestAlarmState = alarmInfoCache.getAlarmState(defineId);
-                if (ObjectUtil.isNull(latestAlarmState)) {
-                    //默认正常报警状态
-                    latestAlarmState = new AlarmState(defineId);
-                }
-                //上锁前报警状态和上锁后状态一样,可以直接报警生成
-                if(alarmState.getState().equals(latestAlarmState.getState())){
-                    handlerNormalWithLock(alarmDefine, dateTime, condition, defineId, endResult, alarmState, paramMap);
-                }else {
-                    //上锁前后状态不一致了,重新进行报警计算
-                    log.warn("报警定义的报警状态加锁前后发生变化,加锁前[{}],加锁后[{}]",alarmState,latestAlarmState);
-                    validIotData(dateTime,meterId,funcId,value);
-                }
-            }
+            handlerNormalWithLock(alarmDefine, dateTime, condition, defineId, endResult, alarmState, paramMap);
         }
         }
     }
     }
 
 
@@ -312,7 +300,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                     if(!"000000".equals(startTime) || !"235959".equals(endTime)){
                     if(!"000000".equals(startTime) || !"235959".equals(endTime)){
                         hasExpire = true;
                         hasExpire = true;
                     }
                     }
-                    if(startTime.compareTo(dateTimeFormat) > 0 && endTime.compareTo(DateUtils.getSdfTimeNotDate(dateTime)) < 0){
+                    if(startTime.compareTo(dateTimeFormat) > 0 || endTime.compareTo(DateUtils.getSdfTimeNotDate(dateTime)) < 0){
                         inEffectTime = false;
                         inEffectTime = false;
                     }
                     }
                 }
                 }
@@ -322,21 +310,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             //之前是报警状态,现在还是报警,报警值没变化
             //之前是报警状态,现在还是报警,报警值没变化
             //之前是是正常时候
             //之前是是正常时候
             if (AlarmState.State.NORMAL.getType().equals(alarmState.getState())) {
             if (AlarmState.State.NORMAL.getType().equals(alarmState.getState())) {
-                synchronized (defineId.intern()) {
-                    AlarmState latestAlarmState = alarmInfoCache.getAlarmState(defineId);
-                    if (ObjectUtil.isNull(latestAlarmState)) {
-                        //默认正常报警状态
-                        latestAlarmState = new AlarmState(defineId);
-                    }
-                    //上锁前报警状态和上锁后状态一样,可以直接报警生成
-                    if(alarmState.getState().equals(latestAlarmState.getState())){
-                        handlerAlarmWithLock(alarmDefine, alarmState, dateTime, condition, defineId, paramMap, effectTime, hasExpire, period);
-                    }else {
-                        //上锁前后状态不一致了,重新进行报警计算
-                        log.warn("报警定义的报警状态加锁前后发生变化,加锁前[{}],加锁后[{}]",alarmState,latestAlarmState);
-                        validIotData(dateTime,meterId,funcId,value);
-                    }
-                }
+                handlerAlarmWithLock(alarmDefine, alarmState, dateTime, condition, defineId, paramMap, effectTime, hasExpire, period);
             }
             }
         } else {
         } else {
             //不在生效时间的段的产生报警,要保存最近一次的数值是否是是报警
             //不在生效时间的段的产生报警,要保存最近一次的数值是否是是报警