Commit a3f58be5714b0c5c24e998ed4f322db427f45629

Authored by YunaiV
1 parent 97260b8e

feat:【IoT 物联网】新增告警恢复场景规则执行类 IotAlertRecoverSceneRuleAction

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/action/IotAlertRecoverSceneRuleAction.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.scene.action;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4   -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
  4 +import cn.hutool.core.util.StrUtil;
5 5 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
6   -import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO;
  6 +import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertRecordDO;
7 7 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotRuleSceneDO;
8 8 import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneActionTypeEnum;
9   -import cn.iocoder.yudao.module.iot.service.alert.IotAlertConfigService;
10 9 import cn.iocoder.yudao.module.iot.service.alert.IotAlertRecordService;
11   -import cn.iocoder.yudao.module.system.api.mail.MailSendApi;
12   -import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
13   -import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
14 10 import jakarta.annotation.Resource;
15 11 import org.springframework.stereotype.Component;
16 12  
17   -import javax.annotation.Nullable;
18 13 import java.util.List;
19 14  
  15 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
  16 +
20 17 // TODO @puhui999、@芋艿:未测试;需要场景联动开发完
21 18 /**
22   - * IoT 告警触发的 {@link IotSceneRuleAction} 实现类
  19 + * IoT 告警恢复的 {@link IotSceneRuleAction} 实现类
23 20 *
24 21 * @author 芋道源码
25 22 */
26 23 @Component
27   -public class IotAlertTriggerSceneRuleAction implements IotSceneRuleAction {
  24 +public class IotAlertRecoverSceneRuleAction implements IotSceneRuleAction {
28 25  
29   - @Resource
30   - private IotAlertConfigService alertConfigService;
31   - @Resource
32   - private IotAlertRecordService alertRecordService;
  26 + private static final String PROCESS_REMARK = "告警自动回复,基于【{}】场景联动规则";
33 27  
34 28 @Resource
35   - private SmsSendApi smsSendApi;
36   - @Resource
37   - private MailSendApi mailSendApi;
38   - @Resource
39   - private NotifyMessageSendApi notifyMessageSendApi;
  29 + private IotAlertRecordService alertRecordService;
40 30  
41 31 @Override
42   - public void execute(@Nullable IotDeviceMessage message,
  32 + public void execute(IotDeviceMessage message,
43 33 IotRuleSceneDO rule, IotRuleSceneDO.ActionConfig actionConfig) throws Exception {
44   - List<IotAlertConfigDO> alertConfigs = alertConfigService.getAlertConfigListBySceneRuleIdAndStatus(
45   - rule.getId(), CommonStatusEnum.ENABLE.getStatus());
46   - if (CollUtil.isEmpty(alertConfigs)) {
  34 + Long deviceId = message != null ? message.getDeviceId() : null;
  35 + List<IotAlertRecordDO> alertRecords = alertRecordService.getAlertRecordListBySceneRuleId(
  36 + rule.getId(), deviceId, false);
  37 + if (CollUtil.isEmpty(alertRecords)) {
47 38 return;
48 39 }
49   - alertConfigs.forEach(alertConfig -> {
50   - // 记录告警记录
51   - alertRecordService.createAlertRecord(alertConfig, message);
52   - // 发送告警消息
53   - sendAlertMessage(alertConfig, message);
54   - });
55   - }
56   -
57   - private void sendAlertMessage(IotAlertConfigDO config, IotDeviceMessage deviceMessage) {
58   - // TODO @芋艿:等场景联动开发完,再实现
59   - // TODO @芋艿:短信
60   - // TODO @芋艿:邮箱
61   - // TODO @芋艿:站内信
  40 + alertRecordService.processAlertRecordList(convertList(alertRecords, IotAlertRecordDO::getId),
  41 + StrUtil.format(PROCESS_REMARK, rule.getName()));
62 42 }
63 43  
64 44 @Override
65 45 public IotRuleSceneActionTypeEnum getType() {
66   - return IotRuleSceneActionTypeEnum.ALERT_TRIGGER;
  46 + return IotRuleSceneActionTypeEnum.ALERT_RECOVER;
67 47 }
68 48  
69 49 }
... ...