From 90d5f028ccdaaaf64286f9d632cb335a4d0544b9 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期二, 07 一月 2025 09:40:52 +0800 Subject: [PATCH] Merge branch '1.5.1' --- sdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java | 225 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 225 insertions(+), 0 deletions(-) diff --git a/sdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java b/sdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java new file mode 100644 index 0000000..29c49cf --- /dev/null +++ b/sdk/src/main/java/com/hdl/sdk/link/core/connect/HDLModBusConnect.java @@ -0,0 +1,225 @@ +package com.hdl.sdk.link.core.connect; + + +import android.text.TextUtils; + +import com.hdl.link.error.HDLLinkCode; +import com.hdl.sdk.link.common.config.TopicConstant; +import com.hdl.sdk.link.common.event.EventDispatcher; +import com.hdl.sdk.link.common.event.EventListener; +import com.hdl.sdk.link.common.exception.HDLLinkException; +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.gateway.GatewayBean; +import com.hdl.sdk.link.core.callback.ModbusCallBack; +import com.hdl.sdk.link.core.config.HDLLinkConfig; +import com.hdl.sdk.link.core.utils.ByteUtils; +import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; + +/** + * Created by hxb on 2021/12/8. + * 鍘熺敓閫氳鐩稿叧鎺ュ彛 + */ +public class HDLModBusConnect { + + private static final String TAG = "HDLModbusConnect"; + private static HDLModBusConnect instance; + /** + * 鍐呴儴鐢紝涓昏鏄鐞嗗鐞嗘帀閫忎紶涓婚鍙妉ink涓婚鍚庯紝杩樺師modbus鍘熺敓鏁版嵁鍙婁富棰樼敤 + */ + private final String allTopic = "/Modbus"; + + /** + * 杩斿洖褰撳墠瀹炰緥锛屼笉瀛樺湪灏卞垱寤哄苟鍚屾椂娉ㄥ唽鐩戝惉浜嬩欢 + * + * @return + */ + public static HDLModBusConnect getInstance() { + if (null == instance) { + instance = new HDLModBusConnect(); + instance.initEventListener(); + } + return instance; + } + + /** + * 娉ㄥ唽鐩戝惉Zigbee鎵�鏈夊師鐢熶富棰樺強鏁版嵁 + * + * @param eventListener + */ + public void registerListener(EventListener eventListener) { + if (null == eventListener) { + return; + } + EventDispatcher.getInstance().register(allTopic, eventListener); + } + + /** + * 绉婚櫎鐩戝惉Zigbee鍘熺敓涓婚鍙婃暟鎹� + * + * @param eventListener + */ + public void removeListener(EventListener eventListener) { + if (null == eventListener) { + return; + } + EventDispatcher.getInstance().remove(allTopic, eventListener); + } + + /** + * 鍒濆鍖栫洃鍚簨浠� + */ + private void initEventListener() { + final EventListener eventListener = new EventListener() { + @Override + public void onMessage(Object msg) { + try { + if (msg instanceof LinkResponse) { + LinkResponse linkResponse = (LinkResponse) msg; + if (linkResponse.getTopic() == null || !linkResponse.getTopic().contains("custom/native/inverter/up")) { + return; + } + byte[] payload = linkResponse.getByteData(); + ModbusResponse modbusResponse = new ModbusResponse(); + String topic; + if (10 < payload.length) { + topic = String.format("Modbus %s", ByteUtils.encodeHexString(new byte[]{payload[0], payload[1], payload[6], payload[7], payload[8], payload[9]}));//搴忓彿+oid + } else { + topic = String.format("Modbus %s", ByteUtils.encodeHexString(new byte[]{payload[0], payload[1]})); + } + modbusResponse.setTopic(topic); + modbusResponse.setData(payload); + 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(topic, modbusResponse); + //鍙戝竷Zigbee鍘熺敓涓婚鍙婃暟鎹� +// EventDispatcher.getInstance().post(allTopic, modbusResponse); + } + } catch (Exception e) { + LogUtils.e(TAG, "LinkResponse杞琈odbusResponse寮傚父:" + e.getMessage()); + } + } + }; + //娉ㄥ唽鐩存帴閫氳鐨勪富棰橈紝鍖呮嫭鐩存帴鍜屼富缃戝叧閫氳鎴栬�呯洿鎺ュ拰浠庣綉鍏抽�氳 + registerListener(String.format(TopicConstant.NATIVE_MODBUS_UP, "+"), eventListener); + //registerListener(String.format(TopicConstant.NATIVE_MODBUS_DOWN_REPLY, "+"), eventListener); + //registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_UP_SLAVE, "+", "+"), eventListener); + } + + + /** + * 鍙戦�佸師鐢熸暟鎹� + * + * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d + * @param payload 鍙戦�佹暟鎹� + * @param baseCallBack 缁撴灉鍥炶皟 + */ + public void Send(String gatewayOidOrGatewayId, byte[] payload, final ModbusCallBack baseCallBack) { + Send(gatewayOidOrGatewayId, payload, 4, baseCallBack, false); + } + + /** + * 鍙戦�佸師鐢熸暟鎹紝瀛愮嚎绋嬪洖璋� + * + * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d + * @param payload 鍙戦�佹暟鎹� + * @param baseCallBack 缁撴灉鍥炶皟 + */ + public void asyncSend(String gatewayOidOrGatewayId, byte[] payload, final ModbusCallBack baseCallBack) { + Send(gatewayOidOrGatewayId, payload, 4, baseCallBack, true); + } + + /** + * 鍙戦�佸師鐢熸暟鎹� + * + * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d + * @param payload 鍙戦�佹暟鎹� + * @param timeout 瓒呮椂鏃堕棿(s) + * @param baseCallBack 缁撴灉鍥炶皟 + */ + public void Send(String gatewayOidOrGatewayId, byte[] payload, int timeout, final ModbusCallBack baseCallBack, boolean useSubThread) { + if (payload == null || payload.length == 0) { + if (baseCallBack != null) { + baseCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR)); + System.out.println("鍙戦�佹暟鎹椂璐熻浇鏁版嵁鏄┖鐨�--->"); + } + return; + } + //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴 + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId); + if (null == gatewayBean) { + LogUtils.i("鎵句笉鍒扮綉鍏筹紝Oid鏄�" + gatewayOidOrGatewayId); + if (null != baseCallBack) { + baseCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); + } + return; + } + + String tempTopic = String.format(TopicConstant.NATIVE_MODBUS_DOWN, gatewayOidOrGatewayId); + LinkRequest request = new LinkRequest(tempTopic, payload, gatewayBean.isLocalEncrypt()); + + request.setCloudTopic(String.format(TopicConstant.NATIVE_MODBUS_DOWN, gatewayBean.getGatewayId())); + if (10 < payload.length) { + request.setReplyTopic(String.format("Modbus %s", ByteUtils.encodeHexString(new byte[]{payload[0], payload[1], payload[6], payload[7], payload[8], payload[9]})));//搴忓彿+oid + } else { + request.setReplyTopic(String.format("Modbus %s", ByteUtils.encodeHexString(new byte[]{payload[0], payload[1]}))); + } + long awaitTime = timeout * 1000L; + + HDLConnectHelper hdlConnectHelper = new HDLConnectHelper(awaitTime, 2, gatewayBean.getIp_address(), 8586, request, new HDLConnectHelper.HdlSocketListener() { + @Override + public void onSucceed(Object msg) { + if (msg instanceof ModbusResponse) { + if (null != baseCallBack) { + baseCallBack.onSuccess(((ModbusResponse) msg).getData()); + } + } else { + LogUtils.e("鍙戦�丮odbus鍥炶皟瀵硅薄绫诲瀷闈炴暟缁勭被鍨嬶紝绫诲瀷鏄�" + msg.getClass()); + } + } + + @Override + public void onFailure(HDLLinkCode hdlLinkCode) { + if (null != baseCallBack) { + baseCallBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); + } + } + }, true, gatewayBean.getDevice_mac(),true); +// hdlConnectHelper.setUseSubThread(useSubThread); + hdlConnectHelper.send(); + } + + /** + * 娉ㄥ唽鐩戝惉 + */ + static void registerListener(String responseTopic, EventListener eventListener) { + if (!TextUtils.isEmpty(responseTopic)) { + EventDispatcher.getInstance().register(responseTopic, eventListener); + } + } + + /** + * 绉婚櫎鐩戝惉 + */ + static void removeListener(String responseTopic, EventListener eventListener) { + if (!TextUtils.isEmpty(responseTopic)) { + EventDispatcher.getInstance().remove(responseTopic, eventListener); + } + } +} -- Gitblit v1.8.0