197 lines
11 KiB
Java
197 lines
11 KiB
Java
package com.zhgd.xmgl.task;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.convert.Convert;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.zhgd.xmgl.call.factory.DevSmsManufacturerFactory;
|
|
import com.zhgd.xmgl.enums.ParamEnum;
|
|
import com.zhgd.xmgl.modules.basicdata.constant.DictionaryConstant;
|
|
import com.zhgd.xmgl.modules.basicdata.entity.DictionaryItem;
|
|
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
|
|
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
|
|
import com.zhgd.xmgl.modules.basicdata.service.impl.DictionaryItemServiceImpl;
|
|
import com.zhgd.xmgl.modules.basicdata.service.impl.NoticeServiceImpl;
|
|
import com.zhgd.xmgl.modules.environment.entity.EnvironmentAlarm;
|
|
import com.zhgd.xmgl.modules.environment.entity.EnvironmentAlarmType;
|
|
import com.zhgd.xmgl.modules.environment.service.IEnvironmentAlarmService;
|
|
import com.zhgd.xmgl.modules.environment.service.IEnvironmentAlarmTypeService;
|
|
import com.zhgd.xmgl.modules.project.entity.EnableMessageDevRule;
|
|
import com.zhgd.xmgl.modules.project.entity.Project;
|
|
import com.zhgd.xmgl.modules.project.service.IEnableMessageDevRuleService;
|
|
import com.zhgd.xmgl.modules.project.service.IProjectService;
|
|
import com.zhgd.xmgl.modules.video.entity.AiAnalyseHardWareAlarmRecord;
|
|
import com.zhgd.xmgl.modules.video.service.IAiAnalyseHardWareAlarmRecordService;
|
|
import com.zhgd.xmgl.modules.xz.entity.XzAiDeductRule;
|
|
import com.zhgd.xmgl.modules.xz.service.IXzAiDeductRuleService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.javacrumbs.shedlock.core.SchedulerLock;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.*;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Slf4j
|
|
@Component
|
|
@RestController
|
|
@RequestMapping("xmgl/task")
|
|
public class MessageDevRuleTask {
|
|
@Lazy
|
|
@Autowired
|
|
ISystemUserService systemUserService;
|
|
@Lazy
|
|
@Autowired
|
|
private DevSmsManufacturerFactory devSmsManufacturerFactory;
|
|
@Lazy
|
|
@Resource
|
|
private IEnvironmentAlarmService environmentAlarmService;
|
|
@Lazy
|
|
@Autowired
|
|
private IEnableMessageDevRuleService enableMessageDevRuleService;
|
|
@Lazy
|
|
@Autowired
|
|
private IEnvironmentAlarmTypeService environmentAlarmTypeService;
|
|
@Lazy
|
|
@Autowired
|
|
private NoticeServiceImpl noticeService;
|
|
@Lazy
|
|
@Autowired
|
|
private IAiAnalyseHardWareAlarmRecordService aiAnalyseHardWareAlarmRecordService;
|
|
@Lazy
|
|
@Autowired
|
|
private DictionaryItemServiceImpl dictionaryItemService;
|
|
@Lazy
|
|
@Autowired
|
|
private IProjectService projectService;
|
|
@Lazy
|
|
@Autowired
|
|
private IXzAiDeductRuleService xzAiDeductRuleService;
|
|
|
|
/**
|
|
* 发送设备的报警通知
|
|
*/
|
|
@SchedulerLock(name = "sendDevMessage", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
|
|
@Scheduled(cron = "0 0 */1 * * ?")
|
|
@RequestMapping("sendDevMessage")
|
|
public void sendDevMessage() {
|
|
try {
|
|
Map<Long, EnvironmentAlarmType> environmentTypeMap = environmentAlarmTypeService.list().stream().collect(Collectors.toMap(EnvironmentAlarmType::getId, Function.identity(), (o1, o2) -> o1));
|
|
List<Project> projects = projectService.list(new LambdaQueryWrapper<Project>());
|
|
Map<String, Map<String, Map<String, List<EnvironmentAlarm>>>> devSn2Parameter2LevelEnvironmentAlarmMap = environmentAlarmService.list(new LambdaQueryWrapper<EnvironmentAlarm>()
|
|
.ge(EnvironmentAlarm::getAlarmTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
|
|
.le(EnvironmentAlarm::getAlarmTime, new Date())
|
|
).stream().collect(Collectors.groupingBy(EnvironmentAlarm::getDeviceId,
|
|
Collectors.groupingBy(ea -> environmentTypeMap.get(ea.getAlarmTypeId()).getAlarmType(),
|
|
Collectors.groupingBy(ea -> Objects.equals(ea.getType(), 0) ? "报警" : "预警"))));
|
|
List<EnableMessageDevRule> ruleList = enableMessageDevRuleService.list(new LambdaQueryWrapper<EnableMessageDevRule>().eq(EnableMessageDevRule::getIsEnabled, 1));
|
|
Map<Long, SystemUser> userMap = systemUserService.list(new LambdaQueryWrapper<SystemUser>()).stream().collect(Collectors.toMap(SystemUser::getUserId, Function.identity(), (o1, o2) -> o1));
|
|
Map<String, List<EnableMessageDevRule>> moduleMap = ruleList.stream().collect(Collectors.groupingBy(EnableMessageDevRule::getMonitoringModuleName));
|
|
for (Project project : projects) {
|
|
List<DictionaryItem> dictList = dictionaryItemService.getDictList(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, project.getProjectSn());
|
|
Map<String, DictionaryItem> dictDataMap = dictList.stream().collect(Collectors.toMap(DictionaryItem::getData, Function.identity(), (o1, o2) -> o1));
|
|
Map<Long, XzAiDeductRule> dicIdMap = xzAiDeductRuleService.list(new LambdaQueryWrapper<XzAiDeductRule>()
|
|
.eq(XzAiDeductRule::getProjectSn, project.getProjectSn())).stream().collect(Collectors.toMap(XzAiDeductRule::getDictionaryItemId, Function.identity(), (o1, o2) -> o1));
|
|
Map<String, Map<String, Map<String, List<AiAnalyseHardWareAlarmRecord>>>> devSn2Parameter2LevelAiMap = aiAnalyseHardWareAlarmRecordService.list(new LambdaQueryWrapper<AiAnalyseHardWareAlarmRecord>()
|
|
.ge(AiAnalyseHardWareAlarmRecord::getCreateTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
|
|
.le(AiAnalyseHardWareAlarmRecord::getCreateTime, new Date())
|
|
).stream().filter(r -> {
|
|
return Objects.nonNull(Optional.ofNullable(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(XzAiDeductRule::getLevel).orElse(null));
|
|
}).collect(Collectors.groupingBy(AiAnalyseHardWareAlarmRecord::getHardwareId,
|
|
Collectors.groupingBy(r1 -> dictDataMap.get(Convert.toStr(r1.getAlarmType())).getName(),
|
|
Collectors.groupingBy(r -> Optional.of(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(r1 -> {
|
|
return xzAiDeductRuleService.getLevelName(r1.getLevel());
|
|
}).orElse(null)))));
|
|
for (Map.Entry<String, List<EnableMessageDevRule>> moduleEntry : moduleMap.entrySet()) {
|
|
List<EnableMessageDevRule> entryRules = moduleEntry.getValue();
|
|
for (EnableMessageDevRule rule : entryRules) {
|
|
//扬尘
|
|
if (Objects.equals(ParamEnum.MessageModuleNameEnum.YCJC.getDesc(), moduleEntry.getKey())) {
|
|
List<EnvironmentAlarm> filterAlarms = Optional.ofNullable(devSn2Parameter2LevelEnvironmentAlarmMap.get(rule.getDevSn()))
|
|
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
|
|
filterAlarms = filterAlarms.stream().filter(alarm ->
|
|
DateUtil.compare(alarm.getAlarmTime(), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0
|
|
&& DateUtil.compare(alarm.getAlarmTime(), new Date()) <= 0).collect(Collectors.toList());
|
|
//sms
|
|
if (CollUtil.isNotEmpty(filterAlarms)) {
|
|
sendNotice(rule, filterAlarms.size(), userMap, "7");
|
|
}
|
|
}
|
|
|
|
//AI
|
|
if (Objects.equals(ParamEnum.MessageModuleNameEnum.AIJC.getDesc(), moduleEntry.getKey())) {
|
|
List<AiAnalyseHardWareAlarmRecord> filterAlarms = Optional.ofNullable(devSn2Parameter2LevelAiMap.get(rule.getDevSn()))
|
|
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
|
|
filterAlarms = filterAlarms.stream().filter(alarm ->
|
|
DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0
|
|
&& DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), new Date()) <= 0).collect(Collectors.toList());
|
|
//sms
|
|
if (CollUtil.isNotEmpty(filterAlarms)) {
|
|
sendNotice(rule, filterAlarms.size(), userMap, "8");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("发送设备的报警通知", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发送通知
|
|
*
|
|
* @param rule
|
|
* @param filterAlarms
|
|
* @param userMap
|
|
* @param type
|
|
*/
|
|
private void sendNotice(EnableMessageDevRule rule, int filterAlarms, Map<Long, SystemUser> userMap, String type) {
|
|
if (Objects.equals(rule.getEnableSmsNotification(), 1) && isSmsSendTime(rule)) {
|
|
String text = StrUtil.format("{}{}{}异常,近{}小时累计报警{}次!",
|
|
rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), rule.getSmsPushFrequency(), filterAlarms);
|
|
List<String> templateParams = Arrays.asList(rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), Convert.toStr(rule.getSmsPushFrequency()), Convert.toStr(filterAlarms));
|
|
enableMessageDevRuleService.sendSms(rule, templateParams, text, userMap);
|
|
}
|
|
//系统站内
|
|
if (Objects.equals(rule.getEnableSystemMessagePush(), 1) && isSystemSendTime(rule)) {
|
|
enableMessageDevRuleService.sendSystemMessage(rule, type);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 是需要发送的时间了
|
|
*
|
|
* @param rule
|
|
* @return
|
|
*/
|
|
private boolean isSystemSendTime(EnableMessageDevRule rule) {
|
|
int hour = DateUtil.hour(new Date(), true);
|
|
return Objects.nonNull(rule.getMessagePushFrequency()) && Objects.nonNull(rule.getMessagePushTimeBegin()) && Objects.nonNull(rule.getMessagePushTimeEnd())
|
|
&& !Objects.equals(rule.getMessagePushFrequency(), 0) && hour % rule.getMessagePushFrequency() == 0
|
|
&& rule.getMessagePushTimeBegin().getHour() <= hour && rule.getMessagePushTimeEnd().getHour() >= hour;
|
|
}
|
|
|
|
/**
|
|
* 是需要发送的时间了
|
|
*
|
|
* @param rule
|
|
* @return
|
|
*/
|
|
private boolean isSmsSendTime(EnableMessageDevRule rule) {
|
|
int hour = DateUtil.hour(new Date(), true);
|
|
return Objects.nonNull(rule.getSmsPushFrequency()) && Objects.nonNull(rule.getSmsPushTimeBegin()) && Objects.nonNull(rule.getSmsPushTimeEnd())
|
|
&& !Objects.equals(rule.getSmsPushFrequency(), 0) && hour % rule.getSmsPushFrequency() == 0
|
|
&& rule.getSmsPushTimeBegin().getHour() <= hour && rule.getSmsPushTimeEnd().getHour() >= hour;
|
|
}
|
|
|
|
|
|
}
|