hxb
2022-02-17 af2cbc2a24b3724e7fc3e6b42e134b21a3144c75
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
@@ -1,8 +1,13 @@
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;
@@ -13,6 +18,7 @@
import com.hdl.sdk.socket.codec.ByteToMessageDecoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
/**
@@ -111,8 +117,9 @@
        }
    }
    @Override
    protected LinkResponse decoder(Object msg) throws Exception {
    protected synchronized LinkResponse decoder(Object msg) throws Exception {
        if (msg instanceof byte[]) {
            bytes.addAll(ByteUtils.toByteList((byte[]) msg));
            //如果多条命令打包在一条数据中,都需要处理完
@@ -154,19 +161,28 @@
                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", "解密 主题:"+response.getTopic()+ " body: "+response.getData());
                        body = bodyBytes;
                    } else {
                        //解密失败,返回原数据
                        response.setData(new String(body, "utf-8"));
                        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"));
                            }
                        } 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());
                //非正常数据,返回
                if (!(bodyString.startsWith("{") || bodyString.startsWith("["))) {
                    continue;
                }
                //解析完成,topic发送一次
                EventDispatcher.getInstance().post(response.getTopic(), response);
            }