|
@@ -1,9 +1,14 @@
|
|
|
package com.kuyuntech.vrv.platform.configuration.core;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmConfigBean;
|
|
|
+import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmRecordBean;
|
|
|
import com.kuyuntech.vrv.coreapi.bean.core.DeviceParamBean;
|
|
|
import com.kuyuntech.vrv.coreapi.bean.mqtt.DeviceParam;
|
|
|
+import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmConfigService;
|
|
|
+import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmRecordService;
|
|
|
import com.kuyuntech.vrv.coreapi.service.core.DeviceParamService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
@@ -20,17 +25,22 @@ import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
|
|
|
import org.springframework.messaging.MessageChannel;
|
|
|
import org.springframework.messaging.MessageHandler;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Configuration
|
|
|
public class MqttConfiguration {
|
|
|
- @Autowired
|
|
|
- private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
|
|
|
@Autowired
|
|
|
private DeviceParamService deviceParamService;
|
|
|
+ @Autowired
|
|
|
+ DeviceAlarmRecordService deviceAlarmRecordService;
|
|
|
+ @Autowired
|
|
|
+ DeviceAlarmConfigService deviceAlarmConfigService;
|
|
|
+
|
|
|
|
|
|
// 消费消息
|
|
|
|
|
@@ -65,7 +75,7 @@ public class MqttConfiguration {
|
|
|
@Bean
|
|
|
public MessageProducer inbound() {
|
|
|
// Paho客户端消息驱动通道适配器,主要用来订阅主题
|
|
|
- MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("askin-paho",
|
|
|
+ MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("A-paho",
|
|
|
mqttClientFactory(), "mqtt/335102012203A500/data", "mqtt/335102012203A500/cmd", "battery", "+/sensor");
|
|
|
adapter.setCompletionTimeout(5000);
|
|
|
|
|
@@ -98,6 +108,7 @@ public class MqttConfiguration {
|
|
|
} else if (topic.equals("collector")) {
|
|
|
System.out.println("采集器的消息:" + payload);
|
|
|
} else {
|
|
|
+ //设备传入
|
|
|
System.out.println("丢弃消息:主题[" + topic + "],负载:" + payload);
|
|
|
|
|
|
//String转Json
|
|
@@ -108,152 +119,133 @@ public class MqttConfiguration {
|
|
|
//取里面
|
|
|
List<DeviceParam> list = JSONObject.parseArray(devices, DeviceParam.class);
|
|
|
|
|
|
- //CK1
|
|
|
- String deviceId = list.get(0).getDeviceId();
|
|
|
- //CK2
|
|
|
- String deviceId2 = list.get(1).getDeviceId();
|
|
|
-
|
|
|
- //取deviceData
|
|
|
- HashMap<String, String> deviceData = list.get(0).getDeviceData();
|
|
|
- //CK2的deviceData
|
|
|
- HashMap<String, String> deviceData2 = list.get(1).getDeviceData();
|
|
|
-
|
|
|
- //存入数据库的新增方法,将第一次读取到的数据存入数据库
|
|
|
-
|
|
|
- //先将数据存入redis,检验所有数据,如果出现数据改变,就执行update更新数据库,并更新redis
|
|
|
-
|
|
|
- //用长度做判断
|
|
|
-// Long size = redisTemplate.opsForHash().size("paramKeyValue");
|
|
|
-// Long size2 = redisTemplate.opsForHash().size("paramKeyValueCK2");
|
|
|
-// System.out.println(size);
|
|
|
-// System.out.println(size2);
|
|
|
-
|
|
|
-// if (size == 0) {
|
|
|
-// //遍历CK1数据
|
|
|
-// for (Map.Entry<String, String> entry : deviceData.entrySet()) {
|
|
|
-//
|
|
|
-// String name = entry.getKey();
|
|
|
-// String value = entry.getValue();
|
|
|
-//
|
|
|
-// //存入hash
|
|
|
-// redisTemplate.opsForHash().put("paramKeyValue", name, value);
|
|
|
-//
|
|
|
-// System.out.println("CK1存入redis成功!");
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (size2 == 0) {
|
|
|
-// //遍历CK2数据
|
|
|
-// for (Map.Entry<String, String> entry : deviceData2.entrySet()) {
|
|
|
-//
|
|
|
-// String name = entry.getKey();
|
|
|
-// String value = entry.getValue();
|
|
|
-//
|
|
|
-// //存入hash
|
|
|
-// redisTemplate.opsForHash().put("paramKeyValueCK2", name, value);
|
|
|
-//
|
|
|
-// System.out.println("CK2存入redis成功!");
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- //遍历redis中hash的每一个键值对和mqtt传过来的数据进行比较
|
|
|
-
|
|
|
- //获取paramKeyValue键值对
|
|
|
-// HashMap<String, String> paramKeyValue = (HashMap<String, String>) redisTemplate.opsForHash().entries("paramKeyValue");
|
|
|
-// HashMap<String, String> paramKeyValueCK2 = (HashMap<String, String>) redisTemplate.opsForHash().entries("paramKeyValueCK2");
|
|
|
-
|
|
|
-// System.out.println("----------------------------");
|
|
|
-// System.out.println(deviceData);
|
|
|
-// System.out.println(paramKeyValue);
|
|
|
-// System.out.println("----------------------------");
|
|
|
-// System.out.println(deviceData2);
|
|
|
-// System.out.println(paramKeyValueCK2);
|
|
|
-// System.out.println("----------------------------");
|
|
|
-
|
|
|
-// boolean result = CompareParamValue.CompareParamValue(paramKeyValue, deviceData);
|
|
|
-// boolean result2 = CompareParamValue.CompareParamValue(paramKeyValueCK2, deviceData2);
|
|
|
-
|
|
|
-// System.out.println("CK1比较的结果:" + result);
|
|
|
-// System.out.println("CK2比较的结果:" + result2);
|
|
|
-
|
|
|
- //如果不相等,清除缓存,重新加入,并更新数据库
|
|
|
-// if (result == false) {
|
|
|
- //清除redis缓存
|
|
|
-// redisTemplate.delete("paramKeyValue");
|
|
|
-
|
|
|
- //重新存入redis
|
|
|
-// for (Map.Entry<String, String> entry : deviceData.entrySet()) {
|
|
|
-//
|
|
|
-// String name = entry.getKey();
|
|
|
-// String value = entry.getValue();
|
|
|
-//
|
|
|
-// //存入hash
|
|
|
-// redisTemplate.opsForHash().put("paramKeyValue", name, value);
|
|
|
-//
|
|
|
-// System.out.println("重新存入redis成功!");
|
|
|
-// }
|
|
|
-
|
|
|
- long id = 199;
|
|
|
-
|
|
|
- //更新数据库
|
|
|
- for (Map.Entry<String, String> entry : deviceData.entrySet()) {
|
|
|
- DeviceParamBean deviceParamBean = new DeviceParamBean();
|
|
|
-
|
|
|
- deviceParamBean.setSn(deviceId);
|
|
|
-
|
|
|
- String name = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
-
|
|
|
-
|
|
|
- deviceParamBean.setId(id);
|
|
|
-
|
|
|
- id = id + 1;
|
|
|
-
|
|
|
- deviceParamBean.setName(name);
|
|
|
- deviceParamBean.setValue(value);
|
|
|
-
|
|
|
- deviceParamBean = this.deviceParamService.updateParam(deviceParamBean);
|
|
|
-
|
|
|
- System.out.println("id为:" + id + "-- 更新后的数据:" + deviceParamBean);
|
|
|
+ //判断是否单个
|
|
|
+ if (list.size() == 1) {
|
|
|
+ System.out.println("这是单个数据的处理");
|
|
|
+
|
|
|
+ //取deviceData
|
|
|
+ HashMap<String, String> deviceData = list.get(0).getDeviceData();
|
|
|
+ System.out.println(deviceData + "=======");
|
|
|
+
|
|
|
+ //判断传来的是不是报警
|
|
|
+ for (Map.Entry<String, String> entry : deviceData.entrySet()) {
|
|
|
+
|
|
|
+ String name = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+
|
|
|
+ System.out.println(name == "WD_GBJ");
|
|
|
+ int var = Integer.parseInt(value);
|
|
|
+
|
|
|
+ if ((name == "WD_GBJ" && var == 1)
|
|
|
+ || (name == "WD_DBJ" && var == 1)
|
|
|
+ || (name == "SD_GBJ" && var == 1)
|
|
|
+ || (name == "SD_DBJ" && var == 1)
|
|
|
+ || (name == "SFJ_GZ" && var == 1)
|
|
|
+ || (name == "PFJ_GZ" && var == 1)
|
|
|
+ || (name == "FLZJ_GZ" && var == 1)
|
|
|
+ || (name == "GW_BJ" && var == 1)
|
|
|
+ || (name == "LWDS_CX" && var == 1)
|
|
|
+ || (name == "LWDS_ZX" && var == 1)
|
|
|
+ || (name == "JZ_GZ" && var == 1)
|
|
|
+ || (name == "QFYC_BJ" && var == 1)
|
|
|
+ || (name == "JZ_JT" && var == 1)) {
|
|
|
+ System.out.println("这是一个报警!!");
|
|
|
+ //找到对应的报警设置
|
|
|
+ DeviceAlarmConfigBean deviceAlarmConfigBean = deviceAlarmConfigService.findByName(name);
|
|
|
+ //添加一个报警记录
|
|
|
+ DeviceAlarmRecordBean deviceAlarmRecordBean = new DeviceAlarmRecordBean();
|
|
|
+ deviceAlarmRecordBean.setAlarmName(deviceAlarmConfigBean.getAlarmName());
|
|
|
+ deviceAlarmRecordBean.setAlarmType(deviceAlarmConfigBean.getAlarmType());
|
|
|
+ deviceAlarmRecordBean.setSn("CK1");
|
|
|
+ deviceAlarmRecordBean.setContent(deviceAlarmConfigBean.getAlarmName());
|
|
|
+ deviceAlarmRecordBean.setAlarmConfigCode(deviceAlarmConfigBean.getCode());
|
|
|
+ deviceAlarmRecordBean.setStartTime(new Date());
|
|
|
+ //状态:1表示未处理,0表示已处理
|
|
|
+ deviceAlarmRecordBean.setStatus(value);
|
|
|
+ deviceAlarmRecordService.add(deviceAlarmRecordBean);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((name == "WD_GBJ" && var == 0)
|
|
|
+ || (name == "WD_DBJ" && var == 0)
|
|
|
+ || (name == "SD_GBJ" && var == 0)
|
|
|
+ || (name == "SD_DBJ" && var == 0)
|
|
|
+ || (name == "SFJ_GZ" && var == 0)
|
|
|
+ || (name == "PFJ_GZ" && var == 0)
|
|
|
+ || (name == "FLZJ_GZ" && var == 0)
|
|
|
+ || (name == "GW_BJ" && var == 0)
|
|
|
+ || (name == "LWDS_CX" && var == 0)
|
|
|
+ || (name == "LWDS_ZX" && var == 0)
|
|
|
+ || (name == "JZ_GZ" && var == 0)
|
|
|
+ || (name == "QFYC_BJ" && var == 0)
|
|
|
+ || (name == "JZ_JT" && var == 0)) {
|
|
|
+ System.out.println("这是一个报警处理,更新为已处理和结束时间!!");
|
|
|
+ //找到对应的报警设置
|
|
|
+ DeviceAlarmConfigBean deviceAlarmConfigBean = deviceAlarmConfigService.findByName(name);
|
|
|
+
|
|
|
+ //状态:1表示未处理,0表示已处理
|
|
|
+ deviceAlarmRecordService.updateParam(deviceAlarmConfigBean.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ System.out.println("这是所有数据的处理");
|
|
|
+
|
|
|
+ //CK1
|
|
|
+ String deviceId = list.get(0).getDeviceId();
|
|
|
+ //CK2
|
|
|
+ String deviceId2 = list.get(1).getDeviceId();
|
|
|
+
|
|
|
+ //取deviceData
|
|
|
+ HashMap<String, String> deviceData = list.get(0).getDeviceData();
|
|
|
+ //CK2的deviceData
|
|
|
+ HashMap<String, String> deviceData2 = list.get(1).getDeviceData();
|
|
|
+
|
|
|
+ long id = 199;
|
|
|
+
|
|
|
+ //更新数据库
|
|
|
+ for (Map.Entry<String, String> entry : deviceData.entrySet()) {
|
|
|
+ DeviceParamBean deviceParamBean = new DeviceParamBean();
|
|
|
+
|
|
|
+ deviceParamBean.setSn(deviceId);
|
|
|
+
|
|
|
+ String name = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+
|
|
|
+
|
|
|
+ deviceParamBean.setId(id);
|
|
|
+
|
|
|
+ id = id + 1;
|
|
|
+
|
|
|
+ deviceParamBean.setName(name);
|
|
|
+ deviceParamBean.setValue(value);
|
|
|
+
|
|
|
+ deviceParamBean = this.deviceParamService.updateParam(deviceParamBean);
|
|
|
+
|
|
|
+ System.out.println("id为:" + id + "-- 更新后的数据:" + deviceParamBean);
|
|
|
|
|
|
- }
|
|
|
-// }
|
|
|
-
|
|
|
- //如果不相等,清除缓存,重新加入,并更新数据库
|
|
|
-// if (result2 == false) {
|
|
|
- //清除redis缓存
|
|
|
-// redisTemplate.delete("paramKeyValueCK2");
|
|
|
-
|
|
|
- //重新存入redis
|
|
|
-// for (Map.Entry<String, String> entry : deviceData2.entrySet()) {
|
|
|
-//
|
|
|
-// String name = entry.getKey();
|
|
|
-// String value = entry.getValue();
|
|
|
-//
|
|
|
-// //存入hash
|
|
|
-// redisTemplate.opsForHash().put("paramKeyValueCK2", name, value);
|
|
|
-//
|
|
|
-// System.out.println("重新存入redis成功!");
|
|
|
-// }
|
|
|
-
|
|
|
- //更新数据库
|
|
|
- for (Map.Entry<String, String> entry : deviceData2.entrySet()) {
|
|
|
- DeviceParamBean deviceParamBean = new DeviceParamBean();
|
|
|
- deviceParamBean.setSn(deviceId2);
|
|
|
-
|
|
|
- for (long i = 298; i < 300; i++) {
|
|
|
- deviceParamBean.setId(i);
|
|
|
}
|
|
|
|
|
|
- String name = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
+ //更新数据库
|
|
|
+ for (Map.Entry<String, String> entry : deviceData2.entrySet()) {
|
|
|
+ DeviceParamBean deviceParamBean = new DeviceParamBean();
|
|
|
+ deviceParamBean.setSn(deviceId2);
|
|
|
|
|
|
- deviceParamBean.setName(name);
|
|
|
- deviceParamBean.setValue(value);
|
|
|
+ for (long i = 308; i < 310; i++) {
|
|
|
+ deviceParamBean.setId(i);
|
|
|
+ }
|
|
|
|
|
|
- deviceParamBean = this.deviceParamService.updateParam(deviceParamBean);
|
|
|
+ String name = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+
|
|
|
+ deviceParamBean.setName(name);
|
|
|
+ deviceParamBean.setValue(value);
|
|
|
+
|
|
|
+ deviceParamBean = this.deviceParamService.updateParam(deviceParamBean);
|
|
|
+ }
|
|
|
}
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
};
|