From 7c8ce9b9a7d3fc1aaa4a621e86415b25ad10a34f Mon Sep 17 00:00:00 2001
From: panlili2024 <14743743+panlili2024@user.noreply.gitee.com>
Date: 星期三, 19 三月 2025 09:36:28 +0800
Subject: [PATCH] 添加source屏扫码流程

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java |  375 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 304 insertions(+), 71 deletions(-)

diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
index ac97593..9b94bcf 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -1,33 +1,38 @@
 package com.hdl.sdk.connect.socket;
 
+import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
+
 import android.text.TextUtils;
+import android.util.Log;
 
 import com.google.gson.Gson;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.reflect.TypeToken;
+import com.hdl.sdk.common.HDLSdk;
+import com.hdl.sdk.common.config.TopicConstant;
 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.IdUtils;
+import com.hdl.sdk.common.utils.IpUtils;
 import com.hdl.sdk.common.utils.LogUtils;
 import com.hdl.sdk.common.utils.ThreadToolUtils;
-import com.hdl.sdk.connect.HDLLink;
+import com.hdl.sdk.common.utils.gson.GsonConvert;
+import com.hdl.sdk.connect.bean.LinkRequest;
+import com.hdl.sdk.connect.bean.LinkResponse;
+import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
+import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
+import com.hdl.sdk.connect.bean.request.GatewayInfoRequest;
 import com.hdl.sdk.connect.bean.response.AuthenticateResponse;
+import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
+import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
 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;
@@ -35,12 +40,9 @@
 import com.hdl.sdk.socket.udp.UdpSocketBoot;
 import com.hdl.sdk.socket.udp.UdpSocketOptions;
 
-import java.net.InetSocketAddress;
 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;
 
 /**
  * Created by jlchen on 11/11/21.
@@ -53,6 +55,7 @@
      * udp榛樿绔彛
      */
     private static final int UDP_PORT = 8585;
+    private static int BIND_UDP_PORT = 8584;
     /**
      * 鍥犱负鑰冭檻鍒颁娇鐢ㄤ竴涓鍙o紝瑕佹敮鎸佹帴鏀跺缃戝叧鐨勬暟鎹紝鎵�浠ュ彧鍏佽浣跨敤涓�涓�
      */
@@ -68,7 +71,7 @@
     /**
      * instance
      */
-    private volatile static HDLAuthSocket instance;
+    private static final HDLAuthSocket instance = new HDLAuthSocket();
 
     private HDLAuthSocket() {
         initListenerGatewayEvent();
@@ -84,14 +87,14 @@
      *
      * @return HDLAuthSocket
      */
-    public static synchronized HDLAuthSocket getInstance() {
-        if (instance == null) {
-            synchronized (HDLLinkConfig.class) {
-                if (instance == null) {
-                    instance = new HDLAuthSocket();
-                }
-            }
-        }
+    public static HDLAuthSocket getInstance() {
+//        if (instance == null) {
+//            synchronized (HDLAuthSocket.class) {
+//        if (instance == null) {
+//            instance = new HDLAuthSocket();
+//        }
+//            }
+//        }
         return instance;
     }
 
@@ -110,17 +113,25 @@
      *
      * @return 杩斿洖褰撳墠瀵硅薄
      */
-    private UdpSocketBoot getUdpBoot() {
+    private synchronized UdpSocketBoot getUdpBoot() {
         try {
             if (udpSocketBoot == null) {
-                udpSocketBoot = UdpClient.init(UDP_PORT, getUdpOptions());
+                //鍖哄垎鏄�8584杩樻槸8585
+                BIND_UDP_PORT = HDLSdk.getInstance().getBindPort();
+                udpSocketBoot = UdpClient.init(BIND_UDP_PORT, getUdpOptions());
                 udpSocketBoot.bind();
+
+                LogUtils.i("鍒濆鍖朣ocket udp");
             }
         } catch (Exception e) {
-            return null;
+            LogUtils.e("鍒濆鍖朣ocket udp寮傚父" + e.getMessage());
         }
 
         return udpSocketBoot;
+    }
+
+    public void init() {
+        getUdpBoot();
     }
 
     /**
@@ -136,20 +147,36 @@
         //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) {
-                NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg);
-                if (bean != null) {
-                    LogUtils.i(TAG, "缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress());
-                    String ipStr = bean.getIPAddress();
-                    if (!TextUtils.isEmpty(ipStr)) {
-                        sendAuthenticateRequest(ipStr, request, callBack);
+                try {
+                    LogUtils.i("鏀跺埌缃戝叧璁よ瘉淇℃伅: ");
+                    NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg);
+                    if (bean != null) {
+                        LogUtils.i("缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress());
+                        if (!TextUtils.isEmpty(bean.getSlave_mac())) {
+                            if (!bean.getSlave_mac().equals(request.getObjects().getDeviceMAC())) {
+                                LogUtils.i("鍏ョ綉楠岃瘉鐨刴ac鍜屾湰鏈轰笉涓�鏍凤紝璇锋眰鐨凪AC鏄�" + bean.getSlave_mac() + " 鏈満鐨勬槸" + request.getObjects().getDeviceMAC());
+                                return;
+                            }
+                        }
+                        //绉婚櫎鐩戝惉
+                        EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+                        HDLLinkConfig.getInstance().setIpAddress(bean.getIPAddress());
+                        if (!TextUtils.isEmpty(bean.getOID())) {
+                            HDLLinkConfig.getInstance().setParentOid(bean.getOID());
+                        }
+                        String ipStr = bean.getIPAddress();
+                        if (!TextUtils.isEmpty(ipStr)) {
+                            sendAuthenticateRequest(ipStr, request, callBack);
+                        }
                     }
+
+                } catch (Exception e) {
+                    LogUtils.e("鍏ョ綉楠岃瘉寮傚父", e.getMessage());
                 }
-                //绉婚櫎鐩戝惉
-                EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
-                LogUtils.i(TAG, "绉婚櫎鐩戝惉 authEvent");
             }
         });
     }
@@ -160,6 +187,54 @@
     public void endAuthenticateRequest() {
         //绉婚櫎鐩戝惉
         EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+    }
+
+    /**
+     * 涓婃姤浠庣綉鍏充俊鎭�
+     *
+     * @param callBack 鍥炶皟
+     */
+    public void UploadGatewayInfo(HDLLinkCallBack callBack) {
+        String time = String.valueOf(System.currentTimeMillis());
+        final BaseLocalResponse<GatewayInfoRequest> data = new BaseLocalResponse<>();
+        data.setId(IdUtils.getUUId());
+        data.setTime_stamp(time);
+
+        final GatewayInfoRequest request = new GatewayInfoRequest();
+        AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean();
+        if (authenticateDeviceInfoBean == null) return;
+        request.setDevice_model(authenticateDeviceInfoBean.getDeviceModel());
+        request.setDevice_mac(authenticateDeviceInfoBean.getDeviceMAC());
+        request.setDevice_name(authenticateDeviceInfoBean.getDeviceName());
+        request.setGateway_type(authenticateDeviceInfoBean.getGateway_type());
+        request.setAccess_mode(authenticateDeviceInfoBean.getAccessMode());
+        request.setOid(authenticateDeviceInfoBean.getOID());
+        request.setSid(authenticateDeviceInfoBean.getSid());
+        request.setSupplier(authenticateDeviceInfoBean.getSupplier());
+
+        if (authenticateDeviceInfoBean.getSupplier() == null) {
+            AuthenticateRequest.RequestBean requestBean = HDLLinkConfig.getInstance().getRequestBean();
+            if (requestBean != null) {
+                request.setSupplier(requestBean.getSupplier());
+            }
+        }
+
+        request.setHw_version(authenticateDeviceInfoBean.getHw_version());
+        request.setFw_version(HDLSdk.getInstance().getVersion());//sdk鐗堟湰
+        request.setIp_address(authenticateDeviceInfoBean.getIPAddress());
+        request.setMaster("false");
+        request.setLocalEncrypt(false);
+        request.setHomeId(HDLLinkConfig.getInstance().getHomeId());
+
+        data.setObjects(request);
+
+        String topic = HDLLinkConfig.getInstance().getFullTopic(TopicConstant.GATEWAY_SEARCH_REPLY);
+        LinkRequest message = new LinkRequest(topic,
+                GsonConvert.getGson().toJson(data));
+
+        String ip = IpUtils.getBroadcastAddress();
+        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, null);
+
     }
 
     /**
@@ -178,30 +253,65 @@
         String requestStr = gs.toJson(request);
         LinkRequest message = new LinkRequest(topic, requestStr);
 
-        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message,new HdlSocketHelper.HdlSocketListener() {
+        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
             @Override
             public void onSucceed(Object msg) {
                 if (callBack == null) return;
                 try {
                     AuthenticateResponse bean = getAuthenticateResponseBean(msg);
                     if (bean != null) {
-                        String localSecret = "";
-                        String gatewayId = "";
-                        String ipAddress = "";
-                        if (bean.getAuth() != null) {
-                            localSecret = bean.getAuth().getLocalSecret();
-                        }
-                        if (bean.getObjects() != null) {
-                            gatewayId = bean.getObjects().getGatewayID();
-                            ipAddress = bean.getObjects().getIPAddress();
-                        }
-                        //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
-                        if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
-                            //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�
-                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
+                        if (bean.getCode().equals("200") || bean.getCode().equals("0")) {
+                            String localSecret = "";
+                            String gatewayId = "";
+                            String ipAddress = "";
+                            String homeId = "";
+                            if (bean.getAuth() != null) {
+                                localSecret = bean.getAuth().getLocalSecret();
+                            }
+                            if (bean.getObjects() != null) {
+                                gatewayId = bean.getObjects().getGatewayID();
+                                ipAddress = bean.getObjects().getIPAddress();
+                                homeId = bean.getObjects().getHomeId();
+                            }
+                            if (ipAddress == null) {
+                                ipAddress = bean.getIp_address();
+                            }
+                            if (!TextUtils.isEmpty(homeId)) {
+                                HDLLinkConfig.getInstance().setHomeId(homeId);
+                            }
+                            HDLLinkConfig.getInstance().setAuthBean(request.getAuth());
+
+                            //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
+                            Log.d("panlili", "璁よ瘉鎴愬姛----->localSecret= " + localSecret);
+                            if (TextUtils.isEmpty(localSecret) && TextUtils.isEmpty(gatewayId)) {
+                                //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�
+                                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
+                            } else {
+                                HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
+                                HDLLinkConfig.getInstance().setRequestBean(request.getAuth().getRequest());
+                                callBack.onSuccess("璁よ瘉鎴愬姛");
+
+//                                UploadDeviceAuth(request.getObjects().getDeviceMAC(), "0", "success", bean.getCode(), null);
+                            }
+                        } else if (bean.getCode().equals("14013")) {
+                            //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
+                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_));
+
+//                            UploadDeviceAuth(request.getObjects().getDeviceMAC(), "-1", "fail", bean.getCode(), null);
+                        } else if (bean.getCode().equals("124013")) {
+                            //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
+                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_124013_ERROR));
+
+                        } else if (bean.getCode().equals("124015")) {
+                            //璁よ瘉澶辫触锛岃澶囧巶瀹朵笉鍖归厤
+                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_124015_ERROR));
+
                         } else {
-                            HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
-                            callBack.onSuccess("璁よ瘉鎴愬姛");
+                            //璁よ瘉澶辫触锛岄敊璇爜锛�
+                            LogUtils.e("璁よ瘉澶辫触锛岄敊璇爜锛�" + bean.getCode());
+                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR));
+
+//                            UploadDeviceAuth(request.getObjects().getDeviceMAC(), "-1", "fail", bean.getCode(), null);
                         }
                     } else {
                         callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
@@ -219,6 +329,66 @@
             }
         });
     }
+
+    public void UploadDeviceAuth(String mac, String result, String message, String auth_code, HDLLinkCallBack callBack) {
+        HDLSocket.getInstance().UploadDeviceAuth(mac, result, message, auth_code, new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String msg) {
+                if (callBack != null) {
+                    callBack.onSuccess(msg);
+                }
+                LogUtils.i(TAG, "onSuccess: " + msg);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                if (callBack != null) {
+                    callBack.onError(e);
+                }
+                LogUtils.i(TAG, "onError: " + e.getMsg() + " " + e.getCode());
+            }
+        });
+    }
+
+
+    public void linkBroadcast(String oid, HDLLinkCallBack callBack) {
+        if (TextUtils.isEmpty(oid)) {
+            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
+        }
+
+        Gson gs = new Gson();
+        JsonArray jsonArray = new JsonArray();
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("oid", oid);
+        jsonArray.add(jsonObject);
+        String requestStr = gs.toJson(jsonArray);
+
+        String topic = String.format(TopicConstant.LINK_BROADCAST, HDLLinkConfig.getInstance().getDeviceInfoBean().getOID());
+        LinkRequest message = new LinkRequest(topic, requestStr);
+
+        String ip = IpUtils.getBroadcastAddress();
+
+        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
+            @Override
+            public void onSucceed(Object msg) {
+                if (callBack == null) return;
+                try {
+                    callBack.onSuccess("鏇存柊鎴愬姛");
+                    LogUtils.i(TAG, "onSucceed: msg= " + msg);
+                } catch (Exception e) {
+                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
+                }
+            }
+
+            @Override
+            public void onFailure() {
+                LogUtils.i(TAG, "onFailure: ");
+                if (callBack == null) return;
+                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
+            }
+        });
+    }
+
 
 //    /**
 //     * 鍙戦�佸叆缃戝強璁よ瘉璇锋眰
@@ -348,11 +518,12 @@
      * 閫氱敤鍙戦�佹寚浠�
      * 1绉掓病鍝嶅簲灏辫浠栭噸鏂板彂閫�,閲嶈瘯3娆�
      *
-     * @param topic    鍙戦�佹暟鎹�
-     * @param bodyStr  鍥炲鐨勪富棰�
-     * @param callBack 鍥炶皟
+     * @param topic     鍙戦�佹暟鎹�
+     * @param bodyStr   鍥炲鐨勪富棰�
+     * @param broadcast 鏄惁瑕佸箍鎾�
+     * @param callBack  鍥炶皟
      */
-    public void udpSendMsg(String topic, String bodyStr, 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));
@@ -369,7 +540,55 @@
 
         LinkRequest message = new LinkRequest(topic, bodyStr);
         String ip = HDLLinkConfig.getInstance().getIpAddress();
+        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);
+                    }
+
+                    @Override
+                    public void onFailure() {
+                        if (callBack == null) return;
+                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
+                    }
+                }
+        );
+    }
+
+    /**
+     * 閫氱敤鍙戦�佹寚浠�
+     * 鍙戦�佷竴娆�
+     *
+     * @param topic     鍙戦�佹暟鎹�
+     * @param bodyStr   鍥炲鐨勪富棰�
+     * @param broadcast 鏄惁瑕佸箍鎾�
+     * @param callBack  鍥炶皟
+     */
+    public void udpSendMsgOneTime(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));
+            }
+            return;
+        }
+
+        if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+            if (callBack != null) {
+                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
+            }
+            return;
+        }
+
+        LinkRequest message = new LinkRequest(topic, bodyStr);
+        String ip = HDLLinkConfig.getInstance().getIpAddress();
+        if (broadcast) {
+            ip = IpUtils.getBroadcastAddress();
+        }
+        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, "", 1, new HdlSocketHelper.HdlSocketListener() {
                     @Override
                     public void onSucceed(Object msg) {
                         if (callBack == null) return;
@@ -388,20 +607,24 @@
     /**
      * 閫氱敤鍙戦�佹寚浠� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
      *
-     * @param topic   鍙戦�佹暟鎹�
-     * @param bodyStr 鍥炲鐨勪富棰�
+     * @param topic     鍙戦�佹暟鎹�
+     * @param bodyStr   鍥炲鐨勪富棰�
+     * @param broadcast 鏄惁骞挎挱
      */
-    public void udpSendMsg(String topic, String bodyStr) {
+    public void udpSendMsg(String topic, String bodyStr, boolean broadcast) {
         if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
-            LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖");
+            LogUtils.e("鍙傛暟涓嶈兘涓虹┖锛屼笉鑳藉彂閫乁DP鏁版嵁");
             return;
         }
         if (!HDLLinkConfig.getInstance().checkIfCertified()) {
-            LogUtils.e("udpSendMsg", "鏈璇侊紝璇峰厛璁よ瘉");
+            LogUtils.e("鏈璇侊紝璇峰厛璁よ瘉鍐嶈皟鐢║DP鍙戦�佹柟娉�");
             return;
         }
         LinkRequest message = new LinkRequest(topic, bodyStr);
-        String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress();
+        String ip = HDLLinkConfig.getInstance().getIpAddress();
+        if (broadcast) {
+            ip = IpUtils.getBroadcastAddress();
+        }
         HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
     }
 
@@ -473,22 +696,25 @@
                             final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
                             }.getType());
                             GatewaySearchBean searchBean = response.getObjects();
+
                             if (searchBean != null && !TextUtils.isEmpty(searchBean.getGatewayId())) {
-                                if (searchBean.getGatewayId().contains(searchGatewayId)) {
+                                searchBean.setIp_address(linkResponse.getSource_ipAddress());
+                                if (searchBean.getGatewayId().contains(searchGatewayId) && !TextUtils.isEmpty(searchGatewayId)) {
                                     removeSearchGatewayEvent();//绉婚櫎鎼滅储缃戝叧鐩戝惉
                                     isSearchGatewaySuccess.set(true);//鎼滅储鎴愬姛鏍囪
                                     searchGatewayCount.set(11);//娆℃暟鏍囪
                                     HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧
-                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑
                                     if (mSearchGatewayCallBack != null) {
                                         mSearchGatewayCallBack.onSuccess(searchBean);
+                                        HDLSocket.getInstance().isBroadcast = true;
+                                        HDLSocket.getInstance().getTcp();
                                     }
                                 }
                             }
                         }
                     }
                 } catch (Exception e) {
-
+                    LogUtils.i("panlili锛�" + e.toString());
                 }
             }
         };
@@ -509,11 +735,15 @@
                             }.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);//璁剧疆褰撳墠缃戝叧
-                                    HDLLinkConfig.getInstance().setLocalEncrypt(gateway.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑
-                                    HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
+                                    if (!TextUtils.isEmpty(gateway.getIp_address())) {
+                                        HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
+                                        LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + HDLLinkConfig.getInstance().getIpAddress());
+                                    }
                                     //鏇存柊褰撳墠缃戝叧鐨勪俊鎭�
                                     HDLLinkConfig.getInstance().reSaveConfig();
                                 }
@@ -521,7 +751,7 @@
                         }
                     }
                 } catch (Exception e) {
-
+                    LogUtils.i("panlili锛�" + e.toString());
                 }
             }
         };
@@ -566,9 +796,12 @@
                 }
 
                 if (!isSearchGatewaySuccess.get()) {
-                    //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
-                    callBackSearchGatewayTimeout();
-                    LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+                    try {
+                        LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+                        //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
+                        callBackSearchGatewayTimeout();
+                    } catch (Exception e) {
+                    }
                 }
             }
         }).start();

--
Gitblit v1.8.0