|
@@ -4,8 +4,15 @@ import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.DeviceconnCacheService;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.TypeCacheService;
|
|
|
import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
|
|
import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
|
|
|
import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
|
|
import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
|
|
|
|
|
+import com.yunfeiyun.agmp.iots.core.mqtt.DeviceTopicService;
|
|
|
|
|
+import com.yunfeiyun.agmp.iots.core.mqtt.modal.MqttTopicValue;
|
|
|
|
|
+import com.yunfeiyun.agmp.iots.device.common.Device;
|
|
|
import com.yunfeiyun.agmp.iots.service.BusinessCoreService;
|
|
import com.yunfeiyun.agmp.iots.service.BusinessCoreService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.util.TextUtils;
|
|
import org.apache.http.util.TextUtils;
|
|
@@ -14,6 +21,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
+import javax.annotation.RegEx;
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
@@ -28,7 +38,12 @@ public class ConnectionManager {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
BusinessCoreService businessCoreService;
|
|
BusinessCoreService businessCoreService;
|
|
|
-
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TypeCacheService typeCacheService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private DeviceTopicService deviceTopicService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private DeviceconnCacheService deviceconnCacheService;
|
|
|
/**
|
|
/**
|
|
|
* 加载数据库设备配置
|
|
* 加载数据库设备配置
|
|
|
*
|
|
*
|
|
@@ -114,5 +129,160 @@ public class ConnectionManager {
|
|
|
return jsonConfig;
|
|
return jsonConfig;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public void createDeviceHandle(IotDevice iotDevice) throws Exception{
|
|
|
|
|
+ IotDeviceconn iotDeviceconn = deviceconnCacheService.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
|
|
+ JSONArray connItemArray = JSONArray.parseArray(iotDeviceconn.getDevconnConfig());
|
|
|
|
|
+ for(Object connItemObject : connItemArray){
|
|
|
|
|
+ JSONObject connItem = JSONObject.from(connItemObject);
|
|
|
|
|
+ String type = connItem.getString("type");
|
|
|
|
|
+ if (TextUtils.isEmpty(type)) {
|
|
|
|
|
+ log.info("【设备创建初始化连接】协议加载 设备:{} 的连接信息中的type 为空:跳过", iotDevice.getDevCode());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ switch (type) {
|
|
|
|
|
+ case "mqtt":
|
|
|
|
|
+ mqttDeviceCreateHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "modbus-tcp": {
|
|
|
|
|
+ //先不处理,对接到了再梳理
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case "http": {
|
|
|
|
|
+ httpDeviceCreateHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ default: {
|
|
|
|
|
+ log.info("【设备:{} 创建初始化连接】其它类型:{},跳过", iotDevice.getDevCode(),type);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("【设备:{} 创建初始化连接】失败 异常信息:", iotDevice.getDevCode(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void mqttDeviceCreateHandle(IotDevice iotDevice) throws Exception {
|
|
|
|
|
+ String serviceName=typeCacheService.getServiceNameByDevTypeBid(iotDevice.getDevtypeBid());
|
|
|
|
|
+ String[] topics= deviceTopicService.getTopic(serviceName ,iotDevice.getDevCode());
|
|
|
|
|
+ List<MqttTopicValue> mqttTopicValues=new ArrayList<>();
|
|
|
|
|
+ //改设备的所有topics
|
|
|
|
|
+ for(String s:topics){
|
|
|
|
|
+ MqttTopicValue mqttTopicValue=new MqttTopicValue();
|
|
|
|
|
+ mqttTopicValue.setDevCode(iotDevice.getDevCode());
|
|
|
|
|
+ mqttTopicValue.setDevId(iotDevice.getDevBid());
|
|
|
|
|
+ mqttTopicValue.setTopic(s);
|
|
|
|
|
+ mqttTopicValues.add(mqttTopicValue);
|
|
|
|
|
+ }
|
|
|
|
|
+ mqttManager.topicSingleSubscribeDevice(iotDevice.getDevconnBid(),serviceName,mqttTopicValues);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void httpDeviceCreateHandle(IotDevice iotDevice) {
|
|
|
|
|
+ httpManager.deviceCreateHandle(iotDevice);
|
|
|
|
|
+ }
|
|
|
|
|
+ public void editDeviceHandle(IotDevice iotDevice){
|
|
|
|
|
+ IotDeviceconn iotDeviceconn = deviceconnCacheService.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
|
|
+ JSONArray connItemArray = JSONArray.parseArray(iotDeviceconn.getDevconnConfig());
|
|
|
|
|
+ for(Object connItemObject : connItemArray){
|
|
|
|
|
+ JSONObject connItem = JSONObject.from(connItemObject);
|
|
|
|
|
+ String type = connItem.getString("type");
|
|
|
|
|
+ if (TextUtils.isEmpty(type)) {
|
|
|
|
|
+ log.info("【设备创建初始化连接】协议加载 设备:{} 的连接信息中的type 为空:跳过", iotDevice.getDevCode());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ switch (type) {
|
|
|
|
|
+ case "mqtt":
|
|
|
|
|
+ mqttEditDeviceHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "modbus-tcp": {
|
|
|
|
|
+ //先不处理,对接到了再梳理
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case "http": {
|
|
|
|
|
+ httpEditDeviceHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ default: {
|
|
|
|
|
+ log.info("【设备:{} 创建初始化连接】其它类型:{},跳过", iotDevice.getDevCode(),type);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("【设备:{} 创建初始化连接】失败 异常信息:", iotDevice.getDevCode(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private void mqttEditDeviceHandle(IotDevice iotDevice) throws Exception {
|
|
|
|
|
+ String serviceName=typeCacheService.getServiceNameByDevTypeBid(iotDevice.getDevtypeBid());
|
|
|
|
|
+ String[] topics= deviceTopicService.getTopic(serviceName ,iotDevice.getDevCode());
|
|
|
|
|
+ List<MqttTopicValue> mqttTopicValues=new ArrayList<>();
|
|
|
|
|
+ //改设备的所有topics
|
|
|
|
|
+ for(String s:topics){
|
|
|
|
|
+ MqttTopicValue mqttTopicValue=new MqttTopicValue();
|
|
|
|
|
+ mqttTopicValue.setDevCode(iotDevice.getDevCode());
|
|
|
|
|
+ mqttTopicValue.setDevId(iotDevice.getDevBid());
|
|
|
|
|
+ mqttTopicValue.setTopic(s);
|
|
|
|
|
+ mqttTopicValues.add(mqttTopicValue);
|
|
|
|
|
+ }
|
|
|
|
|
+ mqttManager.topicSingleSubscribeDevice(iotDevice.getDevconnBid(),serviceName,mqttTopicValues);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void httpEditDeviceHandle(IotDevice iotDevice) {
|
|
|
|
|
+ httpManager.deviceCreateHandle(iotDevice);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void deleteDeviceHandle(IotDevice iotDevice){
|
|
|
|
|
+ IotDeviceconn iotDeviceconn = deviceconnCacheService.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
|
|
+ JSONArray connItemArray = JSONArray.parseArray(iotDeviceconn.getDevconnConfig());
|
|
|
|
|
+ for(Object connItemObject : connItemArray){
|
|
|
|
|
+ JSONObject connItem = JSONObject.from(connItemObject);
|
|
|
|
|
+ String type = connItem.getString("type");
|
|
|
|
|
+ if (TextUtils.isEmpty(type)) {
|
|
|
|
|
+ log.info("【设备创建初始化连接】协议加载 设备:{} 的连接信息中的type 为空:跳过", iotDevice.getDevCode());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ switch (type) {
|
|
|
|
|
+ case "mqtt":
|
|
|
|
|
+ mqttDeleteDeviceHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "modbus-tcp": {
|
|
|
|
|
+ //先不处理,对接到了再梳理
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case "http": {
|
|
|
|
|
+ httpDeleteDeviceHandle(iotDevice);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ default: {
|
|
|
|
|
+ log.info("【设备:{} 创建初始化连接】其它类型:{},跳过", iotDevice.getDevCode(),type);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("【设备:{} 创建初始化连接】失败 异常信息:", iotDevice.getDevCode(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private void mqttDeleteDeviceHandle(IotDevice iotDevice) throws Exception {
|
|
|
|
|
+ String serviceName=typeCacheService.getServiceNameByDevTypeBid(iotDevice.getDevtypeBid());
|
|
|
|
|
+ String[] topics= deviceTopicService.getTopic(serviceName ,iotDevice.getDevCode());
|
|
|
|
|
+ List<MqttTopicValue> mqttTopicValues=new ArrayList<>();
|
|
|
|
|
+ //改设备的所有topics
|
|
|
|
|
+ for(String s:topics){
|
|
|
|
|
+ MqttTopicValue mqttTopicValue=new MqttTopicValue();
|
|
|
|
|
+ mqttTopicValue.setDevCode(iotDevice.getDevCode());
|
|
|
|
|
+ mqttTopicValue.setDevId(iotDevice.getDevBid());
|
|
|
|
|
+ mqttTopicValue.setTopic(s);
|
|
|
|
|
+ mqttTopicValues.add(mqttTopicValue);
|
|
|
|
|
+ }
|
|
|
|
|
+ mqttManager.topicSingleSubscribeDevice(iotDevice.getDevconnBid(),serviceName,mqttTopicValues);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void httpDeleteDeviceHandle(IotDevice iotDevice) {
|
|
|
|
|
+ httpManager.deviceCreateHandle(iotDevice);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|