From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLConnectHelper.java | 432 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 432 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLConnectHelper.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLConnectHelper.java new file mode 100644 index 0000000..18967d5 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLConnectHelper.java @@ -0,0 +1,432 @@ +package com.hdl.sdk.link.core.connect; + +import android.text.TextUtils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +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.utils.ErrorUtils; +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.config.HDLLinkConfig; +import com.hdl.sdk.link.core.utils.EncryptUtil; +import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient; +import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Created by Tong on 2021/11/11. + */ +public class HDLConnectHelper { + + private static final Long DEF_SEND_TIMEOUT = 8000L; + private static final int DEF_MAX_RETRY = 1;//鏈�澶ч噸鍙戞暟 + private static final int DEF_SEND_ONE = 1; + private static final int TCP_PORT = 8586; + private static final int UDP_PORT = 8585; + + private final Long sendAwaitTime; + private final int maxRetry; + + /** + * 鏄惁tcp鍙戦�佺被鍨� + */ + private boolean isTcp; + /** + * 璁惧mac + */ + private String mac; + /** + * 鍙戦�佺殑鐩爣IP + */ + private String ipAddress; + /** + * 鍙戦�佺殑鐩爣鍦板潃 + */ + private int port; + private final LinkRequest linkRequest; + private final EventListener eventListener; + + private final AtomicInteger sendNumber = new AtomicInteger(0); + + private final AtomicBoolean isSend = new AtomicBoolean(false); + + private HdlSocketListener listener; + + private ScheduledExecutorService sendThread; + + private String replyTopic; + + public interface HdlSocketListener { + void onSucceed(Object msg); + + void onFailure(HDLLinkCode hdlLinkCode); + } + + /** + * 鍙戦�乁DP鎴栬�匱CP鏁版嵁 + * + * @param sendAwaitTime 姣忔鍙戦�佺瓑寰呮椂闂� + * @param maxRetry 閲嶈瘯娆℃暟 + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param port 鍙戦�佺洰鏍囩鍙� + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) { + this.sendAwaitTime = sendAwaitTime; + this.maxRetry = maxRetry; + this.ipAddress = ipAddress; + this.port = port; + this.linkRequest = linkRequest; + this.replyTopic = linkRequest.getReplyTopic(); + this.listener = listener; + this.isTcp = isTcp; + + + eventListener = new EventListener() { + @Override + public void onMessage(Object msg) { + isSend.set(true); + try { + if (msg instanceof LinkResponse) { + LinkResponse linkResponse = (LinkResponse) msg; + JSONObject jsonObject = JSON.parseObject(linkResponse.getData()); + String id = jsonObject.getString("id"); + Integer code = jsonObject.getInteger("code"); + /** + * 鍙兘杩斿洖code灞炴�у彲鑳芥病鏈� 娌℃湁鐨勮瘽鐩存帴鎴愬姛 鏈夌殑璇濆彧鏈�200鎵嶄細鎴愬姛 + */ + if (code == null || code.intValue() == 200 || code.intValue() == 0) { + notifySucceed(msg); + }else { + notifyFailure(ErrorUtils.getByCode(code)); + } + } + else if (msg instanceof ZigbeeResponse) { + ZigbeeResponse linkResponse = (ZigbeeResponse) msg; + //TODO 濡傛灉閰嶇疆浠庣綉鍏崇殑淇℃伅锛岄�氳繃涓荤綉鍏宠浆杈撅紝杩欓噷oid瑕佸垽鏂笅 + if (replyTopic.equals(linkResponse.getTopic())) { + notifySucceed(linkResponse.getData()); + } + else{ + notifyFailure(HDLLinkCode.HDL_TOPIC_NOT_RIGHT); + } + } + else{ + notifyFailure(new HDLLinkCode(HDLLinkCode.HDL_OBJECT_NOT_SUPPORT.getCode(), "Object Name:" + msg)); + } + } catch (Exception e) { + notifyFailure(new HDLLinkCode(HDLLinkCode.HDL_APPLICATION_CODE.getCode(), e.getMessage())); + } + } + }; + //娉ㄥ唽鐩戝惉 + registerListener(); + } + + /** + * 鍙戦�乁DP鎴栬�匱CP鏁版嵁(鍙傛暟鏈塵ac) + * + * @param sendAwaitTime 姣忔鍙戦�佺瓑寰呮椂闂� + * @param maxRetry 閲嶈瘯娆℃暟 + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param port 鍙戦�佺洰鏍囩鍙� + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + * @param mac 璁惧mac + */ + public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp,String mac) { + this(sendAwaitTime,maxRetry,ipAddress,port,linkRequest,listener,isTcp); + this.mac = mac; + } + + /** + * 鎸夌収鎸囧畾娆℃暟鍙戯紝鍥炶皟 + * + * @param maxRetry 閲嶈瘯娆℃暟 + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param port 鍙戦�佺洰鏍囩鍙� + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(int maxRetry, String ipAddress, int port, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) { + this(DEF_SEND_TIMEOUT, maxRetry, ipAddress, port, linkRequest, listener, isTcp); + } + + /** + * 鎸夌収鎸囧畾娆℃暟鍙戯紝鍥炶皟 + * + * @param maxRetry 閲嶈瘯娆℃暟 + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(int maxRetry, String ipAddress, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) { + this(maxRetry, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, listener, isTcp); + } + + /** + * 鎸夌収鎸囧畾娆℃暟鍙戯紝涓嶅洖璋� + * + * @param maxRetry 閲嶈瘯娆℃暟 + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(int maxRetry, String ipAddress, + LinkRequest linkRequest, boolean isTcp) { + this(maxRetry, ipAddress, linkRequest, null, isTcp); + } + + /** + * 鎸夌収榛樿閲嶅彂鏈哄埗鍙戦�� + * + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param port 鍙戦�佺洰鏍囩鍙� + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(String ipAddress, int port, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) { + this(DEF_MAX_RETRY, ipAddress, port, linkRequest, listener, isTcp); + } + + /** + * 榛樿绔彛鍙戦�� + * + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(String ipAddress, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) { + this(DEF_SEND_TIMEOUT, DEF_MAX_RETRY, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, listener, isTcp); + } + + /** + * 榛樿绔彛鍙戦��(鍙傛暟鏈塵ac) + * + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param listener 鍥炶皟 + * @param isTcp 鏄惁TCP + * @param mac 璁惧mac + */ + public HDLConnectHelper(String ipAddress, + LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp,String mac) { + this(DEF_SEND_TIMEOUT, DEF_MAX_RETRY, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, listener, isTcp,mac); + } + + + + /** + * 鍙戦�佷竴娆� + * + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(String ipAddress, LinkRequest linkRequest, boolean isTcp) { + this(DEF_SEND_TIMEOUT, DEF_SEND_ONE, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, null, isTcp); + } + + /** + * 鍙戦�佷竴娆� + * + * @param ipAddress 鍙戦�佺洰鏍嘔P + * @param linkRequest 鍙戦�佸璞� + * @param isTcp 鏄惁TCP + */ + public HDLConnectHelper(Long timeout,String ipAddress, LinkRequest linkRequest, boolean isTcp) { + this(timeout, DEF_SEND_ONE, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, null, isTcp); + } + + + + /** + * 娉ㄥ唽鐩戝惉 + */ + private void registerListener() { + if (!TextUtils.isEmpty(replyTopic) && null != listener) { + EventDispatcher.getInstance().register(replyTopic, eventListener); + } + } + + /** + * 绉婚櫎鐩戝惉 + */ + private void removeListener() { + if (!TextUtils.isEmpty(replyTopic)) { + EventDispatcher.getInstance().remove(replyTopic, eventListener); + } + } + + public static boolean isLocal() { + String ip = HDLLinkConfig.getInstance().getIpAddress(); + if (ip == null) { + //濡傛槸鏈湴鏄彲浠ユ悳绱㈠埌ip鐨� + return false; + } + + //鏈湴鏄彲浠ヨ繙绋嬫垚鍔熺殑 + return HDLTcpConnect.getTcpSocketBoot(ip).isConnected(); + } + + public void send() { + + getSendThread().scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + //鍙戦�佹鏁板皬浜庨噸鍙戞鏁� + if ((sendNumber.get() < maxRetry)) { + try { + + //杩樻病鏈夋敹鍒板洖澶嶏紝鍐嶅彂閫� + if (!isSend.get()) { + sendNumber.set(sendNumber.get() + 1); + + //濡傛槸tcp鎴栬�卪qtt + if (isTcp) { + //mqtt + if (TextUtils.isEmpty(ipAddress) || !HDLTcpConnect.getTcpSocketBoot(ipAddress).isConnected()) { + if (!linkRequest.getTopic().endsWith("heartbeat")) {//蹇冭烦涓婚鏁版嵁杩囧锛岃繃婊や笅 + //LogUtils.i("蹇冭烦鍖呭彂閫佹暟鎹細\r\n" + new String(linkRequest.getCloudSendBytes())); + } else { + return;//浜戠鎯呭喌涓嬶紝蹇冭烦鍙互涓嶇敤 + } + String requestTopic = linkRequest.getCloudTopic(); + byte[] encryBytes = null; + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); + if (gatewayBean != null && getGatewayTypeList().contains(gatewayBean.getGatewayType())) { + //姣背娉㈣繙绋媘qtt绉橀挜涓嶄竴鏍� + encryBytes = EncryptUtil.encryBytes(linkRequest.getCloudSendBytes(), gatewayBean.getAesKey()); + } else { + encryBytes = EncryptUtil.encryBytes(linkRequest.getCloudSendBytes(), HDLLinkConfig.getInstance().getAesKey()); + } + if (MqttRecvClient.getInstance() != null) { + MqttRecvClient.getInstance().send(requestTopic, encryBytes); + } + LogUtils.i("杩滅▼鍙戦�佹暟鎹細" + linkRequest.getCloudTopic() + "\r\n" + new String(linkRequest.getCloudSendBytes())); + } + //鏈湴TCP + else { + if (!linkRequest.getTopic().endsWith("heartbeat")) {//蹇冭烦涓婚鏁版嵁杩囧锛岃繃婊や笅 + LogUtils.i("鏈湴鍙戦�佹暟鎹細\r\n" + new String(linkRequest.getSendBytes())); + } + HDLTcpConnect.getTcpSocketBoot(ipAddress).sendMsg(EncryptUtil.getEncryBytes(linkRequest)); + } + } else { + //濡傛灉鏄痷dp + LogUtils.i("鏈湴鍙戦�佹暟鎹甎DP锛�" + new String(linkRequest.getSendBytes())); + HDLUdpConnect.getInstance().getUdpBoot().sendMsg(ipAddress, port, EncryptUtil.getEncryBytes(linkRequest)); + } + } + } catch (Exception e) { + LogUtils.e("鏁版嵁鍙戦�佸紓甯革細", e.getMessage()); + } + } else { + //瓒呭嚭閲嶅彂娆℃暟骞舵病鏈夋敹鍒板洖澶� + if (!isSend.get()) { + if (linkRequest.getTopic().endsWith("heartbeat")) {//蹇冭烦涓婚鍏堜笉閫氱煡 + notifyFailure(null); + } else { + if (!TextUtils.isEmpty(replyTopic) && null != listener) {//闇�瑕佹墦鍗板嚭澶辫触鐨勬棩蹇� + LogUtils.e("鍙戦�佸け璐ユ暟鎹富棰�:" + linkRequest.getTopic()); + } + if (isTcp) { + //mqtt + if (TextUtils.isEmpty(ipAddress) || !HDLTcpConnect.getTcpSocketBoot(ipAddress).isConnected()) { + notifyFailure(HDLLinkCode.HDL_GATEWAY_REMOTE_NOT_RESPONSE); + } + //鏈湴TCP锛屽苟鏄繛鎺ョ姸鎬� + else { + notifyFailure(HDLLinkCode.HDL_TIMEOUT_ERROR); + } + } else { + notifyFailure(HDLLinkCode.HDL_TIMEOUT_ERROR); + } + } + } + } + } + }, 0, sendAwaitTime, TimeUnit.MILLISECONDS); + //initialdelay - 棣栨鎵ц鐨勫欢杩熸椂闂� 0 + //delay - 涓�娆℃墽琛岀粓姝㈠拰涓嬩竴娆℃墽琛屽紑濮嬩箣闂寸殑寤惰繜 + } + + /** + * 鑾峰彇鍙戦�佺嚎绋� + * + * @return 杩斿洖鑾峰彇鍒扮殑绾跨▼ + */ + private ScheduledExecutorService getSendThread() { + if (sendThread == null) { + sendThread = ThreadToolUtils.getInstance().newScheduledThreadPool(1); + } + return sendThread; + } + + /** + * 鍙戦�佸け璐� + */ + private void notifyFailure(HDLLinkCode hdlLinkCode) { + //绉婚櫎鐩戝惉 + removeListener(); + if (sendThread != null) { + sendThread.shutdownNow(); + sendThread = null; + } + if (listener != null && hdlLinkCode != null) { + listener.onFailure(hdlLinkCode); + listener = null; + } + } + + /** + * 鏀寔姣背绫诲瀷 + * + * @return 绫诲瀷鍒楄〃 + */ + public static List<String> getGatewayTypeList() { + List<String> typeList = new ArrayList<>(); + typeList.add("sensor.mmv_sleep");//鐫$湢姣背娉pk + typeList.add("sensor.mmv_pose");//濮挎�佹绫虫尝spk + return typeList; + } + + private void notifySucceed(Object msg) { + //绉婚櫎鐩戝惉 + removeListener(); + if (sendThread != null) { + sendThread.shutdownNow(); + sendThread = null; + } + if (listener != null) { + listener.onSucceed(msg); + listener = null; + } + } +} -- Gitblit v1.8.0