|
|
@@ -0,0 +1,165 @@
|
|
|
+package com.yunfeiyun.agmp.iots.warn.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
+import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnindicator;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.EnumWarnRuleOp;
|
|
|
+import com.yunfeiyun.agmp.iots.warn.model.*;
|
|
|
+import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
|
|
|
+import com.yunfeiyun.agmp.iots.warn.util.WarnMessageBuilderUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class WarnDiseaseService {
|
|
|
+ @Autowired
|
|
|
+ private IotWarnBussinessService iotWarnBussinessService;
|
|
|
+ @Autowired
|
|
|
+ private ReCountService reCountService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 虫害处理入口方法,负责遍历所有设备配置信息并进行相应的处理。
|
|
|
+ */
|
|
|
+ public void process() {
|
|
|
+ List<IotWarnconfigInfoVo> iotWarnconfigInfoVos = iotWarnBussinessService.selectIotWarnconfigYbqInfoList();
|
|
|
+ for (IotWarnconfigInfoVo iotWarnconfigCbdInfoVo : iotWarnconfigInfoVos) {
|
|
|
+ try {
|
|
|
+ handleDevice(iotWarnconfigCbdInfoVo);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("处理设备 {} 时发生错误: {}", iotWarnconfigCbdInfoVo.getIotDevice().getDevBid(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理单个设备的所有预警配置。
|
|
|
+ *
|
|
|
+ * @param iotWarnconfigCbdInfoVo 设备配置信息对象
|
|
|
+ */
|
|
|
+ private void handleDevice(IotWarnconfigInfoVo iotWarnconfigCbdInfoVo) {
|
|
|
+ IotDevice device = iotWarnconfigCbdInfoVo.getIotDevice();
|
|
|
+ String devBid = device.getDevBid();
|
|
|
+ List<IotWarnindicator> warnIndicators = iotWarnconfigCbdInfoVo.getIotWarnindicatorList();
|
|
|
+ IotWarnconfig warnConfig = new IotWarnconfig();
|
|
|
+ BeanUtils.copyProperties(iotWarnconfigCbdInfoVo, warnConfig);
|
|
|
+
|
|
|
+ log.info("开始处理设备ID为 {} 的预警配置", devBid);
|
|
|
+ for (IotWarnindicator indicator : warnIndicators) {
|
|
|
+ if ("computeValue".equals(indicator.getWiCode())) {
|
|
|
+ handleComputeValue(device, indicator, warnConfig);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("完成设备ID为 {} 的所有预警配置处理", devBid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理特定于“computeValue”的逻辑,并调用告警日志处理器。
|
|
|
+ *
|
|
|
+ * @param device 设备对象
|
|
|
+ * @param indicator 预警指标对象
|
|
|
+ * @param warnConfig 预警配置对象
|
|
|
+ */
|
|
|
+ private void handleComputeValue(IotDevice device, IotWarnindicator indicator, IotWarnconfig warnConfig) {
|
|
|
+ IotDevice detailedDevice = iotWarnBussinessService.selectDeviceById(device.getDevBid());
|
|
|
+ if (StringUtils.isNotEmpty(detailedDevice.getExtInfo())) {
|
|
|
+ JSONObject extInfo = JSONObject.parseObject(detailedDevice.getExtInfo());
|
|
|
+ Double computeValue = Double.parseDouble(extInfo.getOrDefault("computeValue", "0.0").toString());
|
|
|
+ WarnStatusDto warnStatusDto = buildWarnStatusDto(computeValue, indicator);
|
|
|
+ if (warnStatusDto != null) {
|
|
|
+ handlerAllWarnLog(device.getDevBid(), warnConfig, warnStatusDto, detailedDevice.getExtInfo());
|
|
|
+ } else {
|
|
|
+ log.warn("【设备预警】病虫害:设备id 不可为空. 设备ID: {}, 设备Code:{} , 未触发: ", detailedDevice.getDevBid(), detailedDevice.getDevCode());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.warn("【设备预警】病虫害:设备id 不可为空. 设备ID: {}, 设备Code:{} , 最新数据为空: ", detailedDevice.getDevBid(), detailedDevice.getDevCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据当前值和预警指标构建WarnStatusDto对象。
|
|
|
+ *
|
|
|
+ * @param currentValue 当前值
|
|
|
+ * @param indicator 预警指标对象
|
|
|
+ * @return 构建好的WarnStatusDto对象
|
|
|
+ */
|
|
|
+ private WarnStatusDto buildWarnStatusDto(Double currentValue, IotWarnindicator indicator) {
|
|
|
+ if (!"0".equals(indicator.getWiStatus())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ EnumWarnRuleOp op = EnumWarnRuleOp.findEnumByCode(indicator.getWiExpression());
|
|
|
+ boolean comparisonResult = CompareUtil.comp(currentValue.toString(), indicator.getWiExpression(), indicator.getWiValue());
|
|
|
+ WarnStatusDto dto = new WarnStatusDto();
|
|
|
+ dto.setDevType(IotDeviceDictEnum.getLv1NameByCode(indicator.getDevtypeBid()));
|
|
|
+ dto.setDevCode(indicator.getDevCode());
|
|
|
+ dto.setName(indicator.getWiName());
|
|
|
+ dto.setValue(currentValue.toString());
|
|
|
+ dto.setUnit(indicator.getWiUnit());
|
|
|
+ dto.setOpt(op.getName());
|
|
|
+ dto.setIndicatorValue(indicator.getWiValue());
|
|
|
+ dto.setWarn(comparisonResult);
|
|
|
+ log.debug("构建告警状态DTO: {}", dto);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理所有的告警日志,包括生成警告消息、准备发送的消息参数,并最终发送告警。
|
|
|
+ *
|
|
|
+ * @param devBid 设备ID
|
|
|
+ * @param config 配置对象
|
|
|
+ * @param dto 告警状态数据传输对象
|
|
|
+ * @param data 设备额外信息
|
|
|
+ */
|
|
|
+ private void handlerAllWarnLog(String devBid, IotWarnconfig config, WarnStatusDto dto, String data) {
|
|
|
+ if (StringUtils.isEmpty(devBid)) {
|
|
|
+ log.error("【设备预警】病虫害:设备id 不可为空. 设备ID: {}, 配置ID: {}", devBid, config.getWcBid());
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), "病虫害:设备id 不可为空");
|
|
|
+ }
|
|
|
+ log.info("开始处理设备ID为 {}, 配置ID为 {} 的预警信息", devBid, config.getWcBid());
|
|
|
+ String message = WarnMessageBuilderUtil.buildWarningMessage(dto.getDevType(), dto.getDevCode(),
|
|
|
+ dto.getName(), dto.getValue(), dto.getUnit(), dto.getOpt(), dto.getIndicatorValue());
|
|
|
+ log.info("根据类型生成警告消息: {}. 设备ID: {}, 配置ID: {}", message, devBid, config.getWcBid());
|
|
|
+
|
|
|
+ WarnResult result = createWarnResult(config, devBid, dto, data);
|
|
|
+ log.info("准备发送告警信息: {}. 设备ID: {}, 配置ID: {}", result.getMessage(), devBid, config.getWcBid());
|
|
|
+ reCountService.handlerMessage(result);
|
|
|
+ log.info("告警信息已发送完成. 设备ID: {}, 配置ID: {}", devBid, config.getWcBid());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建并初始化WarnResult对象,用于后续的告警消息发送。
|
|
|
+ *
|
|
|
+ * @param config 配置对象
|
|
|
+ * @param devBid 设备ID
|
|
|
+ * @param dto 告警状态数据传输对象
|
|
|
+ * @param data 设备额外信息
|
|
|
+ * @return 初始化后的WarnResult对象
|
|
|
+ */
|
|
|
+ private WarnResult createWarnResult(IotWarnconfig config, String devBid, WarnStatusDto dto, String data) {
|
|
|
+ WarnResult result = new WarnResult();
|
|
|
+ result.setMessageId(UUID.randomUUID().toString()); // 假设UUID方法用于生成唯一ID
|
|
|
+ result.setDevId(devBid);
|
|
|
+ result.setTid(config.getTid());
|
|
|
+ result.setConfigId(config.getWcBid());
|
|
|
+ result.setReportData(data);
|
|
|
+ result.setTargetReCount(config.getWcRepeatnum() == null ? 0 : config.getWcRepeatnum());
|
|
|
+ result.setDevtypeBid(config.getDevtypeBid());
|
|
|
+ result.setConfig(config);
|
|
|
+ result.setTriggered(dto.isWarn());
|
|
|
+ result.setMessage(WarnMessageBuilderUtil.buildWarningMessage(
|
|
|
+ dto.getDevType(), dto.getDevCode(), dto.getName(), dto.getValue(), dto.getUnit(), dto.getOpt(), dto.getIndicatorValue()
|
|
|
+ ));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|