作者 钟来

告警系统功能完善

正在显示 18 个修改的文件 包含 461 行增加27 行删除
@@ -37,8 +37,6 @@ public abstract class JwtAuthenticationTokenFilter extends OncePerRequestFilter @@ -37,8 +37,6 @@ public abstract class JwtAuthenticationTokenFilter extends OncePerRequestFilter
37 UsernamePasswordAuthenticationToken authenticationToken = getUsernamePasswordAuthenticationToken(loginUser); 37 UsernamePasswordAuthenticationToken authenticationToken = getUsernamePasswordAuthenticationToken(loginUser);
38 authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); 38 authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
39 SecurityContextHolder.getContext().setAuthentication(authenticationToken); 39 SecurityContextHolder.getContext().setAuthentication(authenticationToken);
40 - }else {  
41 - throw new ServiceException("token验证失败");  
42 } 40 }
43 41
44 chain.doFilter(request, response); 42 chain.doFilter(request, response);
@@ -16,7 +16,6 @@ import org.springframework.security.crypto.password.PasswordEncoder; @@ -16,7 +16,6 @@ import org.springframework.security.crypto.password.PasswordEncoder;
16 * app接口安全策略. 没有{@link Order}注解优先级比上面低 16 * app接口安全策略. 没有{@link Order}注解优先级比上面低
17 */ 17 */
18 @Configuration 18 @Configuration
19 -@Order(1)  
20 public class AdminConfigurerAdapter extends DefaultSecurityConfig { 19 public class AdminConfigurerAdapter extends DefaultSecurityConfig {
21 20
22 @Autowired 21 @Autowired
  1 +package com.zhonglai.luhui.alarm.clas;
  2 +
  3 +import com.zhonglai.luhui.alarm.dto.IotAlertNoticeChannel;
  4 +import com.zhonglai.luhui.alarm.dto.IotAlertUserNotice;
  5 +
  6 +import java.io.Serializable;
  7 +import java.lang.reflect.Field;
  8 +
  9 +import static com.zhonglai.luhui.alarm.config.CachAlarmConfig.user_alarm_config;
  10 +import static com.zhonglai.luhui.alarm.config.CachAlarmConfig.user_alarm_notice_channel;
  11 +
  12 +/**
  13 + * 告警通知渠道配置
  14 + */
  15 +public class IotAlertNoticeChannelUpCach extends UpAlarmFactory<IotAlertNoticeChannel> implements Serializable {
  16 + public IotAlertNoticeChannelUpCach(IotAlertNoticeChannel beforeupAlarmDb, IotAlertNoticeChannel afterupAlarmDb) {
  17 + super(beforeupAlarmDb, afterupAlarmDb);
  18 + }
  19 +
  20 + @Override
  21 + void deleteGenerateAlarm() {
  22 + user_alarm_notice_channel.remove(beforeupAlarmDb.getId());
  23 + }
  24 +
  25 + @Override
  26 + void insertGenerateAlarm() {
  27 + user_alarm_notice_channel.put(afterupAlarmDb.getId(),afterupAlarmDb);
  28 + }
  29 +
  30 + @Override
  31 + void updateGenerateAlarm() {
  32 + Integer key = beforeupAlarmDb.getId();
  33 + IotAlertNoticeChannel iotAlertNoticeChannel = user_alarm_notice_channel.get(key);
  34 + if(null == iotAlertNoticeChannel)
  35 + {
  36 + insertGenerateAlarm();
  37 + }else {
  38 + Field[] fields = afterupAlarmDb.getClass().getDeclaredFields();
  39 +
  40 + for (Field field:fields)
  41 + {
  42 + field.setAccessible(true);
  43 + Object value = null;
  44 + try {
  45 + value = field.get(afterupAlarmDb);
  46 + if (value!= null) {
  47 + field.set(iotAlertNoticeChannel, value);
  48 + }
  49 + } catch (IllegalAccessException e) {
  50 + logger.info("告警通知缓存更新失败【{}】,【{}】",afterupAlarmDb,beforeupAlarmDb);
  51 + logger.error("告警通知缓存更新失败",e);
  52 + return;
  53 + }
  54 + }
  55 + }
  56 + user_alarm_notice_channel.put(key,iotAlertNoticeChannel);
  57 + }
  58 +
  59 + @Override
  60 + Object getNowValue(Object model_name, Integer type) {
  61 + return null;
  62 + }
  63 +}
  1 +package com.zhonglai.luhui.alarm.clas;
  2 +
  3 +import com.zhonglai.luhui.alarm.dto.IotAlertNoticeChannel;
  4 +import com.zhonglai.luhui.alarm.dto.IotAlertUserNotice;
  5 +
  6 +import java.io.Serializable;
  7 +import java.lang.reflect.Field;
  8 +
  9 +import static com.zhonglai.luhui.alarm.config.CachAlarmConfig.user_alarm_config;
  10 +
  11 +public class IotAlertUserNoticeUpCack extends UpAlarmFactory<IotAlertUserNotice> implements Serializable {
  12 + public IotAlertUserNoticeUpCack(IotAlertUserNotice beforeupAlarmDb, IotAlertUserNotice afterupAlarmDb) {
  13 + super(beforeupAlarmDb, afterupAlarmDb);
  14 + }
  15 +
  16 + @Override
  17 + void deleteGenerateAlarm() {
  18 + String key = beforeupAlarmDb.getUser_id()+"|"+beforeupAlarmDb.getType();
  19 + user_alarm_config.remove(key);
  20 + }
  21 +
  22 + @Override
  23 + void insertGenerateAlarm() {
  24 + String key = afterupAlarmDb.getUser_id()+"|"+afterupAlarmDb.getType();
  25 + user_alarm_config.put(key,afterupAlarmDb);
  26 + }
  27 +
  28 + @Override
  29 + void updateGenerateAlarm() {
  30 + String key = beforeupAlarmDb.getUser_id()+"|"+beforeupAlarmDb.getType();
  31 + IotAlertUserNotice iotAlertUserNotice = user_alarm_config.get(key);
  32 + if(null == iotAlertUserNotice)
  33 + {
  34 + insertGenerateAlarm();
  35 + }else {
  36 + Field[] fields = afterupAlarmDb.getClass().getDeclaredFields();
  37 +
  38 + for (Field field:fields)
  39 + {
  40 + field.setAccessible(true);
  41 + Object value = null;
  42 + try {
  43 + value = field.get(afterupAlarmDb);
  44 + if (value!= null) {
  45 + field.set(iotAlertUserNotice, value);
  46 + }
  47 + } catch (IllegalAccessException e) {
  48 + logger.info("告警通知缓存更新失败【{}】,【{}】",afterupAlarmDb,beforeupAlarmDb);
  49 + logger.error("告警通知缓存更新失败",e);
  50 + return;
  51 + }
  52 + }
  53 + }
  54 + user_alarm_config.remove(key);
  55 + key = afterupAlarmDb.getUser_id()+"|"+afterupAlarmDb.getType();
  56 + user_alarm_config.put(key,iotAlertUserNotice);
  57 + }
  58 +
  59 + @Override
  60 + Object getNowValue(Object model_name, Integer type) {
  61 + return null;
  62 + }
  63 +}
@@ -19,7 +19,7 @@ import java.util.List; @@ -19,7 +19,7 @@ import java.util.List;
19 * 告警工厂 19 * 告警工厂
20 */ 20 */
21 public abstract class UpAlarmFactory<T> { 21 public abstract class UpAlarmFactory<T> {
22 - private static final Logger logger = LoggerFactory.getLogger(UpAlarmFactory.class); 22 + protected static final Logger logger = LoggerFactory.getLogger(UpAlarmFactory.class);
23 protected List<IotAlertLog> list = new ArrayList<>(); 23 protected List<IotAlertLog> list = new ArrayList<>();
24 24
25 protected T beforeupAlarmDb; 25 protected T beforeupAlarmDb;
@@ -46,6 +46,13 @@ public abstract class UpAlarmFactory<T> { @@ -46,6 +46,13 @@ public abstract class UpAlarmFactory<T> {
46 return new IotTerminalAlarm(IotTerminal.instantiate(beforecolumns),IotTerminal.instantiate(aftercolumns)); 46 return new IotTerminalAlarm(IotTerminal.instantiate(beforecolumns),IotTerminal.instantiate(aftercolumns));
47 case "mqtt_broker.iot_alert": 47 case "mqtt_broker.iot_alert":
48 return new IotAlertAlarm(IotAlert.instantiate(beforecolumns),IotAlert.instantiate(aftercolumns)); 48 return new IotAlertAlarm(IotAlert.instantiate(beforecolumns),IotAlert.instantiate(aftercolumns));
  49 +
  50 + case "mqtt_broker.iot_alert_notice_channel":
  51 + return new IotAlertNoticeChannelUpCach(IotAlertNoticeChannel.instantiate(beforecolumns),IotAlertNoticeChannel.instantiate(aftercolumns));
  52 + case "mqtt_broker.iot_alert_user_notice":
  53 + return new IotAlertUserNoticeUpCack(IotAlertUserNotice.instantiate(beforecolumns),IotAlertUserNotice.instantiate(aftercolumns));
  54 + case "mqtt_broker.user_openid":
  55 + return new UserOpenidUpCach(UserOpenid.instantiate(beforecolumns),UserOpenid.instantiate(aftercolumns));
49 } 56 }
50 return null; 57 return null;
51 } 58 }
@@ -75,6 +82,10 @@ public abstract class UpAlarmFactory<T> { @@ -75,6 +82,10 @@ public abstract class UpAlarmFactory<T> {
75 82
76 protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue,Integer user_id,String device_name) 83 protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue,Integer user_id,String device_name)
77 { 84 {
  85 + if ("864814074929612".equals(client_id))
  86 + {
  87 + System.out.println(client_id);
  88 + }
78 if(StringUtils.isNotEmpty(newValue)) 89 if(StringUtils.isNotEmpty(newValue))
79 { 90 {
80 JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue); 91 JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue);
@@ -129,7 +140,7 @@ public abstract class UpAlarmFactory<T> { @@ -129,7 +140,7 @@ public abstract class UpAlarmFactory<T> {
129 String alrmname = attributeTriggers.getValueMapName().get(newValue); 140 String alrmname = attributeTriggers.getValueMapName().get(newValue);
130 if(StringUtils.isNotEmpty(alrmname)) 141 if(StringUtils.isNotEmpty(alrmname))
131 { 142 {
132 - IotAlertLog iotAlertLog = new IotAlertLog(iotAlert.getAlertId(),alrmname,iotAlert.getAlertLevel().intValue(),2,client_id,System.currentTimeMillis(),1,iotAlert.getUser_id() ,device_name,null==iotAlert.getUser_id()?1:2); 143 + IotAlertLog iotAlertLog = new IotAlertLog(iotAlert.getAlertId(),alrmname,iotAlert.getAlertLevel().intValue(),2,client_id,System.currentTimeMillis(),1,user_id ,device_name,null==iotAlert.getUser_id()?1:2);
133 list.add(iotAlertLog); 144 list.add(iotAlertLog);
134 } 145 }
135 } 146 }
@@ -145,11 +156,11 @@ public abstract class UpAlarmFactory<T> { @@ -145,11 +156,11 @@ public abstract class UpAlarmFactory<T> {
145 continue; 156 continue;
146 } 157 }
147 List<TriggerTriggers> attributeTriggersList = JSON.parseArray(iotAlert.getTriggers(),TriggerTriggers.class); 158 List<TriggerTriggers> attributeTriggersList = JSON.parseArray(iotAlert.getTriggers(),TriggerTriggers.class);
148 - alarmTriggerTriggers(attributeTriggersList,newValue,iotAlert.getAlertId(),iotAlert.getAlertName(),iotAlert.getAlertLevel().intValue(),client_id,iotAlert.getUser_id(),device_name); 159 + alarmTriggerTriggers(attributeTriggersList,newValue,iotAlert.getAlertId(),iotAlert.getAlertName(),iotAlert.getAlertLevel().intValue(),client_id,user_id,device_name,null==iotAlert.getUser_id()?1:2);
149 } 160 }
150 } 161 }
151 162
152 - public void alarmTriggerTriggers( List<TriggerTriggers> attributeTriggersList,Object newValue,Long alert_id, String alert_name, Integer alert_level, String device_id,Integer userId,String device_name ) 163 + public void alarmTriggerTriggers( List<TriggerTriggers> attributeTriggersList,Object newValue,Long alert_id, String alert_name, Integer alert_level, String device_id,Integer userId,String device_name,Integer alert_config_type)
153 { 164 {
154 if(null != attributeTriggersList && attributeTriggersList.size() != 0) 165 if(null != attributeTriggersList && attributeTriggersList.size() != 0)
155 { 166 {
@@ -188,7 +199,7 @@ public abstract class UpAlarmFactory<T> { @@ -188,7 +199,7 @@ public abstract class UpAlarmFactory<T> {
188 199
189 if(jieguo) 200 if(jieguo)
190 { 201 {
191 - IotAlertLog iotAlertLog = new IotAlertLog(alert_id,alert_name,alert_level,2,device_id,System.currentTimeMillis(),1,userId,device_name,null==userId?1:2); 202 + IotAlertLog iotAlertLog = new IotAlertLog(alert_id,alert_name,alert_level,2,device_id,System.currentTimeMillis(),1,userId,device_name,alert_config_type);
192 list.add(iotAlertLog); 203 list.add(iotAlertLog);
193 } 204 }
194 } 205 }
  1 +package com.zhonglai.luhui.alarm.clas;
  2 +
  3 +import com.zhonglai.luhui.alarm.dto.IotAlertNoticeChannel;
  4 +import com.zhonglai.luhui.alarm.dto.IotAlertUserNotice;
  5 +import com.zhonglai.luhui.alarm.dto.UserOpenid;
  6 +
  7 +import java.io.Serializable;
  8 +import java.lang.reflect.Field;
  9 +import java.util.HashMap;
  10 +import java.util.HashSet;
  11 +import java.util.Map;
  12 +import java.util.Set;
  13 +
  14 +import static com.zhonglai.luhui.alarm.config.CachAlarmConfig.user_alarm_notice_channel;
  15 +import static com.zhonglai.luhui.alarm.config.CachAlarmConfig.xcx_user_openid;
  16 +
  17 +public class UserOpenidUpCach extends UpAlarmFactory<UserOpenid> implements Serializable {
  18 + public UserOpenidUpCach(UserOpenid beforeupAlarmDb, UserOpenid afterupAlarmDb) {
  19 + super(beforeupAlarmDb, afterupAlarmDb);
  20 + }
  21 +
  22 + @Override
  23 + void deleteGenerateAlarm() {
  24 + Map<Integer, Set<String>> mapset = xcx_user_openid.get(beforeupAlarmDb.getXcx_config_id());
  25 + if(null != mapset)
  26 + {
  27 + Set<String> set = mapset.get(beforeupAlarmDb.getUser_id());
  28 + if(null != set)
  29 + {
  30 + set.remove(beforeupAlarmDb.getOpenid());
  31 + }
  32 + }
  33 + }
  34 +
  35 + @Override
  36 + void insertGenerateAlarm() {
  37 + if(null != afterupAlarmDb.getXcx_config_id())
  38 + {
  39 + Map<Integer, Set<String>> mapset = xcx_user_openid.get(afterupAlarmDb.getXcx_config_id());
  40 + if(null == mapset)
  41 + {
  42 + mapset = new HashMap<>();
  43 + xcx_user_openid.put(afterupAlarmDb.getXcx_config_id(),mapset);
  44 + }
  45 + if(null != afterupAlarmDb.getUser_id())
  46 + {
  47 + Set<String> set = mapset.get(afterupAlarmDb.getUser_id());
  48 + if(null == set)
  49 + {
  50 + set = new HashSet<>();
  51 + mapset.put(afterupAlarmDb.getUser_id(),set);
  52 + }
  53 + if(null != afterupAlarmDb.getOpenid())
  54 + {
  55 + set.add(afterupAlarmDb.getOpenid());
  56 + }
  57 +
  58 + }
  59 +
  60 + }
  61 +
  62 + }
  63 +
  64 + @Override
  65 + void updateGenerateAlarm() {
  66 + if(null != beforeupAlarmDb.getXcx_config_id())
  67 + {
  68 + Map<Integer, Set<String>> mapset = xcx_user_openid.get(beforeupAlarmDb.getXcx_config_id());
  69 + if(null == mapset)
  70 + {
  71 + insertGenerateAlarm();
  72 + }else {
  73 + if(null != beforeupAlarmDb.getUser_id())
  74 + {
  75 + Set<String> set = mapset.get(beforeupAlarmDb.getUser_id());
  76 + if(null != set )
  77 + {
  78 + set.remove(beforeupAlarmDb.getOpenid());
  79 + }
  80 + }
  81 + }
  82 + insertGenerateAlarm();
  83 + }
  84 +
  85 + }
  86 +
  87 + @Override
  88 + Object getNowValue(Object model_name, Integer type) {
  89 + return null;
  90 + }
  91 +}
@@ -20,26 +20,29 @@ public class CachAlarmConfig { @@ -20,26 +20,29 @@ public class CachAlarmConfig {
20 20
21 21
22 /** 属性告警路由 */ 22 /** 属性告警路由 */
23 - private static Map<Integer, Map<String, String>> product_attribute_route = new HashMap<>(); 23 + public static Map<Integer, Map<String, String>> product_attribute_route = new HashMap<>();
24 24
25 /** 触发告警路由 */ 25 /** 触发告警路由 */
26 - private static Map<Integer, Map<String, String>> product_trigger_route = new HashMap<>(); 26 + public static Map<Integer, Map<String, String>> product_trigger_route = new HashMap<>();
27 27
28 /** 告警配置 */ 28 /** 告警配置 */
29 - private static Map<Long, IotAlert> alarmConfig = new HashMap<>(); 29 + public static Map<Long, IotAlert> alarmConfig = new HashMap<>();
30 30
31 /** 告警配置 */ 31 /** 告警配置 */
32 - private static Map<String, Integer> device_product = new HashMap<>(); 32 + public static Map<String, Integer> device_product = new HashMap<>();
33 33
34 34
35 /** 用户告警通知配置 */ 35 /** 用户告警通知配置 */
36 - private static Map<String, IotAlertUserNotice> user_alarm_config = new HashMap<>(); 36 + public static Map<String, IotAlertUserNotice> user_alarm_config = new HashMap<>();
37 37
38 /** 用户告警通知渠道 */ 38 /** 用户告警通知渠道 */
39 - private static Map<Integer, IotAlertNoticeChannel> user_alarm_notice_channel = new HashMap<>(); 39 + public static Map<Integer, IotAlertNoticeChannel> user_alarm_notice_channel = new HashMap<>();
40 40
41 /** 用户微信小程序关系 */ 41 /** 用户微信小程序关系 */
42 - private static Map<Integer, Map<Integer, Set<String>>> xcx_user_openid = new HashMap<>(); 42 + public static Map<Integer, Map<Integer, Set<String>>> xcx_user_openid = new HashMap<>();
  43 +
  44 + /** 用户设备关系 */
  45 + public static Map<String, Integer> dervice_user = new HashMap<>();
43 46
44 public static void loadConfig() 47 public static void loadConfig()
45 { 48 {
@@ -63,6 +66,9 @@ public class CachAlarmConfig { @@ -63,6 +66,9 @@ public class CachAlarmConfig {
63 66
64 //加载用户微信小程序关系 67 //加载用户微信小程序关系
65 loadXcxUserOpenid(); 68 loadXcxUserOpenid();
  69 +
  70 + //加载设备用户
  71 + loadDeviceUser();
66 } 72 }
67 73
68 /** 74 /**
@@ -205,6 +211,31 @@ public class CachAlarmConfig { @@ -205,6 +211,31 @@ public class CachAlarmConfig {
205 } 211 }
206 212
207 /** 213 /**
  214 + * 加载设备用户
  215 + */
  216 + private static void loadDeviceUser()
  217 + {
  218 + List<IotTerminal> list = DbOperateUtil.getDeviceUserList();
  219 + if(null != list && list.size() != 0)
  220 + {
  221 + for (IotTerminal iotTerminal:list)
  222 + {
  223 + dervice_user.put(iotTerminal.getId(),iotTerminal.getUser_info_id());
  224 + }
  225 + }
  226 + }
  227 +
  228 + public static void addDeviceUser(String derviceInfoid,Integer userInfoId)
  229 + {
  230 + dervice_user.put(derviceInfoid,userInfoId);
  231 + }
  232 +
  233 + public static Integer getDeviceUser(String derviceInfoid)
  234 + {
  235 + return dervice_user.get(derviceInfoid);
  236 + }
  237 +
  238 + /**
208 * 添加属性告警路由 239 * 添加属性告警路由
209 * @param iotAlert 240 * @param iotAlert
210 */ 241 */
@@ -75,4 +75,9 @@ public class DbOperateUtil { @@ -75,4 +75,9 @@ public class DbOperateUtil {
75 { 75 {
76 return baseDao.findBysql("SELECT * FROM `user_openid`",UserOpenid.class); 76 return baseDao.findBysql("SELECT * FROM `user_openid`",UserOpenid.class);
77 } 77 }
  78 +
  79 + public static List<IotTerminal> getDeviceUserList()
  80 + {
  81 + return baseDao.findBysql("SELECT user_info_id,id FROM `iot_terminal` WHERE user_info_id IS NOT NULL",IotTerminal.class);
  82 + }
78 } 83 }
@@ -13,7 +13,7 @@ public class IotAlertLog @@ -13,7 +13,7 @@ public class IotAlertLog
13 private Long alert_log_id; // bigint NOT NULL AUTO_INCREMENT COMMENT '主键', 13 private Long alert_log_id; // bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
14 private Long alert_id; // bigint DEFAULT NULL COMMENT '告警ID', 14 private Long alert_id; // bigint DEFAULT NULL COMMENT '告警ID',
15 private String alert_name; // varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '告警名称', 15 private String alert_name; // varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '告警名称',
16 - private Integer alert_level; // int NOT NULL COMMENT '告警级别(1=轻微问题,2=提醒通知,3=严重警告)', 16 + private Integer alert_level; // int NOT NULL COMMENT '告警级别(1=提醒通知,2=轻微问题,3=严重警告)',
17 private Integer status; // int NOT NULL COMMENT '处理状态(1=不需要处理,2=未处理,3=已处理)', 17 private Integer status; // int NOT NULL COMMENT '处理状态(1=不需要处理,2=未处理,3=已处理)',
18 private String device_id; // varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备ID', 18 private String device_id; // varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备ID',
19 private Long create_time; // datetime DEFAULT NULL COMMENT '创建时间', 19 private Long create_time; // datetime DEFAULT NULL COMMENT '创建时间',
1 package com.zhonglai.luhui.alarm.dto; 1 package com.zhonglai.luhui.alarm.dto;
2 2
  3 +import com.alibaba.otter.canal.protocol.CanalEntry;
  4 +
  5 +import java.util.List;
  6 +
3 /** 7 /**
4 * 告警通知渠道 8 * 告警通知渠道
5 */ 9 */
@@ -9,6 +13,32 @@ public class IotAlertNoticeChannel { @@ -9,6 +13,32 @@ public class IotAlertNoticeChannel {
9 private String config; // json NOT NULL COMMENT '渠道配置', 13 private String config; // json NOT NULL COMMENT '渠道配置',
10 private String name; // varchar(50) NOT NULL COMMENT '名称', 14 private String name; // varchar(50) NOT NULL COMMENT '名称',
11 15
  16 + public static IotAlertNoticeChannel instantiate(List<CanalEntry.Column> columns) {
  17 + if (null == columns || columns.size() ==0)
  18 + {
  19 + return null;
  20 + }
  21 + IotAlertNoticeChannel iotAlertNoticeChannel = new IotAlertNoticeChannel();
  22 + for (CanalEntry.Column column : columns)
  23 + {
  24 + switch (column.getName())
  25 + {
  26 + case "id":
  27 + iotAlertNoticeChannel.setId(Integer.parseInt(column.getValue()));
  28 + break;
  29 + case "type":
  30 + iotAlertNoticeChannel.setType(Integer.parseInt(column.getValue()));
  31 + break;
  32 + case "config":
  33 + iotAlertNoticeChannel.setConfig(column.getValue());
  34 + break;
  35 + case "name":
  36 + iotAlertNoticeChannel.setName(column.getValue());
  37 + break;
  38 + }
  39 + }
  40 + return iotAlertNoticeChannel;
  41 + }
12 public Integer getId() { 42 public Integer getId() {
13 return id; 43 return id;
14 } 44 }
1 package com.zhonglai.luhui.alarm.dto; 1 package com.zhonglai.luhui.alarm.dto;
2 2
  3 +import com.alibaba.otter.canal.protocol.CanalEntry;
  4 +
  5 +import java.util.List;
  6 +
3 /** 7 /**
4 * 用户告警通知配置 8 * 用户告警通知配置
5 */ 9 */
@@ -10,6 +14,32 @@ public class IotAlertUserNotice { @@ -10,6 +14,32 @@ public class IotAlertUserNotice {
10 private String alert_ids; // varchar(100) DEFAULT '*' COMMENT '告警id集合(关联iot_alert_log和iot_alert_user表的id,多个英文逗号分割,*表示所有)', 14 private String alert_ids; // varchar(100) DEFAULT '*' COMMENT '告警id集合(关联iot_alert_log和iot_alert_user表的id,多个英文逗号分割,*表示所有)',
11 private Integer user_id; // int DEFAULT NULL COMMENT '用户id' 15 private Integer user_id; // int DEFAULT NULL COMMENT '用户id'
12 16
  17 + public static IotAlertUserNotice instantiate(List<CanalEntry.Column> columns) {
  18 + if (null == columns || columns.size() == 0) {
  19 + return null;
  20 + }
  21 + IotAlertUserNotice iotAlertUserNotice = new IotAlertUserNotice();
  22 + for (CanalEntry.Column column : columns) {
  23 + switch (column.getName()) {
  24 + case "id":
  25 + iotAlertUserNotice.setId(Integer.parseInt(column.getValue()));
  26 + break;
  27 + case "type":
  28 + iotAlertUserNotice.setType(Integer.parseInt(column.getValue()));
  29 + break;
  30 + case "channels":
  31 + iotAlertUserNotice.setChannels(column.getValue());
  32 + break;
  33 + case "alert_ids":
  34 + iotAlertUserNotice.setAlert_ids(column.getValue());
  35 + break;
  36 + case "user_id":
  37 + iotAlertUserNotice.setUser_id(Integer.parseInt(column.getValue()));
  38 + break;
  39 + }
  40 + }
  41 + return iotAlertUserNotice;
  42 + }
13 public Integer getId() { 43 public Integer getId() {
14 return id; 44 return id;
15 } 45 }
1 package com.zhonglai.luhui.alarm.dto; 1 package com.zhonglai.luhui.alarm.dto;
2 2
  3 +import com.alibaba.fastjson.JSONObject;
3 import com.alibaba.otter.canal.protocol.CanalEntry; 4 import com.alibaba.otter.canal.protocol.CanalEntry;
4 import com.zhonglai.luhui.alarm.clas.UpAlarmFactory; 5 import com.zhonglai.luhui.alarm.clas.UpAlarmFactory;
  6 +import com.zhonglai.luhui.alarm.config.CachAlarmConfig;
5 7
6 import java.util.List; 8 import java.util.List;
7 9
@@ -117,6 +119,16 @@ public class IotTerminal { @@ -117,6 +119,16 @@ public class IotTerminal {
117 { 119 {
118 return null; 120 return null;
119 } 121 }
  122 + if(iotTerminal.getId().startsWith("864814074929612"))
  123 + {
  124 + System.out.println(iotTerminal.getUser_info_id());
  125 + }
  126 + if(null != iotTerminal.getUser_info_id())
  127 + {
  128 + CachAlarmConfig.addDeviceUser(iotTerminal.getId(),iotTerminal.getUser_info_id());
  129 + }else {
  130 + iotTerminal.setUser_info_id(CachAlarmConfig.getDeviceUser(iotTerminal.getId()));
  131 + }
120 return iotTerminal; 132 return iotTerminal;
121 } 133 }
122 } 134 }
1 package com.zhonglai.luhui.alarm.dto; 1 package com.zhonglai.luhui.alarm.dto;
2 2
  3 +import com.alibaba.otter.canal.protocol.CanalEntry;
  4 +
  5 +import java.util.List;
  6 +
3 public class UserOpenid { 7 public class UserOpenid {
4 private Integer user_id; // int DEFAULT NULL COMMENT '用户id', 8 private Integer user_id; // int DEFAULT NULL COMMENT '用户id',
5 private String openid; // varchar(50) DEFAULT NULL COMMENT '微信用户的唯一标识', 9 private String openid; // varchar(50) DEFAULT NULL COMMENT '微信用户的唯一标识',
6 private Integer xcx_config_id; // int DEFAULT NULL COMMENT '微信平台的配置参数表id', 10 private Integer xcx_config_id; // int DEFAULT NULL COMMENT '微信平台的配置参数表id',
7 11
  12 + public static UserOpenid instantiate(List<CanalEntry.Column> columns) {
  13 + if (null == columns || columns.size() ==0)
  14 + {
  15 + return null;
  16 + }
  17 + UserOpenid userOpenid = new UserOpenid();
  18 + for (CanalEntry.Column column : columns)
  19 + {
  20 + switch (column.getName())
  21 + {
  22 + case "user_id":
  23 + userOpenid.setUser_id(Integer.parseInt(column.getValue()));
  24 + break;
  25 + case "openid":
  26 + userOpenid.setOpenid(column.getValue());
  27 + break;
  28 + case "xcx_config_id":
  29 + userOpenid.setXcx_config_id(Integer.parseInt(column.getValue()));
  30 + break;
  31 +
  32 + }
  33 + }
  34 + return userOpenid;
  35 + }
  36 +
8 public Integer getUser_id() { 37 public Integer getUser_id() {
9 return user_id; 38 return user_id;
10 } 39 }
@@ -3,6 +3,7 @@ package com.zhonglai.luhui.alarm.handle; @@ -3,6 +3,7 @@ package com.zhonglai.luhui.alarm.handle;
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.zhonglai.luhui.alarm.clas.IotAlertAlarm; 4 import com.zhonglai.luhui.alarm.clas.IotAlertAlarm;
5 import com.zhonglai.luhui.alarm.clas.IotTerminalAlarm; 5 import com.zhonglai.luhui.alarm.clas.IotTerminalAlarm;
  6 +import com.zhonglai.luhui.alarm.config.CachAlarmConfig;
6 import com.zhonglai.luhui.alarm.dao.DbOperateUtil; 7 import com.zhonglai.luhui.alarm.dao.DbOperateUtil;
7 import com.zhonglai.luhui.alarm.dto.*; 8 import com.zhonglai.luhui.alarm.dto.*;
8 import com.zhonglai.luhui.service.dao.util.StringUtils; 9 import com.zhonglai.luhui.service.dao.util.StringUtils;
@@ -27,18 +28,17 @@ public class TimerAlarmJob implements Job { @@ -27,18 +28,17 @@ public class TimerAlarmJob implements Job {
27 List<TriggerTriggers> triggersList = timerTriggers.getTriggersList(); 28 List<TriggerTriggers> triggersList = timerTriggers.getTriggersList();
28 String alertName = iotAlert.getAlertName(); 29 String alertName = iotAlert.getAlertName();
29 Long productId = iotAlert.getProductId(); 30 Long productId = iotAlert.getProductId();
30 - Integer user_id = iotAlert.getUser_id();  
31 String terminalids = timerTriggers.getTerminalids(); 31 String terminalids = timerTriggers.getTerminalids();
32 32
33 List<IotAlertLog> list = new ArrayList<>(); 33 List<IotAlertLog> list = new ArrayList<>();
34 34
35 - List<IotTerminal> iotTerminallist = DbOperateUtil.getTimerAlarmIotTerminal(productId,terminalids,user_id); 35 + List<IotTerminal> iotTerminallist = DbOperateUtil.getTimerAlarmIotTerminal(productId,terminalids,iotAlert.getUser_id());
36 if (null != iotTerminallist && iotTerminallist.size() != 0 ) 36 if (null != iotTerminallist && iotTerminallist.size() != 0 )
37 { 37 {
38 for (IotTerminal iotTerminal:iotTerminallist) 38 for (IotTerminal iotTerminal:iotTerminallist)
39 { 39 {
40 IotTerminalAlarm iotTerminalAlarm = new IotTerminalAlarm(iotTerminal,iotTerminal); 40 IotTerminalAlarm iotTerminalAlarm = new IotTerminalAlarm(iotTerminal,iotTerminal);
41 - iotTerminalAlarm.alarmTriggerTriggers(triggersList,iotTerminal.getThings_model_value(),iotAlert.getAlertId(),alertName,iotAlert.getAlertLevel().intValue(),iotTerminal.getId(),iotAlert.getUser_id(), iotTerminal.getName()); 41 + iotTerminalAlarm.alarmTriggerTriggers(triggersList,iotTerminal.getThings_model_value(),iotAlert.getAlertId(),alertName,iotAlert.getAlertLevel().intValue(),iotTerminal.getId(),CachAlarmConfig.getDeviceUser(iotTerminal.getId()), iotTerminal.getName(),null==iotAlert.getUser_id()?1:2);
42 List<IotAlertLog> serviceDtolist = iotTerminalAlarm.getList(); 42 List<IotAlertLog> serviceDtolist = iotTerminalAlarm.getList();
43 if(null != serviceDtolist && serviceDtolist.size()!=0) 43 if(null != serviceDtolist && serviceDtolist.size()!=0)
44 { 44 {
@@ -46,10 +46,7 @@ public class SendAlarmService { @@ -46,10 +46,7 @@ public class SendAlarmService {
46 } 46 }
47 switch (iotAlertLog.getAlert_level()) 47 switch (iotAlertLog.getAlert_level())
48 { 48 {
49 - case 1: //轻微问题  
50 - notids.add(iotAlertLog.getAlert_log_id());  
51 - break;  
52 - case 2: //提醒通知 49 + case 1: //提醒通知
53 if(NoticeFactory.notice(iotAlertLog)) 50 if(NoticeFactory.notice(iotAlertLog))
54 { 51 {
55 ids.add(iotAlertLog.getAlert_log_id()); 52 ids.add(iotAlertLog.getAlert_log_id());
@@ -57,6 +54,9 @@ public class SendAlarmService { @@ -57,6 +54,9 @@ public class SendAlarmService {
57 notids.add(iotAlertLog.getAlert_log_id()); 54 notids.add(iotAlertLog.getAlert_log_id());
58 } 55 }
59 break; 56 break;
  57 + case 2: //轻微问题
  58 + notids.add(iotAlertLog.getAlert_log_id());
  59 + break;
60 case 3: //严重警告 60 case 3: //严重警告
61 61
62 if(NoticeFactory.request(iotAlertLog)) 62 if(NoticeFactory.request(iotAlertLog))
@@ -63,19 +63,19 @@ public class TriggerAlarmService { @@ -63,19 +63,19 @@ public class TriggerAlarmService {
63 // 连接到Canal服务器 63 // 连接到Canal服务器
64 connector.connect(); 64 connector.connect();
65 // 订阅指定的表(这里以database.table为例) 65 // 订阅指定的表(这里以database.table为例)
66 - connector.subscribe("mqtt_broker\\.(iot_device|iot_terminal|iot_alert)"); 66 + connector.subscribe("mqtt_broker\\.(iot_device|iot_terminal|iot_alert|iot_alert_notice_channel|iot_alert_user_notice|user_openid)");
67 } 67 }
68 68
69 private static void handle() throws InterruptedException { 69 private static void handle() throws InterruptedException {
70 while (ThreadPoolUtil.executor.getPoolSize()-ThreadPoolUtil.executor.getActiveCount()>0) 70 while (ThreadPoolUtil.executor.getPoolSize()-ThreadPoolUtil.executor.getActiveCount()>0)
71 { 71 {
72 - logger.info("总线程数{},已使用线程数{},剩下线程数{}",ThreadPoolUtil.executor.getPoolSize(),ThreadPoolUtil.executor.getActiveCount(),ThreadPoolUtil.executor.getPoolSize()-ThreadPoolUtil.executor.getActiveCount()); 72 +// logger.info("总线程数{},已使用线程数{},剩下线程数{}",ThreadPoolUtil.executor.getPoolSize(),ThreadPoolUtil.executor.getActiveCount(),ThreadPoolUtil.executor.getPoolSize()-ThreadPoolUtil.executor.getActiveCount());
73 73
74 // 获取指定数量的数据 74 // 获取指定数量的数据
75 Message message = connector.getWithoutAck(100); 75 Message message = connector.getWithoutAck(100);
76 long batchId = message.getId(); 76 long batchId = message.getId();
77 int size = message.getEntries().size(); 77 int size = message.getEntries().size();
78 - logger.info("拉取的数据库同步数据量{}",size); 78 +// logger.info("拉取的数据库同步数据量{}",size);
79 if (batchId != -1 && size > 0) { 79 if (batchId != -1 && size > 0) {
80 ThreadPoolUtil.executor.schedule(() -> { 80 ThreadPoolUtil.executor.schedule(() -> {
81 try { 81 try {
  1 +package com.zhonglai.luhui.api.controller.iot;
  2 +
  3 +import com.ruoyi.common.annotation.Log;
  4 +import com.ruoyi.common.core.domain.AjaxResult;
  5 +import com.ruoyi.common.core.page.TableDataInfo;
  6 +import com.ruoyi.common.enums.BusinessType;
  7 +import com.zhonglai.luhui.action.BaseController;
  8 +import com.zhonglai.luhui.device.domain.IotAlertLog;
  9 +import com.zhonglai.luhui.device.service.IIotAlertLogService;
  10 +import com.zhonglai.luhui.security.utils.SecurityUtils;
  11 +import com.zhonglai.luhui.sys.utils.ExcelUtil;
  12 +import io.swagger.annotations.Api;
  13 +import io.swagger.annotations.ApiOperation;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.security.access.prepost.PreAuthorize;
  16 +import org.springframework.web.bind.annotation.*;
  17 +
  18 +import javax.servlet.http.HttpServletResponse;
  19 +import java.util.List;
  20 +
  21 +/**
  22 + * 设备告警Controller
  23 + *
  24 + * @author kerwincui
  25 + * @date 2022-01-13
  26 + */
  27 +@Api(tags = "设备告警日志")
  28 +@RestController
  29 +@RequestMapping("/iot/alertLog")
  30 +public class IotAlertLogController extends BaseController
  31 +{
  32 + @Autowired
  33 + private IIotAlertLogService alertLogService;
  34 +
  35 + @ApiOperation(value = "查询设备告警列表",notes = "参数说明:" +
  36 + "params.beginTime 开始时间" +
  37 + "params.endTime 结束时间" +
  38 + "alertName 告警名称为模糊匹配")
  39 + @GetMapping("/list")
  40 + public TableDataInfo list(IotAlertLog alertLog)
  41 + {
  42 + alertLog.setUser_id(SecurityUtils.getUserId().intValue());
  43 + startPage();
  44 + List<IotAlertLog> list = alertLogService.selectAlertLogList(alertLog);
  45 + return getDataTable(list);
  46 + }
  47 +
  48 + /**
  49 + * 导出设备告警列表
  50 + */
  51 + @ApiOperation("导出设备告警列表")
  52 + @Log(title = "设备告警", businessType = BusinessType.EXPORT)
  53 + @PostMapping("/export")
  54 + public void export(HttpServletResponse response, IotAlertLog alertLog)
  55 + {
  56 + alertLog.setUser_id(SecurityUtils.getUserId().intValue());
  57 + List<IotAlertLog> list = alertLogService.selectAlertLogList(alertLog);
  58 + ExcelUtil<IotAlertLog> util = new ExcelUtil<IotAlertLog>(IotAlertLog.class);
  59 + util.exportExcel(response, list, "设备告警数据");
  60 + }
  61 +
  62 + /**
  63 + * 获取设备告警详细信息
  64 + */
  65 + @ApiOperation("获取设备告警详细信息")
  66 + @GetMapping(value = "/{alertLogId}")
  67 + public AjaxResult getInfo(@PathVariable("alertLogId") Long alertLogId)
  68 + {
  69 + return AjaxResult.success(alertLogService.selectAlertLogByAlertLogId(alertLogId));
  70 + }
  71 +
  72 +}
1 -# 项目相关配置 jhlt: # 名称 name: zhonglai # 版本 version: 3.8.2 # 版权年份 copyrightYear: 2022 # 实例演示开关 demoEnabled: true # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath) profile: D:/ruoyi/uploadPath # 获取ip地址开关 addressEnabled: false # 验证码类型 math 数组计算 char 字符验证 captchaType: math # 开发环境配置 server: # 服务器的HTTP端口,默认为8080 port: 18080 servlet: # 应用的访问路径 context-path: / tomcat: # tomcat的URI编码 uri-encoding: UTF-8 # 连接数满后的排队数,默认为100 accept-count: 1000 threads: # tomcat最大线程数,默认为200 max: 800 # Tomcat启动初始化的线程数,默认值10 min-spare: 100 # 日志配置 logging: level: com.ruoyi: debug org.springframework: warn # Spring配置 spring: # 资源信息 messages: # 国际化资源文件路径 basename: i18n/messages profiles: active: druid # 文件上传 servlet: multipart: # 单个文件大小 max-file-size: 10MB # 设置总上传的文件大小 max-request-size: 20MB # 服务模块 devtools: restart: # 热部署开关 enabled: true # redis 配置 redis: # 地址 host: 47.112.163.61 # 端口,默认为6379 port: 9527 # 数据库索引 database: 1 # 密码 password: Luhui586 # 连接超时时间 timeout: 10s lettuce: pool: # 连接池中的最小空闲连接 min-idle: 0 # 连接池中的最大空闲连接 max-idle: 8 # 连接池的最大数据库连接数 max-active: 8 # #连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1ms # token配置 token: # 令牌自定义标识 header: Authorization # 令牌密钥 secret: abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) expireTime: 1440 rediskey: lh-api # MyBatis配置 mybatis: # 搜索指定包别名 typeAliasesPackage: com.ruoyi.**.domain,com.zhonglai.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/**/*Mapper.xml # 加载全局的配置文件 configLocation: classpath:mybatis/mybatis-config.xml # PageHelper分页插件 pagehelper: helperDialect: mysql supportMethodsArguments: true params: count=countSql # Swagger配置 swagger: # 是否开启swagger enabled: true # 请求前缀 pathMapping: /dev-api # 防止XSS攻击 xss: # 过滤开关 enabled: true # 排除链接(多个用逗号分隔) excludes: /system/notice # 匹配链接 urlPatterns: /system/*,/monitor/*,/tool/* mqtt: client: device_life: 180 sys: ## // 对于登录login 注册register 验证码captchaImage 允许匿名访问 antMatchers: /login/ApiLogin/*,/content/**,/test/**,/fish/FishPriceCollection/* # NameServer地址 rocketmq: name-server: 47.115.144.179:9876 # 默认的消息组 producer: group: deviceCommand send-message-timeout: 30000 send-topic: lh-mqtt-service-deviceCommand-test send-tags: 1  
  1 +# 项目相关配置 jhlt: # 名称 name: zhonglai # 版本 version: 3.8.2 # 版权年份 copyrightYear: 2022 # 实例演示开关 demoEnabled: true # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath) profile: D:/ruoyi/uploadPath # 获取ip地址开关 addressEnabled: false # 验证码类型 math 数组计算 char 字符验证 captchaType: math # 开发环境配置 server: # 服务器的HTTP端口,默认为8080 port: 18080 servlet: # 应用的访问路径 context-path: / tomcat: # tomcat的URI编码 uri-encoding: UTF-8 # 连接数满后的排队数,默认为100 accept-count: 1000 threads: # tomcat最大线程数,默认为200 max: 800 # Tomcat启动初始化的线程数,默认值10 min-spare: 100 # 日志配置 logging: level: com.ruoyi: debug org.springframework: warn com.zhonglai.luhui: debug # Spring配置 spring: # 资源信息 messages: # 国际化资源文件路径 basename: i18n/messages profiles: active: druid # 文件上传 servlet: multipart: # 单个文件大小 max-file-size: 10MB # 设置总上传的文件大小 max-request-size: 20MB # 服务模块 devtools: restart: # 热部署开关 enabled: true # redis 配置 redis: # 地址 host: 47.112.163.61 # 端口,默认为6379 port: 9527 # 数据库索引 database: 1 # 密码 password: Luhui586 # 连接超时时间 timeout: 10s lettuce: pool: # 连接池中的最小空闲连接 min-idle: 0 # 连接池中的最大空闲连接 max-idle: 8 # 连接池的最大数据库连接数 max-active: 8 # #连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1ms # token配置 token: # 令牌自定义标识 header: Authorization # 令牌密钥 secret: abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) expireTime: 1440 rediskey: lh-api # MyBatis配置 mybatis: # 搜索指定包别名 typeAliasesPackage: com.ruoyi.**.domain,com.zhonglai.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/**/*Mapper.xml # 加载全局的配置文件 configLocation: classpath:mybatis/mybatis-config.xml # PageHelper分页插件 pagehelper: helperDialect: mysql supportMethodsArguments: true params: count=countSql # Swagger配置 swagger: # 是否开启swagger enabled: true # 请求前缀 pathMapping: /dev-api # 防止XSS攻击 xss: # 过滤开关 enabled: true # 排除链接(多个用逗号分隔) excludes: /system/notice # 匹配链接 urlPatterns: /system/*,/monitor/*,/tool/* mqtt: client: device_life: 180 sys: ## // 对于登录login 注册register 验证码captchaImage 允许匿名访问 antMatchers: /login/ApiLogin/*,/content/**,/test/**,/fish/FishPriceCollection/* # NameServer地址 rocketmq: name-server: 47.115.144.179:9876 # 默认的消息组 producer: group: deviceCommand send-message-timeout: 30000 send-topic: lh-mqtt-service-deviceCommand-test send-tags: 1