作者 钟来

添加告警短信和语音通知

@@ -31,6 +31,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @@ -31,6 +31,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
31 <if test="params.beginTime != null "> and create_time &gt;= #{params.beginTime}</if> 31 <if test="params.beginTime != null "> and create_time &gt;= #{params.beginTime}</if>
32 <if test="params.endTime != null "> and create_time &lt;= #{params.endTime}</if> 32 <if test="params.endTime != null "> and create_time &lt;= #{params.endTime}</if>
33 </where> 33 </where>
  34 + order by create_time desc
34 </select> 35 </select>
35 36
36 <select id="selectAlertLogByAlertLogId" parameterType="Long" resultMap="AlertLogResult"> 37 <select id="selectAlertLogByAlertLogId" parameterType="Long" resultMap="AlertLogResult">
@@ -40,11 +40,17 @@ public class IotDeviceAlarm extends UpAlarmFactory<IotDevice> implements Seriali @@ -40,11 +40,17 @@ public class IotDeviceAlarm extends UpAlarmFactory<IotDevice> implements Seriali
40 { 40 {
41 CachAlarmConfig.putDeviceProduct(afterupAlarmDb.getClient_id(), afterupAlarmDb.getProduct_id()); 41 CachAlarmConfig.putDeviceProduct(afterupAlarmDb.getClient_id(), afterupAlarmDb.getProduct_id());
42 } 42 }
  43 +
  44 + Integer product_id = CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getClient_id());
  45 + if(-1==product_id) //没有产品id的直接排除
  46 + {
  47 + return;
  48 + }
43 //通过变更的数据获得对应的告警规则 49 //通过变更的数据获得对应的告警规则
44 - valueUp(beforeupAlarmDb.getClient_id(),CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getClient_id()),beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),null,"主机"+afterupAlarmDb.getClient_id()); 50 + valueUp(beforeupAlarmDb.getClient_id(),product_id,beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),null,"主机"+afterupAlarmDb.getClient_id());
45 51
46 //在线状态 52 //在线状态
47 - if(null != afterupAlarmDb.getStatus() && null != beforeupAlarmDb.getStatus() && beforeupAlarmDb.getStatus()-afterupAlarmDb.getStatus()!=0) 53 + if(null != afterupAlarmDb.getStatus() && null != beforeupAlarmDb.getStatus() && beforeupAlarmDb.getStatus()-afterupAlarmDb.getStatus()!=0 && afterupAlarmDb.getStatus()==4)
48 { 54 {
49 switch (afterupAlarmDb.getStatus()) 55 switch (afterupAlarmDb.getStatus())
50 { 56 {
@@ -38,11 +38,16 @@ public class IotTerminalAlarm extends UpAlarmFactory<IotTerminal> implements Ser @@ -38,11 +38,16 @@ public class IotTerminalAlarm extends UpAlarmFactory<IotTerminal> implements Ser
38 38
39 @Override 39 @Override
40 public void updateGenerateAlarm() { 40 public void updateGenerateAlarm() {
  41 + Integer product_id = CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getId());
  42 + if(-1==product_id) //没有产品id的直接排除
  43 + {
  44 + return;
  45 + }
41 //通过变更的数据获得对应的告警规则 46 //通过变更的数据获得对应的告警规则
42 - valueUp(beforeupAlarmDb.getId(), CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getId()),beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),beforeupAlarmDb.getUser_info_id(),null==beforeupAlarmDb.getName()?beforeupAlarmDb.getId():beforeupAlarmDb.getName()); 47 + valueUp(beforeupAlarmDb.getId(),product_id ,beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),beforeupAlarmDb.getUser_info_id(),null==beforeupAlarmDb.getName()?beforeupAlarmDb.getId():beforeupAlarmDb.getName());
43 48
44 //在线状态 49 //在线状态
45 - if(null != afterupAlarmDb.getOnline() && null != beforeupAlarmDb.getOnline() && beforeupAlarmDb.getOnline()-afterupAlarmDb.getOnline()!=0) 50 + if(null != afterupAlarmDb.getOnline() && null != beforeupAlarmDb.getOnline() && beforeupAlarmDb.getOnline()-afterupAlarmDb.getOnline()!=0 && afterupAlarmDb.getOnline()==4)
46 { 51 {
47 switch (afterupAlarmDb.getOnline()) 52 switch (afterupAlarmDb.getOnline())
48 { 53 {
@@ -83,10 +83,6 @@ public abstract class UpAlarmFactory<T> { @@ -83,10 +83,6 @@ public abstract class UpAlarmFactory<T> {
83 83
84 protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue,Integer user_id,String device_name) 84 protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue,Integer user_id,String device_name)
85 { 85 {
86 - if ("864814074929612".equals(client_id))  
87 - {  
88 - System.out.println(client_id);  
89 - }  
90 if(StringUtils.isNotEmpty(newValue)) 86 if(StringUtils.isNotEmpty(newValue))
91 { 87 {
92 JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue); 88 JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue);
@@ -103,7 +99,7 @@ public abstract class UpAlarmFactory<T> { @@ -103,7 +99,7 @@ public abstract class UpAlarmFactory<T> {
103 JSONObject newObject = newjson.getJSONObject(key); 99 JSONObject newObject = newjson.getJSONObject(key);
104 String type = newObject.getString("type"); 100 String type = newObject.getString("type");
105 nv = GenericComparator.convertToType(newObject.getString("saveView"),type); 101 nv = GenericComparator.convertToType(newObject.getString("saveView"),type);
106 - if(oldjson.containsKey("key")) 102 + if(oldjson.containsKey(key))
107 { 103 {
108 ov = GenericComparator.convertToType(oldjson.getJSONObject(key).getString("saveView"),type); 104 ov = GenericComparator.convertToType(oldjson.getJSONObject(key).getString("saveView"),type);
109 }else { 105 }else {
@@ -409,10 +409,11 @@ public class CachAlarmConfig { @@ -409,10 +409,11 @@ public class CachAlarmConfig {
409 { 409 {
410 try { 410 try {
411 productId = DbOperateUtil.getProductId(deviceId); 411 productId = DbOperateUtil.getProductId(deviceId);
412 - if (null != productId) 412 + if (null == productId || productId<12) //排除没有产品id,或者小于12的产品id
413 { 413 {
414 - device_product.put(deviceId,productId); 414 + productId = -1;
415 } 415 }
  416 + device_product.put(deviceId,productId);
416 } catch (SQLException e) { 417 } catch (SQLException e) {
417 throw new RuntimeException(e); 418 throw new RuntimeException(e);
418 } 419 }
@@ -518,7 +519,7 @@ public class CachAlarmConfig { @@ -518,7 +519,7 @@ public class CachAlarmConfig {
518 { 519 {
519 if(user_phone.containsKey(user_id) && null != user_phone.get(user_id)) 520 if(user_phone.containsKey(user_id) && null != user_phone.get(user_id))
520 { 521 {
521 - return user_phone.get(user_id); 522 + return new HashSet<>(user_phone.get(user_id));
522 } 523 }
523 return null; 524 return null;
524 } 525 }
@@ -84,7 +84,7 @@ public class DbOperateUtil { @@ -84,7 +84,7 @@ public class DbOperateUtil {
84 } 84 }
85 public static List<UserAlertPhone> getUserPhoneList() 85 public static List<UserAlertPhone> getUserPhoneList()
86 { 86 {
87 - return baseDao.findBysql("SELECT user_id,phone FROM `user_alert_phone` WHERE user_info_id IS NOT NULL",UserAlertPhone.class); 87 + return baseDao.findBysql("SELECT user_id,phone FROM `user_alert_phone` WHERE user_id IS NOT NULL",UserAlertPhone.class);
88 } 88 }
89 89
90 } 90 }
@@ -64,7 +64,8 @@ public interface NoticeFactory { @@ -64,7 +64,8 @@ public interface NoticeFactory {
64 } 64 }
65 if(null != noticeFactory) 65 if(null != noticeFactory)
66 { 66 {
67 - rb=rb&&noticeFactory.send(iotAlertLog); 67 + boolean nb = noticeFactory.send(iotAlertLog);
  68 + rb=rb&&nb;
68 } 69 }
69 70
70 } 71 }
@@ -56,6 +56,7 @@ public class AliyunSmsNoticeImpl implements NoticeFactory { @@ -56,6 +56,7 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
56 56
57 try { 57 try {
58 send(client,iotAlertLog); 58 send(client,iotAlertLog);
  59 + return true;
59 } catch (Exception e) { 60 } catch (Exception e) {
60 logger.info("发送短信告警异常",e); 61 logger.info("发送短信告警异常",e);
61 }finally { 62 }finally {
@@ -86,6 +87,8 @@ public class AliyunSmsNoticeImpl implements NoticeFactory { @@ -86,6 +87,8 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
86 } 87 }
87 88
88 private SendSmsResponse send(AsyncClient client,IotAlertLog iotAlertLog) throws ExecutionException, InterruptedException { 89 private SendSmsResponse send(AsyncClient client,IotAlertLog iotAlertLog) throws ExecutionException, InterruptedException {
  90 + if(null != phones && phones.size()!=0)
  91 + {
89 JSONObject jsonObject = new JSONObject(); 92 JSONObject jsonObject = new JSONObject();
90 jsonObject.put("userName",aliyunSmsNoticeDto.getSubjectName()+"用户"); 93 jsonObject.put("userName",aliyunSmsNoticeDto.getSubjectName()+"用户");
91 jsonObject.put("deviceName",iotAlertLog.getDevice_name()); 94 jsonObject.put("deviceName",iotAlertLog.getDevice_name());
@@ -99,6 +102,9 @@ public class AliyunSmsNoticeImpl implements NoticeFactory { @@ -99,6 +102,9 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
99 .build(); 102 .build();
100 CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest); 103 CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
101 SendSmsResponse resp = response.get(); 104 SendSmsResponse resp = response.get();
  105 + System.out.println("阿里云短信通知结果:"+JSONObject.toJSONString(resp.getBody()));
102 return resp; 106 return resp;
103 } 107 }
  108 + return null;
  109 + }
104 } 110 }
@@ -52,6 +52,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory { @@ -52,6 +52,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory {
52 AsyncClient client = createSendClient(); 52 AsyncClient client = createSendClient();
53 try { 53 try {
54 send(client,iotAlertLog); 54 send(client,iotAlertLog);
  55 + return true;
55 } catch (Exception e) { 56 } catch (Exception e) {
56 logger.info("发送短信告警异常",e); 57 logger.info("发送短信告警异常",e);
57 }finally { 58 }finally {
@@ -100,7 +101,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory { @@ -100,7 +101,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory {
100 .build(); 101 .build();
101 CompletableFuture<SingleCallByTtsResponse> response = client.singleCallByTts(singleCallByTtsRequest); 102 CompletableFuture<SingleCallByTtsResponse> response = client.singleCallByTts(singleCallByTtsRequest);
102 SingleCallByTtsResponse resp = response.get(); 103 SingleCallByTtsResponse resp = response.get();
103 - System.out.println(JSONObject.toJSONString(resp)); 104 + System.out.println("阿里云电话通知结果:"+JSONObject.toJSONString(resp));
104 } 105 }
105 106
106 } 107 }
@@ -35,9 +35,10 @@ public class SendAlarmService { @@ -35,9 +35,10 @@ public class SendAlarmService {
35 logger.info("开始发送告警消息"); 35 logger.info("开始发送告警消息");
36 try { 36 try {
37 List<IotAlertLog> list = DbOperateUtil.getIotAlertLogList(2,100); //未处理的告警 37 List<IotAlertLog> list = DbOperateUtil.getIotAlertLogList(2,100); //未处理的告警
38 - if(null != list && list.size()!=0) 38 + logger.info("获取未处理的告警");
  39 + while (null != list && list.size()!=0)
39 { 40 {
40 - logger.info("有告警数据:{}",list.size()); 41 +
41 List<Long> ids = new ArrayList<>(); 42 List<Long> ids = new ArrayList<>();
42 List<Long> notids = new ArrayList<>(); 43 List<Long> notids = new ArrayList<>();
43 44
@@ -68,6 +69,7 @@ public class SendAlarmService { @@ -68,6 +69,7 @@ public class SendAlarmService {
68 notids.add(iotAlertLog.getAlert_log_id()); 69 notids.add(iotAlertLog.getAlert_log_id());
69 break; 70 break;
70 case 3: //严重警告 71 case 3: //严重警告
  72 + logger.info("严重警告:{}", JSONObject.toJSONString(iotAlertLog));
71 73
72 if(NoticeFactory.request(iotAlertLog)) 74 if(NoticeFactory.request(iotAlertLog))
73 { 75 {
@@ -89,6 +91,9 @@ public class SendAlarmService { @@ -89,6 +91,9 @@ public class SendAlarmService {
89 { 91 {
90 DbOperateUtil.updateIotAlertLogStatus(notids,1); 92 DbOperateUtil.updateIotAlertLogStatus(notids,1);
91 } 93 }
  94 + logger.info("告警数据 {},发送告警 {},不需要发送告警 {}",list.size(),ids.size(),notids.size());
  95 + list = DbOperateUtil.getIotAlertLogList(2,100); //未处理的告警
  96 + logger.info("获取未处理的告警");
92 } 97 }
93 }catch (Exception e) 98 }catch (Exception e)
94 { 99 {
@@ -29,7 +29,7 @@ public class TriggerAlarmService { @@ -29,7 +29,7 @@ public class TriggerAlarmService {
29 29
30 // 创建Canal连接器 30 // 创建Canal连接器
31 private static CanalConnector connector = CanalConnectors.newSingleConnector( 31 private static CanalConnector connector = CanalConnectors.newSingleConnector(
32 - new InetSocketAddress("127.0.0.1", 11111), 32 + new InetSocketAddress(null==System.getProperty("canal_service_ip")?"8.129.82.37":System.getProperty("canal_service_ip"), null==System.getProperty("canal_service_port")?11111:Integer.parseInt(System.getProperty("canal_service_port"))),
33 "example", 33 "example",
34 "", 34 "",
35 "" 35 ""
@@ -136,6 +136,14 @@ public class UserInfoAlarmController extends BaseController { @@ -136,6 +136,14 @@ public class UserInfoAlarmController extends BaseController {
136 136
137 String channels = (String) map.get("channels"); 137 String channels = (String) map.get("channels");
138 map.put("channelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+channels+")")); 138 map.put("channelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+channels+")"));
  139 +
  140 + if(null != map.get("online_channels"))
  141 + {
  142 + map.put("onlineChannelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+map.get("online_channels")+")"));
  143 + }else {
  144 + map.put("onlineChannelList",null);
  145 + }
  146 +
139 } 147 }
140 } 148 }
141 149
@@ -227,7 +235,7 @@ public class UserInfoAlarmController extends BaseController { @@ -227,7 +235,7 @@ public class UserInfoAlarmController extends BaseController {
227 uPiotAlertUserNotice.setOnline_channels(online_channels); 235 uPiotAlertUserNotice.setOnline_channels(online_channels);
228 } 236 }
229 int upi = 0; 237 int upi = 0;
230 - if(StringUtils.isNotEmpty(uPiotAlertUserNotice.getChannels()) || StringUtils.isNotEmpty(uPiotAlertUserNotice.getAlert_ids())) 238 + if(StringUtils.isNotEmpty(uPiotAlertUserNotice.getChannels()) || StringUtils.isNotEmpty(uPiotAlertUserNotice.getAlert_ids())|| StringUtils.isNotEmpty(uPiotAlertUserNotice.getOnline_channels()))
231 { 239 {
232 upi = publicService.updateObject(uPiotAlertUserNotice,"id"); 240 upi = publicService.updateObject(uPiotAlertUserNotice,"id");
233 } 241 }