SmsAlarmJob.java
34.0 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package com.skua.modules.quartz.job;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skua.core.util.chuanglan.HttpSenderMsg;
import com.skua.core.cache.RedisUtil;
import com.skua.core.context.SpringContextUtils;
import com.skua.modules.alarm.entity.AlarmCheckRecordMastertable;
import com.skua.modules.alarm.entity.AlarmCheckRecordSubtable;
import com.skua.modules.alarm.entity.AlarmNoticeRecord;
import com.skua.modules.alarm.service.IAlarmCheckRecordMastertableService;
import com.skua.modules.alarm.service.IAlarmCheckRecordSubtableService;
import com.skua.modules.alarm.service.IAlarmNoticeRecordService;
import com.skua.modules.quartz.util.BaseUtil;
import com.skua.modules.quartz.util.JKBean;
import com.skua.modules.quartz.util.TimeUtils;
import com.skua.core.service.impl.PgQueryServiceImpl;
import com.skua.modules.quartz.vo.ChartsBean;
import com.skua.modules.quartz.vo.ChartsParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
/**
* 报警后台定时器
*/
@Slf4j
public class SmsAlarmJob implements Job {
private static JdbcTemplate master_database = (JdbcTemplate) SpringContextUtils.getBean("master");
@Autowired
private RedisUtil redisUtil;
@Autowired
private IAlarmCheckRecordMastertableService alarmCheckRecordMastertableService;
@Autowired
private IAlarmCheckRecordSubtableService alarmCheckRecordSubtableService;
@Autowired
private IAlarmNoticeRecordService alarmNoticeRecordService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
List<JKBean> list = getStandParams();
getData(list);
}
private List<JKBean> getStandParams() {
List<JKBean> list = new ArrayList<>();
String sql = "SELECT p.*,a.phone as rephone,"
+ "a.realname as recname,b.metric_name as metricName,"
+ "c.depart_name as facName,b.metric_unit as util"
+ " FROM alarm_standard_params p "
+ " LEFT JOIN sys_user a on SUBSTRING_INDEX(p.standard_params_msg_send_user,',','-1') = a.id"
+ " LEFT JOIN sys_monitor_metric_info b on p.standard_params_monitorid = b.id"
+ " LEFT JOIN sys_depart c on p.factory_id = c.id"
+ " WHERE p.del_flag = 1 AND standard_params_msg_status = 1 "
+ " GROUP BY p.id ORDER BY p.create_time ASC";
list = JSONArray.parseArray(JSON.toJSONString(master_database.queryForList(sql)), JKBean.class);
log.info("厂配置信息:" + list);
return list;
}
private String getUserPhones(String userIds) {
List<JKBean> list = new ArrayList<>();
String phones = "";
String sql = " SELECT phone from sys_user WHERE id in("+userIds+"'-1') ";
list = JSONArray.parseArray(JSON.toJSONString(master_database.queryForList(sql)), JKBean.class);
for (int i = 0; i < list.size(); i++) {
if(list.get(i).get("phone")!=null){
phones = phones + list.get(i).getStr("phone")+",";
}
}
return phones;
}
/**
* 数据比较。发送短信
* @param list
*/
public void getData(List<JKBean> list) {
for (JKBean jkBean : list) {
String mobilePhones = "rephone";
Integer type = jkBean.getInteger("standard_params_type");
String programId = jkBean.getStr("standard_params_monitorid");
//报警设定类型 1:超标报警 2:预报警 3:趋势报警
String alarmType = jkBean.getStr("standard_params_msg_label");
//趋势报警波动率
String volatility = jkBean.getStr("standard_params_monitorid_field");
//趋势报警判断时长
String judgeTime = jkBean.getStr("standard_params_monitorid_text");
String standard_params_id = jkBean.getStr("id");
String factoryId = jkBean.getStr("factory_id");
String reviceUsers = jkBean.getStr("standard_params_reciewr");
String phones = "18519526218";
if(reviceUsers.length() > 0){
StringBuffer userIds = new StringBuffer("");
String[] users = reviceUsers.split("\\,");
for (int i = 0; i < users.length; ++i){
userIds.append("'"+users[i].toString()+"',");
}
phones = getUserPhones(userIds.toString());
}
jkBean.set(mobilePhones, phones);
Integer timeData = null;
Double time = null;
//模拟量 超标报警
if (type == 0 && "1".equals(alarmType)) {
Double result = null;
Integer statu = 0;
boolean runState = true;
try {
List<String> objects = new ArrayList<>();
objects.add(programId);
String lastData = queryLastDataMonitor(objects, factoryId);
log.info("查询实时数据" + lastData);
System.out.println(lastData);
//将查到的数据转为Map
Map obj = JSONObject.parseObject(lastData, Map.class);
if (obj != null && !obj.isEmpty()) {
Object val = obj.get(programId);
if (val != null && !"".equals(val)) {
result = Double.parseDouble(val.toString());
}
Object vtime = obj.get("time");
if (vtime != null && !"".equals(vtime)) {
//将实时数据的时间转换为int类型
BigDecimal time1 = new BigDecimal(vtime.toString());
timeData = time1.intValue();
}
} else {
continue;
}
log.info( programId + "---" + runState);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
if (timeData != null) {
time = (double) timeData;
}
//查询到实时数据 进入短信判断逻辑
if (result != null && time != null && (runState)) {
//将查询结果和查询时间放入JKbean 后边存子表
jkBean.set("che_alas_value", result);
jkBean.set("che_alas_time", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"));
String sengMsg = null;
Map<String, Object> map = new HashMap<String, Object>();
String resultString = "";
String saveSendMsg = "";
log.info("查询实时数据结果:" + result + ",标准最大值:" + jkBean.getDouble("standard_params_totop") + ",标准最小值:" + jkBean.getDouble("standard_params_tofloor"));
//判断指标 超标报警 编辑短信
if (result < jkBean.getDouble("standard_params_tofloor")) {
statu = 1;
jkBean.set("che_alas_desc", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "低于设定标准最小值" + jkBean.getDouble("standard_params_tofloor") + "。");
sengMsg = TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("facName") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "低于标准最小值。请您及时采取处理措施。";
saveSendMsg = sengMsg;
} else if (result > jkBean.getDouble("standard_params_totop")) {
statu = 1;
jkBean.set("che_alas_desc", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "高于设定标准最大值" + jkBean.getDouble("standard_params_totop") + "。");
sengMsg = TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("facName") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "高于标准最大值。请您及时采取处理措施。";
saveSendMsg = sengMsg;
} else {
statu = 0;
log.info("没报警,挺好");
}
//需要发送短信
if (statu == 1) {
log.info("短信内容:+" + sengMsg);
map.put("msg", sengMsg);
map.put("phone", jkBean.getStr(mobilePhones));
log.info("----指标超标,进入发送频次判断逻辑----");
//取上次发送时间
String sendTimeExt = getRedisValue(standard_params_id + "sendTimeExt");
log.info("从redis中取出上次发送时间戳:" + sendTimeExt);
if (sendTimeExt == null || "".equals(sendTimeExt)) {
log.info("----上次发送时间为空 进入发送短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//将发送信息保存到报警检测记录主表
String masterId = savaDataToMaster(jkBean, standard_params_id);
//将发送时间存入redis
redisUtil.set(standard_params_id + "sendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
//将主表ID存入Redis
redisUtil.set(standard_params_id + "MasterId", masterId);
//将发送记录保存到记录表
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(), result + "", 0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
//将查询信息存入报警检测记录子表
savaDataToSlave(jkBean);
//发送短信
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
} else {
//上次发送时间不为空
Long timeComp = Long.parseLong(sendTimeExt);
log.info("----上次发送时间不为空 开始判断时间间隔----");
log.info("上次发送时间时间戳:" + timeComp);
log.info("当前时间时间戳:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
log.info("时间差(秒):" + (BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp));
log.info("配置设定时间间隔(秒):" + jkBean.getDouble("standard_params_freq") * 3600);
//上次发送时间间隔大于等于设定报警频率时才发送短信
if ((BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp) >= jkBean.getDouble("standard_params_freq") * 3600) {
log.info("----大于等于设定报警频率,进入发送短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//发送短信步骤
String masterId2 = savaDataToMaster(jkBean, standard_params_id);
redisUtil.set(standard_params_id + "MasterId", masterId2);
redisUtil.set(standard_params_id + "sendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(),result + "", 0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
//存子表
savaDataToSlave(jkBean);
} else {
log.info("----未超过设定报警间隔,不发送----");
}
}
}
} else {
log.info("此指标:" + programId + "未查到值,时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
//预报警
} else if (type == 0 && "2".equals(alarmType)) {
//查询相同指标,相同发送人,超标报警的最新一条报警信息
JKBean alarmMsg = getStandParamByLabel(programId, jkBean.getStr("standard_params_msg_send_user"), "1");
boolean isNotEmpty = !Objects.isNull(alarmMsg) && !alarmMsg.isEmpty();
boolean b = false;
if (isNotEmpty) {
String strTime = alarmMsg.getStr("che_alas_time");
Date alasTime = TimeUtils.getDateFromString(strTime, "yyyy-MM-dd HH:mm:ss");
double freq = alarmMsg.getDouble("standard_params_freq") * 3600;
b = BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - BaseUtil.dateToTimeStamp(alasTime, "yyyy-MM-dd HH:mm:ss") > freq;
}
// 查询最近的超标报警信息为空 或 不在超标报警的发送间隔中 进入预报警判断
if (!isNotEmpty || b) {
Double result = null;
Integer statu = 0;
JKBean stateLimit = new JKBean();
boolean runState = true;
int runNum = 1;
try {
ArrayList<String> objects = new ArrayList<>();
objects.add(programId);
log.info("预报警查询字段" + objects);
System.out.println("=========test========" + objects);
String lastData = queryLastDataMonitor(objects, factoryId);
log.info("预报警查询实时数据" + lastData);
System.out.println(lastData);
//将查到的数据转为Map
Map obj = JSONObject.parseObject(lastData, Map.class);
if (obj != null && !obj.isEmpty()) {
Object val = obj.get(programId);
if (val != null && !"".equals(val)) {
result = Double.parseDouble(val.toString());
}
Object vtime = obj.get("time");
if (vtime != null && !"".equals(vtime)) {
System.out.println("----------" + vtime.toString());
//将实时数据的时间转换为int类型
BigDecimal time1 = new BigDecimal(vtime.toString());
timeData = time1.intValue();
}
} else {
continue;
}
log.info( programId + "---" + runState);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
if (timeData != null) {
time = (double) timeData;
}
//查询到实时数据 进入短信判断逻辑
if (result != null && time != null && (runState)) {
//将查询结果和查询时间放入JKbean 后边存子表
jkBean.set("che_alas_value", result);
jkBean.set("che_alas_time", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"));
String sengMsg = null;
Map<String, Object> map = new HashMap<String, Object>();
String resultString = "";
String saveSendMsg = "";
log.info("预报警查询实时数据结果:" + result + ",标准最大值:" + jkBean.getDouble("standard_params_totop") + ",标准最小值:" + jkBean.getDouble("standard_params_tofloor"));
//判断指标 超标报警 编辑短信
if (result < jkBean.getDouble("standard_params_tofloor")) {
statu = 1;
jkBean.set("che_alas_desc", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "低于设定标准最小值" + jkBean.getDouble("standard_params_tofloor") + ",报警类型:预报警。");
sengMsg = TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("facName") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "低于标准最小值,报警类型:预报警。请您及时采取处理措施。";
saveSendMsg = sengMsg;
} else if (result > jkBean.getDouble("standard_params_totop")) {
statu = 1;
jkBean.set("che_alas_desc", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "高于设定标准最大值" + jkBean.getDouble("standard_params_totop") + ",报警类型:预报警。");
sengMsg = TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("facName") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "高于标准最大值,报警类型:预报警。请您及时采取处理措施。";
saveSendMsg = sengMsg;
} else {
statu = 0;
log.info("预报警没报警,挺好");
}
//需要发送短信
if (statu == 1) {
log.info("预报警短信内容:+" + sengMsg);
map.put("msg", sengMsg);
map.put("phone", jkBean.getStr(mobilePhones));
log.info("----预报警指标超标,进入发送频次判断逻辑----");
//取上次发送时间
String sendTimeExt = getRedisValue(standard_params_id + "forecastSendTimeExt");
log.info("从redis中取出上次发送预报警时间戳:" + sendTimeExt);
if (sendTimeExt == null || "".equals(sendTimeExt)) {
log.info("----上次发送时间为空 进入发送预报警短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//将发送信息保存到报警检测记录主表
String masterId = savaDataToMaster(jkBean, standard_params_id);
//将发送时间存入redis
redisUtil.set(standard_params_id + "forecastSendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
//将主表ID存入Redis
redisUtil.set(standard_params_id + "MasterId", masterId);
//将发送记录保存到记录表
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(), result + "", 0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
//将查询信息存入报警检测记录子表
savaDataToSlave(jkBean);
//发送短信
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
} else {
//上次发送时间不为空
Long timeComp = Long.parseLong(sendTimeExt);
log.info("----预报警上次发送时间不为空 开始判断时间间隔----");
log.info("上次发送时间时间戳:" + timeComp);
log.info("当前时间时间戳:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
log.info("时间差(秒):" + (BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp));
log.info("配置设定时间间隔(秒):" + jkBean.getDouble("standard_params_freq") * 3600);
//上次发送时间间隔大于等于设定报警频率时才发送短信
if ((BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp) >= jkBean.getDouble("standard_params_freq") * 3600) {
log.info("----大于等于设定预报警报警频率,进入发送短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//发送短信步骤
String masterId2 = savaDataToMaster(jkBean, standard_params_id);
redisUtil.set(standard_params_id + "MasterId", masterId2);
redisUtil.set(standard_params_id + "forecastSendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(),result + "",0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
//存子表
savaDataToSlave(jkBean);
} else {
log.info("----未超过设定预报警报警间隔,不发送----");
}
}
}
} else {
log.info("此指标:" + programId + "未查到值,时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
//趋势报警
} else if (type == 0 && "3".equals(alarmType)) {
Double result = null;
Integer statu = 0;
boolean runState = true;
if(!StringUtils.isNotBlank(judgeTime) || !StringUtils.isNotBlank(volatility)){
continue;
}
Function<String,Double> fun = Double::parseDouble;
Double judgeTimeValue = fun.apply(judgeTime);
Double volatilityValue = fun.apply(volatility);
try {
ArrayList<String> objects = new ArrayList<>();
objects.add(programId);
log.info("趋势报警查询字段" + objects);
System.out.println("=========test========" + objects);
String lastData = queryLastDataMonitor(objects, factoryId);
log.info("趋势报警查询实时数据" + lastData);
System.out.println(lastData);
//将查到的数据转为Map
Map obj = JSONObject.parseObject(lastData, Map.class);
if (obj != null && !obj.isEmpty()) {
Object val = obj.get(programId);
if (val != null && !"".equals(val)) {
result = Double.parseDouble(val.toString());
}
Object vtime = obj.get("time");
if (vtime != null && !"".equals(vtime)) {
System.out.println("----------" + vtime.toString());
//将实时数据的时间转换为int类型
BigDecimal time1 = new BigDecimal(vtime.toString());
timeData = time1.intValue();
}
} else {
continue;
}
log.info( programId + "---" + runState);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
if (timeData != null) {
time = (double) timeData;
}
//查询到实时数据 进入短信判断逻辑
if (result != null && time != null && (runState)) {
//将查询结果和查询时间放入JKbean 后边存子表
jkBean.set("che_alas_value", result);
jkBean.set("che_alas_time", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"));
String sengMsg = null;
Map<String, Object> map = new HashMap<String, Object>();
String resultString = "";
String saveSendMsg = "";
//获取设定时长之前的实时数据
Double trendValue = getTrendValue(programId, Integer.parseInt(judgeTime.toString()), factoryId);
//目前的值更小
if(trendValue > result){
continue;
}
//计算波动率
Double bodong = (result - trendValue) / judgeTimeValue;
log.info("趋势报警查询实时数据结果:" + result + ",标准最大值:" + jkBean.getDouble("standard_params_totop") + ",标准最小值:" + jkBean.getDouble("standard_params_tofloor"));
//判断指标 超标报警 编辑短信
if (bodong > volatilityValue) {
statu = 1;
jkBean.set("che_alas_desc", TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + ",高于设定波动率:" + jkBean.getDouble("standard_params_totop") + "。");
sengMsg = TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss") + "," + jkBean.getStr("facName") + "," + jkBean.getStr("standard_params_name") + ":" + result + jkBean.getStr("util") + "高于设定波动率。请您及时采取处理措施。";
saveSendMsg = sengMsg;
} else {
statu = 0;
log.info("趋势报警没报警,挺好");
}
//需要发送短信
if (statu == 1) {
log.info("趋势报警短信内容:+" + sengMsg);
map.put("msg", sengMsg);
map.put("phone", jkBean.getStr(mobilePhones));
log.info("----趋势报警指标超标,进入发送频次判断逻辑----");
//取上次发送时间
String sendTimeExt = getRedisValue(standard_params_id + "trendSendTimeExt");
log.info("从redis中取出上次发送趋势报警时间戳:" + sendTimeExt);
if (sendTimeExt == null || "".equals(sendTimeExt)) {
log.info("----上次发送时间为空 进入发送趋势报警短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//将发送信息保存到报警检测记录主表
String masterId = savaDataToMaster(jkBean, standard_params_id);
//将发送时间存入redis
redisUtil.set(standard_params_id + "trendSendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
//将主表ID存入Redis
redisUtil.set(standard_params_id + "MasterId", masterId);
//将发送记录保存到记录表
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(),result + "", 0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
//将查询信息存入报警检测记录子表
savaDataToSlave(jkBean);
//发送短信
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
} else {
//上次发送时间不为空
Long timeComp = Long.parseLong(sendTimeExt);
log.info("----趋势报警上次发送时间不为空 开始判断时间间隔----");
log.info("上次发送时间时间戳:" + timeComp);
log.info("当前时间时间戳:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
log.info("时间差(秒):" + (BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp));
log.info("配置设定时间间隔(秒):" + jkBean.getDouble("standard_params_freq") * 3600);
//上次发送时间间隔大于等于设定报警频率时才发送短信
if ((BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss") - timeComp) >= jkBean.getDouble("standard_params_freq") * 3600) {
log.info("----大于等于设定趋势报警报警频率,进入发送短信逻辑----");
HttpSenderMsg.sendMsg(map.get("msg").toString(), map.get("phone").toString());
//发送短信步骤
String masterId2 = savaDataToMaster(jkBean, standard_params_id);
redisUtil.set(standard_params_id + "MasterId", masterId2);
redisUtil.set(standard_params_id + "trendSendTimeExt", BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss").toString());
savaData(statu, TimeUtils.TimeToString(time, "yyyy-MM-dd HH:mm:ss"), jkBean.get("id").toString(), result + "", 0, saveSendMsg, jkBean.getStr("standard_params_reciewr").toString(), jkBean.getStr("recname"), factoryId);
log.info(jkBean.getStr(mobilePhones) + ":发送状态:" + "---sendTimeExt:" + BaseUtil.dateToTimeStamp(new Date(), "yyyy-MM-dd HH:mm:ss"));
//存子表
savaDataToSlave(jkBean);
} else {
log.info("----未超过设定趋势报警报警间隔,不发送----");
}
}
}
} else {
log.info("此指标:" + programId + "未查到值,时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
}
}
private String getRedisValue(String key) {
Boolean hasKey = redisUtil.hasKey(key);
if (!hasKey) {
return null;
}else{
return redisUtil.get(key).toString();
}
}
/**
* 保存信息到信息记录表
*
* @param alarm_status
* @param alarm_time
* @param standard_params_id
* @param alarm_value
*/
public void savaData(Integer alarm_status, String alarm_time, String standard_params_id, String alarm_value, Integer detail,
String sendMsg, String recordOwnerIds, String recordRename, String factoryId) {
if(recordOwnerIds.length() > 0){
String[] users = recordOwnerIds.split("\\,");
for (int i = 0; i < users.length; ++i){
AlarmNoticeRecord alarmNoticeRecord= new AlarmNoticeRecord();
alarmNoticeRecord.setRecordAlarmStatus(alarm_status.toString());
alarmNoticeRecord.setRecordAlarmTime(alarm_time);
alarmNoticeRecord.setRecordParamsId(standard_params_id);
alarmNoticeRecord.setCreateCmpy(factoryId);
alarmNoticeRecord.setFactoryId(factoryId);
alarmNoticeRecord.setRecordAlarmValue(alarm_value);
alarmNoticeRecord.setRecordType(1);
alarmNoticeRecord.setRecordTypeDetail(detail);
alarmNoticeRecord.setRecordAlarmContext(sendMsg);
alarmNoticeRecord.setRecordOwner(users[i].toString());
alarmNoticeRecord.setRecordRecname(recordRename);
alarmNoticeRecordService.save(alarmNoticeRecord);
}
}
}
/**
* 保存信息到报警检测记录主表
*
* @param jkBean
*/
public String savaDataToMaster(JKBean jkBean, String standard_params_id) {
String masterId = getRedisValue(standard_params_id);
Integer state = null;
if (StringUtils.isNotBlank(masterId)) {
JKBean masterMsg = getMasterMsg(masterId);
state = masterMsg.getInteger("che_alam_state");
}
//若redis中无materid 或 此master状态为已处理或者误报警 则主表插一条新纪录
if ((state != null && state != 1) || !StringUtils.isNotBlank(masterId)) {
AlarmCheckRecordMastertable alarmCheckRecordMastertable = new AlarmCheckRecordMastertable();
alarmCheckRecordMastertable.setStaParId(jkBean.getStr("id"));
alarmCheckRecordMastertable.setStaParName(jkBean.getStr("standard_params_monitorid"));
alarmCheckRecordMastertable.setCheAlamType(Integer.parseInt(jkBean.getStr("standard_params_type")));
alarmCheckRecordMastertable.setCheAlamStartTime(jkBean.getStr("che_alas_time"));
alarmCheckRecordMastertable.setCheAlamType(1);//che_alam_state
alarmCheckRecordMastertable.setCheAlamMdesc(jkBean.getStr("che_alas_desc"));
alarmCheckRecordMastertable.setFactoryId(jkBean.getStr("factory_id"));
alarmCheckRecordMastertableService.save(alarmCheckRecordMastertable);
masterId = alarmCheckRecordMastertable.getId();
}
return masterId;
}
/**
* 根据主表id查询主表信息
*
* @param
* @return
*/
public JKBean getMasterMsg(String masterId) {
List<JKBean> list = new ArrayList<>();
JKBean mastermsg = new JKBean();
String sql = "SELECT * FROM alarm_check_record_mastertable " +
" where che_alam_id = '" + masterId + "' and del_flag = 1";
list = JSONArray.parseArray(JSON.toJSONString(master_database.queryForList(sql)), JKBean.class);
if (!list.isEmpty()) {
mastermsg = list.get(0);
}
return mastermsg;
}
/**
* 保存信息到报警检测记录子表
*
* @param jkBean
*/
public void savaDataToSlave(JKBean jkBean) {
String masterId = getRedisValue(jkBean.get("id") + "MasterId");//主表Id
String staParId = jkBean.getStr("id");//报警配置id
String staParName = jkBean.getStr("standard_params_monitorid");//报警配置名
Double cheAlasValue = jkBean.getDouble("che_alas_value");//报警检测值
String cheAlasType = jkBean.getStr("standard_params_type");//报警检测类型
String cheAlasTime = jkBean.getStr("che_alas_time");//报警数据时间(查询到实时数据的时间)
String cheAlasDesc = jkBean.getStr("che_alas_desc");//报警描述
String factoryId= jkBean.getStr("factory_id");//所属厂区
AlarmCheckRecordSubtable alarmCheckRecordSubtable = new AlarmCheckRecordSubtable();
alarmCheckRecordSubtable.setCheAlamId(masterId);
alarmCheckRecordSubtable.setStaParId(staParId);
alarmCheckRecordSubtable.setStaParName(staParName);
alarmCheckRecordSubtable.setCheAlasValue(cheAlasValue);
alarmCheckRecordSubtable.setCheAlasType(cheAlasType);
alarmCheckRecordSubtable.setCheAlasTime(cheAlasTime);
alarmCheckRecordSubtable.setCheAlasDesc(cheAlasDesc);
alarmCheckRecordSubtable.setFactoryId(factoryId);
alarmCheckRecordSubtableService.save(alarmCheckRecordSubtable);
}
public Double getTrendValue(String point,Integer offset,String factoryId){
Double res = null;
ArrayList<ChartsBean> objectKMYC = new ArrayList<>();
ChartsBean chartsBean = new ChartsBean();
chartsBean.setFiled(point);
chartsBean.setFunc("mean");
chartsBean.setFormula("");
chartsBean.setFiledAlias("");
objectKMYC.add(chartsBean);
String startTime = DateUtil.format(DateUtil.offsetHour(localToUTC(), -offset), "yyyy-MM-dd'T'HH:mm:00.SSS'Z'");
String endTime = DateUtil.format(DateUtil.offsetHour(localToUTC(), (-offset)+1), "yyyy-MM-dd'T'HH:mm:00.SSS'Z'");
ChartsParam chartsParamKMYC = new ChartsParam();
chartsParamKMYC.setStartTime(startTime);
chartsParamKMYC.setEndTime(endTime);
chartsParamKMYC.setFields(objectKMYC);
chartsParamKMYC.setGroupType("time");
chartsParamKMYC.setTimePeriod("1h");
chartsParamKMYC.setFactoryId(factoryId);
List<Map<String, Object>> KMYCMAP = fetchChartsData(chartsParamKMYC);
if(!Objects.isNull(KMYCMAP) && !KMYCMAP.isEmpty()){
Map<String, Object> map = KMYCMAP.get(0);
if(!Objects.isNull(map) && !map.isEmpty()){
Object o = map.get(point);
if(!Objects.isNull(o) && StringUtils.isNotBlank(o.toString()))
res = Double.parseDouble(o.toString());
}
}
return res;
}
//获取UTC时间
public static Date localToUTC() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date localDate= new Date();
long localTimeInMillis=localDate.getTime();
/** long时间转换成Calendar */
Calendar calendar= Calendar.getInstance();
calendar.setTimeInMillis(localTimeInMillis);
/** 取得时间偏移量 */
int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
/** 取得夏令时差 */
int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
/** 从本地时间里扣除这些差量,即可以取得UTC时间*/
calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
/** 取得的时间就是UTC标准时间 */
Date utcDate=new Date(calendar.getTimeInMillis());
return utcDate;
}
/**
* 根据指标、用户、报警类型查询最近一条的报警信息
*
* @param
* @return
*/
public static JKBean getStandParamByLabel(String parName, String userId, String label) {
List<JKBean> list = new ArrayList<>();
String sql = "SELECT a.sta_par_id,a.sta_par_name,a.che_alas_time,b.standard_params_freq " +
" FROM sk_prodata_manage.sy_check_alarm_record_subtable a " +
" LEFT JOIN sk_prodata_manage.sy_standard_params b " +
" on a.sta_par_id = b.standard_params_id " +
" where b.standard_params_name = '" + parName + "' " +
" and b.standard_params_msg_send_user = '" + userId + "' " +
" and b.standard_params_msg_label = '" + label + "' order by a.che_alas_time limit 1";
JKBean resultMap = new JKBean();
list = JSONArray.parseArray(JSON.toJSONString(master_database.queryForList(sql)), JKBean.class);
if (!list.isEmpty()) {
resultMap = list.get(0);
}
return resultMap;
}
private List<Map<String, Object>> fetchChartsData(ChartsParam chartsParam) {
String fields = "";
StringBuffer sb = new StringBuffer("");
List<ChartsBean> point = chartsParam.getFields();
for (int i = 0; i < point.size(); i++) {
sb.append(point.get(i).getFiled()+",");
}
fields = sb.toString();
List<Map<String, Object>> list = new PgQueryServiceImpl()
.queryFactoryInfosForAlarm(chartsParam.getFactoryId(),fields.substring(0,fields.length()-1));
return list;
}
private String queryLastDataMonitor(List<String> point, String factoryId) {
String fields = "";
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < point.size(); i++) {
sb.append(point.get(i)+",");
}
if(sb.toString().length() > 0){
fields = sb.toString();
List<Map<String, Object>> list = new PgQueryServiceImpl()
.queryFactoryInfosForAlarm(factoryId,fields.substring(0,fields.length()-1));
return JSONObject.toJSONString(list.get(0));
}else{
return "";
}
}
}