|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package cn.iocoder.yudao.module.iot.service.alert;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.recrod.IotAlertRecordPageReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
|
|
|
|
|
@ -8,19 +9,26 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertRecordDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.alert.IotAlertRecordMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.framework.job.core.IotSchedulerManager;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.framework.mqtt.config.ProvincialPlatformMqttProperties;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.job.alert.IotAlertRecordPushJob;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.quartz.SchedulerException;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IoT 告警记录 Service 实现类
|
|
|
|
|
*
|
|
|
|
|
* @author 芋道源码
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@Validated
|
|
|
|
|
public class IotAlertRecordServiceImpl implements IotAlertRecordService {
|
|
|
|
|
@ -31,6 +39,12 @@ public class IotAlertRecordServiceImpl implements IotAlertRecordService {
|
|
|
|
|
@Resource
|
|
|
|
|
private IotDeviceService deviceService;
|
|
|
|
|
|
|
|
|
|
@Resource(name = "iotSchedulerManager")
|
|
|
|
|
private IotSchedulerManager schedulerManager;
|
|
|
|
|
|
|
|
|
|
@Resource(required = false)
|
|
|
|
|
private ProvincialPlatformMqttProperties mqttProperties;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IotAlertRecordDO getAlertRecord(Long id) {
|
|
|
|
|
return alertRecordMapper.selectById(id);
|
|
|
|
|
@ -54,6 +68,11 @@ public class IotAlertRecordServiceImpl implements IotAlertRecordService {
|
|
|
|
|
// 批量更新告警记录的处理状态
|
|
|
|
|
alertRecordMapper.updateList(ids, IotAlertRecordDO.builder()
|
|
|
|
|
.processStatus(true).processRemark(processRemark).build());
|
|
|
|
|
|
|
|
|
|
// 停止所有相关告警的推送定时任务
|
|
|
|
|
for (Long alertRecordId : ids) {
|
|
|
|
|
stopAlertPushJob(alertRecordId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ -77,4 +96,79 @@ public class IotAlertRecordServiceImpl implements IotAlertRecordService {
|
|
|
|
|
return record.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Long createAlertRecord(IotAlertRecordDO alertRecordDO) {
|
|
|
|
|
alertRecordMapper.insert(alertRecordDO);
|
|
|
|
|
|
|
|
|
|
// 启动定时任务推送告警记录到省平台
|
|
|
|
|
startAlertPushJob(alertRecordDO.getId());
|
|
|
|
|
|
|
|
|
|
return alertRecordDO.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 启动告警推送定时任务
|
|
|
|
|
*
|
|
|
|
|
* @param alertRecordId 告警记录编号
|
|
|
|
|
*/
|
|
|
|
|
private void startAlertPushJob(Long alertRecordId) {
|
|
|
|
|
// 检查是否启用省平台MQTT推送
|
|
|
|
|
if (mqttProperties == null || !Boolean.TRUE.equals(mqttProperties.getEnabled())) {
|
|
|
|
|
log.debug("[startAlertPushJob][省平台MQTT推送未启用,跳过启动定时任务,告警ID: {}]", alertRecordId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 构建任务名称和数据
|
|
|
|
|
String jobName = buildAlertPushJobName(alertRecordId);
|
|
|
|
|
Map<String, Object> jobDataMap = MapUtil.of(IotAlertRecordPushJob.JOB_DATA_KEY_ALERT_RECORD_ID, alertRecordId);
|
|
|
|
|
|
|
|
|
|
// 构建CRON表达式:每N秒执行一次(从配置中读取)
|
|
|
|
|
Integer pushIntervalSeconds = mqttProperties.getPushIntervalSeconds();
|
|
|
|
|
String cronExpression = String.format("0/%d * * * * ?", pushIntervalSeconds);
|
|
|
|
|
|
|
|
|
|
// 注册定时任务
|
|
|
|
|
schedulerManager.addOrUpdateJob(
|
|
|
|
|
IotAlertRecordPushJob.class,
|
|
|
|
|
jobName,
|
|
|
|
|
cronExpression,
|
|
|
|
|
jobDataMap
|
|
|
|
|
);
|
|
|
|
|
log.info("[startAlertPushJob][启动告警推送定时任务成功,告警ID: {}, 推送间隔: {}秒]",
|
|
|
|
|
alertRecordId, pushIntervalSeconds);
|
|
|
|
|
} catch (SchedulerException e) {
|
|
|
|
|
log.error("[startAlertPushJob][启动告警推送定时任务失败,告警ID: {}]", alertRecordId, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止告警推送定时任务
|
|
|
|
|
*
|
|
|
|
|
* @param alertRecordId 告警记录编号
|
|
|
|
|
*/
|
|
|
|
|
private void stopAlertPushJob(Long alertRecordId) {
|
|
|
|
|
// 检查是否启用省平台MQTT推送
|
|
|
|
|
if (mqttProperties == null || !Boolean.TRUE.equals(mqttProperties.getEnabled())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String jobName = buildAlertPushJobName(alertRecordId);
|
|
|
|
|
schedulerManager.deleteJob(jobName);
|
|
|
|
|
log.info("[stopAlertPushJob][停止告警推送定时任务成功,告警ID: {}]", alertRecordId);
|
|
|
|
|
} catch (SchedulerException e) {
|
|
|
|
|
log.error("[stopAlertPushJob][停止告警推送定时任务失败,告警ID: {}]", alertRecordId, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建告警推送任务名称
|
|
|
|
|
*
|
|
|
|
|
* @param alertRecordId 告警记录编号
|
|
|
|
|
* @return 任务名称
|
|
|
|
|
*/
|
|
|
|
|
private String buildAlertPushJobName(Long alertRecordId) {
|
|
|
|
|
return String.format("%s_%d", IotAlertRecordPushJob.class.getSimpleName(), alertRecordId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|