|
|
@@ -9,6 +9,7 @@ import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
|
|
|
import com.yunfeiyun.agmp.iot.common.model.mq.IotDeviceEditMqModel;
|
|
|
import com.yunfeiyun.agmp.iot.common.service.DeviceconnCacheService;
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.IotMqttTopicCacheService;
|
|
|
import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
|
|
|
import com.yunfeiyun.agmp.iots.core.mqtt.DeviceTopicService;
|
|
|
import com.yunfeiyun.agmp.iots.core.mqtt.modal.MqttTopicValue;
|
|
|
@@ -40,6 +41,8 @@ public class ConnectionManager {
|
|
|
private DeviceTopicService deviceTopicService;
|
|
|
@Resource
|
|
|
private DeviceconnCacheService deviceconnCacheService;
|
|
|
+ @Resource
|
|
|
+ private IotMqttTopicCacheService iotMqttTopicCacheService;
|
|
|
|
|
|
private boolean INIT_SUCCESS = false;
|
|
|
|
|
|
@@ -69,6 +72,7 @@ public class ConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
this.INIT_SUCCESS = true;
|
|
|
+ log.info("【初始化】所有协议加载完成");
|
|
|
}
|
|
|
|
|
|
public boolean initCompleted(){
|
|
|
@@ -170,6 +174,65 @@ public class ConnectionManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void createDeviceByBatchHandle(IotDeviceconn iotDeviceconn,List<IotDevice> iotDeviceList){
|
|
|
+ iotDeviceconn = deviceconnCacheService.getIotDeviceConnByDevconnBid(iotDeviceconn.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 为空:跳过");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ switch (type) {
|
|
|
+ case "mqtt":
|
|
|
+ mqttDeviceCreateByBatchHandle(iotDeviceconn,iotDeviceList);
|
|
|
+ break;
|
|
|
+ case "modbus-tcp": {
|
|
|
+ //先不处理,对接到了再梳理
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "http": {
|
|
|
+ httpDeviceCreateByBatchHandle(iotDeviceconn,iotDeviceList);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ log.info("【设备批量创建初始化连接】其它类型:{},跳过", type);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("【设备批量创建初始化连接】失败 异常信息:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void httpDeviceCreateByBatchHandle(IotDeviceconn iotDeviceconn, List<IotDevice> iotDeviceList) {
|
|
|
+ httpManager.deviceCreateByBatchHandle(iotDeviceconn,iotDeviceList);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mqttDeviceCreateByBatchHandle(IotDeviceconn iotDeviceconn,List<IotDevice> iotDeviceList) throws MqttException {
|
|
|
+ IotDeviceDictEnum iotDeviceDictEnum = IotDeviceDictEnum.findEnumByCode(iotDeviceconn.getDevtypeBid());
|
|
|
+ String serviceName = iotDeviceDictEnum.getServiceName();
|
|
|
+ List<MqttTopicValue> mqttTopicValues = new ArrayList<>();
|
|
|
+ for(IotDevice iotDevice : iotDeviceList){
|
|
|
+ String[] topics = deviceTopicService.getTopic(serviceName, iotDevice.getDevCode());
|
|
|
+ //改设备的所有topics
|
|
|
+ for (String s : topics) {
|
|
|
+ MqttTopicValue mqttTopicValue = new MqttTopicValue();
|
|
|
+ mqttTopicValue.setDevCode(iotDevice.getDevCode());
|
|
|
+ mqttTopicValue.setDevId(iotDevice.getDevBid());
|
|
|
+ mqttTopicValue.setServiceName(IotDeviceDictEnum.findServiceNameByDevTypeBid(iotDevice.getDevtypeBid()));
|
|
|
+ mqttTopicValue.setTopic(s);
|
|
|
+ iotMqttTopicCacheService.addTopicCache(iotDevice.getDevCode(),topics);
|
|
|
+ mqttTopicValues.add(mqttTopicValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String connId = mqttManager.getMqttConnectionId(iotDeviceconn);
|
|
|
+ mqttManager.topicSingleSubscribeDevice(connId, serviceName, mqttTopicValues);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设备删除时,删除设备连接
|
|
|
*/
|
|
|
@@ -301,6 +364,7 @@ public class ConnectionManager {
|
|
|
IotDeviceDictEnum iotDeviceDictEnum = IotDeviceDictEnum.findEnumByCode(iotDevice.getDevtypeBid());
|
|
|
String serviceName = iotDeviceDictEnum.getServiceName();
|
|
|
String[] topics = deviceTopicService.getTopic(serviceName, iotDevice.getDevCode());
|
|
|
+
|
|
|
List<MqttTopicValue> mqttTopicValues = new ArrayList<>();
|
|
|
//改设备的所有topics
|
|
|
for (String s : topics) {
|
|
|
@@ -314,6 +378,7 @@ public class ConnectionManager {
|
|
|
IotDeviceconn iotDeviceconn = deviceconnCacheService.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
|
|
|
String connId = mqttManager.getMqttConnectionId(iotDeviceconn);
|
|
|
mqttManager.topicSingleSubscribeDevice(connId, serviceName, mqttTopicValues);
|
|
|
+ iotMqttTopicCacheService.addTopicCache(iotDevice.getDevCode(),topics);
|
|
|
}
|
|
|
|
|
|
private void httpDeviceCreateHandle(IotDevice iotDevice) {
|
|
|
@@ -343,6 +408,7 @@ public class ConnectionManager {
|
|
|
IotDeviceconn iotDeviceconn = deviceconnCacheService.getIotDeviceConnByIotDevice(iotDevice);
|
|
|
String connId = mqttManager.getMqttConnectionId(iotDeviceconn);
|
|
|
mqttManager.topicBatchUnSubscribeDevices(connId, serviceName, mqttTopicValues);
|
|
|
+ iotMqttTopicCacheService.removeTopicCache(iotDevice.getDevCode());
|
|
|
}
|
|
|
|
|
|
private void httpDeleteDeviceHandle(IotDevice iotDevice) {
|