package com.hdl.sdk.connect.bean;
|
|
import android.text.TextUtils;
|
|
import androidx.annotation.NonNull;
|
|
import com.hdl.sdk.common.utils.LogUtils;
|
import com.hdl.sdk.connect.config.HDLLinkConfig;
|
import com.hdl.sdk.common.utils.ByteUtils;
|
import com.hdl.sdk.connect.utils.AesUtil;
|
|
/**
|
* Created by Tong on 2021/9/29.
|
*/
|
public class LinkRequest {
|
private String topic;
|
private String data;
|
|
private int length;
|
|
public LinkRequest() {
|
}
|
|
public LinkRequest(String topic, String data) {
|
this.topic = topic;
|
setData(data);
|
}
|
|
public String getTopic() {
|
return topic;
|
}
|
|
public void setTopic(String topic) {
|
this.topic = topic;
|
}
|
|
public String getData() {
|
return data;
|
}
|
|
public void setData(String data) {
|
this.data = data;
|
if (!TextUtils.isEmpty(data)) {
|
setLength(getBytesLength(data));
|
} else {
|
setLength(0);
|
}
|
|
}
|
|
public int getLength() {
|
return length;
|
}
|
|
private void setLength(int length) {
|
this.length = length;
|
}
|
|
@NonNull
|
@Override
|
public String toString() {
|
return "Topic:" +
|
getTopic() +
|
"\r\n" +
|
"Length:" +
|
getLength() +
|
"\r\n\r\n" +
|
getData();
|
}
|
|
|
private int getBytesLength(String str) {
|
return ByteUtils.stringToBytes(str).length;
|
}
|
|
|
/**
|
* 获取发送数据byte
|
*
|
* @return
|
*/
|
public byte[] getSendBytes() {
|
try {
|
//判断是否需要加密
|
if (HDLLinkConfig.getInstance().ifNeedEncrypt(topic)) {
|
//需要加密
|
byte[] dataBytes = AesUtil.aesEncrypt(ByteUtils.stringToBytes(data), HDLLinkConfig.getInstance().getLocalSecret());
|
String headString = "Topic:" + getTopic() + "\r\n" + "Length:" + dataBytes.length + "\r\n" + "\r\n";
|
byte[] headBytes = headString.getBytes("utf-8");
|
byte[] sendBytes = ByteUtils.concatBytes(headBytes, dataBytes);
|
LogUtils.i("发送数据:\r\n" + headString + "\r\n" + data);
|
return sendBytes;
|
|
} else {
|
String sendString = this.toString();
|
LogUtils.i("发送数据:\r\n" + sendString);
|
return sendString.getBytes("utf-8");
|
}
|
|
} catch (Exception e) {
|
return new byte[]{};
|
}
|
}
|
}
|