作者 钟来

添加告警短信和语音通知

... ... @@ -31,6 +31,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="params.beginTime != null "> and create_time &gt;= #{params.beginTime}</if>
<if test="params.endTime != null "> and create_time &lt;= #{params.endTime}</if>
</where>
order by create_time desc
</select>
<select id="selectAlertLogByAlertLogId" parameterType="Long" resultMap="AlertLogResult">
... ...
... ... @@ -40,11 +40,17 @@ public class IotDeviceAlarm extends UpAlarmFactory<IotDevice> implements Seriali
{
CachAlarmConfig.putDeviceProduct(afterupAlarmDb.getClient_id(), afterupAlarmDb.getProduct_id());
}
Integer product_id = CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getClient_id());
if(-1==product_id) //没有产品id的直接排除
{
return;
}
//通过变更的数据获得对应的告警规则
valueUp(beforeupAlarmDb.getClient_id(),CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getClient_id()),beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),null,"主机"+afterupAlarmDb.getClient_id());
valueUp(beforeupAlarmDb.getClient_id(),product_id,beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),null,"主机"+afterupAlarmDb.getClient_id());
//在线状态
if(null != afterupAlarmDb.getStatus() && null != beforeupAlarmDb.getStatus() && beforeupAlarmDb.getStatus()-afterupAlarmDb.getStatus()!=0)
if(null != afterupAlarmDb.getStatus() && null != beforeupAlarmDb.getStatus() && beforeupAlarmDb.getStatus()-afterupAlarmDb.getStatus()!=0 && afterupAlarmDb.getStatus()==4)
{
switch (afterupAlarmDb.getStatus())
{
... ...
... ... @@ -38,11 +38,16 @@ public class IotTerminalAlarm extends UpAlarmFactory<IotTerminal> implements Ser
@Override
public void updateGenerateAlarm() {
Integer product_id = CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getId());
if(-1==product_id) //没有产品id的直接排除
{
return;
}
//通过变更的数据获得对应的告警规则
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());
valueUp(beforeupAlarmDb.getId(),product_id ,beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value(),beforeupAlarmDb.getUser_info_id(),null==beforeupAlarmDb.getName()?beforeupAlarmDb.getId():beforeupAlarmDb.getName());
//在线状态
if(null != afterupAlarmDb.getOnline() && null != beforeupAlarmDb.getOnline() && beforeupAlarmDb.getOnline()-afterupAlarmDb.getOnline()!=0)
if(null != afterupAlarmDb.getOnline() && null != beforeupAlarmDb.getOnline() && beforeupAlarmDb.getOnline()-afterupAlarmDb.getOnline()!=0 && afterupAlarmDb.getOnline()==4)
{
switch (afterupAlarmDb.getOnline())
{
... ...
... ... @@ -83,10 +83,6 @@ public abstract class UpAlarmFactory<T> {
protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue,Integer user_id,String device_name)
{
if ("864814074929612".equals(client_id))
{
System.out.println(client_id);
}
if(StringUtils.isNotEmpty(newValue))
{
JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue);
... ... @@ -103,7 +99,7 @@ public abstract class UpAlarmFactory<T> {
JSONObject newObject = newjson.getJSONObject(key);
String type = newObject.getString("type");
nv = GenericComparator.convertToType(newObject.getString("saveView"),type);
if(oldjson.containsKey("key"))
if(oldjson.containsKey(key))
{
ov = GenericComparator.convertToType(oldjson.getJSONObject(key).getString("saveView"),type);
}else {
... ...
... ... @@ -409,10 +409,11 @@ public class CachAlarmConfig {
{
try {
productId = DbOperateUtil.getProductId(deviceId);
if (null != productId)
if (null == productId || productId<12) //排除没有产品id,或者小于12的产品id
{
device_product.put(deviceId,productId);
productId = -1;
}
device_product.put(deviceId,productId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
... ... @@ -518,7 +519,7 @@ public class CachAlarmConfig {
{
if(user_phone.containsKey(user_id) && null != user_phone.get(user_id))
{
return user_phone.get(user_id);
return new HashSet<>(user_phone.get(user_id));
}
return null;
}
... ...
... ... @@ -84,7 +84,7 @@ public class DbOperateUtil {
}
public static List<UserAlertPhone> getUserPhoneList()
{
return baseDao.findBysql("SELECT user_id,phone FROM `user_alert_phone` WHERE user_info_id IS NOT NULL",UserAlertPhone.class);
return baseDao.findBysql("SELECT user_id,phone FROM `user_alert_phone` WHERE user_id IS NOT NULL",UserAlertPhone.class);
}
}
... ...
... ... @@ -64,7 +64,8 @@ public interface NoticeFactory {
}
if(null != noticeFactory)
{
rb=rb&&noticeFactory.send(iotAlertLog);
boolean nb = noticeFactory.send(iotAlertLog);
rb=rb&&nb;
}
}
... ...
... ... @@ -56,6 +56,7 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
try {
send(client,iotAlertLog);
return true;
} catch (Exception e) {
logger.info("发送短信告警异常",e);
}finally {
... ... @@ -86,6 +87,8 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
}
private SendSmsResponse send(AsyncClient client,IotAlertLog iotAlertLog) throws ExecutionException, InterruptedException {
if(null != phones && phones.size()!=0)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("userName",aliyunSmsNoticeDto.getSubjectName()+"用户");
jsonObject.put("deviceName",iotAlertLog.getDevice_name());
... ... @@ -99,6 +102,9 @@ public class AliyunSmsNoticeImpl implements NoticeFactory {
.build();
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
SendSmsResponse resp = response.get();
System.out.println("阿里云短信通知结果:"+JSONObject.toJSONString(resp.getBody()));
return resp;
}
return null;
}
}
... ...
... ... @@ -52,6 +52,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory {
AsyncClient client = createSendClient();
try {
send(client,iotAlertLog);
return true;
} catch (Exception e) {
logger.info("发送短信告警异常",e);
}finally {
... ... @@ -100,7 +101,7 @@ public class AliyunTTSNoticeImpl implements NoticeFactory {
.build();
CompletableFuture<SingleCallByTtsResponse> response = client.singleCallByTts(singleCallByTtsRequest);
SingleCallByTtsResponse resp = response.get();
System.out.println(JSONObject.toJSONString(resp));
System.out.println("阿里云电话通知结果:"+JSONObject.toJSONString(resp));
}
}
... ...
... ... @@ -35,9 +35,10 @@ public class SendAlarmService {
logger.info("开始发送告警消息");
try {
List<IotAlertLog> list = DbOperateUtil.getIotAlertLogList(2,100); //未处理的告警
if(null != list && list.size()!=0)
logger.info("获取未处理的告警");
while (null != list && list.size()!=0)
{
logger.info("有告警数据:{}",list.size());
List<Long> ids = new ArrayList<>();
List<Long> notids = new ArrayList<>();
... ... @@ -68,6 +69,7 @@ public class SendAlarmService {
notids.add(iotAlertLog.getAlert_log_id());
break;
case 3: //严重警告
logger.info("严重警告:{}", JSONObject.toJSONString(iotAlertLog));
if(NoticeFactory.request(iotAlertLog))
{
... ... @@ -89,6 +91,9 @@ public class SendAlarmService {
{
DbOperateUtil.updateIotAlertLogStatus(notids,1);
}
logger.info("告警数据 {},发送告警 {},不需要发送告警 {}",list.size(),ids.size(),notids.size());
list = DbOperateUtil.getIotAlertLogList(2,100); //未处理的告警
logger.info("获取未处理的告警");
}
}catch (Exception e)
{
... ...
... ... @@ -29,7 +29,7 @@ public class TriggerAlarmService {
// 创建Canal连接器
private static CanalConnector connector = CanalConnectors.newSingleConnector(
new InetSocketAddress("127.0.0.1", 11111),
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"))),
"example",
"",
""
... ...
... ... @@ -136,6 +136,14 @@ public class UserInfoAlarmController extends BaseController {
String channels = (String) map.get("channels");
map.put("channelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+channels+")"));
if(null != map.get("online_channels"))
{
map.put("onlineChannelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+map.get("online_channels")+")"));
}else {
map.put("onlineChannelList",null);
}
}
}
... ... @@ -227,7 +235,7 @@ public class UserInfoAlarmController extends BaseController {
uPiotAlertUserNotice.setOnline_channels(online_channels);
}
int upi = 0;
if(StringUtils.isNotEmpty(uPiotAlertUserNotice.getChannels()) || StringUtils.isNotEmpty(uPiotAlertUserNotice.getAlert_ids()))
if(StringUtils.isNotEmpty(uPiotAlertUserNotice.getChannels()) || StringUtils.isNotEmpty(uPiotAlertUserNotice.getAlert_ids())|| StringUtils.isNotEmpty(uPiotAlertUserNotice.getOnline_channels()))
{
upi = publicService.updateObject(uPiotAlertUserNotice,"id");
}
... ...