From 99bc815e07e39354f51421b77f4012ffd35594d8 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 18:03:00 +0800
Subject: [PATCH] 2023年06月28日18:02:58
---
HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLZigbeeConnect.java | 358 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 358 insertions(+), 0 deletions(-)
diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLZigbeeConnect.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLZigbeeConnect.java
new file mode 100644
index 0000000..cf10133
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLZigbeeConnect.java
@@ -0,0 +1,358 @@
+package com.hdl.sdk.link.core.connect;
+
+
+import android.text.TextUtils;
+
+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.HDLLinkCode;
+import com.hdl.sdk.link.common.exception.HDLLinkException;
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.common.utils.ThreadToolUtils;
+import com.hdl.sdk.link.core.bean.LinkRequest;
+import com.hdl.sdk.link.core.bean.LinkResponse;
+import com.hdl.sdk.link.core.bean.ZigbeeResponse;
+import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
+import com.hdl.sdk.link.core.callback.ZigbeeCallBack;
+import com.hdl.sdk.link.core.config.HDLLinkConfig;
+import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by hxb on 2021/12/8.
+ * 鍘熺敓閫氳鐩稿叧鎺ュ彛
+ */
+public class HDLZigbeeConnect {
+
+ private static final String TAG="HDLZigbeeConnect";
+ private static HDLZigbeeConnect instance;
+ /**
+ * 鍐呴儴鐢紝涓昏鏄鐞嗗鐞嗘帀閫忎紶涓婚鍙妉ink涓婚鍚庯紝杩樺師Zigbee鍘熺敓鏁版嵁鍙婁富棰樼敤
+ */
+ private final String zigbeeAllTopic = "/Zigbee";
+
+ /**
+ * 杩斿洖褰撳墠瀹炰緥锛屼笉瀛樺湪灏卞垱寤哄苟鍚屾椂娉ㄥ唽鐩戝惉浜嬩欢
+ *
+ * @return
+ */
+ public static HDLZigbeeConnect getInstance() {
+ if (null == instance) {
+ instance = new HDLZigbeeConnect();
+ instance.initEventListener();
+ }
+ return instance;
+ }
+
+ /**
+ * 娉ㄥ唽鐩戝惉Zigbee鎵�鏈夊師鐢熶富棰樺強鏁版嵁
+ *
+ * @param eventListener
+ */
+ public void registerListener(EventListener eventListener) {
+ if(null==eventListener){
+ return;
+ }
+ EventDispatcher.getInstance().register(zigbeeAllTopic, eventListener);
+ }
+
+ /**
+ * 绉婚櫎鐩戝惉Zigbee鍘熺敓涓婚鍙婃暟鎹�
+ *
+ * @param eventListener
+ */
+ public void removeListener(EventListener eventListener) {
+ if(null==eventListener){
+ return;
+ }
+ EventDispatcher.getInstance().remove(zigbeeAllTopic, eventListener);
+ }
+
+ /**
+ * 鍒濆鍖栫洃鍚簨浠�
+ */
+ private void initEventListener() {
+ 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());
+ }
+ }
+ };
+ //
+ //娉ㄥ唽鐩存帴閫氳鐨勪富棰橈紝鍖呮嫭鐩存帴鍜屼富缃戝叧閫氳鎴栬�呯洿鎺ュ拰浠庣綉鍏抽�氳
+// registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_DOWN_REPLY, "+"), eventListener);
+
+ //娉ㄥ唽鐩存帴閫氳鐨勪富棰橈紝鍖呮嫭鐩存帴鍜屼富缃戝叧閫氳鎴栬�呯洿鎺ュ拰浠庣綉鍏抽�氳
+ registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_UP, "+"), eventListener);
+
+ //娉ㄥ唽閫氳繃涓荤綉鍏抽�忎紶浠庣綉鍏崇殑涓婚
+// registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_DOWN_SLAVE_REPLY, "+", "+"), eventListener);
+
+ registerListener(String.format(TopicConstant.NATIVE_ZIGBEE_UP_SLAVE, "+", "+"), eventListener);
+ }
+
+ /**
+ * 鍙戦�佸師鐢熸暟鎹�
+ *
+ * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d
+ * @param responeTopic 鍥炲涓婚
+ * @param payload 鍙戦�佹暟鎹�
+ * @param zigbeeCallBack 缁撴灉鍥炶皟
+ */
+ public void Send(String gatewayOidOrGatewayId,String responeTopic, String payload, final ZigbeeCallBack zigbeeCallBack) {
+ //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴
+ GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId);
+ if (null == gatewayBean) {
+ LogUtils.i("鎵句笉鍒扮綉鍏筹紝Oid鏄�" + gatewayOidOrGatewayId);
+ if (null != zigbeeCallBack) {
+ zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST));
+ }
+ return;
+ }
+
+ String tempTopic = String.format(TopicConstant.NATIVE_ZIGBEE_DOWN, gatewayOidOrGatewayId);
+ LinkRequest request = new LinkRequest(tempTopic, payload, gatewayBean.getIsLocalEncrypt());
+
+ if ("true".equals(gatewayBean.getMaster())) {
+ request.setCloudTopic(String.format(TopicConstant.NATIVE_ZIGBEE_DOWN, HDLLinkConfig.getInstance().getGatewayId()));
+ } else {
+ request.setCloudTopic(String.format(TopicConstant.NATIVE_ZIGBEE_DOWN_SLAVE, HDLLinkConfig.getInstance().getGatewayId(), gatewayOidOrGatewayId));
+ }
+ request.setReplyTopic(responeTopic);
+// final boolean[] isCallBack = {false};
+// //閫忎紶鍛戒护涓婚澶勭悊
+// final EventListener eventListener = new EventListener() {
+// @Override
+// public void onMessage(Object msg) {
+// if (msg instanceof ZigbeeResponse) {
+// ZigbeeResponse linkResponse = (ZigbeeResponse) msg;
+// //TODO 濡傛灉閰嶇疆浠庣綉鍏崇殑淇℃伅锛岄�氳繃涓荤綉鍏宠浆杈撅紝杩欓噷oid瑕佸垽鏂笅
+// if (responeTopic.equals(linkResponse.getTopic())) {
+// isCallBack[0] = true;
+// removeListener(zigbeeAllTopic, this);
+// if (null != zigbeeCallBack) {
+// zigbeeCallBack.onSuccess(linkResponse.getData());
+// }
+// }
+// }
+// }
+// };
+// //娉ㄥ唽鐩戝惉
+// registerListener(zigbeeAllTopic, eventListener);
+
+ long timeout = 8 * 1000;
+ /**
+ * 闇�瑕佺壒娈婂鐞嗕竴浜涗富棰� 闇�瑕佸欢闀胯秴鏃舵椂闂�
+ */
+ if (responeTopic.equals("Bind/SetBind_Respon") || responeTopic.equals("Bind/ClearBindInfo_Respon")) {
+ timeout = 20 * 1000;
+ }
+// //涓�瀹氭椂闂村悗杩樻病鏈夋帴鏀跺埌鏁版嵁锛屽氨鍥炶皟澶辫触
+// ScheduledExecutorService scheduledExecutorService = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
+// scheduledExecutorService.schedule(new Runnable() {
+// @Override
+// public void run() {
+// removeListener(zigbeeAllTopic, eventListener);
+// scheduledExecutorService.shutdownNow();
+// if (!isCallBack[0]) {
+// if (null != zigbeeCallBack) {
+// zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_Zigbee_FAILURE_ERROR));
+// }
+// }
+// }
+// }, timeout, TimeUnit.SECONDS);
+ new HDLConnectHelper(timeout, 1, gatewayBean.getIp_address(), 8586, request, new HDLConnectHelper.HdlSocketListener() {
+ @Override
+ public void onSucceed(Object msg) {
+ if (msg instanceof String) {
+ if (null != zigbeeCallBack) {
+ zigbeeCallBack.onSuccess(msg+"");
+ }
+ }
+ }
+
+ @Override
+ public void onFailure(HDLLinkCode hdlLinkCode) {
+ if (null != zigbeeCallBack) {
+ zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode));
+ }
+ }
+ }, true).send();
+ }
+
+
+
+ /**
+ * 鍙戦�佸師鐢熼�忎紶鍛戒护鏁版嵁
+ *
+ * @param gatewayOidOrGatewayId 鐩爣缃戝叧鐨刼id鎴栬�呯綉鍏矷d
+ * @param responeTopic 鍥炲涓婚
+ * @param payload 鍙戦�佹暟鎹�
+ * @param zigbeeCallBack 缁撴灉鍥炶皟
+ */
+ public void SendThrough(String gatewayOidOrGatewayId, String responeTopic, String payload, final ZigbeeCallBack zigbeeCallBack) {
+// //濡傛灉鏈湴鏈夐摼鎺ヨ繖涓綉鍏�,鍒欑敤鏈湴杩炴帴
+// GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId);
+// if (null == gatewayBean) {
+// LogUtils.i("鎵句笉鍒扮綉鍏筹紝Oid鏄�" + gatewayOidOrGatewayId);
+// if (null != zigbeeCallBack) {
+// zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST));
+// }
+// return;
+// }
+// boolean isLocal = gatewayBean.getIsLocalGateway();
+// //濡傛灉鏄湰鍦伴�氳
+// if (isLocal == true) {
+//
+// String tempTopic = String.format("/user/%s/custom/native/zigbee/down", gatewayOidOrGatewayId);
+// String tempTopicReply = String.format("/user/%s/custom/native/zigbee/up", gatewayOidOrGatewayId);
+//
+// final boolean[] isCallBack = {false};
+// //閫忎紶鍛戒护涓婚澶勭悊
+// final EventListener eventListener = new EventListener() {
+// @Override
+// public void onMessage(Object msg) {
+// if (msg instanceof LinkResponse) {
+// LinkResponse linkResponse = (LinkResponse) msg;
+// //TODO 濡傛灉閰嶇疆浠庣綉鍏崇殑淇℃伅锛岄�氳繃涓荤綉鍏宠浆杈撅紝杩欓噷oid瑕佸垽鏂笅
+// String body = getZigbeeData(responeTopic, linkResponse);
+// if (null != body) {
+// isCallBack[0] = true;
+// removeListener(tempTopicReply, this);
+// if (null != zigbeeCallBack) {
+// zigbeeCallBack.onSuccess(body);
+// }
+// }
+// }
+// }
+// };
+// //娉ㄥ唽鐩戝惉
+// registerListener(tempTopicReply, eventListener);
+//
+// //涓�瀹氭椂闂村悗杩樻病鏈夋帴鏀跺埌鏁版嵁锛屽氨鍥炶皟澶辫触
+// ScheduledExecutorService scheduledExecutorService = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
+// scheduledExecutorService.schedule(new Runnable() {
+// @Override
+// public void run() {
+// removeListener(tempTopicReply, eventListener);
+// scheduledExecutorService.shutdownNow();
+// if (!isCallBack[0]) {
+// if (null != zigbeeCallBack) {
+// zigbeeCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_Zigbee_FAILURE_ERROR));
+// }
+// }
+// }
+// }, 5, TimeUnit.SECONDS);
+//
+// //鏈湴鍙戦��
+// LinkRequest request = new LinkRequest(tempTopic, payload, gatewayBean.getIsLocalEncrypt());
+// new HDLConnectHelper(gatewayBean.getIp_address(), request,true).send();
+// } else {
+// //璇锋眰涓婚
+// String tempTopic = null;
+// //鍥炲涓婚
+// String tempTopicReply = null;
+//
+// //杩滅▼鍙戦��
+// if ("true".equals(gatewayBean.getMaster())) {
+// tempTopic = String.format("/user/%s/custom/native/zigbee/down", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId());
+// tempTopicReply = String.format("/user/%s/custom/native/zigbee/up", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId());
+// } else {
+// tempTopic = String.format("/user/%s/custom/native/zigbee/slaveoid/%s/down", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId(), gatewayOidOrGatewayId);
+// tempTopicReply = String.format("/user/%s/custom/native/zigbee/slaveoid/%s/up", com.hdl.sdk.link.core.config.HDLLinkConfig.getInstance().getGatewayId(), "+");
+// }
+// //TODO 鍚庣画瀹屽杽浜戠鐨勫彂閫佹帴鏀舵柟娉�
+// }
+ }
+
+
+ /**
+ * 澶勭悊zigbee鍥炲鐨勬暟鎹�
+ *
+ * @param responeTopic 鍥炲涓婚
+ * @param linkResponse 鍥炲鐨勯�忎紶鏁版嵁
+ */
+ private static String getZigbeeData(String responeTopic, LinkResponse linkResponse) {
+ //涓婚涓虹┖涓嶅鐞�
+ if(TextUtils.isEmpty(responeTopic)){
+ return null;
+ }
+
+ String body = linkResponse.getData();
+ int index = body.indexOf("{");
+ //zigbee鍥炲鐨勬暟鎹墠鏈変富棰橈紝鍚庨潰鎵嶆槸鏁版嵁
+ if (index <= 0)
+ return null;
+
+ String zigbeeTopic = body.substring(0, index).trim();
+ //zigbee鐨勮礋杞芥暟鎹�
+ String bodyData = body.substring(index);
+
+ //涓嶆槸褰撳墠璇锋眰鐨勬暟鎹紝涓嶅鐞�
+ if (!zigbeeTopic.startsWith(responeTopic)) {
+ return null;
+ }
+
+ return bodyData;
+ //Zigbee浠ュ墠鐨勬帴鏀堕�昏緫
+// HdlZbGatewayReceiveLogic.Current.ZigbeeOldReceiveLogic(reportTopic, bodyData, gatewayMac);
+ }
+
+ /**
+ * 娉ㄥ唽鐩戝惉
+ */
+ 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