规则报警和异步线程池优化2

This commit is contained in:
guoshengxiong 2025-10-25 16:41:35 +08:00
parent 8447b867f3
commit 8d2e1a2e08
7 changed files with 184 additions and 96 deletions

View File

@ -120,7 +120,7 @@ public class AsyncAiAnalyse {
} }
/** /**
* 发送消息 * 发送通知规则消息
* *
* @param typeName * @param typeName
* @param location * @param location
@ -141,18 +141,20 @@ public class AsyncAiAnalyse {
); );
if (rule != null) { if (rule != null) {
String monitoringLevelName = xzAiDeductRuleService.getLevelName(rule.getLevel()); String monitoringLevelName = xzAiDeductRuleService.getLevelName(rule.getLevel());
EnableMessageDevRule messageDevRule = enableMessageDevRuleService.getRule(ParamEnum.MessageModuleNameEnum.AIJC, hardwareId, dictionaryItem.getName(), monitoringLevelName); EnableMessageDevRule messageDevRule = enableMessageDevRuleService.getEnableRule(ParamEnum.MessageModuleNameEnum.AIJC, hardwareId, dictionaryItem.getName(), monitoringLevelName, projectSn);
if (messageDevRule != null) { if (messageDevRule != null) {
if (Objects.equals(messageDevRule.getEnableSmsNotification(), 1)) { if (Objects.equals(messageDevRule.getEnableSmsNotification(), 1) && Objects.equals(messageDevRule.getSmsPushFrequency(),0)) {
List<String> templateParams = new ArrayList<>(); List<String> templateParams = new ArrayList<>();
//报警类型报警名称设备名称报警值阈值超标时间超标量 //报警类型报警名称设备名称报警值阈值超标时间超标量
templateParams.add(typeName); templateParams.add(typeName);
templateParams.add(location); templateParams.add(location);
String text = StrUtil.format("AI警报{},位置{}", typeName, location); String text = StrUtil.format("AI警报{},位置{}", typeName, location);
enableMessageDevRuleService.sendSms(messageDevRule, templateParams, text, null); enableMessageDevRuleService.sendSmsIfEnable(messageDevRule, templateParams, text, null);
} }
enableMessageDevRuleService.sendSystemMessage(messageDevRule, "8"); if (Objects.equals(messageDevRule.getMessagePushFrequency(),0)) {
enableMessageDevRuleService.sendBroadcast(messageDevRule); enableMessageDevRuleService.sendSystemMessageIfEnable(messageDevRule, "8");
}
enableMessageDevRuleService.sendBroadcastIfEnable(messageDevRule);
} }
} }
}); });

View File

@ -810,7 +810,7 @@ public class EnvironmentAlarmServiceImpl extends ServiceImpl<EnvironmentAlarmMap
} }
/** /**
* 发送短信 * 发送通知规则
* @param devname * @param devname
* @param environmentAlarm * @param environmentAlarm
* @param alarmName * @param alarmName
@ -821,9 +821,9 @@ public class EnvironmentAlarmServiceImpl extends ServiceImpl<EnvironmentAlarmMap
String threshold = NumberUtil.sub(environmentAlarm.getAlarmValue(), environmentAlarm.getExceed()) + ""; String threshold = NumberUtil.sub(environmentAlarm.getAlarmValue(), environmentAlarm.getExceed()) + "";
String time = DateUtil.formatDateTime(environmentAlarm.getAlarmTime()); String time = DateUtil.formatDateTime(environmentAlarm.getAlarmTime());
String monitoringParameterName = environmentAlarmTypeService.getById(environmentAlarm.getAlarmTypeId()).getAlarmType(); String monitoringParameterName = environmentAlarmTypeService.getById(environmentAlarm.getAlarmTypeId()).getAlarmType();
EnableMessageDevRule messageDevRule = enableMessageDevRuleService.getRule(ParamEnum.MessageModuleNameEnum.YCJC, environmentAlarm.getDeviceId(), monitoringParameterName, monitoringLevelName); EnableMessageDevRule messageDevRule = enableMessageDevRuleService.getEnableRule(ParamEnum.MessageModuleNameEnum.YCJC, environmentAlarm.getDeviceId(), monitoringParameterName, monitoringLevelName, environmentAlarm.getProjectSn());
if (messageDevRule != null) { if (messageDevRule != null) {
if (Objects.equals(messageDevRule.getEnableSmsNotification(), 1)) { if (Objects.equals(messageDevRule.getEnableSmsNotification(), 1) && Objects.equals(messageDevRule.getSmsPushFrequency(),0)) {
List<String> templateParams = new ArrayList<>(); List<String> templateParams = new ArrayList<>();
//报警类型报警名称设备名称报警值阈值超标时间超标量 //报警类型报警名称设备名称报警值阈值超标时间超标量
templateParams.add(monitoringLevelName); templateParams.add(monitoringLevelName);
@ -835,10 +835,12 @@ public class EnvironmentAlarmServiceImpl extends ServiceImpl<EnvironmentAlarmMap
templateParams.add(environmentAlarm.getExceed() + ""); templateParams.add(environmentAlarm.getExceed() + "");
String text = StrUtil.format("扬尘超标警报!报警类型:{},报警名称:{},设备名称:{},报警值:{},阈值:{},超标时间:{},超标量:{}", String text = StrUtil.format("扬尘超标警报!报警类型:{},报警名称:{},设备名称:{},报警值:{},阈值:{},超标时间:{},超标量:{}",
monitoringLevelName, alarmName, devname, environmentAlarm.getAlarmValue() + "", threshold, time, environmentAlarm.getExceed() + ""); monitoringLevelName, alarmName, devname, environmentAlarm.getAlarmValue() + "", threshold, time, environmentAlarm.getExceed() + "");
enableMessageDevRuleService.sendSms(messageDevRule, templateParams, text, null); enableMessageDevRuleService.sendSmsIfEnable(messageDevRule, templateParams, text, null);
} }
enableMessageDevRuleService.sendSystemMessage(messageDevRule, "7"); if (Objects.equals(messageDevRule.getMessagePushFrequency(), 0)) {
enableMessageDevRuleService.sendBroadcast(messageDevRule); enableMessageDevRuleService.sendSystemMessageIfEnable(messageDevRule, "7");
}
enableMessageDevRuleService.sendBroadcastIfEnable(messageDevRule);
} }
}); });
} }

View File

@ -222,6 +222,7 @@ public class EnableMessageDevController {
Map<String, EnableMessageDev> devMap = enableMessageDevService.list(new LambdaQueryWrapper<EnableMessageDev>() Map<String, EnableMessageDev> devMap = enableMessageDevService.list(new LambdaQueryWrapper<EnableMessageDev>()
.in(EnableMessageDev::getDevSn, devSnList) .in(EnableMessageDev::getDevSn, devSnList)
.eq(EnableMessageDev::getMonitoringModule, monitoringModule) .eq(EnableMessageDev::getMonitoringModule, monitoringModule)
.eq(EnableMessageDev::getProjectSn, projectSn)
).stream().collect(Collectors.toMap(EnableMessageDev::getDevSn, Function.identity(), (o1, o2) -> o1)); ).stream().collect(Collectors.toMap(EnableMessageDev::getDevSn, Function.identity(), (o1, o2) -> o1));
for (String devSn : devSnList) { for (String devSn : devSnList) {
EnableMessageDev dev = devMap.get(devSn); EnableMessageDev dev = devMap.get(devSn);

View File

@ -2,6 +2,7 @@ package com.zhgd.xmgl.modules.project.controller;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhgd.annotation.OperLog; import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result; import com.zhgd.jeecg.common.api.vo.Result;
@ -23,6 +24,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -231,6 +233,7 @@ public class EnableMessageDevRuleController {
@OperLog(operModul = "通知设备规则管理", operType = "", operDesc = "根据devSn、模块、参数、等级去编辑通知设备规则信息") @OperLog(operModul = "通知设备规则管理", operType = "", operDesc = "根据devSn、模块、参数、等级去编辑通知设备规则信息")
@ApiOperation(value = "根据devSn、模块、参数、等级去编辑通知设备规则信息", notes = "根据devSn、模块、参数、等级去编辑通知设备规则信息", httpMethod = "POST") @ApiOperation(value = "根据devSn、模块、参数、等级去编辑通知设备规则信息", notes = "根据devSn、模块、参数、等级去编辑通知设备规则信息", httpMethod = "POST")
@PostMapping(value = "/editByCondition") @PostMapping(value = "/editByCondition")
@Transactional(rollbackFor = Exception.class)
public Result editByCondition(@RequestBody EnableMessageDevRuleDto enableMessageDevRuleDto) { public Result editByCondition(@RequestBody EnableMessageDevRuleDto enableMessageDevRuleDto) {
String devSn = enableMessageDevRuleDto.getDevSn(); String devSn = enableMessageDevRuleDto.getDevSn();
String monitoringModuleName = enableMessageDevRuleDto.getMonitoringModuleName(); String monitoringModuleName = enableMessageDevRuleDto.getMonitoringModuleName();
@ -247,6 +250,13 @@ public class EnableMessageDevRuleController {
} else { } else {
enableMessageDevRuleDto.setId(db.getId()); enableMessageDevRuleDto.setId(db.getId());
enableMessageDevRuleService.updateById(enableMessageDevRuleDto); enableMessageDevRuleService.updateById(enableMessageDevRuleDto);
enableMessageDevRuleService.update(null, new LambdaUpdateWrapper<EnableMessageDevRule>()
.set(EnableMessageDevRule::getMessagePushTimeBegin, enableMessageDevRuleDto.getMessagePushTimeBegin())
.set(EnableMessageDevRule::getMessagePushTimeEnd, enableMessageDevRuleDto.getMessagePushTimeEnd())
.set(EnableMessageDevRule::getSmsPushTimeBegin, enableMessageDevRuleDto.getSmsPushTimeBegin())
.set(EnableMessageDevRule::getSmsPushTimeEnd, enableMessageDevRuleDto.getSmsPushTimeEnd())
.eq(EnableMessageDevRule::getId, db.getId())
);
} }
return Result.ok(); return Result.ok();
} }

View File

@ -69,15 +69,16 @@ public interface IEnableMessageDevRuleService extends IService<EnableMessageDevR
EnableMessageDevRuleVo queryById(String id); EnableMessageDevRuleVo queryById(String id);
/** /**
* 是否开启实时推送 * 获取开启的实时推送规则
* *
* @param moduleEnum * @param moduleEnum
* @param devSn * @param devSn
* @param monitoringParameterName * @param monitoringParameterName
* @param monitoringLevelName * @param monitoringLevelName
* @param projectSn
* @return * @return
*/ */
EnableMessageDevRule getRule(ParamEnum.MessageModuleNameEnum moduleEnum, String devSn, String monitoringParameterName, String monitoringLevelName); EnableMessageDevRule getEnableRule(ParamEnum.MessageModuleNameEnum moduleEnum, String devSn, String monitoringParameterName, String monitoringLevelName, String projectSn);
/** /**
@ -89,20 +90,20 @@ public interface IEnableMessageDevRuleService extends IService<EnableMessageDevR
* @param userMap 不传就会查数据库 * @param userMap 不传就会查数据库
* @return * @return
*/ */
void sendSms(EnableMessageDevRule rule, List<String> templateParams, String text, @Nullable Map<Long, SystemUser> userMap); void sendSmsIfEnable(EnableMessageDevRule rule, List<String> templateParams, String text, @Nullable Map<Long, SystemUser> userMap);
/** /**
* 发送系统站内消息 * 开启就发送系统站内消息
* *
* @param rule * @param rule
* @param type * @param type
*/ */
void sendSystemMessage(EnableMessageDevRule rule, String type); void sendSystemMessageIfEnable(EnableMessageDevRule rule, String type);
/** /**
* 发送广播 * 开启就发送广播
* *
* @param rule * @param rule
*/ */
void sendBroadcast(EnableMessageDevRule rule); void sendBroadcastIfEnable(EnableMessageDevRule rule);
} }

View File

@ -35,6 +35,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -68,6 +69,52 @@ public class EnableMessageDevRuleServiceImpl extends ServiceImpl<EnableMessageDe
@Autowired @Autowired
private NoticeServiceImpl noticeService; private NoticeServiceImpl noticeService;
/**
* 当前时间在系统消息推送时间
*
* @param rule
* @return
*/
public static boolean isInSystemTime(EnableMessageDevRule rule) {
LocalTime currentTime = LocalTime.now();
if (rule.getMessagePushTimeBegin() == null || rule.getMessagePushTimeEnd() == null) {
return true;
}
// 跨天情况结束时间 < 开始时间
if (rule.getMessagePushTimeEnd().isBefore(rule.getMessagePushTimeBegin())) {
return !currentTime.isBefore(rule.getMessagePushTimeBegin()) || !currentTime.isAfter(rule.getMessagePushTimeEnd());
}
// 正常情况
else {
return !currentTime.isBefore(rule.getMessagePushTimeBegin()) && !currentTime.isAfter(rule.getMessagePushTimeEnd());
}
}
/**
* 当前时间在短信消息推送时间
*
* @param rule
* @return
*/
public static boolean isInSmsTime(EnableMessageDevRule rule) {
LocalTime currentTime = LocalTime.now();
if (rule.getSmsPushTimeBegin() == null || rule.getSmsPushTimeEnd() == null) {
return true;
}
// 跨天情况结束时间 < 开始时间
if (rule.getSmsPushTimeEnd().isBefore(rule.getSmsPushTimeBegin())) {
return !currentTime.isBefore(rule.getSmsPushTimeBegin()) || !currentTime.isAfter(rule.getSmsPushTimeEnd());
}
// 正常情况
else {
return !currentTime.isBefore(rule.getSmsPushTimeBegin()) && !currentTime.isAfter(rule.getSmsPushTimeEnd());
}
}
@Override @Override
public IPage<EnableMessageDevRuleVo> queryPageList(HashMap<String, Object> param) { public IPage<EnableMessageDevRuleVo> queryPageList(HashMap<String, Object> param) {
QueryWrapper<EnableMessageDevRuleVo> queryWrapper = this.getQueryWrapper(param); QueryWrapper<EnableMessageDevRuleVo> queryWrapper = this.getQueryWrapper(param);
@ -127,10 +174,11 @@ public class EnableMessageDevRuleServiceImpl extends ServiceImpl<EnableMessageDe
} }
@Override @Override
public EnableMessageDevRule getRule(ParamEnum.MessageModuleNameEnum moduleEnum, String devSn, String monitoringParameterName, String monitoringLevelName) { public EnableMessageDevRule getEnableRule(ParamEnum.MessageModuleNameEnum moduleEnum, String devSn, String monitoringParameterName, String monitoringLevelName, String projectSn) {
EnableMessageDev messageDev = enableMessageDevService.getOne(new LambdaQueryWrapper<EnableMessageDev>() EnableMessageDev messageDev = enableMessageDevService.getOne(new LambdaQueryWrapper<EnableMessageDev>()
.eq(EnableMessageDev::getMonitoringModule, moduleEnum.getValue()) .eq(EnableMessageDev::getMonitoringModule, moduleEnum.getValue())
.eq(EnableMessageDev::getDevSn, devSn) .eq(EnableMessageDev::getDevSn, devSn)
.eq(EnableMessageDev::getProjectSn, projectSn)
); );
if (messageDev == null || Objects.equals(messageDev.getIsEnable(), 0)) { if (messageDev == null || Objects.equals(messageDev.getIsEnable(), 0)) {
return null; return null;
@ -141,6 +189,7 @@ public class EnableMessageDevRuleServiceImpl extends ServiceImpl<EnableMessageDe
.eq(EnableMessageDevRule::getProjectSn, messageDev.getProjectSn()) .eq(EnableMessageDevRule::getProjectSn, messageDev.getProjectSn())
.eq(EnableMessageDevRule::getMonitoringParameterName, monitoringParameterName) .eq(EnableMessageDevRule::getMonitoringParameterName, monitoringParameterName)
.eq(EnableMessageDevRule::getMonitoringLevelName, monitoringLevelName) .eq(EnableMessageDevRule::getMonitoringLevelName, monitoringLevelName)
.eq(EnableMessageDevRule::getIsEnabled, 1)
); );
} }
@ -159,25 +208,24 @@ public class EnableMessageDevRuleServiceImpl extends ServiceImpl<EnableMessageDe
} }
@Override @Override
public void sendSms(EnableMessageDevRule rule, List<String> templateParams, String text, Map<Long, SystemUser> userMap) { public void sendSmsIfEnable(EnableMessageDevRule rule, List<String> templateParams, String text, Map<Long, SystemUser> userMap) {
DevSmsManufacturer messageManufacturer = devSmsManufacturerFactory.getMessageManufacturer(rule.getProjectSn()); if (rule != null && Objects.equals(rule.getEnableSmsNotification(), 1) && EnableMessageDevRuleServiceImpl.isInSmsTime(rule)) {
if (messageManufacturer != null) { DevSmsManufacturer messageManufacturer = devSmsManufacturerFactory.getMessageManufacturer(rule.getProjectSn());
if (userMap == null) { if (messageManufacturer != null) {
userMap = systemUserService.list(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getSn, rule.getProjectSn())).stream().collect(Collectors.toMap(SystemUser::getUserId, Function.identity(), (o1, o2) -> o1)); if (userMap == null) {
} userMap = systemUserService.list(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getSn, rule.getProjectSn())).stream().collect(Collectors.toMap(SystemUser::getUserId, Function.identity(), (o1, o2) -> o1));
List<String> phoneNums = this.getPhoneNums(rule.getSmsAlarmRecipientIds(), userMap); }
if (CollUtil.isNotEmpty(phoneNums)) { List<String> phoneNums = this.getPhoneNums(rule.getSmsAlarmRecipientIds(), userMap);
messageManufacturer.sendMsg(phoneNums, rule.getNotificationSmsTemplateId(), templateParams, text); if (CollUtil.isNotEmpty(phoneNums)) {
messageManufacturer.sendMsg(phoneNums, rule.getNotificationSmsTemplateId(), templateParams, text);
}
} }
} }
} }
@Override @Override
public void sendSystemMessage(EnableMessageDevRule rule, String type) { public void sendSystemMessageIfEnable(EnableMessageDevRule rule, String type) {
if (rule == null) { if (rule != null && Objects.equals(rule.getEnableSystemMessagePush(), 1) && StrUtil.isNotBlank(rule.getAlarmRecipientIds()) && EnableMessageDevRuleServiceImpl.isInSystemTime(rule)) {
return;
}
if (Objects.equals(rule.getEnableSystemMessagePush(), 1) && StrUtil.isNotBlank(rule.getAlarmRecipientIds())) {
List<Long> ids = StrUtil.split(rule.getAlarmRecipientIds(), ",").stream().map(Convert::toLong).collect(Collectors.toList()); List<Long> ids = StrUtil.split(rule.getAlarmRecipientIds(), ",").stream().map(Convert::toLong).collect(Collectors.toList());
NoticeMessagePromptTone tone = null; NoticeMessagePromptTone tone = null;
//系统站内声音提示 //系统站内声音提示
@ -191,12 +239,14 @@ public class EnableMessageDevRuleServiceImpl extends ServiceImpl<EnableMessageDe
} }
@Override @Override
public void sendBroadcast(EnableMessageDevRule rule) { public void sendBroadcastIfEnable(EnableMessageDevRule rule) {
if (Objects.equals(rule.getEnableBroadcastNotification(), 1) && StrUtil.isNotBlank(rule.getBroadcastDevices()) && StrUtil.isNotBlank(rule.getVoiceFileConfiguration())) { if (Objects.equals(rule.getEnableBroadcastNotification(), 1) && StrUtil.isNotBlank(rule.getBroadcastDevices()) && StrUtil.isNotBlank(rule.getVoiceFileConfiguration())) {
BroadcastManufacturer manufacturer = broadcastManufacturerFactory.getManufacturer(new MapBuilder<String, Object>() BroadcastManufacturer manufacturer = broadcastManufacturerFactory.getManufacturer(new MapBuilder<String, Object>()
.put("projectSn", rule.getProjectSn()) .put("projectSn", rule.getProjectSn())
.build()); .build());
manufacturer.playVoiceFile(StrUtil.split(rule.getBroadcastDevices(), ","), FileUtils.urlToFileName(rule.getVoiceFileConfiguration()), FileUtils.urlToFilePath(rule.getVoiceFileConfiguration())); if (manufacturer != null) {
manufacturer.playVoiceFile(StrUtil.split(rule.getBroadcastDevices(), ","), FileUtils.urlToFileName(rule.getVoiceFileConfiguration()), FileUtils.urlToFilePath(rule.getVoiceFileConfiguration()));
}
} }
} }

View File

@ -28,6 +28,7 @@ import com.zhgd.xmgl.modules.xz.entity.XzAiDeductRule;
import com.zhgd.xmgl.modules.xz.service.IXzAiDeductRuleService; import com.zhgd.xmgl.modules.xz.service.IXzAiDeductRuleService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock; import net.javacrumbs.shedlock.core.SchedulerLock;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@ -89,25 +90,27 @@ public class MessageDevRuleTask {
try { try {
Map<Long, EnvironmentAlarmType> environmentTypeMap = environmentAlarmTypeService.list().stream().collect(Collectors.toMap(EnvironmentAlarmType::getId, Function.identity(), (o1, o2) -> o1)); 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>()); List<Project> projects = projectService.list(new LambdaQueryWrapper<Project>());
Map<String, Map<String,Map<String, Map<String, List<EnvironmentAlarm>>>>> devSn2ProjectSn2Parameter2LevelEnvironmentAlarmMap = environmentAlarmService.list(new LambdaQueryWrapper<EnvironmentAlarm>() Map<String, Map<String, Map<String, Map<String, List<EnvironmentAlarm>>>>> devSn2ProjectSn2Parameter2LevelEnvironmentAlarmMap = environmentAlarmService.list(new LambdaQueryWrapper<EnvironmentAlarm>()
.ge(EnvironmentAlarm::getAlarmTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1))) .ge(EnvironmentAlarm::getAlarmTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
.le(EnvironmentAlarm::getAlarmTime, new Date()) .le(EnvironmentAlarm::getAlarmTime, new Date())
).stream().collect(Collectors.groupingBy(EnvironmentAlarm::getDeviceId, ).stream().collect(Collectors.groupingBy(EnvironmentAlarm::getDeviceId,
Collectors.groupingBy(EnvironmentAlarm::getProjectSn, Collectors.groupingBy(EnvironmentAlarm::getProjectSn,
Collectors.groupingBy(ea -> environmentTypeMap.get(ea.getAlarmTypeId()).getAlarmType(), Collectors.groupingBy(ea -> environmentTypeMap.get(ea.getAlarmTypeId()).getAlarmType(),
Collectors.groupingBy(ea -> Objects.equals(ea.getType(), 0) ? "报警" : "预警"))))); Collectors.groupingBy(ea -> Objects.equals(ea.getType(), 0) ? "报警" : "预警")))));
List<EnableMessageDevRule> ruleList = enableMessageDevRuleService.list(new LambdaQueryWrapper<EnableMessageDevRule>().eq(EnableMessageDevRule::getIsEnabled, 1)); 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<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)); Map<String, Map<String, List<EnableMessageDevRule>>> projectSn2ModuleMap = ruleList.stream().collect(Collectors.groupingBy(EnableMessageDevRule::getProjectSn, Collectors.groupingBy(EnableMessageDevRule::getMonitoringModuleName)));
List<AiAnalyseHardWareAlarmRecord> alarmRecords = aiAnalyseHardWareAlarmRecordService.list(new LambdaQueryWrapper<AiAnalyseHardWareAlarmRecord>()
.ge(AiAnalyseHardWareAlarmRecord::getCreateTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
.le(AiAnalyseHardWareAlarmRecord::getCreateTime, DateUtil.formatDateTime(new Date())
));
for (Project project : projects) { for (Project project : projects) {
List<DictionaryItem> dictList = dictionaryItemService.getDictList(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, project.getProjectSn()); String projectSn = project.getProjectSn();
List<DictionaryItem> dictList = dictionaryItemService.getDictList(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, projectSn);
Map<String, DictionaryItem> dictDataMap = dictList.stream().collect(Collectors.toMap(DictionaryItem::getData, Function.identity(), (o1, o2) -> o1)); 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>() 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)); .eq(XzAiDeductRule::getProjectSn, projectSn)).stream().collect(Collectors.toMap(XzAiDeductRule::getDictionaryItemId, Function.identity(), (o1, o2) -> o1));
Map<String, Map<String,Map<String, Map<String, List<AiAnalyseHardWareAlarmRecord>>>>> devSn2ProjectSn2Parameter2LevelAiMap = aiAnalyseHardWareAlarmRecordService.list(new LambdaQueryWrapper<AiAnalyseHardWareAlarmRecord>() Map<String, Map<String, Map<String, Map<String, List<AiAnalyseHardWareAlarmRecord>>>>> devSn2ProjectSn2Parameter2LevelAiMap = alarmRecords.stream().filter(r -> {
.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)); 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, }).collect(Collectors.groupingBy(AiAnalyseHardWareAlarmRecord::getHardwareId,
Collectors.groupingBy(AiAnalyseHardWareAlarmRecord::getProjectSn, Collectors.groupingBy(AiAnalyseHardWareAlarmRecord::getProjectSn,
@ -115,6 +118,10 @@ public class MessageDevRuleTask {
Collectors.groupingBy(r -> Optional.of(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(r1 -> { Collectors.groupingBy(r -> Optional.of(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(r1 -> {
return xzAiDeductRuleService.getLevelName(r1.getLevel()); return xzAiDeductRuleService.getLevelName(r1.getLevel());
}).orElse(null)))))); }).orElse(null))))));
Map<String, List<EnableMessageDevRule>> moduleMap = projectSn2ModuleMap.get(projectSn);
if (CollUtil.isEmpty(moduleMap)) {
continue;
}
for (Map.Entry<String, List<EnableMessageDevRule>> moduleEntry : moduleMap.entrySet()) { for (Map.Entry<String, List<EnableMessageDevRule>> moduleEntry : moduleMap.entrySet()) {
List<EnableMessageDevRule> entryRules = moduleEntry.getValue(); List<EnableMessageDevRule> entryRules = moduleEntry.getValue();
for (EnableMessageDevRule rule : entryRules) { for (EnableMessageDevRule rule : entryRules) {
@ -123,26 +130,41 @@ public class MessageDevRuleTask {
List<EnvironmentAlarm> filterAlarms = Optional.ofNullable(devSn2ProjectSn2Parameter2LevelEnvironmentAlarmMap.get(rule.getDevSn())) List<EnvironmentAlarm> filterAlarms = Optional.ofNullable(devSn2ProjectSn2Parameter2LevelEnvironmentAlarmMap.get(rule.getDevSn()))
.map(m -> m.get(rule.getProjectSn())) .map(m -> m.get(rule.getProjectSn()))
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>()); .map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
filterAlarms = filterAlarms.stream().filter(alarm -> List<EnvironmentAlarm> filterAlarms1 = getFilterEnvironmentAlarmRecords(filterAlarms, rule.getSmsPushFrequency());
DateUtil.compare(alarm.getAlarmTime(), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0 if (CollUtil.isNotEmpty(filterAlarms1)) {
&& DateUtil.compare(alarm.getAlarmTime(), new Date()) <= 0).collect(Collectors.toList()); sendSmsNotice(rule, filterAlarms1.size(), userMap);
//sms }
List<EnvironmentAlarm> filterAlarms2 = getFilterEnvironmentAlarmRecords(filterAlarms, rule.getMessagePushFrequency());
if (CollUtil.isNotEmpty(filterAlarms2)) {
//系统站内
if (!Objects.equals(rule.getMessagePushFrequency(), 0)) {
enableMessageDevRuleService.sendSystemMessageIfEnable(rule, "8");
}
}
if (CollUtil.isNotEmpty(filterAlarms)) { if (CollUtil.isNotEmpty(filterAlarms)) {
sendNotice(rule, filterAlarms.size(), userMap, "7"); //广播
enableMessageDevRuleService.sendBroadcastIfEnable(rule);
} }
} }
//AI //AI
if (Objects.equals(ParamEnum.MessageModuleNameEnum.AIJC.getDesc(), moduleEntry.getKey())) { else if (Objects.equals(ParamEnum.MessageModuleNameEnum.AIJC.getDesc(), moduleEntry.getKey())) {
List<AiAnalyseHardWareAlarmRecord> filterAlarms = Optional.ofNullable(devSn2ProjectSn2Parameter2LevelAiMap.get(rule.getDevSn())) List<AiAnalyseHardWareAlarmRecord> filterAlarms = Optional.ofNullable(devSn2ProjectSn2Parameter2LevelAiMap.get(rule.getDevSn()))
.map(m -> m.get(rule.getProjectSn())) .map(m -> m.get(rule.getProjectSn()))
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>()); .map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
filterAlarms = filterAlarms.stream().filter(alarm -> List<AiAnalyseHardWareAlarmRecord> filterAlarms1 = getFilterAiAlarmRecords(filterAlarms, rule.getSmsPushFrequency());
DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0 if (CollUtil.isNotEmpty(filterAlarms1)) {
&& DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), new Date()) <= 0).collect(Collectors.toList()); sendSmsNotice(rule, filterAlarms1.size(), userMap);
//sms }
List<AiAnalyseHardWareAlarmRecord> filterAlarms2 = getFilterAiAlarmRecords(filterAlarms, rule.getMessagePushFrequency());
if (CollUtil.isNotEmpty(filterAlarms2)) {
//系统站内
if (!Objects.equals(rule.getMessagePushFrequency(), 0)) {
enableMessageDevRuleService.sendSystemMessageIfEnable(rule, "8");
}
}
if (CollUtil.isNotEmpty(filterAlarms)) { if (CollUtil.isNotEmpty(filterAlarms)) {
sendNotice(rule, filterAlarms.size(), userMap, "8"); //广播
enableMessageDevRuleService.sendBroadcastIfEnable(rule);
} }
} }
} }
@ -154,53 +176,53 @@ public class MessageDevRuleTask {
} }
/** /**
* 发送通知 * 根据时间和频率获取过滤后扬尘报警记录
*
* @param filterAlarms
* @param frequency
* @return
*/
@NotNull
private List<EnvironmentAlarm> getFilterEnvironmentAlarmRecords(List<EnvironmentAlarm> filterAlarms, Integer frequency) {
if (Objects.isNull(frequency) || Objects.equals(frequency, 0)) {
return new ArrayList<>();
}
return filterAlarms.stream().filter(alarm ->
DateUtil.compare(alarm.getAlarmTime(), DateUtil.offsetHour(new Date(), frequency * -1)) >= 0
&& DateUtil.compare(alarm.getAlarmTime(), new Date()) <= 0).collect(Collectors.toList());
}
/**
* 根据时间和频率获取过滤后AI报警记录
*
* @param filterAlarms
* @param frequency
* @return
*/
@NotNull
private List<AiAnalyseHardWareAlarmRecord> getFilterAiAlarmRecords(List<AiAnalyseHardWareAlarmRecord> filterAlarms, Integer frequency) {
if (Objects.isNull(frequency) || Objects.equals(frequency, 0)) {
return new ArrayList<>();
}
return filterAlarms.stream().filter(alarm ->
DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), DateUtil.offsetHour(new Date(), frequency * -1)) >= 0
&& DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), new Date()) <= 0).collect(Collectors.toList());
}
/**
* 发送sms通知
* *
* @param rule * @param rule
* @param filterAlarms * @param filterAlarms
* @param userMap * @param userMap
* @param type
*/ */
private void sendNotice(EnableMessageDevRule rule, int filterAlarms, Map<Long, SystemUser> userMap, String type) { private void sendSmsNotice(EnableMessageDevRule rule, int filterAlarms, Map<Long, SystemUser> userMap) {
if (Objects.equals(rule.getEnableSmsNotification(), 1) && isSmsSendTime(rule)) { if (Objects.equals(rule.getEnableSmsNotification(), 1) && !Objects.equals(rule.getSmsPushFrequency(), 0)) {
String text = StrUtil.format("{}{}{}异常,近{}小时累计报警{}次!", String text = StrUtil.format("{}{}{}异常,近{}小时累计报警{}次!",
rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), rule.getSmsPushFrequency(), filterAlarms); 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)); List<String> templateParams = Arrays.asList(rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), Convert.toStr(rule.getSmsPushFrequency()), Convert.toStr(filterAlarms));
enableMessageDevRuleService.sendSms(rule, templateParams, text, userMap); enableMessageDevRuleService.sendSmsIfEnable(rule, templateParams, text, userMap);
} }
//系统站内
if (Objects.equals(rule.getEnableSystemMessagePush(), 1) && isSystemSendTime(rule)) {
enableMessageDevRuleService.sendSystemMessage(rule, type);
}
//广播
enableMessageDevRuleService.sendBroadcast(rule);
} }
/**
* 是系统消息需要发送的时间了
*
* @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;
}
} }