From d71e3a495a125811889fdf7c2a2ec0650e28262d Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期四, 25 十二月 2025 17:41:21 +0800
Subject: [PATCH] 最新版本541200007最新版本541200007

---
 server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/mqtt/MqttPushCallback.java |  141 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/mqtt/MqttPushCallback.java b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/mqtt/MqttPushCallback.java
new file mode 100644
index 0000000..a4372cf
--- /dev/null
+++ b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/mqtt/MqttPushCallback.java
@@ -0,0 +1,141 @@
+package com.doumee.service.business.impl.mqtt;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.doumee.core.utils.Constants;
+import com.doumee.core.utils.DateUtil;
+import com.doumee.dao.business.DeviceDataMapper;
+import com.doumee.dao.business.DeviceMapper;
+import com.doumee.dao.business.model.Device;
+import com.doumee.dao.business.model.DeviceData;
+import com.doumee.mqtt.config.MqttClientInit;
+import com.doumee.mqtt.config.MqttConfig;
+import com.doumee.mqtt.service.MqttBizService;
+import com.doumee.service.business.DeviceService;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.util.DataUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.eclipse.paho.client.mqttv3.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author T14
+ */
+@Service
+@Slf4j
+public class MqttPushCallback implements MqttCallback {
+
+
+        @Autowired
+        private DeviceMapper deviceMapper;
+        @Autowired
+        private DeviceDataMapper deviceDataMapper;
+        @Override
+        public void connectionLost(Throwable cause) {
+            // 杩炴帴涓㈠け鍚庯紝涓�鑸湪杩欓噷闈㈣繘琛岄噸杩�
+           log.error("杩炴帴鏂紑锛岄噸杩炰腑");
+            MqttClientInit.refreshClient();
+        }
+
+        @Override
+        public void deliveryComplete(IMqttDeliveryToken token) {
+           log.error("deliveryComplete---------" + token.isComplete());
+        }
+        @Override
+        public void messageArrived(String topic, MqttMessage message) throws Exception {
+            // subscribe鍚庡緱鍒扮殑娑堟伅浼氭墽琛屽埌杩欓噷闈�
+            try {
+                Date date = new Date();
+               log.error("鎺ユ敹娑堟伅涓婚 : " + DateUtil.getPlusTime2(date) + topic);
+               log.error("鎺ユ敹娑堟伅Qos : " + DateUtil.getPlusTime2(date) +  message.getQos());
+               log.error("鎺ユ敹娑堟伅鍐呭 : " +  DateUtil.getPlusTime2(date) + new String(message.getPayload()));
+                //  ------------TODO-----------鐩戝惉鏁版嵁
+                //-----------------/dev/MTS/98CC4D121E5A/status/json
+                if(StringUtils.contains(topic,"/status/json")){
+                    dealDeviceDataT30A(topic,message);
+                }
+
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+        }
+
+    private void dealDeviceDataT30A(String topic, MqttMessage message) {
+        try {
+            Date date = new Date();
+            //濡傛灉鏄┖寮�涓婃姤鏁版嵁
+            String t[] = topic.replace("/dev/","").replace("/status/json","").split("/");
+            String no =t[0];
+            String doorNo = t[1];
+            Device device = deviceMapper.selectOne(new QueryWrapper<Device>().lambda()
+                    .eq(Device::getNo,no)
+                    .eq(Device::getIsdeleted,no)
+                    .eq(Device::getDoorNo,doorNo));
+            if(device!=null && Constants.equalsInteger(device.getIsUsed(),Constants.ZERO)){
+                //濡傛灉璁惧瀛樺湪骞朵笖璁惧鍦ㄧ敤锛岃褰曚笂鎶ユ暟鎹�
+                List<DeviceData> dataList = new ArrayList<>();
+                JSONObject jsob = JSONObject.parseObject(new String(message.getPayload()));
+                if(jsob.getJSONArray("data")!=null && jsob.getJSONArray("data").size()>0){
+                    for (int i = 0; i <  jsob.getJSONArray("data").size(); i++) {
+                        JSONObject jsonObject = jsob.getJSONArray("data").getJSONObject(i);
+                        DeviceData data = new DeviceData();
+                        data.setDataJson(jsob.toJSONString());
+                        data.setCreateDate(date);
+                        data.setEditDate(date);
+                        data.setVal2("0");//鐢垫祦鍊�
+                        data.setVal3("0");
+                        data.setVal4("0");
+                        data.setVal5("0");
+                        data.setVal6("0");
+                        data.setHappenTime(DateUtil.getPlusTime2(new Date(jsob.getLong("timeMS"))));
+                        //妯″潡搴忓彿
+                        if(jsonObject.getJSONObject("mcbComSta")!=null
+                                && jsonObject.getJSONObject("mcbComSta").getString("busAddr")!=null){
+                            data.setVal1(jsonObject.getJSONObject("mcbComSta").getString("busAddr"));
+                            //<0>琛ㄧず鍒嗛椄锛�<1>琛ㄧず鍚堥椄锛屾棤绗﹀彿
+                            data.setVal7(jsonObject.getJSONObject("mcbComSta").getString("on"));
+                        }
+                        if(jsonObject.getJSONObject("mcbSta")!=null){
+                            //鐢垫祦
+                            if(jsonObject.getJSONObject("mcbSta").getString("cur")!=null){
+                                data.setVal2(jsonObject.getJSONObject("mcbSta").getString("cur"));
+                            }
+                            //鐢靛帇
+                            if(jsonObject.getJSONObject("mcbSta").getString("vol")!=null){
+                                data.setVal3(jsonObject.getJSONObject("mcbSta").getString("vol"));
+                            }
+                            //娓╁害
+                            if(jsonObject.getJSONObject("mcbSta").getString("tmp")!=null){
+                                data.setVal4(jsonObject.getJSONObject("mcbSta").getString("tmp"));
+                            }
+                            //鏈夊姛鍔熺巼鍊硷紝鍗曚綅kW
+                            if(jsonObject.getJSONObject("mcbSta").getString("pwrP")!=null){
+                                data.setVal5(jsonObject.getJSONObject("mcbSta").getString("pwrP"));
+                            }
+                            //N鐩哥數娴佸�硷紝鍗曚綅A
+                            if(jsonObject.getJSONObject("mcbSta").getString("curN")!=null){
+                                data.setVal6(jsonObject.getJSONObject("mcbSta").getString("curN"));
+                            }
+                        }
+                        data.setDeviceId(device.getId().toString());
+                        data.setDataType(Constants.ZERO);
+                        if(StringUtils.isNotBlank(data.getVal1())){
+                            dataList.add(data );
+                        }
+                    }
+                }
+                if(dataList.size()>0){
+                    deviceDataMapper.insert(dataList);
+                }
+            }
+        }catch (Exception e){
+            log.error("===============topic鏁版嵁璁板綍澶辫触:"+topic+e.getMessage());
+        }
+    }
+}

--
Gitblit v1.9.3