562935844@qq.com
2022-12-28 30e96788f05f5ce28b58d991051c79e9c952c824
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
@@ -1,20 +1,14 @@
package com.hdl.sdk.connect.protocol;
import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.common.event.EventDispatcher;
import com.hdl.sdk.common.utils.ByteUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.config.HDLLinkConfig;
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;
@@ -111,7 +105,7 @@
            }
            list.clear();
            for(int i=0;i<tempList.size();i++){
            for (int i = 0; i < tempList.size(); i++) {
                list.add(tempList.get(i));
            }
        }
@@ -119,7 +113,7 @@
    @Override
    protected synchronized 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));
            //如果多条命令打包在一条数据中,都需要处理完
@@ -157,6 +151,7 @@
                    bytes.add(recevieBytes[i]);
                }
                LinkResponse response = new LinkResponse();
                response.setSource_ipAddress(ipaddress);
                response.setTopic(topic);
                if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) {
                    //需要解密
@@ -165,11 +160,13 @@
                        body = bodyBytes;
                    } else {
                        try {
                            LogUtils.e("解密失败,数据内容是:\r\n");
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                                LogUtils.e(Base64.getEncoder().encodeToString(body));
                            else {
                                LogUtils.e(new String(body, "utf-8"));
                            //之前的版本这块是明文的
                            if (!topic.contains("heartbeat_reply")) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                                    LogUtils.e("解密失败,数据内容是:\r\n" + Base64.getEncoder().encodeToString(body));
                                else {
                                    LogUtils.e("解密失败,数据内容是:\r\n" + new String(body, "utf-8"));
                                }
                            }
                        } catch (Exception e) {
                        }
@@ -178,9 +175,10 @@
                String bodyString = new String(body, "utf-8");
                response.setData(bodyString);
                LogUtils.i( "接收到数据:" + response.getTopic() + "\r\n" + response.getData());
                LogUtils.i("接收到数据:" + response.getTopic() + "\r\n" + response.getData());
                //非正常数据,返回
                if (!(bodyString.startsWith("{") || bodyString.startsWith("["))) {
                if (!((bodyString.startsWith("{") && bodyString.endsWith("}"))
                        || (bodyString.startsWith("[") && bodyString.endsWith("]")))) {
                    continue;
                }
                //解析完成,topic发送一次