From 739d5bad0d24c5f4913f8d8e09292b5b98991fac Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期五, 10 十二月 2021 16:59:58 +0800
Subject: [PATCH] 2021-12-10 1.开启UDP心跳

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java |   93 +++++++++++++++++++++++++++++++---------------
 1 files changed, 62 insertions(+), 31 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 820d1a7..46757ec 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
@@ -99,7 +99,9 @@
         pipeLine.add(new LinkMessageDecoder());
         pipeLine.add(new LinkMessageEncoder());
         options.setHandleMessage(pipeLine);
-        options.setEnabledHeartbeat(false);
+        options.setHeartbeatTimeInterval(10*1000L);
+        options.setHeartbeatData("UDP");
+        options.setEnabledHeartbeat(true);//鏄惁寮�鍚績璺冲寘鍙戦�佹娴�
         return options;
     }
 
@@ -170,20 +172,31 @@
             @Override
             public void onSucceed(Object msg) {
                 if (callBack == null) return;
-                AuthenticateResponse bean = getAuthenticateResponseBean(msg);
-                if (bean != null) {
-                    String localSecret = bean.getAuth().getLocalSecret();
-                    String gatewayId = bean.getObjects().getGatewayID();
-                    String ipAddress = bean.getObjects().getIPAddress();
-                    //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
-                    if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
-                        //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�
-                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
+                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));
+                        } else {
+                            HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
+                            callBack.onSuccess("璁よ瘉鎴愬姛");
+                        }
                     } else {
-                        HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
-                        callBack.onSuccess("璁よ瘉鎴愬姛");
+                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                     }
-                } else {
+                } catch (Exception e) {
                     callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                 }
             }
@@ -285,8 +298,6 @@
     }
 
 
-
-
     /**
      * 璁惧鎺у埗
      */
@@ -350,6 +361,21 @@
         );
     }
 
+    /**
+     * 閫氱敤鍙戦�佹寚浠� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
+     *
+     * @param topic   鍙戦�佹暟鎹�
+     * @param bodyStr 鍥炲鐨勪富棰�
+     */
+    public void udpSendMsg(String topic, String bodyStr) {
+        if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
+            LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖");
+            return;
+        }
+        LinkRequest message = new LinkRequest(topic, bodyStr);
+        String ip = IpUtils.getBroadcastAddress();
+        getUdpBoot(ip).sendMsg(message.getSendBytes());
+    }
 
     private GatewaySearchBean getGatewaySearchBean(Object msg) {
         GatewaySearchBean searchBean = null;
@@ -399,13 +425,15 @@
     /**
      * 缃戝叧鎼滅储鐩稿叧
      */
-    private static final int MAX_SEARCH_COUNT  = 10;//鎬诲叡鎼滅储娴嬭瘯
-    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);;
+    private static final int MAX_SEARCH_COUNT = 10;//鎬诲叡鎼滅储娴嬭瘯
+    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);
+    ;
     private final AtomicBoolean isSearchGatewaySuccess = new AtomicBoolean(true);
     private String searchGatewayId = "";
     private SearchGatewayCallBack mSearchGatewayCallBack;
-    private void initSearchGatewayEvent(){
-        LogUtils.i("鎼滅储缃戝叧--","initSearchGatewayEvent");
+
+    private void initSearchGatewayEvent() {
+        LogUtils.i("鎼滅储缃戝叧--", "initSearchGatewayEvent");
         searchGatewayEvent = new EventListener() {
             @Override
             public void onMessage(Object msg) {
@@ -413,6 +441,7 @@
                     if (msg instanceof LinkResponse) {
                         LinkResponse linkResponse = (LinkResponse) msg;
                         String data = linkResponse.getData();
+                        LogUtils.i("鎼滅储鍒扮綉鍏�->" + data);
                         if (!TextUtils.isEmpty(data)) {
                             final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
                             }.getType());
@@ -422,13 +451,14 @@
                                     removeSearchGatewayEvent();//绉婚櫎鎼滅储缃戝叧鐩戝惉
                                     isSearchGatewaySuccess.set(true);//鎼滅储鎴愬姛鏍囪
                                     searchGatewayCount.set(11);//娆℃暟鏍囪
-                                    if(mSearchGatewayCallBack != null){
+                                    HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧
+                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑
+                                    if (mSearchGatewayCallBack != null) {
                                         mSearchGatewayCallBack.onSuccess(searchBean);
                                     }
                                 }
                             }
                         }
-
                     }
                 } catch (Exception e) {
 
@@ -461,11 +491,11 @@
         new Thread(new Runnable() {
             @Override
             public void run() {
-                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get()) ) {
+                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
                     try {
                         //鎼滅储缃戝叧
                         searchGatewayCount.set(searchGatewayCount.get() + 1);
-                        LogUtils.i("鎼滅储缃戝叧--","鎼滅储缃戝叧绗�"+searchGatewayCount.get()+"娆�");
+                        LogUtils.i("鎼滅储缃戝叧--", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
                         getUdpBoot(ip).sendMsg(message.getSendBytes());
                         Thread.sleep(1000L);
                     } catch (InterruptedException e) {
@@ -473,10 +503,10 @@
                     }
                 }
 
-                if(!isSearchGatewaySuccess.get()){
+                if (!isSearchGatewaySuccess.get()) {
                     //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
                     callBackSearchGatewayTimeout();
-                    LogUtils.e("鎼滅储缃戝叧--","鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+                    LogUtils.e("鎼滅储缃戝叧--", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
                 }
             }
         }).start();
@@ -486,27 +516,28 @@
     /**
      * 娉ㄥ唽鎼滅储缃戝叧鐩戝惉
      */
-    private void registerSearchGatewayEvent(){
-        LogUtils.i("鎼滅储缃戝叧--","娉ㄥ唽鎼滅储缃戝叧鐩戝惉");
+    private void registerSearchGatewayEvent() {
+        LogUtils.i("鎼滅储缃戝叧--", "娉ㄥ唽鎼滅储缃戝叧鐩戝惉");
         EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
     }
+
     /**
      * 绉婚櫎鎼滅储缃戝叧鐩戝惉
      */
-    private void removeSearchGatewayEvent(){
-        LogUtils.i("鎼滅储缃戝叧--","绉婚櫎鎼滅储缃戝叧鐩戝惉");
+    private void removeSearchGatewayEvent() {
+        LogUtils.i("鎼滅储缃戝叧--", "绉婚櫎鎼滅储缃戝叧鐩戝惉");
         EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
     }
 
     /**
      * 鍥炶皟鎼滅储缃戝叧瓒呮椂
      */
-    private void callBackSearchGatewayTimeout(){
+    private void callBackSearchGatewayTimeout() {
         removeSearchGatewayEvent();
         ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
             @Override
             public void run() {
-                if(mSearchGatewayCallBack != null){
+                if (mSearchGatewayCallBack != null) {
                     mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
                 }
             }

--
Gitblit v1.8.0