EquipAlarmJob.java
16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.skua.modules.quartz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.skua.modules.alarmtmp.entity.AlarmCustomRuleRepository;
import com.skua.modules.alarmtmp.entity.AlarmRecordHistory;
import com.skua.modules.alarmtmp.entity.AlarmRuleConfigPublicTab;
import com.skua.modules.alarmtmp.entity.AlarmRuleLevelConfig;
import com.skua.modules.alarmtmp.mapper.AlarmRuleConfigMapper;
import com.skua.modules.alarmtmp.service.AlarmRecordHistoryService;
import com.skua.modules.alarmtmp.service.AlarmRuleLevelConfigService;
import com.skua.modules.alarmtmp.service.IAlarmCustomRuleRepositoryService;
import com.skua.modules.alarmtmp.service.IAlarmRuleConfigPublicTabService;
import com.skua.modules.alarmtmp.vo.AlarmRuleConfigAndLevelVO;
import com.skua.modules.alarmtmp.vo.SystemNoticeVO;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.modules.system.datestandard.service.ISysMonitorMetricInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.skua.modules.util.SystemNoticeUtils;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @program: skboot
* @description: 设备报警
* @author: xie chao
* @create: 2020-11-02 13:48
*/
@Slf4j
@Component
public class EquipAlarmJob implements Job {
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private SystemNoticeUtils systemNoticeUtils;
@Autowired
private ISysMonitorMetricInfoService sysMonitorMetricInfoService;
@Autowired
private AlarmRuleConfigMapper alarmRuleConfigMapper;
@Autowired
private IAlarmCustomRuleRepositoryService alarmCustomRuleRepositoryService;
@Autowired
private IAlarmRuleConfigPublicTabService alarmRuleConfigPublicTabService;
@Autowired
private AlarmRuleLevelConfigService alarmRuleLevelConfigService;
private static String prefix = "【金控数矿】";
//private static String prefix = "";
private static String departNameTemplete = "${departName}";
private static String contentTemplete = "${alarmParamName}";
private static String suffix = "故障,请处理!";
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
log.info("设备报警任务开始");
//查询pg设备警报
List<AlarmRuleConfigAndLevelVO> excessAlarmPgList = checkPgExcessAlarm();
if (excessAlarmPgList != null && excessAlarmPgList.size() > 0) {
//获取设备信号报警类别
QueryWrapper<AlarmRuleConfigPublicTab> alarmRuleConfigPublicTabQueryWrapper = new QueryWrapper<AlarmRuleConfigPublicTab>();
alarmRuleConfigPublicTabQueryWrapper.eq("java_bean_tag", "equipAlarmJob");
AlarmRuleConfigPublicTab alarmRuleConfigPublicTab = alarmRuleConfigPublicTabService.getOne(alarmRuleConfigPublicTabQueryWrapper);
//将故障报警恢复的状态修改成正常状态
updStatus(excessAlarmPgList,alarmRuleConfigPublicTab.getId());
sendMessage(excessAlarmPgList);
}
log.info("设备报警任务结束");
} catch (Exception e) {
e.printStackTrace();
log.info("设备报警任务异常:" + e);
}
}
/**
* <pre>
* 更新已恢复运行的设备故障报警状态
* </pre>
* @param excessAlarmPgList 设备信号故障报警列表
* @param alarmRuleType 设备信号故障报警类别
* @author Li Yuanyuan, 2023年12月28日 下午7:42:31
* @Description: TODO(这里描述这个方法的需求变更情况)
*/
private void updStatus(List<AlarmRuleConfigAndLevelVO> excessAlarmPgList,String alarmRuleType) {
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("alarm_rule_type", alarmRuleType);
queryWrapper.eq("handle_status", 0);
queryWrapper.orderByDesc("record_time");
List<AlarmRecordHistory> list = alarmRecordHistoryService.list(queryWrapper);
if (list != null && list.size() > 0) {
//遍历
Set<String> set = new HashSet<>();
excessAlarmPgList.forEach(v -> set.add(v.getAlarmProgramCode()));
List<AlarmRecordHistory> result = Lists.newArrayList();
list.forEach(v -> {
String alarmParamCode = v.getAlarmParamCode();
if (!set.contains(alarmParamCode)) {
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
alarmRecordHistory.setId(v.getId());
alarmRecordHistory.setHandleStatus(1);
alarmRecordHistory.setHandleUser("admin");
alarmRecordHistory.setHandleTime(DateUtils.getDate());
result.add(alarmRecordHistory);
}
});
if (result != null && result.size() > 0) {
alarmRecordHistoryService.updateBatchById(result);
}
}
}
/**
* 判断pg数据库中超标的警报
*/
public List<AlarmRuleConfigAndLevelVO> checkPgExcessAlarm() {
//存放厂ID和指标的List集合
Map<String, List<String>> departIndexCodeListMap = new HashMap<>();
//查询报警配置表
//List<NewAlarmRuleConfigVO> list = alarmRuleConfigService.getEquipAlarmInfo();
List<AlarmRuleConfigAndLevelVO> alarmRuleConfigList = alarmRuleConfigMapper.getAlarmRuleConfigList("equipAlarmJob");
//获取设备信号报警方案规则类别
QueryWrapper<AlarmCustomRuleRepository> alarmRuleConfigQueryWrapper = new QueryWrapper<AlarmCustomRuleRepository>();
alarmRuleConfigQueryWrapper.eq("bean_service_name", "equipAlarmJob");
AlarmCustomRuleRepository alarmCustomRuleRepository = alarmCustomRuleRepositoryService.getOne(alarmRuleConfigQueryWrapper);
//报警方案规则类别
String alarmCustomRuleTypeTreepath = alarmCustomRuleRepository.getAlarmRuleTypeTreepath();
//报警方案规则id
String alarmRepositoryId = alarmCustomRuleRepository.getId();
alarmRuleConfigList.forEach(v -> {
//向报警规则对象装入报警方案规则类别相关数据
String[] alarmCustomRuleTypeTreeArray = alarmCustomRuleTypeTreepath.split(",");
v.setAlarmCustomRuleTopType(alarmCustomRuleTypeTreeArray[0]);
//预留报警方案规则类别二级配置
//v.setAlarmCustomRuleSecondType(alarmCustomRuleTypeTreeArray[1]);
v.setAlarmCustomRuleType(alarmRepositoryId);
//预留报警方案id配置
//v.setAlarmCustomRuleId("");
v.setAlarmCustomRuleTypeTreepath(alarmCustomRuleTypeTreepath+","+alarmRepositoryId);
//向报警报警规则对象装入报警类别相关数据
String alarmTypeTreePath = v.getAlarmRuleTypeTreepath();
String[] treeArray = alarmTypeTreePath.split(",");
v.setAlarmRuleTopType(treeArray[0]);
v.setAlarmRuleSecondType(treeArray[1]);
//设备状态判定点位
String alarmProgramCode = v.getAlarmProgramCode();
if (StringUtils.isNotBlank(alarmProgramCode)) {
List<String> alarmProgramCodeList = departIndexCodeListMap.get(v.getDepartId());
if (alarmProgramCodeList!=null&&alarmProgramCodeList.size()>0) {
alarmProgramCodeList.add(alarmProgramCode);
} else {
alarmProgramCodeList = new ArrayList<String>();
alarmProgramCodeList.add(alarmProgramCode);
departIndexCodeListMap.put(v.getDepartId(), alarmProgramCodeList);
}
}
//获取报警级别
QueryWrapper<AlarmRuleLevelConfig> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("alarm_rule_id", v.getId());
queryWrapper.eq("config_status", 1);
List<AlarmRuleLevelConfig> alarmRuleLevelConfigList = alarmRuleLevelConfigService.list(queryWrapper);
if(alarmRuleLevelConfigList!=null&&alarmRuleLevelConfigList.size()>0) {
v.setAlarmRuleLevelConfig(alarmRuleLevelConfigList.get(0));
}
});
Map<String, Map<String, Map<String, Object>>> resultListMap = new HashMap<>();
//按照机构获取设备状态
Set<String> departIdKeySet = departIndexCodeListMap.keySet();
for(String departId : departIdKeySet) {
List<String> indexCodeList = departIndexCodeListMap.get(departId);
Map<String, Map<String, Object>> handleIndexCodeToEquipmentStateMap = new HashMap<String,Map<String,Object>>();
if(indexCodeList!=null&&indexCodeList.size()>0) {
Map<String, Map<String, Object>> indexCodeToEquipmentStateMap = sysMonitorMetricInfoService.getEquipRunState(indexCodeList);
//过滤出故障设备
Set<String> indexCodeKeySet = indexCodeToEquipmentStateMap.keySet();
for(String indexCode : indexCodeKeySet) {
Map<String,Object> tempMap = indexCodeToEquipmentStateMap.get(indexCode);
//判定设备运行状态0停止/1运行/2故障
String equipmentState = ConvertUtils.getString(tempMap.get("value"));
if("2".equals(equipmentState)) {
handleIndexCodeToEquipmentStateMap.put(indexCode, tempMap);
}
}
resultListMap.put(departId, handleIndexCodeToEquipmentStateMap);
}
}
///存放超标指标数据
List<AlarmRuleConfigAndLevelVO> excessList = new ArrayList<>();
alarmRuleConfigList.forEach(v -> {
String alarmProgramCode = v.getAlarmProgramCode();
if (StringUtils.isNotBlank(alarmProgramCode)) {
//通过厂ID得到厂的实际所有报警指标的实际数值
Map<String, Map<String, Object>> mapList = resultListMap.get(v.getDepartId());
if (!mapList.isEmpty()) {
mapList.forEach((key, value) -> {
//判断指标是否相等
if (alarmProgramCode.equals(key)) {
v.setTime(ConvertUtils.getString(value.get("time")));
excessList.add(v);
return;
}
});
}
}
});
return excessList;
}
/**
* 判断是否需要发送短信和通知
*/
public void sendMessage(List<AlarmRuleConfigAndLevelVO> alarmList) {
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<AlarmRecordHistory> alarmRecordHistoryList = new ArrayList<>();
alarmList.forEach(alarmRuleConfigVO -> {
String metricCode = alarmRuleConfigVO.getAlarmProgramCode();
String metricName = alarmRuleConfigVO.getAlarmParamName();
String departName = alarmRuleConfigVO.getDepartName();
String departId = alarmRuleConfigVO.getDepartId();
String content = "";
content = contentTemplete.replace("${alarmParamName}", metricName);
departName = departNameTemplete.replace("${departName}", departName);
//查询短信上一条报警是否处理,未处理就不新增报警记录
//TODO 先从数据库中获取,后期优化放redis中
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("depart_id", departId);
queryWrapper.eq("alarm_param_code", metricCode);
queryWrapper.eq("alarm_rule_type", alarmRuleConfigVO.getAlarmRuleType());
queryWrapper.orderByDesc("record_time").last("limit 1");
AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if ((alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1)) {
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
AlarmRuleLevelConfig alarmRuleLevelConfig = alarmRuleConfigVO.getAlarmRuleLevelConfig();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmParamCode(metricCode);
alarmRecordHistory.setAlarmParamName(metricName);
alarmRecordHistory.setAlarmWorkName(metricName+"报警");
alarmRecordHistory.setAlarmRuleLevelName(alarmRuleLevelConfig.getAlarmRuleLevelName());
alarmRecordHistory.setAlarmRuleLevelId(alarmRuleLevelConfig.getId());
alarmRecordHistory.setAlarmRuleType(alarmRuleConfigVO.getAlarmRuleType());
try {
alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmRecordHistory.setDepartId(departId);
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setDelFlag(1);
//添加报警类别数据
alarmRecordHistory.setHandleMethon(alarmRuleConfigVO.getAlarmRecommend());
alarmRecordHistory.setAlarmRuleTopType(alarmRuleConfigVO.getAlarmRuleTopType());
alarmRecordHistory.setAlarmRuleSecondType(alarmRuleConfigVO.getAlarmRuleSecondType());
alarmRecordHistory.setAlarmRuleTypeTreepath(alarmRuleConfigVO.getAlarmRuleTypeTreepath());
//添加报警方案规则类别数据
alarmRecordHistory.setAlarmCustomRuleTopType(alarmRuleConfigVO.getAlarmCustomRuleTopType());
alarmRecordHistory.setAlarmCustomRuleSecondType(alarmRuleConfigVO.getAlarmCustomRuleSecondType());
alarmRecordHistory.setAlarmCustomRuleType(alarmRuleConfigVO.getAlarmCustomRuleType());
alarmRecordHistory.setAlarmCustomRuleTypeTreepath(alarmRuleConfigVO.getAlarmCustomRuleTypeTreepath());
//构造报警指标数据json
List<Map<String,Object>> indexDataList = new ArrayList<Map<String,Object>>();
Map<String,Object> indexJsonMap = new HashMap<String,Object>();
indexJsonMap.put("indexCode", metricCode);
indexJsonMap.put("indexName", metricName);
//预留设备id存储
indexJsonMap.put("indexValue", "");
indexDataList.add(indexJsonMap);
alarmRecordHistory.setAlarmCustomIndexJsonData(JSONUtil.toJsonStr(indexDataList));
////${departName}在${date}时检测到设备${conent}故障,请处理!
alarmRecordHistory.setAlarmContent("检测到设备" + content + suffix);
alarmRecordHistory.setCreateBy("admin");
alarmRecordHistory.setActualAvgValue("设备故障");
alarmRecordHistoryList.add(alarmRecordHistory);
//判断是否需要发送系统通知 (内容跟报警记录信息相同)
if ("1".equals(alarmRuleLevelConfig.getIsSendNotice())) {
if (StringUtils.isNotBlank(alarmRuleLevelConfig.getAlarmNoticeRecipient())) {
SystemNoticeVO systemNoticeVO = new SystemNoticeVO(alarmRuleLevelConfig.getAlarmNoticeRecipient(),
alarmRuleConfigVO.getAlarmRuleLevelName(), prefix+departName + "在" + date + "时检测到设备" + content + suffix, departId, "4");
systemNoticeUtils.dealNoticeMessage(systemNoticeVO);
}
}
}
});
alarmRecordHistoryService.saveBatch(alarmRecordHistoryList);
}
}