From 2a01ef5e49422cca49bc7476fc1b8be8c8556561 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期四, 08 九月 2022 10:46:57 +0800
Subject: [PATCH] 最新sdk java版本

---
 HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLUdpConnect.java |  342 +++++++++++++++++++++-----------------------------------
 1 files changed, 129 insertions(+), 213 deletions(-)

diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLUdpConnect.java
similarity index 64%
rename from HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
rename to HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLUdpConnect.java
index 6986188..edfaaf7 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
+++ b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLUdpConnect.java
@@ -1,55 +1,55 @@
-package com.hdl.sdk.connect.socket;
+package com.hdl.sdk.link.core.connect;
+
 
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
 import com.google.gson.reflect.TypeToken;
-import com.hdl.sdk.common.event.EventDispatcher;
-import com.hdl.sdk.common.event.EventListener;
-import com.hdl.sdk.common.exception.HDLLinkCode;
-import com.hdl.sdk.common.exception.HDLLinkException;
-import com.hdl.sdk.common.utils.LogUtils;
-import com.hdl.sdk.common.utils.TextUtils;
-import com.hdl.sdk.common.utils.ThreadToolUtils;
-import com.hdl.sdk.connect.bean.response.AuthenticateResponse;
-import com.hdl.sdk.connect.bean.response.NetworkAccessBroadcastResponse;
-import com.hdl.sdk.connect.callback.BaseCallBack;
-import com.hdl.sdk.connect.callback.HDLLinkCallBack;
-import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack;
-import com.hdl.sdk.connect.config.HDLLinkConfig;
-import com.hdl.sdk.common.config.TopicConstant;
-import com.hdl.sdk.common.utils.IdUtils;
-import com.hdl.sdk.common.utils.IpUtils;
-import com.hdl.sdk.common.utils.gson.GsonConvert;
-import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
-import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
-import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
-import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
-import com.hdl.sdk.connect.bean.LinkRequest;
-import com.hdl.sdk.connect.bean.LinkResponse;
-import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
-import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
-import com.hdl.sdk.socket.client.UdpClient;
-import com.hdl.sdk.socket.codec.MessagePipeLine;
-import com.hdl.sdk.socket.udp.UdpSocketBoot;
-import com.hdl.sdk.socket.udp.UdpSocketOptions;
+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.IpUtils;
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.common.utils.TextUtils;
+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.gateway.GatewayBean;
+import com.hdl.sdk.link.core.bean.request.AuthenticateRequest;
+import com.hdl.sdk.link.core.bean.response.AuthenticateResponse;
+import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
+import com.hdl.sdk.link.core.bean.response.GatewaySearchBean;
+import com.hdl.sdk.link.core.bean.response.NetworkAccessBroadcastResponse;
+import com.hdl.sdk.link.core.callback.BaseCallBack;
+import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
+import com.hdl.sdk.link.core.callback.HDLLinkResponseCallBack;
+import com.hdl.sdk.link.common.config.TopicConstant;
+import com.hdl.sdk.link.common.utils.IdUtils;
+import com.hdl.sdk.link.common.utils.gson.GsonConvert;
+import com.hdl.sdk.link.core.config.HDLLinkConfig;
+import com.hdl.sdk.link.core.protocol.LinkMessageDecoder;
+import com.hdl.sdk.link.core.protocol.LinkMessageEncoder;
+import com.hdl.sdk.link.socket.client.UdpClient;
+import com.hdl.sdk.link.socket.codec.MessagePipeLine;
+import com.hdl.sdk.link.socket.udp.UdpSocketBoot;
+import com.hdl.sdk.link.socket.udp.UdpSocketOptions;
 
-import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
+import static com.hdl.sdk.link.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
 
 /**
  * Created by jlchen on 11/11/21.
  *
  * @Description : HDLAuthSocket 鐢变簬鍓嶆湡宸茬粡鍛藉悕濂斤紝涓嶅仛鏇存敼锛屽彲鐢ㄤ綔Udp鏈嶅姟绔娇鐢�
  */
-public class HDLAuthSocket {
+public class HDLUdpConnect {
     private static final String TAG = "HDLAuth";
     /**
      * udp榛樿绔彛
      */
-    private static final int UDP_PORT = 8585;
+    public static final int UDP_PORT = 8585;
     /**
      * 鍥犱负鑰冭檻鍒颁娇鐢ㄤ竴涓鍙o紝瑕佹敮鎸佹帴鏀跺缃戝叧鐨勬暟鎹紝鎵�浠ュ彧鍏佽浣跨敤涓�涓�
      */
@@ -65,30 +65,26 @@
     /**
      * instance
      */
-    private  static final HDLAuthSocket instance=new HDLAuthSocket();
+    private volatile static HDLUdpConnect instance;
 
-    private HDLAuthSocket() {
+    private HDLUdpConnect() {
         initListenerGatewayEvent();
         initSearchGatewayEvent();
     }
-
-//    public interface CallBack extends BaseCallBack {
-//        void onSuccess(String msg);
-//    }
 
     /**
      * getInstance
      *
      * @return HDLAuthSocket
      */
-    public  static HDLAuthSocket getInstance() {
-//        if (instance == null) {
-//            synchronized (HDLAuthSocket.class) {
-//        if (instance == null) {
-//            instance = new HDLAuthSocket();
-//        }
-//            }
-//        }
+    public static synchronized HDLUdpConnect getInstance() {
+        if (instance == null) {
+            synchronized (HDLLinkConfig.class) {
+                if (instance == null) {
+                    instance = new HDLUdpConnect();
+                }
+            }
+        }
         return instance;
     }
 
@@ -107,14 +103,28 @@
      *
      * @return 杩斿洖褰撳墠瀵硅薄
      */
-    private synchronized UdpSocketBoot getUdpBoot() {
+    public synchronized UdpSocketBoot getUdpBoot() {
+        if (null == initUdp()) {
+            return null;
+        }
+        return udpSocketBoot;
+    }
+
+
+    /**
+     * 鍒濆鍖杣dp 鐩戝惉鍔熻兘
+     *
+     * @return 杩斿洖褰撳墠瀵硅薄
+     */
+    public synchronized UdpSocketBoot initUdp() {
         try {
             if (udpSocketBoot == null) {
-                udpSocketBoot = UdpClient.init(UDP_PORT, getUdpOptions());
+                udpSocketBoot = UdpClient.init("0.0.0.0",UDP_PORT, getUdpOptions());
                 udpSocketBoot.bind();
             }
         } catch (Exception e) {
-              LogUtils.e("鍒濆鍖朣ocket udp寮傚父"+e.getMessage());
+            LogUtils.e("鍒濆鍖栫綉鍏冲け璐�:"+e.getMessage());
+            return null;
         }
 
         return udpSocketBoot;
@@ -133,22 +143,20 @@
         //2.鏋勫缓鐩戝惉Listener
 //        authEvent =
         //3.鐩戝惉缃戝叧骞挎挱鐨勫叆缃戞寚浠�
-        EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
         EventDispatcher.getInstance().register(TopicConstant.GATEWAY_AUTH_BROADCAST, new EventListener() {
             @Override
             public void onMessage(Object msg) {
-                LogUtils.i("鏀跺埌缃戝叧璁よ瘉淇℃伅: ");
                 NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg);
                 if (bean != null) {
-                    //绉婚櫎鐩戝惉
-                    EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
-                    LogUtils.i("缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress());
+                    LogUtils.i(TAG, "缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress());
                     String ipStr = bean.getIPAddress();
                     if (!TextUtils.isEmpty(ipStr)) {
                         sendAuthenticateRequest(ipStr, request, callBack);
                     }
                 }
-
+                //绉婚櫎鐩戝惉
+                EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+                LogUtils.i(TAG, "绉婚櫎鐩戝惉 authEvent");
             }
         });
     }
@@ -175,14 +183,11 @@
         String topic = DEIVCE_AUTH_REQUEST;
         Gson gs = new Gson();
         String requestStr = gs.toJson(request);
-        LinkRequest message = new LinkRequest(topic, requestStr);
+        LinkRequest message = new LinkRequest(topic, requestStr, false);
 
-        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
+        new HDLConnectHelper(ip, message, requestStr + "_reply", new HDLConnectHelper.HdlSocketListener() {
             @Override
             public void onSucceed(Object msg) {
-
-                LogUtils.i("onSucceed msg锛�" + msg);
-
                 if (callBack == null) return;
                 try {
                     AuthenticateResponse bean = getAuthenticateResponseBean(msg);
@@ -206,10 +211,10 @@
                                 HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                                 callBack.onSuccess("璁よ瘉鎴愬姛");
                             }
-                        } else if (bean.getCode().equals("14013")){
+                        } else if (bean.getCode().equals("14013")) {
                             //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
-                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_));
-                        } else{
+                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR));
+                        } else {
                             //璁よ瘉澶辫触锛岄敊璇爜锛�
                             LogUtils.e("璁よ瘉澶辫触锛岄敊璇爜锛�" + bean.getCode());
                             callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR));
@@ -228,9 +233,8 @@
                 if (callBack == null) return;
                 callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
             }
-        });
+        }, false).send();
     }
-
 
 //    /**
 //     * 鍙戦�佸叆缃戝強璁よ瘉璇锋眰
@@ -284,14 +288,6 @@
         void onSuccess(GatewaySearchBean gatewaySearchBean);
     }
 
-    /**
-     * 寮�濮嬫悳绱㈡墍鏈夌綉鍏筹紝鏈夌綉鍏冲洖澶嶅氨鍥炶皟锛屼笂灞傝嚜宸卞仛鍘婚噸鍒ゆ柇
-     *
-     * @param callBack 鍥炶皟
-     */
-    public void startSearchAllGateway(SearchGatewayCallBack callBack) {
-
-    }
 
     /**
      * 鏆傚仠鎼滅储缃戝叧
@@ -325,47 +321,16 @@
         searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, UDP_PORT, callBack);
     }
 
-
-    /**
-     * 璁惧鎺у埗
-     */
-    public void propertyDown(List<DeviceControlRequest> request, HDLLinkCallBack callBack) {
-        String time = String.valueOf(System.currentTimeMillis());
-        final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>();
-        data.setId(IdUtils.getUUId());
-        data.setTime_stamp(time);
-        data.setObjects(request);
-        String topic = HDLLinkConfig.getInstance().getFullTopic(TopicConstant.PROPERTY_DOWN);
-        LinkRequest message = new LinkRequest(topic,
-                GsonConvert.getGson().toJson(data));
-
-        String ip = IpUtils.getBroadcastAddress();
-        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
-            @Override
-            public void onSucceed(Object msg) {
-                if (callBack == null) return;
-                callBack.onSuccess("鎺у埗鎴愬姛");
-            }
-
-            @Override
-            public void onFailure() {
-                if (callBack == null) return;
-                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_CONTROL_FAILURE_ERROR));
-            }
-        });
-
-    }
-
     /**
      * 閫氱敤鍙戦�佹寚浠�
      * 1绉掓病鍝嶅簲灏辫浠栭噸鏂板彂閫�,閲嶈瘯3娆�
      *
-     * @param topic    鍙戦�佹暟鎹�
-     * @param bodyStr  鍥炲鐨勪富棰�
+     * @param topic     鍙戦�佹暟鎹�
+     * @param bodyStr   鍥炲鐨勪富棰�
      * @param broadcast 鏄惁瑕佸箍鎾�
-     * @param callBack 鍥炶皟
+     * @param callBack  鍥炶皟
      */
-    public void udpSendMsg(String topic, String bodyStr,boolean broadcast,  HDLLinkResponseCallBack callBack) {
+    public void udpSendMsg(String topic, String bodyStr, boolean broadcast, HDLLinkResponseCallBack callBack) {
         if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
             if (callBack != null) {
                 callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
@@ -380,49 +345,49 @@
             return;
         }
 
-        LinkRequest message = new LinkRequest(topic, bodyStr);
+        LinkRequest message = new LinkRequest(topic, bodyStr, false);
         String ip = HDLLinkConfig.getInstance().getIpAddress();
-        if(broadcast) {
+        if (broadcast) {
             ip = IpUtils.getBroadcastAddress();
         }
-        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
-                    @Override
-                    public void onSucceed(Object msg) {
-                        if (callBack == null) return;
-                        callBack.onSuccess((LinkResponse) msg);
-                    }
+        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
+            @Override
+            public void onSucceed(Object msg) {
+                if (callBack == null) return;
+                callBack.onSuccess((LinkResponse) msg);
+            }
 
-                    @Override
-                    public void onFailure() {
-                        if (callBack == null) return;
-                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
-                    }
-                }
-        );
+            @Override
+            public void onFailure() {
+                if (callBack == null) return;
+                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
+            }
+        }
+                , false).send();
     }
 
     /**
      * 閫氱敤鍙戦�佹寚浠� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
      *
-     * @param topic   鍙戦�佹暟鎹�
-     * @param bodyStr 鍥炲鐨勪富棰�
+     * @param topic     鍙戦�佹暟鎹�
+     * @param bodyStr   鍥炲鐨勪富棰�
      * @param broadcast 鏄惁骞挎挱
      */
-    public void udpSendMsg(String topic, String bodyStr,boolean broadcast) {
+    public void udpSendMsg(String topic, String bodyStr, boolean broadcast) {
         if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
-            LogUtils.e( "鍙傛暟涓嶈兘涓虹┖锛屼笉鑳藉彂閫乁DP鏁版嵁");
+            LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖");
             return;
         }
         if (!HDLLinkConfig.getInstance().checkIfCertified()) {
-            LogUtils.e("鏈璇侊紝璇峰厛璁よ瘉鍐嶈皟鐢║DP鍙戦�佹柟娉�");
+            LogUtils.e("udpSendMsg", "鏈璇侊紝璇峰厛璁よ瘉");
             return;
         }
-        LinkRequest message = new LinkRequest(topic, bodyStr);
+        LinkRequest message = new LinkRequest(topic, bodyStr, HDLLinkConfig.getInstance().isLocalEncrypt());
         String ip = HDLLinkConfig.getInstance().getIpAddress();
-        if(broadcast) {
+        if (broadcast) {
             ip = IpUtils.getBroadcastAddress();
         }
-        HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
+        new HDLConnectHelper(ip, message, false).send();
     }
 
     private GatewaySearchBean getGatewaySearchBean(Object msg) {
@@ -435,7 +400,6 @@
                 }.getType());
                 searchBean = response.getObjects();
             }
-
         }
         return searchBean;
     }
@@ -494,12 +458,11 @@
                             }.getType());
                             GatewaySearchBean searchBean = response.getObjects();
                             if (searchBean != null && !TextUtils.isEmpty(searchBean.getGatewayId())) {
-                                searchBean.setIp_address(linkResponse.getSource_ipAddress());
-                                if (searchBean.getGatewayId().contains(searchGatewayId)&&!TextUtils.isEmpty(searchGatewayId)) {
+                                if (searchBean.getGatewayId().contains(searchGatewayId)) {
                                     removeSearchGatewayEvent();//绉婚櫎鎼滅储缃戝叧鐩戝惉
                                     isSearchGatewaySuccess.set(true);//鎼滅储鎴愬姛鏍囪
                                     searchGatewayCount.set(11);//娆℃暟鏍囪
-                                    HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧
+                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑
                                     if (mSearchGatewayCallBack != null) {
                                         mSearchGatewayCallBack.onSuccess(searchBean);
                                     }
@@ -523,22 +486,28 @@
                     if (msg instanceof LinkResponse) {
                         LinkResponse linkResponse = (LinkResponse) msg;
                         String data = linkResponse.getData();
-                        LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + data);
+//                        LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + data);
                         if (!TextUtils.isEmpty(data)) {
-                            final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
+                            final BaseLocalResponse<GatewayBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewayBean>>() {
                             }.getType());
-                            GatewaySearchBean gateway = response.getObjects();
-                            if (gateway != null && !TextUtils.isEmpty(gateway.getGatewayId())) {
-                                //鍙兘缃戝叧甯﹁繃鏉ョ殑ip涓嶅
-                                gateway.setIp_address(linkResponse.getSource_ipAddress());
-                                //涓荤綉鍏冲苟涓旀槸褰撳墠缁戝畾鐨勭綉鍏�
-                                if ("true".equals(gateway.getMaster().toLowerCase()) && gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) {
-                                    HDLLinkConfig.getInstance().setCurrentGateway(gateway);//璁剧疆褰撳墠缃戝叧
-                                    if(!TextUtils.isEmpty( gateway.getIp_address())) {
-                                        HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
+                            GatewayBean gateway = response.getObjects();
+                            if (gateway != null) {
+                                if (!TextUtils.isEmpty(HDLLinkConfig.getInstance().getGatewayId())) {
+                                    //涓荤綉鍏冲苟涓旀槸褰撳墠缁戝畾鐨勭綉鍏�
+                                    if ("true".equals(gateway.getMaster().toLowerCase())) {
+                                        if (gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())
+                                                || gateway.getDevice_mac().equals(HDLLinkConfig.getInstance().getGatewayId())
+                                                || gateway.getOid().equals(HDLLinkConfig.getInstance().getGatewayId())) {
+                                            HDLLinkConfig.getInstance().setLocalEncrypt(gateway.getIsLocalEncrypt());//璁剧疆鏄惁鍔犲瘑
+                                            HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
+                                            //鏇存柊褰撳墠缃戝叧鐨勪俊鎭�
+                                            HDLLinkConfig.getInstance().reSaveConfig();
+                                        }
                                     }
-                                    //鏇存柊褰撳墠缃戝叧鐨勪俊鎭�
-                                    HDLLinkConfig.getInstance().reSaveConfig();
+                                }
+
+                                if(TextUtils.isEmpty(gateway.getGatewayId())||gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) {
+                                    HDLTcpConnect.initTcp(gateway.getIp_address());//鍒濆鍖朤CP杩炴帴
                                 }
                             }
                         }
@@ -570,7 +539,7 @@
         jsonObject.addProperty("id", IdUtils.getUUId());
         jsonObject.addProperty("time_stamp", time);
         LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
-                jsonObject.toString());
+                jsonObject.toString(), false);
         //娉ㄥ唽鎼滅储缃戝叧鐩戝惉
         registerSearchGatewayEvent();
         new Thread(new Runnable() {
@@ -580,8 +549,8 @@
                     try {
                         //鎼滅储缃戝叧
                         searchGatewayCount.set(searchGatewayCount.get() + 1);
-                        LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
-                        getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
+                        LogUtils.i("鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
+                        new HDLConnectHelper(ipAddress, message, false).send();
                         Thread.sleep(1000L);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
@@ -589,11 +558,9 @@
                 }
 
                 if (!isSearchGatewaySuccess.get()) {
-                    try {
-                        LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
-                        //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
-                        callBackSearchGatewayTimeout();
-                    }catch (Exception e){}
+                    //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
+                    callBackSearchGatewayTimeout();
+                    LogUtils.e("鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
                 }
             }
         }).start();
@@ -604,7 +571,7 @@
      * 娉ㄥ唽鎼滅储缃戝叧鐩戝惉
      */
     private void registerSearchGatewayEvent() {
-        LogUtils.i("鎼滅储缃戝叧", "娉ㄥ唽鎼滅储缃戝叧鐩戝惉");
+        LogUtils.i("娉ㄥ唽鎼滅储缃戝叧鐩戝惉");
         EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
     }
 
@@ -621,61 +588,10 @@
      */
     private void callBackSearchGatewayTimeout() {
         removeSearchGatewayEvent();
-        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                if (mSearchGatewayCallBack != null) {
-                    mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
-                }
-            }
-        });
-        LogUtils.i("callBackSearchGatewayTimeout onError锛氭悳绱㈢綉鍏冲け璐ワ紝瓒呮椂");
-
+        if (mSearchGatewayCallBack != null) {
+            mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
+        }
     }
 
 
-    /**
-     * 鎼滅储鍦ㄧ嚎鎵�鏈夌殑缃戝叧锛屽寘鎷綋鍓嶄綇瀹呯殑鍙婃病鏈夌粦瀹氳繃鍏跺畠浣忓畢鐨勭綉鍏�
-     *
-     * @param ipAddress 鐩爣鐨処P鍦板潃
-     * @param port      鐩爣鐨勭鍙�
-     * @param callBack  鍥炶皟
-     */
-    public void searchGateway(String ipAddress, int port, SearchGatewayCallBack callBack) {
-//        this.searchGatewayId = gatewayId;
-//        this.mSearchGatewayCallBack = callBack;
-//        //閲嶇疆鍙傛暟
-//        searchGatewayCount.set(0);
-//        isSearchGatewaySuccess.set(false);
-        String time = String.valueOf(System.currentTimeMillis());
-        JsonObject jsonObject = new JsonObject();
-        jsonObject.addProperty("id", IdUtils.getUUId());
-        jsonObject.addProperty("time_stamp", time);
-        LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
-                jsonObject.toString());
-        //娉ㄥ唽鎼滅储缃戝叧鐩戝惉
-        registerSearchGatewayEvent();
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
-                    try {
-                        //鎼滅储缃戝叧
-                        searchGatewayCount.set(searchGatewayCount.get() + 1);
-                        LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
-                        getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
-                        Thread.sleep(1000L);
-                    } catch (InterruptedException e) {
-                        e.printStackTrace();
-                    }
-                }
-
-                if (!isSearchGatewaySuccess.get()) {
-                    //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
-                    callBackSearchGatewayTimeout();
-                    LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
-                }
-            }
-        }).start();
-    }
 }

--
Gitblit v1.8.0