Просмотр исходного кода

阶段提交:解析json数组

yf_zn 1 год назад
Родитель
Сommit
52e69570e5

+ 21 - 0
src/main/java/com/yunfeiyun/agmp/iots/common/modal/IotDeviceconnResVo.java

@@ -0,0 +1,21 @@
+package com.yunfeiyun.agmp.iots.common.modal;
+
+import com.yunfeiyun.agmp.common.annotation.Excel;
+import com.yunfeiyun.agmp.iot.common.domain.IotBaseEntity;
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
+import lombok.Data;
+
+/**
+ * 设备连接配置对象 IotDeviceconn
+ *
+ * @author 杨晓辉
+ * @date 2024-11-06
+ */
+@Data
+public class IotDeviceconnResVo extends IotDeviceconn
+{
+    private String devclassName;
+    private String firmName;
+    private String devNum;
+    private String firmBid;
+}

+ 51 - 38
src/main/java/com/yunfeiyun/agmp/iots/core/manager/ConnectionManager.java

@@ -1,7 +1,9 @@
 package com.yunfeiyun.agmp.iots.core.manager;
 
+import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
 import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
 import com.yunfeiyun.agmp.iots.service.BusinessCoreService;
 import lombok.extern.slf4j.Slf4j;
@@ -35,65 +37,76 @@ public class ConnectionManager {
     public void init() throws MqttException {
         log.info("【初始化】设备型号 构建链接 协议");
         //先把所有型号查出来
-        List<TosDevicetypeResVo> iotFirmdevResVos = businessCoreService.selectTosDevicetypeResVoList();
-        for (TosDevicetypeResVo tosDeviceType : iotFirmdevResVos) {
-            log.info("【初始化】【开始】协议加载,厂家:{},类型:{} ,配置:{}", tosDeviceType.getFirmName(), tosDeviceType.getDevtypeBid(), tosDeviceType.getDevTypeConfig());
-            //将配置信息转换成jsonObject
-            JSONObject jsonConfig = parseConfigJson(tosDeviceType.getDevTypeConfig());
+        List<IotDeviceconnResVo> iotDeviceConnResVoList = businessCoreService.selectTosDevicetypeResVoList();
+        for (IotDeviceconnResVo iotDeviceconnResVo : iotDeviceConnResVoList) {
+            log.info("【初始化】【开始】协议加载,厂家:{},类型:{} ,配置:{}", iotDeviceconnResVo.getFirmName(), iotDeviceconnResVo.getDevtypeBid(), iotDeviceconnResVo.getDevconnConfig());
+            //将配置信息转换成jsonObject,这是个数组
+            JSONArray jsonConfig = parseConfigJson(iotDeviceconnResVo.getDevconnConfig());
 
             if (jsonConfig == null) {
-                log.info("【初始化】协议加载,厂家:{},失败:配置为空", tosDeviceType.getFirmName());
+                log.info("【初始化】协议加载,厂家:{},失败:配置为空", iotDeviceconnResVo.getFirmName());
                 continue;
             }
-
-            String type = jsonConfig.getString("type");
-            if (TextUtils.isEmpty(type)) {
-                log.info("【初始化】协议加载,厂家:{} type 为空:跳过", tosDeviceType.getFirmName());
-                continue;
+            // 遍历多个配置
+            for (int j = 0; j < jsonConfig.size(); j++) {
+                buildSingleMqttCoreByConfig(iotDeviceconnResVo, jsonConfig.getJSONObject(j));
             }
-            // 对核心构建方法catch,错了目前跳过
-            try {
-                switch (type) {
-                    case "mqtt":
-                        mqttManager.buildMqttConnection(tosDeviceType, jsonConfig);
-                        break;
-                    case "modbus-tcp": {
-                        //先不处理,对接到了再梳理
-                        break;
-                    }
-                    case "http": {
-                        httpManager.buildHttpConnection(tosDeviceType, jsonConfig);
-                        break;
-                    }
-                    default: {
-                        log.info("【初始化】其它类型:{},跳过", type);
-                        continue;
-                    }
-                }
-                log.info("【初始化】【完成】协议加载,厂家:{},类型:{} ,", tosDeviceType.getFirmName(), tosDeviceType.getDevtypeBid());
-            } catch (Exception e) {
-                log.error("【构建{}失败】 异常信息: {}, {}", type, e.getMessage(), e);
-            }
-
 
         }
 
     }
 
     /**
+     * 基于单个配置json创建连接,原子类方法,不可拆分
+     *
+     * @param iotDeviceconnResVo
+     * @param jsonConfig
+     */
+    void buildSingleMqttCoreByConfig(IotDeviceconnResVo iotDeviceconnResVo, JSONObject jsonConfig) {
+        String type = jsonConfig.getString("type");
+        if (TextUtils.isEmpty(type)) {
+            log.info("【初始化】协议加载,厂家:{} type 为空:跳过", iotDeviceconnResVo.getFirmName());
+            return;
+        }
+        // 对核心构建方法catch,错了目前跳过
+        try {
+            switch (type) {
+                case "mqtt":
+                    mqttManager.buildMqttConnection(iotDeviceconnResVo, jsonConfig);
+                    break;
+                case "modbus-tcp": {
+                    //先不处理,对接到了再梳理
+                    break;
+                }
+                case "http": {
+                    httpManager.buildHttpConnection(iotDeviceconnResVo, jsonConfig);
+                    break;
+                }
+                default: {
+                    log.info("【初始化】其它类型:{},跳过", type);
+                    return;
+                }
+            }
+            log.info("【初始化】【完成】协议加载,厂家:{},类型:{} ,", iotDeviceconnResVo.getFirmName(), iotDeviceconnResVo.getDevtypeBid());
+        } catch (Exception e) {
+            log.error("【构建{}失败】 异常信息: {}, {}", type, e.getMessage(), e);
+        }
+    }
+
+    /**
      * json的解析,后续定位为参数公共校验等
      *
      * @param config
      * @return
      */
-    private JSONObject parseConfigJson(String config) {
+    private JSONArray parseConfigJson(String config) {
         if (StringUtils.isEmpty(config)) {
             log.error("【初始化】协议加载, 对接配置 empty, {}", config);
             return null;
         }
-        JSONObject jsonConfig = null;
+        JSONArray jsonConfig = null;
         try {
-            jsonConfig = JSONObject.parseObject(config);
+            jsonConfig = JSONArray.from(config);
         } catch (Exception e) {
             log.error("【初始化】解析配置文件错误: \n" + config + "\n" + e);
         }

+ 15 - 14
src/main/java/com/yunfeiyun/agmp/iots/core/manager/HttpManager.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.yunfeiyun.agmp.common.utils.JSONUtils;
 import com.yunfeiyun.agmp.iot.common.domain.resvo.IotFirmdevResVo;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
 import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
 import com.yunfeiyun.agmp.iots.core.http.*;
 import com.yunfeiyun.agmp.iots.device.service.XphHttpDevice;
@@ -38,7 +39,7 @@ public class HttpManager {
      * 初始化
      * 注意:原来的getFirmDevBid(原来的配置id)暂时换成了getDevtypeBid,真正实现时候看是否正确
      */
-    public void buildHttpConnection(TosDevicetypeResVo tosDeviceType, JSONObject jsonConfig) {
+    public void buildHttpConnection(IotDeviceconnResVo iotDeviceconnResVo, JSONObject jsonConfig) {
 
         log.info("【http初始化】加载配置{}", jsonConfig);
 
@@ -46,7 +47,7 @@ public class HttpManager {
 
         HttpConfig httpConfig = null;
         try {
-            httpConfig = JSONUtils.toObject(tosDeviceType.getDevTypeConfig(), HttpConfig.class);
+            httpConfig = JSONUtils.toObject(iotDeviceconnResVo.getDevconnConfig(), HttpConfig.class);
         } catch (Exception e) {
             log.error("【http初始化】异常", e);
             return;
@@ -58,7 +59,7 @@ public class HttpManager {
             return;
         }
         String service = httpConfig.getService();
-        String firmBid = tosDeviceType.getFirmBid();
+        String firmBid = iotDeviceconnResVo.getFirmBid();
         if (ServiceNameConst.SERVICE_XMZN_ZNDP.equals(httpConfig.getService())) {
 
             if (hmClientByServiceName.get(ServiceNameConst.SERVICE_XMZN_ZNDP) != null) {
@@ -67,18 +68,18 @@ public class HttpManager {
             }
 
             XmznHttpClient xmznHttpClient = new XmznHttpClient();
-            hmClients.put(tosDeviceType.getDevtypeBid(), xmznHttpClient);
+            hmClients.put(iotDeviceconnResVo.getDevtypeBid(), xmznHttpClient);
 
-            xmznHttpClient.init(tosDeviceType.getFirmBid(), httpConfig, tosDeviceType.getDevtypeBid());
+            xmznHttpClient.init(iotDeviceconnResVo.getFirmBid(), httpConfig, iotDeviceconnResVo.getDevtypeBid());
 
             hmClientByServiceName.put(ServiceNameConst.SERVICE_XMZN_ZNDP, xmznHttpClient);
             //hmClientByServiceName.put(httpConfig.getServiceName(), xmznHttpClient);
             log.info("【小马智农】http-client初始化结束");
         } else if (ServiceNameConst.SERVICE_ZHAO_HE_SF.equals(httpConfig.getService())) {
             ZhaoHeSfHttpClient zhaoHeSfHttpClient = new ZhaoHeSfHttpClient();
-            zhaoHeSfHttpClient.init(tosDeviceType.getFirmBid(), httpConfig, tosDeviceType.getDevtypeBid());
+            zhaoHeSfHttpClient.init(iotDeviceconnResVo.getFirmBid(), httpConfig, iotDeviceconnResVo.getDevtypeBid());
 
-            hmClients.put(tosDeviceType.getDevtypeBid(), zhaoHeSfHttpClient);
+            hmClients.put(iotDeviceconnResVo.getDevtypeBid(), zhaoHeSfHttpClient);
 
             hmClientByServiceName.put(ServiceNameConst.SERVICE_ZHAO_HE_SF, zhaoHeSfHttpClient);
             log.info("【兆赫水肥机】http-client初始化结束");
@@ -87,15 +88,15 @@ public class HttpManager {
 
             AdznHttpConfig adznHttpConfig = null;
             try {
-                adznHttpConfig = JSONUtils.toObject(tosDeviceType.getDevTypeConfig(), AdznHttpConfig.class);
+                adznHttpConfig = JSONUtils.toObject(iotDeviceconnResVo.getDevconnConfig(), AdznHttpConfig.class);
             } catch (Exception e) {
                 log.error("【http初始化】异常", e);
                 return;
             }
 
-            adznHttpClient.init(tosDeviceType.getFirmBid(), adznHttpConfig, tosDeviceType.getDevtypeBid());
+            adznHttpClient.init(iotDeviceconnResVo.getFirmBid(), adznHttpConfig, iotDeviceconnResVo.getDevtypeBid());
 
-            hmClients.put(tosDeviceType.getDevtypeBid(), adznHttpClient);
+            hmClients.put(iotDeviceconnResVo.getDevtypeBid(), adznHttpClient);
 
             hmClientByServiceName.put(ServiceNameConst.SERVICE_ADZN_GSSQ, adznHttpClient);
             log.info("【爱迪智农】http-client初始化结束");
@@ -105,7 +106,7 @@ public class HttpManager {
                 try {
                     XphHttpClient xphHttpClient = new XphHttpClient();
                     xphHttpClient.init(firmBid, httpConfig, null);
-                    hmClients.put(tosDeviceType.getDevtypeBid(), xphHttpClient);
+                    hmClients.put(iotDeviceconnResVo.getDevtypeBid(), xphHttpClient);
                     hmClientByServiceName.put(ServiceNameConst.SERVICE_XPH_HTTP, xphHttpClient);
 
                     xphHttpDevice.initElementInfo();
@@ -123,7 +124,7 @@ public class HttpManager {
                 try {
                     XphHttpClient xphHttpClient = new XphHttpClient();
                     xphHttpClient.init(firmBid, httpConfig, null);
-                    hmClients.put(tosDeviceType.getDevtypeBid(), xphHttpClient);
+                    hmClients.put(iotDeviceconnResVo.getDevtypeBid(), xphHttpClient);
                     hmClientByServiceName.put(ServiceNameConst.SERVICE_XPH_GP_QXZ, xphHttpClient);
 
                     xphHttpGpQxzDevice.initElementInfo();
@@ -137,8 +138,8 @@ public class HttpManager {
             }
         } else if (ServiceNameConst.SERVICE_ZJSF_XYCB.equals(service)) {
             ZjsfXycbHttpClient zjsfXycbHttpClient = new ZjsfXycbHttpClient();
-            zjsfXycbHttpClient.init(tosDeviceType.getFirmBid(), httpConfig, tosDeviceType.getDevtypeBid());
-            hmClients.put(tosDeviceType.getDevtypeBid(), zjsfXycbHttpClient);
+            zjsfXycbHttpClient.init(iotDeviceconnResVo.getFirmBid(), httpConfig, iotDeviceconnResVo.getDevtypeBid());
+            hmClients.put(iotDeviceconnResVo.getDevtypeBid(), zjsfXycbHttpClient);
             hmClientByServiceName.put(ServiceNameConst.SERVICE_ZJSF_XYCB, zjsfXycbHttpClient);
             log.info("【中捷四方性诱测报】http-client初始化结束");
         } else {

+ 20 - 19
src/main/java/com/yunfeiyun/agmp/iots/core/manager/MqttManager.java

@@ -7,6 +7,7 @@ import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.enums.DevType;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
 import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
 import com.yunfeiyun.agmp.iots.core.mqtt.DeviceTopicService;
 import com.yunfeiyun.agmp.iots.core.mqtt.network.MqttConfig;
@@ -73,15 +74,15 @@ public class MqttManager {
      * 根据配置构建 MqttCoreConnection
      * 创建连接,底层统一调这个
      *
-     * @param tosDeviceType
+     * @param iotDeviceconnResVo
      * @param jsonConfig
      * @throws MqttException
      */
-    public void buildMqttConnection(TosDevicetypeResVo tosDeviceType, JSONObject jsonConfig) throws MqttException {
-        log.info("【开始构建MQTT连接】 tosDeviceTypeName: {}, tosDeviceTypeId:{}, jsonConfig: {}", tosDeviceType.getDevclassName(), tosDeviceType.getDevtypeBid(), jsonConfig);
+    public void buildMqttConnection(IotDeviceconnResVo iotDeviceconnResVo, JSONObject jsonConfig) throws MqttException {
+        log.info("【开始构建MQTT连接】 tosDeviceTypeName: {}, tosDeviceTypeId:{}, jsonConfig: {}", iotDeviceconnResVo.getDevclassName(), iotDeviceconnResVo.getDevtypeBid(), jsonConfig);
 
         // 构建配置
-        MqttConfig cfgYf = buildMqttConfig(tosDeviceType, jsonConfig);
+        MqttConfig cfgYf = buildMqttConfig(iotDeviceconnResVo, jsonConfig);
         // 厂家id
         String firmBizId = cfgYf.getFirmBizId();
         //服务类Bean名称
@@ -162,13 +163,13 @@ public class MqttManager {
     /**
      * 将json配置文件解析,构建配置
      *
-     * @param tosDeviceType
+     * @param iotDeviceconnResVo
      * @param jsonConfig
      * @return
      */
-    MqttConfig buildMqttConfig(TosDevicetypeResVo tosDeviceType, JSONObject jsonConfig) {
+    MqttConfig buildMqttConfig(IotDeviceconnResVo iotDeviceconnResVo, JSONObject jsonConfig) {
         MqttConfig cfgYf = new MqttConfig();
-        String firmBizId = tosDeviceType.getFirmBid();
+        String firmBizId = iotDeviceconnResVo.getFirmBid();
         String serviceName = jsonConfig.getString("service");
         String type = jsonConfig.getString("type");
 
@@ -176,9 +177,9 @@ public class MqttManager {
         cfgYf.setFirmBizId(firmBizId);
         cfgYf.setType(type);
         //其他信息
-        cfgYf.setFirmName(tosDeviceType.getFirmName());
-        cfgYf.setDeviceType(DevType.valueOfCode(tosDeviceType.getDevtypeBid()));
-        cfgYf.setDeviceTypeBizId(tosDeviceType.getDevtypeBid());
+        cfgYf.setFirmName(iotDeviceconnResVo.getFirmName());
+        cfgYf.setDeviceType(DevType.valueOfCode(iotDeviceconnResVo.getDevtypeBid()));
+        cfgYf.setDeviceTypeBizId(iotDeviceconnResVo.getDevtypeBid());
 
         cfgYf.setIp(jsonConfig.getString("ip"));
         cfgYf.setPort(jsonConfig.getString("port"));
@@ -484,30 +485,30 @@ public class MqttManager {
         throwDeprecatedMethod("重新订阅逻辑不完善,需要梳理构建新的逻辑");
 //        IotFirmdev iotFirmdev = new IotFirmdev();
 //        List<IotFirmdevResVo> iotFirmdevResVos = iotFirmdevService.selectIotFirmdevList(iotFirmdev);
-//        for (IotFirmdevResVo tosDeviceType : iotFirmdevResVos) {
+//        for (IotFirmdevResVo iotDeviceconnResVo : iotFirmdevResVos) {
 //            try {
-//                JSONObject jsonConfig = JSONObject.parseObject(tosDeviceType.getFirmdevCfg());
+//                JSONObject jsonConfig = JSONObject.parseObject(iotDeviceconnResVo.getFirmdevCfg());
 //                String type = jsonConfig.getString("type");
 //                if (!Objects.equals(type, "mqtt")) {
 //                    continue;
 //                }
-//                startSubscribe(tosDeviceType, jsonConfig);
+//                startSubscribe(iotDeviceconnResVo, jsonConfig);
 //            } catch (Exception e) {
-//                log.error("【设备重新订阅】【订阅】【重连】 解析配置文件错误: \n" + tosDeviceType.getFirmdevCfg() + "\n" + e);
+//                log.error("【设备重新订阅】【订阅】【重连】 解析配置文件错误: \n" + iotDeviceconnResVo.getFirmdevCfg() + "\n" + e);
 //            }
 //        }
     }
 
     /**
-     * @param tosDeviceType
+     * @param iotDeviceconnResVo
      * @param jsonConfig
      * @deprecated 逻辑需要基于connectionId梳理
      * 两个小时内没有产生更新便认为是设备订阅失效,重新订阅
      */
     @Deprecated
-    private void startSubscribe(TosDevicetypeResVo tosDeviceType, JSONObject jsonConfig) {
-        log.info("【设备重新订阅】【订阅】【重连】:设备:{} {}", tosDeviceType, jsonConfig);
-        MqttConfig cfgYf = buildMqttConfig(tosDeviceType, jsonConfig);
+    private void startSubscribe(IotDeviceconnResVo iotDeviceconnResVo, JSONObject jsonConfig) {
+        log.info("【设备重新订阅】【订阅】【重连】:设备:{} {}", iotDeviceconnResVo, jsonConfig);
+        MqttConfig cfgYf = buildMqttConfig(iotDeviceconnResVo, jsonConfig);
         String firmBizId = cfgYf.getFirmBizId();
         String serviceName = cfgYf.getServiceName();
         String deviceTypeId = cfgYf.getDeviceTypeBizId();
@@ -537,7 +538,7 @@ public class MqttManager {
         try {
             MqttClient mqttClient = mqttCore.getClient();
             if (!mqttClient.isConnected()) {
-                buildMqttConnection(tosDeviceType, jsonConfig);
+                buildMqttConnection(iotDeviceconnResVo, jsonConfig);
             } else {
                 mqttCore.unsubscribe(topics);
                 mqttCore.subscribe(topics);

+ 2 - 1
src/main/java/com/yunfeiyun/agmp/iots/device/mapper/BusinessCoreMapper.java

@@ -1,5 +1,6 @@
 package com.yunfeiyun.agmp.iots.device.mapper;
 
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
 import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
 
 import java.util.List;
@@ -10,5 +11,5 @@ public interface BusinessCoreMapper {
      *
      * @return
      */
-    List<TosDevicetypeResVo> selectTosDevicetypeResVoList();
+    List<IotDeviceconnResVo> selectTosDevicetypeResVoList();
 }

+ 2 - 1
src/main/java/com/yunfeiyun/agmp/iots/service/BusinessCoreService.java

@@ -1,5 +1,6 @@
 package com.yunfeiyun.agmp.iots.service;
 
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
 import com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo;
 import com.yunfeiyun.agmp.iots.device.mapper.BusinessCoreMapper;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,7 +22,7 @@ public class BusinessCoreService {
      *
      * @return
      */
-    public List<TosDevicetypeResVo> selectTosDevicetypeResVoList() {
+    public List<IotDeviceconnResVo> selectTosDevicetypeResVoList() {
 
         return businessCoreMapper.selectTosDevicetypeResVoList();
     }

+ 5 - 21
src/main/resources/mapper/BusinessCoreMapper.xml

@@ -7,28 +7,12 @@
 
     <!-- * 这个方法的定位就是:获取设备型号的信息,用于构建链接 *-->
     <select id="selectTosDevicetypeResVoList"
-            resultType="com.yunfeiyun.agmp.iots.common.modal.TosDevicetypeResVo">
+            resultType="com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo">
         SELECT
-        tdt.id,
-        tdt.devtypeBid,
-        tdt.devclassBid,
-        tdt.devtypeName,
-        tdt.devtypeCode,
-        tdt.firmBid,
-        tdt.devtypePreview,
-        tdt.devtypeRemark,
-        tdt.devTypeConfig,
-        tdt.devtypeCreator,
-        tdt.devtypeModifier,
-        tdt.devtypeModifieddate,
-        tdt.devtypeCreateddate,
-        tdc.devclassName,
-        tf.firmName,
-        tf.firmShortname
-        FROM
-        TosDevicetype tdt
-        LEFT JOIN TosDeviceclass tdc on tdc.devclassBid = tdt.devclassBid
-        LEFT JOIN TosFirm tf on tf.firmBid = tdt.firmBid
+        *
+        from
+        IotDeviceconn
+
     </select>