| | |
| | | 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.HDLLink;
|
| | | import com.hdl.sdk.connect.bean.LinkResponse;
|
| | | import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
|
| | | import com.hdl.sdk.connect.bean.response.DeviceDeleteResponse;
|
| | | 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.List;
|
| | |
|
| | | import android.util.Base64;
|
| | |
|
| | | import kotlin.ParameterName;
|
| | |
|
| | | /**
|
| | | * Created by Tong on 2021/9/22.
|
| | |
| | | }
|
| | |
|
| | | 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 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));
|
| | | //如果多条命令打包在一条数据中,都需要处理完
|
| | |
| | | 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", "解密 主题:"+response.getTopic()+ " body: "+response.getData());
|
| | | body = bodyBytes;
|
| | | } else {
|
| | | //解密失败,返回原数据
|
| | | response.setData(new String(body, "utf-8"));
|
| | | try {
|
| | | //之前的版本这块是明文的
|
| | | if (!topic.contains("heartbeat_reply")) {
|
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
| | | LogUtils.e("解密失败,数据内容是:\r\n" + Base64.encodeToString(body, Base64.NO_WRAP));
|
| | | else {
|
| | | LogUtils.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 updateLocalSecret = String.format(TopicConstant.LINK_BROADCAST, HDLLinkConfig.getInstance().getGatewayId());
|
| | | String deleteNetwork = "";
|
| | | if (HDLLinkConfig.getInstance().getDeviceInfoBean() != null) {
|
| | | deleteNetwork = String.format(TopicConstant.DELETE_NOTIFY, HDLLinkConfig.getInstance().getDeviceInfoBean().getOID());
|
| | | }
|
| | |
|
| | | 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(updateLocalSecret) || response.getTopic().equals(TopicConstant.LINK_BROADCAST)) {
|
| | | try {
|
| | | DeviceInfoResponse deviceInfoResponse = GsonConvert.getGson().fromJson(response.getData(), new TypeToken<DeviceInfoResponse>() {
|
| | | }.getType());
|
| | |
|
| | | if (!TextUtils.isEmpty(deviceInfoResponse.getObjects().getLocalSecret())) {
|
| | | byte[] baseBytes = Base64.decode(deviceInfoResponse.getObjects().getLocalSecret(), Base64.NO_WRAP);
|
| | | 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", "更新密钥----->localSecret= " + localSecret);
|
| | | HDLLinkConfig.getInstance().setLocalSecret(localSecret);
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | LogUtils.i("LinkMessageDecoder.java:getLocalSecret----->e= " + e.getMessage());
|
| | | }
|
| | | } else if (response.getTopic().equals(deleteNetwork)) {
|
| | | try {
|
| | | DeviceDeleteResponse deviceDeleteResponse = GsonConvert.getGson().fromJson(response.getData(), new TypeToken<DeviceDeleteResponse>() {
|
| | | }.getType());
|
| | | HDLSocket.getInstance().deleteNetwork(deviceDeleteResponse.getObjects().get(0).getOID(), new HDLLinkCallBack() {
|
| | | @Override
|
| | | public void onSuccess(String msg) {
|
| | | LogUtils.i("deleteNetwork onSucceed");
|
| | | if (HDLLink.getInstance().listener != null) {
|
| | | HDLLink.getInstance().listener.onSuccess(msg);
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void onError(HDLLinkException e) {
|
| | | LogUtils.i("deleteNetwork onError");
|
| | | if (HDLLink.getInstance().listener != null) {
|
| | | HDLLink.getInstance().listener.onFailure();
|
| | | }
|
| | | }
|
| | | });
|
| | | } catch (Exception e) {
|
| | | LogUtils.i("LinkMessageDecoder.java:deleteNetwork----->e= " + e.getMessage());
|
| | | }
|
| | | }
|
| | |
|
| | | //非正常数据,返回
|
| | | if (!((bodyString.startsWith("{") && bodyString.endsWith("}"))
|
| | | || (bodyString.startsWith("[") && bodyString.endsWith("]")))) {
|
| | | continue;
|
| | | }
|
| | | //解析完成,topic发送一次
|
| | | EventDispatcher.getInstance().post(response.getTopic(), response);
|
| | | }
|
| | | return null;
|
| | |
|
| | | // //解析流
|
| | | // byte[] data = (byte[]) msg;
|
| | | // bytes.addAll(ByteUtils.toByteList(data));
|
| | | //
|
| | | // byte[] byteArray = ByteUtils.toByteArray(bytes);
|
| | | // int headIndex = ByteUtils.getByteIndexOf(byteArray, head);
|
| | | // if (headIndex > 0) {
|
| | | // //移动到head 开始位置
|
| | | // 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;
|
| | | //
|
| | | // //解析头部
|
| | | // 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", "解密 主题:"+response.getTopic()+ " body: "+response.getData());
|
| | | // } else {
|
| | | // //解密失败,返回原数据
|
| | | // 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);
|
| | | // }
|
| | | // }
|
| | | // //解析完成,topic发送一次
|
| | | // EventDispatcher.getInstance().post(response.getTopic(), response);
|
| | | // return response;
|
| | | // }
|
| | | // } else if (bodyLength == 0) {
|
| | | // //body为空
|
| | | // return response;
|
| | | // }
|
| | |
|
| | | }
|
| | | return null;
|