From bef9c91484bc811aba53fa2cca6dab5a2434e446 Mon Sep 17 00:00:00 2001
From: bsw215583320 <baoshiwei121@163.com>
Date: 星期五, 08 十一月 2024 08:35:37 +0800
Subject: [PATCH] 增加异常处理

---
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java |   93 ++++++++++++++++++++++++++--------------------
 1 files changed, 52 insertions(+), 41 deletions(-)

diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
index cb5861d..94392d4 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
@@ -97,9 +97,14 @@
 
         }
         if (topic.startsWith("$SYS/brokers/") && topic.endsWith("disconnected")) {
-          String clientid = messageJson.getString("clientid");
-          redisUtil.del(MqttConstant.MQTT_ONLINE_CLIENT + clientid);
-          System.err.println(String.format("璁惧: %s涓嬬嚎", clientid));
+          try {
+            String clientid = messageJson.getString("clientid");
+            redisUtil.del(MqttConstant.MQTT_ONLINE_CLIENT + clientid);
+            System.err.println(String.format("璁惧: %s涓嬬嚎", clientid));
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
+
         }
         parseAdminCommand(topic, mqttMessage);
 
@@ -183,34 +188,36 @@
 
         // 鎺ユ敹璁惧瀹炴椂鏁版嵁
       case MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA:
-        try {
-          RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class);
-          realTimeDataService.realTimeDataHandle(vo);
-        } catch (Exception e) {
-          e.printStackTrace();
-        }
+        ThreadUtil.execute(() -> {
+          try {
+            RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class);
+            realTimeDataService.realTimeDataHandle(vo);
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
+        });
 
         break;
       case MqttConstant.TENANT_UP_PREFIX_EQU:
-        try {
-          DryEquipment equipment = (DryEquipment) messageJson.get("equipment");
+        ThreadUtil.execute(() -> {
+          try {
+            DryEquipment equipment = (DryEquipment) messageJson.get("equipment");
 
-          DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode());
+            DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode());
+            if (dryEquipment == null) {
+              equipmentService.save(equipment);
+            }
 
-          if (dryEquipment == null) {
-            equipmentService.save(equipment);
+            // 鑾峰彇璁惧鎵�灞炶溅闂�
+            DryShop shop = (DryShop) messageJson.get("shop");
+            shop.setTenantId(equipment.getTenantId());
+            dryShopService.save(shop);
+
+          } catch (Exception e) {
+            e.printStackTrace();
           }
+        });
 
-
-
-          // 鑾峰彇璁惧鎵�灞炶溅闂�
-          DryShop shop = (DryShop) messageJson.get("shop");
-          shop.setTenantId(equipment.getTenantId());
-          dryShopService.save(shop);
-
-        } catch (Exception e) {
-          e.printStackTrace();
-        }
     }
 
   }
@@ -254,28 +261,32 @@
 
 
         break;
-      case MqttConstant.SERVICE_REQ_EQU_TOPIC:
+      case MqttConstant.SERVICE_REQ_PREFIX:
         log.debug("鏀跺埌璁惧璇︾粏淇℃伅鏌ヨ璇锋眰");
-        // 鏍规嵁璁惧缂栫爜鏌ヨ璁惧淇℃伅
-        String code = messageJson.getString("code");
-        DryEquipment equipmentVo = equipmentService.selectByTenantIdEquipmentId(TenantContext.getTenant(),code);
-        // 鏍规嵁璁惧杞﹂棿id鏌ヨ杞﹂棿淇℃伅
-        DryShop shop = dryShopService.getById(equipmentVo.getShopId());
+        String tenantId = messageJson.getString("tenantId");
+        if (tenantId!=null && tenantId.equals(TenantContext.getTenant())) {
+          // 鏍规嵁璁惧缂栫爜鏌ヨ璁惧淇℃伅
+          String code = messageJson.getString("code");
+          DryEquipment equipmentVo = equipmentService.selectByTenantIdEquipmentId(tenantId,code);
+          // 鏍规嵁璁惧杞﹂棿id鏌ヨ杞﹂棿淇℃伅
+          DryShop shop = dryShopService.getById(equipmentVo.getShopId());
 
-        JSONObject res = new JSONObject();
+          JSONObject res = new JSONObject();
 
-        res.put("tenant", TenantContext.getTenant());
-        res.put("equipment", equipmentVo);
-        res.put("shop", shop);
-        try {
-          MqttMessage sendMessage = new MqttMessage(res.toJSONString().getBytes());
-          sendMessage.setQos(0);
-          mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_EQU, sendMessage);
-          // baseCommonService.addLog(res.toString(), CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_2);
-        } catch (Exception e) {
-          e.printStackTrace();
+          res.put("tenant", TenantContext.getTenant());
+          res.put("equipment", equipmentVo);
+          res.put("shop", shop);
+          try {
+            MqttMessage sendMessage = new MqttMessage(res.toJSONString().getBytes());
+            sendMessage.setQos(0);
+            mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_EQU, sendMessage);
+            // baseCommonService.addLog(res.toString(), CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_2);
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
         }
 
+
         break;
 
     }

--
Gitblit v1.9.3