|
|
@@ -1,10 +1,14 @@
|
|
|
package com.yunfeiyun.agmp.iots.warn.service;
|
|
|
|
|
|
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
+import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
|
|
|
import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -12,54 +16,89 @@ import org.springframework.stereotype.Service;
|
|
|
* 重复次数服务
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class ReCountService {
|
|
|
@Autowired
|
|
|
private IotWarnBussinessService iotWarnBussinessService;
|
|
|
|
|
|
/**
|
|
|
* 针对预警解析出来的结果进行处理
|
|
|
- * 注:WarnResult是WarnService和ReCountService对接的重要对象,里面的对象参数保证都传过来
|
|
|
*
|
|
|
* @param warnResult
|
|
|
*/
|
|
|
public void handlerMessage(WarnResult warnResult) {
|
|
|
// 注意必要字段必须传过来
|
|
|
+ validateMessageParam(warnResult);
|
|
|
+
|
|
|
+ String messageId = warnResult.getMessageId();
|
|
|
+ log.info("【设备预警】消息标识{}:处理数据:{}", messageId, warnResult);
|
|
|
// 触发了预警,进行校验处理
|
|
|
if (warnResult.isTriggered()) {
|
|
|
- // 当前的获取重复次数
|
|
|
Long reCount = iotWarnBussinessService.selectIotWarnCountByDevAndConfig(warnResult.getDevId(), warnResult.getConfigId());
|
|
|
- // 设定的重复次数阀值
|
|
|
int targetReCount = warnResult.getTargetReCount();
|
|
|
- // 当前重复的次数
|
|
|
int thisReCount = reCount == null ? 0 : reCount.intValue();
|
|
|
- // 达到阀值,进入产生预警记录
|
|
|
+ log.info("【设备预警】消息标识{}:当前设备ID:{}, 预警配置ID:{} 的重复次数为: {}", messageId, warnResult.getDevId(), warnResult.getConfigId(), thisReCount);
|
|
|
if (thisReCount + 1 > targetReCount) {
|
|
|
- // 已经达到阀值,直接存储,次数也不用增加了
|
|
|
- iotWarnBussinessService.insertWarnRecord(buildWarnMessage(warnResult));
|
|
|
+ log.info("【设备预警】消息标识{}:达到或超过阈值, 不再增加重复次数", messageId);
|
|
|
+ iotWarnBussinessService.insertWarnRecord(buildWarnMessage(messageId, warnResult));
|
|
|
} else if (thisReCount + 1 == targetReCount) {
|
|
|
- // 初次达到阀值,并增加数值
|
|
|
+ log.info("【设备预警】消息标识{}:初次达到阈值, 增加重复次数并生成预警记录", messageId);
|
|
|
iotWarnBussinessService.incrementReCount(reCount, warnResult);
|
|
|
- // 存储预警
|
|
|
- iotWarnBussinessService.insertWarnRecord(buildWarnMessage(warnResult));
|
|
|
+ iotWarnBussinessService.insertWarnRecord(buildWarnMessage(messageId, warnResult));
|
|
|
} else {
|
|
|
- //没达到阀值,只增加数值
|
|
|
+ log.info("【设备预警】消息标识{}:未达阈值, 增加重复次数", messageId);
|
|
|
iotWarnBussinessService.incrementReCount(reCount, warnResult);
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
- //没有触发,则清除之前的
|
|
|
- cleanReCount(warnResult.getDevId(), warnResult.getConfigId());
|
|
|
+ log.info("【设备预警】消息标识{}:未触发预警, 清除之前的重复计数", messageId);
|
|
|
+ cleanReCount(messageId, warnResult.getDevId(), warnResult.getConfigId());
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 构建告警信息
|
|
|
+ * 验证 WarnResult 对象的所有必要参数是否有效
|
|
|
*
|
|
|
- * @param warnResult
|
|
|
- * @return
|
|
|
+ * @param warnResult 要验证的预警结果对象
|
|
|
*/
|
|
|
- IotWarnlog buildWarnMessage(WarnResult warnResult) {
|
|
|
+ public void validateMessageParam(WarnResult warnResult) {
|
|
|
+ if (StringUtils.isEmpty(warnResult.getMessageId())) {
|
|
|
+ log.error("【设备预警】消息标识 不可为空");
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "消息标识不可为空");
|
|
|
+ }
|
|
|
+ String messageId = warnResult.getMessageId();
|
|
|
+ if (StringUtils.isEmpty(warnResult.getReportData())) {
|
|
|
+ log.error("【设备预警】消息标识{}:上报数据不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "上报数据不可为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(warnResult.getDevId())) {
|
|
|
+ log.error("【设备预警】消息标识{}:设备id不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "设备id不可为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(warnResult.getConfigId())) {
|
|
|
+ log.error("【设备预警】消息标识{}:配置id不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "配置id不可为空");
|
|
|
+ }
|
|
|
+ if (warnResult.getTargetReCount() < 0) {
|
|
|
+ log.error("【设备预警】消息标识{}:重复次数阈值必须大于等于0", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "重复次数阈值必须大于等于0");
|
|
|
+ }
|
|
|
+ if (warnResult.getConfig() == null) {
|
|
|
+ log.error("【设备预警】消息标识{}:配置对象不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "配置对象不可为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(warnResult.getTid())) {
|
|
|
+ log.error("【设备预警】消息标识{}:tid不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "tid不可为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(warnResult.getDevtypeBid())) {
|
|
|
+ log.error("【设备预警】消息标识{}:设备型号id不可为空", messageId);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "设备型号id不可为空");
|
|
|
+ }
|
|
|
+ log.info("【设备预警】{}:所有参数验证通过");
|
|
|
+ }
|
|
|
+
|
|
|
+ IotWarnlog buildWarnMessage(String messageId, WarnResult warnResult) {
|
|
|
IotWarnconfig config = warnResult.getConfig();
|
|
|
IotWarnlog iotWarnlog = new IotWarnlog();
|
|
|
iotWarnlog.setWlBid(IdUtils.fastUUID());
|
|
|
@@ -72,32 +111,24 @@ public class ReCountService {
|
|
|
iotWarnlog.setWlCreateddate(DateUtils.dateTimeNow());
|
|
|
iotWarnlog.setWlData(warnResult.getReportData());
|
|
|
iotWarnlog.setTid(config.getTid());
|
|
|
+ log.info("【设备预警】消息标识{}:构建预警信息: {}", messageId, iotWarnlog);
|
|
|
return iotWarnlog;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 如果这次没触发,但之前有值(重复次数),需要将重复次数为0
|
|
|
- *
|
|
|
- * @param devId
|
|
|
- * @param configId
|
|
|
- */
|
|
|
- public void cleanReCount(String devId, String configId) {
|
|
|
-
|
|
|
+ public void cleanReCount(String messageId, String devId, String configId) {
|
|
|
Long count = iotWarnBussinessService.selectIotWarnCountByDevAndConfig(devId, configId);
|
|
|
- //优化缓存情况
|
|
|
if (count == null) {
|
|
|
+ log.info("【设备预警】消息标识{}:没有找到对应的重复计数, 设备ID:{}, 配置ID:{}", messageId, devId, configId);
|
|
|
return;
|
|
|
}
|
|
|
if (count != 0) {
|
|
|
+ log.info("【设备预警】消息标识{}:重置设备ID:{}, 配置ID:{} 的重复计数", messageId, devId, configId);
|
|
|
iotWarnBussinessService.resetReCountByDevIdAndConfigId(devId, configId);
|
|
|
- return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 清空重复次数
|
|
|
- */
|
|
|
public void resetAllReCount() {
|
|
|
+ log.info("清空所有重复次数");
|
|
|
iotWarnBussinessService.resetReCount();
|
|
|
}
|
|
|
}
|