|
@@ -0,0 +1,219 @@
|
|
|
|
|
+package com.yunfeiyun.agmp.iots.warn.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.enums.warn.MsgBusType;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.enums.warn.MsgType;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.enums.warn.WarnLogSendStatus;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.framework.message.MessageDto;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.framework.mq.rabbitmq.enums.AgmpActionEnums;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.framework.mq.rabbitmq.model.SynAgmpInfoDto;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceTypeLv1Enum;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnpolicy;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnreceiver;
|
|
|
|
|
+import com.yunfeiyun.agmp.iots.mq.provider.AgmpIotMqProviderService;
|
|
|
|
|
+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;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 消息中心
|
|
|
|
|
+ * 重复次数服务产生告警信息后发给消息中心发送消息
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class MsgService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private IotWarnBussinessService iotWarnBussinessService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AgmpIotMqProviderService agmpMqProviderService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 1. 判断是否触发
|
|
|
|
|
+ * 2. 如果已经触发预警,获取配置信息IotWarnpolicy,检查是即使发送还是周期性发送wpType:0即时推送,1选定时间
|
|
|
|
|
+ * 3. 如果是及时发送
|
|
|
|
|
+ * 3.1 如果是站内信web,直接入库,代表发送
|
|
|
|
|
+ * 3.2 首先入库,状态待发送,然后放入队列,异步发送
|
|
|
|
|
+ * 4. 如果是选定时间
|
|
|
|
|
+ * 4.1 获取需要发送的时间段,还要获取上一次发送的时间,以及配置信息的发送周期
|
|
|
|
|
+ * 4.2 如果在这个发送时间段,离上次发送消息已经过了一个周期,则发送,否则不发送
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param warnResult
|
|
|
|
|
+ */
|
|
|
|
|
+ public void handleWarn(WarnResult warnResult) {
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!warnResult.isTriggered()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String messageId = warnResult.getMessageId();
|
|
|
|
|
+ IotWarnpolicy iotWarnpolicy = iotWarnBussinessService.selectWarnPolicy(warnResult.getConfigId());
|
|
|
|
|
+ if (iotWarnpolicy == null) {
|
|
|
|
|
+ log.info("未找到配置信息,配置id:{}", warnResult.getConfigId());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ warnResult.setIotWarnpolicy(iotWarnpolicy);
|
|
|
|
|
+ List<IotWarnreceiver> iotWarnreceiverList = iotWarnBussinessService.selectWarnReceiverByConfigId(warnResult.getConfigId());
|
|
|
|
|
+ if (iotWarnreceiverList.isEmpty()) {
|
|
|
|
|
+ iotWarnreceiverList = new ArrayList<>();
|
|
|
|
|
+ }
|
|
|
|
|
+ List<String> userIds = iotWarnreceiverList.stream().map(IotWarnreceiver::getWruserId).collect(Collectors.toList());
|
|
|
|
|
+ warnResult.setReceiverIds(userIds);
|
|
|
|
|
+ String wpType = iotWarnpolicy.getWpType();
|
|
|
|
|
+ boolean reSend = false;
|
|
|
|
|
+ if (wpType.equals("0")) {
|
|
|
|
|
+ // 即时推送
|
|
|
|
|
+ log.info("【告警通知】消息标识{}:当前设备ID:{},即时推送", messageId, warnResult.getDevId());
|
|
|
|
|
+ saveWarnMsg(warnResult);
|
|
|
|
|
+ } else if (wpType.equals("1")) {
|
|
|
|
|
+ // 选定时间
|
|
|
|
|
+ String deliveryTimePeriod = iotWarnpolicy.getDeliveryTimePeriod();
|
|
|
|
|
+ String startTime = deliveryTimePeriod.split("-")[0];
|
|
|
|
|
+ String endTime = deliveryTimePeriod.split("-")[1];
|
|
|
|
|
+ Date startDate = DateUtils.parseDate(DateUtils.getDate()+" "+startTime);
|
|
|
|
|
+ Date endDate = DateUtils.parseDate(DateUtils.getDate()+" "+endTime);
|
|
|
|
|
+ log.info("【告警通知】消息标识{}:当前设备ID:{},时间段推送:deliveryTimePeriod{},start:{},end:{}", messageId, warnResult.getDevId(),deliveryTimePeriod,startDate,endDate);
|
|
|
|
|
+ if (DateUtils.isBetween(new Date(), startDate, endDate)) {
|
|
|
|
|
+ // 发送频率,每N时,数字类型
|
|
|
|
|
+ String wpFrequency = StringUtils.isEmpty(iotWarnpolicy.getWpFrequency())?"0":iotWarnpolicy.getWpFrequency();
|
|
|
|
|
+ // 判断是否到了发送时间
|
|
|
|
|
+ IotWarnlog lastedUnSendWarnLog = getLastedUnSendWarnLog(warnResult.getConfigId());
|
|
|
|
|
+ if (lastedUnSendWarnLog != null) {
|
|
|
|
|
+ // 根据最后一次发送时间,结合发送频率,如果上次距离当下超过发送频率,那就可以发送
|
|
|
|
|
+ if (!DateUtils.isBetween(new Date(), DateUtils.parseDate(lastedUnSendWarnLog.getWlCreateddate()), DateUtils.addHours(DateUtils.parseDate(lastedUnSendWarnLog.getWlCreateddate()), Integer.parseInt(wpFrequency)))) {
|
|
|
|
|
+ saveWarnMsg(warnResult);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reSend = true;
|
|
|
|
|
+ log.info("还没到发送时间,下次发送时间:{}", DateUtils.addHours(DateUtils.parseDate(lastedUnSendWarnLog.getWlCreateddate()), Integer.parseInt(wpFrequency)).toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ saveWarnMsg(warnResult);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reSend = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ //统一处理补发逻辑
|
|
|
|
|
+ reSendWarn(warnResult.getWlBid(), reSend, warnResult.getConfigId(), iotWarnpolicy.getDeliveryTimePeriod(), iotWarnpolicy.getWpFrequency());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("handleWarn error", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 补发逻辑
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param wlId
|
|
|
|
|
+ * @param reSendStatus
|
|
|
|
|
+ * @param wlBid
|
|
|
|
|
+ * @param deliveryTimePeriod
|
|
|
|
|
+ * @param wpFrequency
|
|
|
|
|
+ */
|
|
|
|
|
+ void reSendWarn(String wlId, boolean reSendStatus, String wlBid, String deliveryTimePeriod, String wpFrequency) {
|
|
|
|
|
+ if (reSendStatus) {
|
|
|
|
|
+ //标记需要补发
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //标记不需要补发
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void saveWarnMsg(WarnResult warnResult) {
|
|
|
|
|
+ SynAgmpInfoDto synAgmpInfoDto = new SynAgmpInfoDto();
|
|
|
|
|
+ synAgmpInfoDto.setAction(AgmpActionEnums.AGMP_SEND_MSG.getCode());
|
|
|
|
|
+ synAgmpInfoDto.setData(JSONObject.from(resolvePtsMsgByIotWarnlog(warnResult)));
|
|
|
|
|
+ synAgmpInfoDto.setDesc("发送消息");
|
|
|
|
|
+ //标记告警记录,已发送;
|
|
|
|
|
+ iotWarnBussinessService.updateWarnLogSendStatus(DateUtils.dateTimeNow(), warnResult.getWlBid(), WarnLogSendStatus.SEND_SUCCESS.getCode());
|
|
|
|
|
+ agmpMqProviderService.sendToAgmpIot(JSONObject.toJSONString(synAgmpInfoDto));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public IotWarnlog getLastedUnSendWarnLog(String wcBid) {
|
|
|
|
|
+ // 获取最新的一条未发送的告警
|
|
|
|
|
+ IotWarnlog iotWarnlog = iotWarnBussinessService.getLastedUnSendWarnLog(wcBid);
|
|
|
|
|
+ return iotWarnlog;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 告警日志转为消息对象
|
|
|
|
|
+ */
|
|
|
|
|
+ public MessageDto resolvePtsMsgByIotWarnlog(WarnResult warnResult) {
|
|
|
|
|
+ MessageDto messageDto = new MessageDto();
|
|
|
|
|
+ IotWarnconfig iotWarnconfig = warnResult.getConfig();
|
|
|
|
|
+ IotWarnpolicy iotWarnpolicy = warnResult.getIotWarnpolicy();
|
|
|
|
|
+ // 直接映射或推测可能的对应关系
|
|
|
|
|
+ messageDto.setMsgbatchId(warnResult.getMessageId()); // 假设消息标识作为消息批次标识
|
|
|
|
|
+ messageDto.setMsgbatchContent(warnResult.getMessage()); // 使用预警消息作为消息批次内容
|
|
|
|
|
+ messageDto.setMsgbatchContenttype("1"); // 假定使用固定内容,具体依据业务需求确定
|
|
|
|
|
+ messageDto.setMsgbatchMsgtype(MsgType.WARN_INFO.getCode());
|
|
|
|
|
+ messageDto.setMsgbatchBiztype(resolveMsgBusTypeByDevtype(warnResult.getDevtypeBid()).getCode()); // 业务类型,这里假设为预警配置ID
|
|
|
|
|
+ messageDto.setMsgbatchBizobj(warnResult.getDevId()); // 设备ID作为业务对象
|
|
|
|
|
+ messageDto.setMsgbatchLevel(iotWarnconfig.getWcLevel()); // 消息等级,这里默认为1,实际应用中应依据具体情况设置
|
|
|
|
|
+ messageDto.setMsgbatchSource("iots"); // 消息来源,假设来自物联网系统
|
|
|
|
|
+ messageDto.setMsgbatchSender("system"); // 发送者,这里假设为系统发送
|
|
|
|
|
+ messageDto.setMsgbatchChannel(iotWarnpolicy.getWpChannel()); // 通知渠道,这里假设通过电子邮件发送,需要根据实际情况调整
|
|
|
|
|
+ JSONObject extra = new JSONObject();
|
|
|
|
|
+ extra.put("location", "/iotm/warning/record");
|
|
|
|
|
+ extra.put("permission", "iotm:warningPlanning:historyRecord");
|
|
|
|
|
+ extra.put("handleId", warnResult.getWlBid());
|
|
|
|
|
+ messageDto.setMsgbatchExtra(extra.toJSONString());
|
|
|
|
|
+ messageDto.setMsgbatchHandler(warnResult.getReceiverIds()); // 处理人暂时为空,根据业务逻辑补充
|
|
|
|
|
+ messageDto.setMsgbatchHandlerType("1"); // 处理人暂时为空,根据业务逻辑补充
|
|
|
|
|
+ messageDto.setMsgbatchCreateddate(DateUtils.dateTimeNow()); // 创建时间
|
|
|
|
|
+ messageDto.setMsgbatchTitle("设备告警"); // 消息标题,这里假设为物联网设备警告
|
|
|
|
|
+ messageDto.setMsgbatchTarget("PTS"); // 目标子系统,这里假设为目标设备型号ID
|
|
|
|
|
+ messageDto.setWlBid(warnResult.getWlBid()); // 物联网预警ID
|
|
|
|
|
+ return messageDto;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //根据设备型号判断对应那种告警类型
|
|
|
|
|
+ public MsgBusType resolveMsgBusTypeByDevtype(String devtypeBid) {
|
|
|
|
|
+ String devClass = IotDeviceDictEnum.getLv1CodeByCode(devtypeBid);
|
|
|
|
|
+ IotDeviceTypeLv1Enum iotDeviceTypeLv1Enum = IotDeviceTypeLv1Enum.findEnumByCode(devClass);
|
|
|
|
|
+ switch (iotDeviceTypeLv1Enum) {
|
|
|
|
|
+ case QXZ: {
|
|
|
|
|
+ return MsgBusType.WARN_QX;
|
|
|
|
|
+ }
|
|
|
|
|
+ case SQZ:
|
|
|
|
|
+ case GSSQ: {
|
|
|
|
|
+ return MsgBusType.WARN_SQ;
|
|
|
|
|
+ }
|
|
|
|
|
+ //病害
|
|
|
|
|
+ case YBQ_DWB:
|
|
|
|
|
+ case YBQ_CMB:
|
|
|
|
|
+ case YBQ_DBB:
|
|
|
|
|
+ case YBQ_TXB:
|
|
|
|
|
+ case YBQ_BFB: {
|
|
|
|
|
+ return MsgBusType.WARN_DISEASE;
|
|
|
|
|
+ }
|
|
|
|
|
+ //虫害
|
|
|
|
|
+ case CBD:
|
|
|
|
|
+ return MsgBusType.WARN_PEST;
|
|
|
|
|
+ default:
|
|
|
|
|
+ return MsgBusType.WARN_DEVICE_COMMON;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 插入消息内容
|
|
|
|
|
+ */
|
|
|
|
|
+ public void saveWarnMsg(IotWarnlog iotWarnlog) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 定时任务:获取到了需要发送的时间段,获取最新的一条未发送的告警进行发送
|
|
|
|
|
+ */
|
|
|
|
|
+}
|