From 86116427931cc724ae1e3105738a34e0619ca287 Mon Sep 17 00:00:00 2001
From: 562935844@qq.com
Date: 星期二, 04 四月 2023 16:12:47 +0800
Subject: [PATCH] 增加网关未上云密钥为空的问题处理

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java |  156 +++++++++++++++++++++++++++-------------------------
 1 files changed, 81 insertions(+), 75 deletions(-)

diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
index 834c242..e5eebc8 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
@@ -1,19 +1,35 @@
 package com.hdl.sdk.connect.protocol;
 
 
+import android.os.Build;
+import android.text.TextUtils;
 import android.util.Log;
 
-import com.hdl.sdk.common.utils.LogUtils;
-import com.hdl.sdk.connect.config.HDLLinkConfig;
+import androidx.annotation.RequiresApi;
+
+import com.google.gson.reflect.TypeToken;
+import com.hdl.sdk.common.config.TopicConstant;
 import com.hdl.sdk.common.event.EventDispatcher;
+import com.hdl.sdk.common.exception.HDLLinkException;
 import com.hdl.sdk.common.utils.ByteUtils;
+import com.hdl.sdk.common.utils.LogUtils;
+import com.hdl.sdk.common.utils.SPUtils;
+import com.hdl.sdk.common.utils.gson.GsonConvert;
 import com.hdl.sdk.connect.bean.LinkResponse;
+import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
+import com.hdl.sdk.connect.bean.response.DeviceInfoResponse;
+import com.hdl.sdk.connect.callback.HDLLinkCallBack;
+import com.hdl.sdk.connect.config.HDLLinkConfig;
+import com.hdl.sdk.connect.socket.HDLAuthSocket;
+import com.hdl.sdk.connect.socket.HDLSocket;
 import com.hdl.sdk.connect.utils.AesUtil;
-import com.hdl.sdk.connect.utils.ProtocolParse;
 import com.hdl.sdk.socket.codec.ByteToMessageDecoder;
 
 import java.util.ArrayList;
+import java.util.Base64;
 import java.util.List;
+
+import kotlin.ParameterName;
 
 /**
  * Created by Tong on 2021/9/22.
@@ -105,14 +121,16 @@
             }
 
             list.clear();
-            for(int i=0;i<tempList.size();i++){
+            for (int i = 0; i < tempList.size(); i++) {
                 list.add(tempList.get(i));
             }
         }
     }
 
+
+    @RequiresApi(api = Build.VERSION_CODES.O)
     @Override
-    protected LinkResponse decoder(Object msg) throws Exception {
+    protected synchronized LinkResponse decoder(Object msg, String ipaddress) throws Exception {
         if (msg instanceof byte[]) {
             bytes.addAll(ByteUtils.toByteList((byte[]) msg));
             //濡傛灉澶氭潯鍛戒护鎵撳寘鍦ㄤ竴鏉℃暟鎹腑锛岄兘闇�瑕佸鐞嗗畬
@@ -150,88 +168,76 @@
                     bytes.add(recevieBytes[i]);
                 }
                 LinkResponse response = new LinkResponse();
+                response.setSource_ipAddress(ipaddress);
                 response.setTopic(topic);
                 if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) {
                     //闇�瑕佽В瀵�
                     byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret());
-//                        byte[] bodyBytes = AESUtils.decryptAES(body,AuthenticateConfig.getInstance().getLocalSecret());
                     if (bodyBytes != null) {
-                        response.setData(new String(bodyBytes, "utf-8"));
-//                            LogUtils.i("TAG", "瑙e瘑 涓婚锛�"+response.getTopic()+ " body: "+response.getData());
+                        body = bodyBytes;
                     } else {
-                        //瑙e瘑澶辫触锛岃繑鍥炲師鏁版嵁
-                        response.setData(new String(body, "utf-8"));
+                        try {
+                            //涔嬪墠鐨勭増鏈繖鍧楁槸鏄庢枃鐨�
+                            if (!topic.contains("heartbeat_reply")) {
+                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+                                    LogUtils.e("瑙e瘑澶辫触锛屾暟鎹唴瀹规槸锛歕r\n" + Base64.getEncoder().encodeToString(body));
+                                else {
+                                    LogUtils.e("瑙e瘑澶辫触锛屾暟鎹唴瀹规槸锛歕r\n" + new String(body, "utf-8"));
+                                }
+                            }
+                        } catch (Exception e) {
+                        }
                     }
-
-                } else {
-                    response.setData(new String(body, "utf-8"));
                 }
-                LogUtils.i( "LinkMessageDecoder->decoder:" + response.getTopic() + "\r\n" + response.getData());
+
+                String bodyString = new String(body, "utf-8");
+                response.setData(bodyString);
+                LogUtils.i("鎺ユ敹鍒版暟鎹�:" + response.getTopic() + "\r\n" + response.getData());
+
+                String receivetopic = String.format(TopicConstant.LINK_BROADCAST, HDLLinkConfig.getInstance().getGatewayId());
+
+                if (response.getTopic().equals("/user/all/custom/gateway/search")) {
+                    HDLAuthSocket.getInstance().UploadGatewayInfo(new HDLLinkCallBack() {
+                        @Override
+                        public void onSuccess(String msg) {
+                            LogUtils.i("UploadGatewayInfo onSucceed");
+                        }
+
+                        @Override
+                        public void onError(HDLLinkException e) {
+                            LogUtils.i("UploadGatewayInfo onError");
+                        }
+                    });
+                } else if (response.getTopic().equals(receivetopic)) {
+                    try {
+                        DeviceInfoResponse deviceInfoResponse = GsonConvert.getGson().fromJson(response.getData(), new TypeToken<DeviceInfoResponse>() {
+                        }.getType());
+
+                        if (!TextUtils.isEmpty(deviceInfoResponse.getObjects().getLocalSecret())) {
+                            byte[] baseBytes = Base64.getDecoder().decode(deviceInfoResponse.getObjects().getLocalSecret());
+                            String mackey = "";
+                            if (!TextUtils.isEmpty(SPUtils.getString("auth_mackey_key", ""))) {
+                                mackey = SPUtils.getString("auth_mackey_key", "");
+                                byte[] bodyBytes = AesUtil.aesDecrypt(baseBytes, mackey.substring(mackey.length() - 16));
+                                String localSecret = new String(bodyBytes, "utf-8");
+                                Log.d("panlili", "LinkMessageDecoder.java:----->localSecret= " + localSecret);
+                                HDLLinkConfig.getInstance().setLocalSecret(localSecret);
+                            }
+                        }
+                    } catch (Exception e) {
+                        LogUtils.i("LinkMessageDecoder.java:getLocalSecret----->e= " + e.getMessage());
+                    }
+                }
+
+                //闈炴甯告暟鎹紝杩斿洖
+                if (!((bodyString.startsWith("{") && bodyString.endsWith("}"))
+                        || (bodyString.startsWith("[") && bodyString.endsWith("]")))) {
+                    continue;
+                }
                 //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆�
                 EventDispatcher.getInstance().post(response.getTopic(), response);
             }
             return null;
-
-//            //瑙f瀽娴�
-//            byte[] data = (byte[]) msg;
-//            bytes.addAll(ByteUtils.toByteList(data));
-//
-//            byte[] byteArray = ByteUtils.toByteArray(bytes);
-//            int headIndex = ByteUtils.getByteIndexOf(byteArray, head);
-//            if (headIndex > 0) {
-//                //绉诲姩鍒癶ead 寮�濮嬩綅缃�
-//                bytes.subList(0, headIndex).clear();
-//                byteArray = ByteUtils.toByteArray(bytes);
-//            }
-//
-//            int bodyIndex = ByteUtils.getByteIndexOf(byteArray, body);
-//            if (bodyIndex < 0) {
-//                //澶撮儴鏈幏鍙栧畬鎴�
-//                return null;
-//            }
-//            int bodyStartIndex = bodyIndex + body.length;
-//
-//            //瑙f瀽澶撮儴
-//            ProtocolParse parse = new ProtocolParse(byteArray);
-//            response.setTopic(parse.getTopic());
-//
-//            int bodyLength = parse.getLength();
-//            if (bodyLength > 0) {
-//                if (byteArray.length >= bodyLength + bodyStartIndex) {
-//                    byte[] body = ByteUtils.getRangeBytes(bytes, bodyStartIndex, bodyStartIndex + bodyLength);
-//
-//                    if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) {
-//                        //闇�瑕佽В瀵�
-//                        byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret());
-////                        byte[] bodyBytes = AESUtils.decryptAES(body,AuthenticateConfig.getInstance().getLocalSecret());
-//                        if (bodyBytes != null) {
-//                            response.setData(new String(bodyBytes, "utf-8"));
-////                            LogUtils.i("TAG", "瑙e瘑 涓婚锛�"+response.getTopic()+ " body: "+response.getData());
-//                        } else {
-//                            //瑙e瘑澶辫触锛岃繑鍥炲師鏁版嵁
-//                            response.setData(new String(body, "utf-8"));
-//                        }
-//
-//                    } else {
-//                        response.setData(new String(body, "utf-8"));
-//                    }
-//
-//                    if (byteArray.length >= bodyLength + bodyStartIndex) {
-//                        //淇濆瓨浣欑暀
-//                        byte[] remaining = ByteUtils.getRangeBytes(bytes, bodyStartIndex + bodyLength, byteArray.length);
-//                        bytes.clear();
-//                        for (byte b : remaining) {
-//                            bytes.add(b);
-//                        }
-//                    }
-//                    //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆�
-//                    EventDispatcher.getInstance().post(response.getTopic(), response);
-//                    return response;
-//                }
-//            } else if (bodyLength == 0) {
-//                //body涓虹┖
-//                return response;
-//            }
 
         }
         return null;

--
Gitblit v1.8.0