From 7e2a837e26aafca3d49d35f9704442659da8654c Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 02 十一月 2023 17:05:57 +0800
Subject: [PATCH] 硬件协议对接

---
 server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java |  138 ++++++++++++++++++++++++++++++++-------------
 1 files changed, 98 insertions(+), 40 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java b/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java
index 8a8f01e..0b483c2 100644
--- a/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java
+++ b/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java
@@ -6,11 +6,10 @@
 import com.doumee.core.exception.BusinessException;
 import com.doumee.core.mqtt.config.MqttConfig;
 import com.doumee.core.mqtt.service.MqttToolService;
+import com.doumee.dao.business.MemberRidesMapper;
 import com.doumee.dao.business.MqttLogMapper;
-import com.doumee.dao.business.model.Bikes;
-import com.doumee.dao.business.model.Locks;
-import com.doumee.dao.business.model.MemberRides;
-import com.doumee.dao.business.model.MqttLog;
+import com.doumee.dao.business.SitesMapper;
+import com.doumee.dao.business.model.*;
 import com.doumee.service.business.*;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -32,6 +31,9 @@
     @Autowired
     @Lazy
     MemberRidesService memberRidesService;
+    @Lazy
+    @Autowired
+    SitesMapper sitesMapper;
     @Autowired
     private MqttLogMapper mqttLogMapper;
     @Autowired
@@ -41,24 +43,21 @@
        log.info("mqtt娑堟伅璁㈤槄==================="+param);
         String info = Constants.MqttTopic.sub_lockInfo.substring(Constants.MqttTopic.sub_lockInfo.lastIndexOf("/")+1) ;
         String closeLock = Constants.MqttTopic.sub_closeLock.substring(Constants.MqttTopic.sub_closeLock.lastIndexOf("/")+1) ;
-        if(topic.indexOf(Constants.MqttTopic.topic_index)!=0
-                ||topic.split("/").length < 5
-                || (!StringUtils.contains(topic, info)
-                   &&!StringUtils.contains(topic,closeLock))){
-            log.error("mqtt娑堟伅璁㈤槄===========鏃犳晥鏁版嵁========"+param);
-            return;
-        }
-
-        String[] ss = topic.split("/");
-        String siteid =  ss[2];//绔欑偣缂栫爜
-        String lockid =  ss[3];//閿佸ご缂栫爜
-        if(StringUtils.isBlank(siteid)||StringUtils.isBlank(lockid)){
-            //濡傛灉閿佸ご缂栫爜涓虹┖
-            log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+param);
-            return;
+        String health = Constants.MqttTopic.sub_health.substring(Constants.MqttTopic.sub_health.lastIndexOf("/")+1) ;
+        String brokers = "brokers";
+        if(!StringUtils.contains(topic, brokers)){
+            if(topic.indexOf(Constants.MqttTopic.topic_index)!=0
+                    ||topic.split("/").length < 4
+                    || (!StringUtils.contains(topic, info)
+                    &&!StringUtils.contains(topic, health)
+                    &&!StringUtils.contains(topic,closeLock))){
+                log.error("mqtt娑堟伅璁㈤槄===========鏃犳晥鏁版嵁========"+param);
+                return;
+            }
         }
         MqttLog mqttLog = new MqttLog();
         mqttLog.setMsgId(msgId);
+        mqttLog.setTopic(topic);
         int msgCount  =mqttLogMapper.selectCount(new QueryWrapper<MqttLog>().lambda().eq(MqttLog::getMsg, param).eq(MqttLog::getType, Constants.ZERO));
         if(msgCount>0){
             log.error("mqtt娑堟伅璁㈤槄==============宸叉秷璐规暟鎹�====="+param);
@@ -68,27 +67,86 @@
         String logInfo = "";
         int result =0;
         try {
-            if(StringUtils.contains(topic, info)){
-                //濡傛灉閿佸ご淇℃伅涓婃姤
-                Locks locks  = JSONObject.parseObject(param, Locks.class);
-                locks.setSiteId(siteid);
-                locks.setCode(lockid);
-                locks.setInfo(logId);
-                result = memberRidesService.mqttLockInfoEvent(locks);
-                logInfo = "mqtt娑堟伅璁㈤槄閿佸ご淇℃伅";
-                log.info("mqtt娑堟伅璁㈤槄=========閿佷俊鎭�==========鎴愬姛");
-            }
-            if(StringUtils.contains(topic, closeLock)){
-                //濡傛灉杩樿溅涓婃姤
-                JSONObject pjson  = JSONObject.parseObject(param);
-                MemberRides bikes = new MemberRides();
-                bikes.setBikeCode(pjson.getString("bikeCode"));
-                bikes.setBackLockId( lockid);
-                bikes.setBackSiteId( siteid);
-                bikes.setBackCommondId(logId);
-                result = memberRidesService.mqttCloseBikeEvent(bikes);
-                logInfo = "mqtt娑堟伅璁㈤槄杩樿溅娑堟伅";
-                log.info("mqtt娑堟伅璁㈤槄=========杩樿溅==========鎴愬姛");
+            if(!topic.contains(brokers)){
+                String[] ss = topic.split("/");
+                String siteid =  ss[2];//绔欑偣缂栫爜
+                if(StringUtils.isBlank(siteid) ){
+                    //濡傛灉閿佸ご缂栫爜涓虹┖
+                    log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param);
+                    return;
+                }
+                if(StringUtils.contains(topic, info)){
+                    //濡傛灉閿佸ご淇℃伅涓婃姤
+                    String lockid =  ss[3];//閿佸ご缂栫爜
+                    if( StringUtils.isBlank(lockid)){
+                        //濡傛灉閿佸ご缂栫爜涓虹┖
+                        log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param);
+                        return;
+                    }
+                    Locks locks  = JSONObject.parseObject(param, Locks.class);
+                    locks.setSiteId(siteid);
+                    locks.setCode(lockid);
+                    locks.setInfo(logId);
+                    result = memberRidesService.mqttLockInfoEvent(locks);
+                    logInfo = "mqtt娑堟伅璁㈤槄閿佸ご淇℃伅";
+                    log.info("mqtt娑堟伅璁㈤槄=========閿佷俊鎭�==========鎴愬姛");
+                }else if(StringUtils.contains(topic, closeLock)){
+                    //濡傛灉杩樿溅涓婃姤
+                    String lockid =  ss[3];//閿佸ご缂栫爜
+                    if( StringUtils.isBlank(lockid)){
+                        //濡傛灉閿佸ご缂栫爜涓虹┖
+                        log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param);
+                        return;
+                    }
+                    JSONObject pjson  = JSONObject.parseObject(param);
+                    MemberRides bikes = new MemberRides();
+                    bikes.setBikeCode(pjson.getString("bikeCode"));
+                    bikes.setBackLockId( lockid);
+                    bikes.setBackSiteId( siteid);
+                    bikes.setBackCommondId(logId);
+                    result = memberRidesService.mqttCloseBikeEvent(bikes);
+                    logInfo = "mqtt娑堟伅璁㈤槄杩樿溅娑堟伅";
+                    log.info("mqtt娑堟伅璁㈤槄=========杩樿溅==========鎴愬姛");
+                }else if(StringUtils.contains(topic, health)){
+                    //蹇冭烦娑堟伅=
+                    Sites site = new Sites();
+                    site.setId(siteid);
+                    site.setOnline(Constants.ZERO);
+                    site.setLastLinkDate(new Date());
+                    sitesMapper.updateById(site);//鏇存柊绔欑偣鐘舵��
+                    logInfo = "mqtt娑堟伅璁㈤槄蹇冭烦娑堟伅";
+                    log.info("mqtt娑堟伅璁㈤槄=========蹇冭烦娑堟伅==========鎴愬姛");
+
+                }
+            }else {
+                //濡傛灉绔欑偣涓婁笅绾挎秷鎭�
+                JSONObject pjson = JSONObject.parseObject(param);
+                String clientId = String.valueOf(pjson.get("clientid"));
+                if(clientId.contains("doumee")){
+                    log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥璁㈤槄鐘舵��====="+topic+param);
+                    return;
+                }
+                Sites site = sitesMapper.selectById(clientId);
+                if(site == null){
+                    log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥璁㈤槄鐘舵��====="+topic+param);
+                    return;
+                }
+                if (topic.endsWith("disconnected")) {
+                    if(Constants.formatIntegerNum(site.getOnline()) == Constants.ONE){
+                        log.error("mqtt娑堟伅璁㈤槄==============宸茶闃呯姸鎬佹帀绾�====="+param);
+                        return;
+                    }
+                    site.setOnline(Constants.ONE);
+                } else {
+                    if(Constants.formatIntegerNum(site.getOnline()) == Constants.ZERO){
+                        log.error("mqtt娑堟伅璁㈤槄==============宸茶闃呯姸鎬佷笂绾�====="+param);
+                        return;
+                    }
+                    site.setOnline(Constants.ZERO);
+                }
+                site.setLastLinkDate(new Date());
+                sitesMapper.updateById(site);//鏇存柊绔欑偣鐘舵��
+                log.info("mqtt娑堟伅璁㈤槄=========绔欑偣涓婁笅绾�==========鎴愬姛");
             }
         }catch (BusinessException e){
             e.printStackTrace();

--
Gitblit v1.9.3