From ab4fde3dd12361b4092fd52060a66017a060236b Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期四, 29 六月 2023 15:34:20 +0800
Subject: [PATCH] Merge branch 'hxb' into wjc

---
 HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java |   98 +++++++++++++++++++++++-------------------------
 1 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java
index e38c3ad..241fee5 100644
--- a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java
@@ -11,9 +11,9 @@
 import com.hdl.sdk.link.common.utils.LogUtils;
 import com.hdl.sdk.link.core.bean.LinkRequest;
 import com.hdl.sdk.link.core.bean.LinkResponse;
+import com.hdl.sdk.link.core.bean.ModbusResponse;
 import com.hdl.sdk.link.core.bean.ZigbeeResponse;
 import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
-import com.hdl.sdk.link.core.callback.BaseCallBack;
 import com.hdl.sdk.link.core.callback.ModbusCallBack;
 import com.hdl.sdk.link.core.callback.ZigbeeCallBack;
 import com.hdl.sdk.link.core.config.HDLLinkConfig;
@@ -28,9 +28,9 @@
     private static final String TAG="HDLModbusConnect";
     private static HDLModBusConnect instance;
     /**
-     * 鍐呴儴鐢紝涓昏鏄鐞嗗鐞嗘帀閫忎紶涓婚鍙妉ink涓婚鍚庯紝杩樺師Zigbee鍘熺敓鏁版嵁鍙婁富棰樼敤
+     * 鍐呴儴鐢紝涓昏鏄鐞嗗鐞嗘帀閫忎紶涓婚鍙妉ink涓婚鍚庯紝杩樺師modbus鍘熺敓鏁版嵁鍙婁富棰樼敤
      */
-    private final String zigbeeAllTopic = "/Modbus";
+    private final String allTopic = "/Modbus";
 
     /**
      * 杩斿洖褰撳墠瀹炰緥锛屼笉瀛樺湪灏卞垱寤哄苟鍚屾椂娉ㄥ唽鐩戝惉浜嬩欢
@@ -54,7 +54,7 @@
         if(null==eventListener){
             return;
         }
-        EventDispatcher.getInstance().register(zigbeeAllTopic, eventListener);
+        EventDispatcher.getInstance().register(allTopic, eventListener);
     }
 
     /**
@@ -66,7 +66,7 @@
         if(null==eventListener){
             return;
         }
-        EventDispatcher.getInstance().remove(zigbeeAllTopic, eventListener);
+        EventDispatcher.getInstance().remove(allTopic, eventListener);
     }
 
     /**
@@ -76,44 +76,39 @@
         final EventListener eventListener = new EventListener() {
             @Override
             public void onMessage(Object msg) {
-//                try {
-//                    if (msg instanceof LinkResponse) {
-//                        LinkResponse linkResponse = (LinkResponse) msg;
-//                        String body = linkResponse.getData();
-//                        int index = body.indexOf("{");
-//                        //zigbee鍥炲鐨勬暟鎹墠鏈変富棰橈紝鍚庨潰鎵嶆槸鏁版嵁
-//                        if (index <= 0)
-//                            return;
-//
-//                        String zigbeeTopic = body.substring(0, index).trim();
-//                        //zigbee鐨勮礋杞芥暟鎹�
-//                        String bodyData = body.substring(index);
-//
-//                        ZigbeeResponse zigbeeResponse = new ZigbeeResponse();
-//                        zigbeeResponse.setTopic(zigbeeTopic);
-//                        zigbeeResponse.setData(bodyData);
-//                        String oid = null;
-//                        //鏄惁鏄�氳繃涓荤綉鍏抽�忎紶涓婚
-//                        if (linkResponse.getTopic().contains("/slaveoid/")) {
-//                            oid = linkResponse.getTopic().split("/")[8];
-//                        } else {
-//                            oid = linkResponse.getTopic().split("/")[2];
-//                        }
-//                        zigbeeResponse.setOid(oid);
-//                        for (GatewayBean gatewayBean : HDLLinkLocalGateway.getInstance().getGatewayList()) {
-//                            if (oid.equals(gatewayBean.getGatewayId()) || oid.equals(gatewayBean.getDevice_mac()) || oid.equals(gatewayBean.getOid())) {
-//                                //涓婇潰鐨刼id鍙兘鏄綉鍏砳d鎴栬�卪ac鎴栬�呮槸oid锛屼笉绠℃槸鍝釜缁熶竴浣跨敤oid琛ㄧず鏂瑰紡
-//                                zigbeeResponse.setOid(gatewayBean.getOid());
-//                                break;
-//                            }
-//                        }
-//                        EventDispatcher.getInstance().post(zigbeeTopic, zigbeeResponse);
-//                        //鍙戝竷Zigbee鍘熺敓涓婚鍙婃暟鎹�
-//                        EventDispatcher.getInstance().post(zigbeeAllTopic, zigbeeResponse);
-//                    }
-//                } catch (Exception e) {
-//                    LogUtils.e(TAG,"LinkResponse杞琙igbeeResponse寮傚父:"+ e.getMessage());
-//                }
+                try {
+                    if (msg instanceof LinkResponse) {
+                        LinkResponse linkResponse = (LinkResponse) msg;
+                        if(linkResponse.getTopic()==null||!linkResponse.getTopic().contains("custom/native/inverter/"))
+                        {
+                            return;
+                        }
+                        byte []data = linkResponse.getByteData();
+                        ModbusResponse  modbusResponse= new ModbusResponse();
+                        modbusResponse.setTopic("Modbus"+data[0]+data[1]);
+                        modbusResponse.setData(data);
+                        String oid = null;
+                        //鏄惁鏄�氳繃涓荤綉鍏抽�忎紶涓婚
+                        if (linkResponse.getTopic().contains("/slaveoid/")) {
+                            oid = linkResponse.getTopic().split("/")[8];
+                        } else {
+                            oid = linkResponse.getTopic().split("/")[2];
+                        }
+                        modbusResponse.setOid(oid);
+                        for (GatewayBean gatewayBean : HDLLinkLocalGateway.getInstance().getGatewayList()) {
+                            if (oid.equals(gatewayBean.getGatewayId()) || oid.equals(gatewayBean.getDevice_mac()) || oid.equals(gatewayBean.getOid())) {
+                                //涓婇潰鐨刼id鍙兘鏄綉鍏砳d鎴栬�卪ac鎴栬�呮槸oid锛屼笉绠℃槸鍝釜缁熶竴浣跨敤oid琛ㄧず鏂瑰紡
+                                modbusResponse.setOid(gatewayBean.getOid());
+                                break;
+                            }
+                        }
+                        EventDispatcher.getInstance().post(allTopic, modbusResponse);
+                        //鍙戝竷Zigbee鍘熺敓涓婚鍙婃暟鎹�
+//                        EventDispatcher.getInstance().post(allTopic, zigbeeResponse);
+                    }
+                } catch (Exception e) {
+                    LogUtils.e(TAG,"LinkResponse杞琈odbusResponse寮傚父:"+ e.getMessage());
+                }
             }
         };
         //娉ㄥ唽鐩存帴閫氳鐨勪富棰橈紝鍖呮嫭鐩存帴鍜屼富缃戝叧閫氳鎴栬�呯洿鎺ュ拰浠庣綉鍏抽�氳
@@ -127,11 +122,10 @@
      * 鍙戦�佸師鐢熸暟鎹�
      *
      * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d
-     * @param responeTopic          鍥炲涓婚
      * @param payload               鍙戦�佹暟鎹�
      * @param baseCallBack        缁撴灉鍥炶皟
      */
-    public void Send(String gatewayOidOrGatewayId,String responeTopic, String payload, final ModbusCallBack baseCallBack) {
+    public void Send(String gatewayOidOrGatewayId, byte []payload, final ModbusCallBack baseCallBack) {
         //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴
         GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId);
         if (null == gatewayBean) {
@@ -150,17 +144,19 @@
         } else {
             request.setCloudTopic(String.format(TopicConstant.NATIVE_MODBUS_DOWN_SLAVE, HDLLinkConfig.getInstance().getGatewayId(), gatewayOidOrGatewayId));
         }
-        request.setReplyTopic(responeTopic);
+        request.setReplyTopic("Modbus"+payload[0]+payload[1]);
         long timeout = 2 * 1000;
 
         new HDLConnectHelper(timeout, 1, gatewayBean.getIp_address(), 8586, request, new HDLConnectHelper.HdlSocketListener() {
             @Override
             public void onSucceed(Object msg) {
-//                if (msg instanceof String) {
-//                    if (null != baseCallBack) {
-//                        baseCallBack.onSuccess(msg+"");
-//                    }
-//                }
+                if (msg instanceof Byte[]) {
+                    if (null != baseCallBack) {
+                        baseCallBack.onSuccess((byte[])msg);
+                    }
+                }else{
+                    LogUtils.e("鍙戦�丮odbus鍥炶皟瀵硅薄绫诲瀷闈炴暟缁勭被鍨嬶紝绫诲瀷鏄�"+msg.getClass());
+                }
             }
 
             @Override

--
Gitblit v1.8.0