|
@@ -2,15 +2,17 @@ package com.persagy.service.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
-import com.google.common.collect.Lists;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.googlecode.aviator.AviatorEvaluator;
|
|
import com.googlecode.aviator.AviatorEvaluator;
|
|
import com.googlecode.aviator.Expression;
|
|
import com.googlecode.aviator.Expression;
|
|
import com.persagy.cache.AlarmInfoCache;
|
|
import com.persagy.cache.AlarmInfoCache;
|
|
|
|
+import com.persagy.cache.AlarmLastTimeCache;
|
|
import com.persagy.cache.CreatedAlarmIdsCache;
|
|
import com.persagy.cache.CreatedAlarmIdsCache;
|
|
import com.persagy.cache.CurrentDataCache;
|
|
import com.persagy.cache.CurrentDataCache;
|
|
import com.persagy.client.GroupNettyClient;
|
|
import com.persagy.client.GroupNettyClient;
|
|
import com.persagy.entity.*;
|
|
import com.persagy.entity.*;
|
|
-import com.persagy.entity.util.ConditionUtil;
|
|
|
|
|
|
+import com.persagy.utils.condition.ConditionUtils;
|
|
import com.persagy.job.ExpireAlarmQueue;
|
|
import com.persagy.job.ExpireAlarmQueue;
|
|
import com.persagy.repository.AlarmRecordRepository;
|
|
import com.persagy.repository.AlarmRecordRepository;
|
|
import com.persagy.service.AlarmHandleService;
|
|
import com.persagy.service.AlarmHandleService;
|
|
@@ -54,6 +56,8 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
AlarmRecordRepository alarmRecordRepository;
|
|
AlarmRecordRepository alarmRecordRepository;
|
|
@Autowired
|
|
@Autowired
|
|
CreatedAlarmIdsCache createdAlarmIdsCache;
|
|
CreatedAlarmIdsCache createdAlarmIdsCache;
|
|
|
|
+ @Autowired
|
|
|
|
+ AlarmLastTimeCache alarmLastTimeCache;
|
|
/**
|
|
/**
|
|
* 集团编码
|
|
* 集团编码
|
|
*/
|
|
*/
|
|
@@ -76,7 +80,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public void handleIotMsg(String msg) throws SchedulerException, InterruptedException {
|
|
|
|
|
|
+ public void handleIotMsg(String msg) throws Exception {
|
|
log.info("接收到采集值:[{}]", msg);
|
|
log.info("接收到采集值:[{}]", msg);
|
|
// 校验采集值
|
|
// 校验采集值
|
|
boolean validateIotMsgResult = validateIotMsg(msg);
|
|
boolean validateIotMsgResult = validateIotMsg(msg);
|
|
@@ -116,7 +120,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @since: 2021/2/1 3:44 下午
|
|
* @since: 2021/2/1 3:44 下午
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
- private void handleIotData(String dataTime, String meterId, String funcIdStr, String valueStr) throws InterruptedException {
|
|
|
|
|
|
+ private void handleIotData(String dataTime, String meterId, String funcIdStr, String valueStr) throws Exception {
|
|
/* 如果iot数据为一组数据,先更新缓存中这组iot数据值,然后将这一组iot数据拆分为一条条iot数据处理 */
|
|
/* 如果iot数据为一组数据,先更新缓存中这组iot数据值,然后将这一组iot数据拆分为一条条iot数据处理 */
|
|
String funcIdSeparator = ",";
|
|
String funcIdSeparator = ",";
|
|
String[] funcIdArray = funcIdStr.split(funcIdSeparator);
|
|
String[] funcIdArray = funcIdStr.split(funcIdSeparator);
|
|
@@ -131,7 +135,9 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
|
|
|
|
// 将一组数据拆分为单条处理
|
|
// 将一组数据拆分为单条处理
|
|
for (String funcId : funcIdArray) {
|
|
for (String funcId : funcIdArray) {
|
|
- handleSingleIotData(dataTime, meterId, funcId);
|
|
|
|
|
|
+ if (alarmInfoCache.hasKey(meterId, funcId)) {
|
|
|
|
+ handleSingleIotData(dataTime, meterId, funcId);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -147,7 +153,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @since: 2021/2/1 4:01 下午
|
|
* @since: 2021/2/1 4:01 下午
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
- private void handleSingleIotData(String dataTime, String meterId, String funcId) throws InterruptedException {
|
|
|
|
|
|
+ private void handleSingleIotData(String dataTime, String meterId, String funcId) throws Exception {
|
|
// 获取和采集值相关的报警定义
|
|
// 获取和采集值相关的报警定义
|
|
List<AlarmDefine> alarmDefines = alarmInfoCache.getAlarmDefinitionIdByMeterFuncId(meterId, funcId);
|
|
List<AlarmDefine> alarmDefines = alarmInfoCache.getAlarmDefinitionIdByMeterFuncId(meterId, funcId);
|
|
for (AlarmDefine alarmDefine : alarmDefines) {
|
|
for (AlarmDefine alarmDefine : alarmDefines) {
|
|
@@ -189,11 +195,11 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
if (triggerResult) {
|
|
if (triggerResult) {
|
|
log.info("满足报警条件");
|
|
log.info("满足报警条件");
|
|
//报警的时候不考虑报警恢复,因为同时报警和报警恢复是不应该出现的
|
|
//报警的时候不考虑报警恢复,因为同时报警和报警恢复是不应该出现的
|
|
- handlerNowDataAlarm(alarmDefine, dataTime, paramMap);
|
|
|
|
|
|
+ handleAlarmTriggerData(alarmDefine, dataTime, paramMap);
|
|
} else {
|
|
} else {
|
|
log.info("不满足报警条件");
|
|
log.info("不满足报警条件");
|
|
//当前数据正常
|
|
//当前数据正常
|
|
- handlerNowDataNormal(alarmDefine, dataTime, endResult, paramMap);
|
|
|
|
|
|
+ handlerNormalData(alarmDefine, dataTime, endResult, paramMap);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
log.warn("部分信息点没有数值");
|
|
log.warn("部分信息点没有数值");
|
|
@@ -332,7 +338,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @since: 2020/10/21 16:46
|
|
* @since: 2020/10/21 16:46
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
- private void handlerNowDataNormal(
|
|
|
|
|
|
+ private void handlerNormalData(
|
|
AlarmDefine alarmDefine, String dataTime,
|
|
AlarmDefine alarmDefine, String dataTime,
|
|
Boolean endResult, HashMap<String, Object> paramMap) throws InterruptedException {
|
|
Boolean endResult, HashMap<String, Object> paramMap) throws InterruptedException {
|
|
String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
|
|
String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
|
|
@@ -364,9 +370,11 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
|
|
|
|
/* 超过报警恢复设置的持续时间 */
|
|
/* 超过报警恢复设置的持续时间 */
|
|
log.info("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
|
|
log.info("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
|
|
- // 拼装恢复后的边缘端报警信息
|
|
|
|
|
|
+
|
|
ZktAlarmRecordDO alarmRecordDO = getZktAlarmRecordDOWhenAlarmSuspend(defineId, dataTime, paramMap);
|
|
ZktAlarmRecordDO alarmRecordDO = getZktAlarmRecordDOWhenAlarmSuspend(defineId, dataTime, paramMap);
|
|
String alarmId = alarmRecordDO.getAlarmId();
|
|
String alarmId = alarmRecordDO.getAlarmId();
|
|
|
|
+ // 报警恢复时,alarmLastTimeCache中对应的报警标记为可删除
|
|
|
|
+ alarmLastTimeCache.setDeleteAvailable(alarmId);
|
|
// 如果云端已经完成报警记录的创建,直接发送更新报警状态消息,并删除数据库中的报警信息
|
|
// 如果云端已经完成报警记录的创建,直接发送更新报警状态消息,并删除数据库中的报警信息
|
|
if (createdAlarmIdsCache.contains(alarmId)) {
|
|
if (createdAlarmIdsCache.contains(alarmId)) {
|
|
deleteZktAlarmRecordWhenAlarmSuspend(defineId);
|
|
deleteZktAlarmRecordWhenAlarmSuspend(defineId);
|
|
@@ -473,16 +481,15 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @since: 2020/12/17 4:40 下午
|
|
* @since: 2020/12/17 4:40 下午
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
- private void sendAlarmContinueMessage(String alarmId, long lastTime, HashMap<String, Object> paramMap, String projectId)
|
|
|
|
- throws InterruptedException {
|
|
|
|
- NettyMessage<JSONObject> nettyMessage = new NettyMessage<>(11, projectId);
|
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
|
- jsonObject.put("id", alarmId);
|
|
|
|
- jsonObject.put("lastTime", lastTime);
|
|
|
|
- jsonObject.put("iotData", paramMap);
|
|
|
|
- jsonObject.put("groupCode", groupCode);
|
|
|
|
- nettyMessage.setContent(Lists.newArrayList(jsonObject));
|
|
|
|
- groupNettyClient.sendMessage(nettyMessage.toString());
|
|
|
|
|
|
+ private void initAlarmContinueMsg(String alarmId, long lastTime, HashMap<String, Object> paramMap, String projectId)
|
|
|
|
+ throws Exception {
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ ObjectNode objectNode = objectMapper.createObjectNode();
|
|
|
|
+ objectNode.put("id", alarmId);
|
|
|
|
+ objectNode.put("lastTime", lastTime);
|
|
|
|
+ objectNode.set("iotData", objectMapper.readTree(objectMapper.writeValueAsString(paramMap)));
|
|
|
|
+ objectNode.put("groupCode", groupCode);
|
|
|
|
+ alarmLastTimeCache.setAlarmLastMsg(alarmId, objectNode);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -497,16 +504,16 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
* @since: 2020/10/20 22:31
|
|
* @since: 2020/10/20 22:31
|
|
* @version: V1.0
|
|
* @version: V1.0
|
|
*/
|
|
*/
|
|
- private void handlerNowDataAlarm(
|
|
|
|
|
|
+ private void handleAlarmTriggerData(
|
|
AlarmDefine alarmDefine, String dataTime,
|
|
AlarmDefine alarmDefine, String dataTime,
|
|
- HashMap<String, Object> paramMap) throws InterruptedException {
|
|
|
|
|
|
+ HashMap<String, Object> paramMap) throws Exception {
|
|
// 根据defineId获取报警定义状态
|
|
// 根据defineId获取报警定义状态
|
|
String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
|
|
String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
|
|
AlarmDefineState alarmDefineState = getAlarmDefineStateFromCacheOrDb(defineId);
|
|
AlarmDefineState alarmDefineState = getAlarmDefineStateFromCacheOrDb(defineId);
|
|
|
|
|
|
Condition condition = alarmDefine.getCondition();
|
|
Condition condition = alarmDefine.getCondition();
|
|
// 判断报警是否在有效期内
|
|
// 判断报警是否在有效期内
|
|
- boolean inEffectTime = ConditionUtil.inEffectiveTime(condition, dataTime);
|
|
|
|
|
|
+ boolean inEffectTime = ConditionUtils.inEffectiveTime(condition, dataTime);
|
|
|
|
|
|
if (inEffectTime) {
|
|
if (inEffectTime) {
|
|
// 已经产生报警
|
|
// 已经产生报警
|
|
@@ -515,11 +522,10 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
String alarmStartTime = alarmDefineState.getAlarmStartTime();
|
|
String alarmStartTime = alarmDefineState.getAlarmStartTime();
|
|
long lastTime = DateUtils.betweenTwoTimeSecond(alarmStartTime, dataTime);
|
|
long lastTime = DateUtils.betweenTwoTimeSecond(alarmStartTime, dataTime);
|
|
String alarmId = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO()).getAlarmId();
|
|
String alarmId = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO()).getAlarmId();
|
|
- // 如果已经产生了报警记录
|
|
|
|
if (StringUtils.isNotBlank(alarmId)) {
|
|
if (StringUtils.isNotBlank(alarmId)) {
|
|
- log.info("报警已经产生,向云端推送报警持续消息");
|
|
|
|
- // 发送报警仍在继续的消息
|
|
|
|
- sendAlarmContinueMessage(alarmId, lastTime, paramMap, alarmDefine.getProjectId());
|
|
|
|
|
|
+ alarmLastTimeCache.put(alarmId, Integer.valueOf(String.valueOf(lastTime)));
|
|
|
|
+ log.info("初始化报警持续消息");
|
|
|
|
+ initAlarmContinueMsg(alarmId, lastTime, paramMap, alarmDefine.getProjectId());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//之前是是正常状态
|
|
//之前是是正常状态
|
|
@@ -540,6 +546,9 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
if (alarmLastTime >= condition.getTriggerUphold()) {
|
|
if (alarmLastTime >= condition.getTriggerUphold()) {
|
|
log.info("报警持续时间大于设定的持续时间,产生一条报警");
|
|
log.info("报警持续时间大于设定的持续时间,产生一条报警");
|
|
String alarmId = StringUtil.getUUID();
|
|
String alarmId = StringUtil.getUUID();
|
|
|
|
+ // 有新的报警产生,alarmLastTimeMap中报警标记为可删除
|
|
|
|
+ alarmLastTimeCache.updateAlarmId(defineId, alarmId);
|
|
|
|
+ alarmLastTimeCache.setDeleteAvailable(alarmId);
|
|
// 边缘端保存报警信息
|
|
// 边缘端保存报警信息
|
|
saveZktAlarmRecordWhenAlarmStart(alarmId, alarmDefine, dataTime);
|
|
saveZktAlarmRecordWhenAlarmStart(alarmId, alarmDefine, dataTime);
|
|
// 报警定义状态更新为未处理
|
|
// 报警定义状态更新为未处理
|
|
@@ -579,14 +588,14 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
|
|
*/
|
|
*/
|
|
private LocalDateTime getExpireLocalDateTime(String triggerTime, Condition condition) {
|
|
private LocalDateTime getExpireLocalDateTime(String triggerTime, Condition condition) {
|
|
// 判断过期设置是否生效
|
|
// 判断过期设置是否生效
|
|
- boolean expireSetEffective = ConditionUtil.expireSetEffective(condition);
|
|
|
|
|
|
+ boolean expireSetEffective = ConditionUtils.expireSetEffective(condition);
|
|
|
|
|
|
if (!expireSetEffective) {
|
|
if (!expireSetEffective) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
//过期时间
|
|
//过期时间
|
|
- String expireTime = ConditionUtil.getEffectEndTime(condition);
|
|
|
|
|
|
+ String expireTime = ConditionUtils.getEffectEndTime(condition);
|
|
LocalTime localTime = LocalTime.parse(expireTime, DateTimeFormatter.ofPattern(DateUtils.sdfTimeNotDate));
|
|
LocalTime localTime = LocalTime.parse(expireTime, DateTimeFormatter.ofPattern(DateUtils.sdfTimeNotDate));
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.sdfDay);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.sdfDay);
|
|
LocalDate triggerDate = LocalDate.parse(DateUtils.getDate(triggerTime), formatter);
|
|
LocalDate triggerDate = LocalDate.parse(DateUtils.getDate(triggerTime), formatter);
|