| | |
| | |
|
| | | import androidx.annotation.NonNull;
|
| | |
|
| | | 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 void setData(String data) {
|
| | | this.data = data;
|
| | | if (!TextUtils.isEmpty(data)) {
|
| | | setLength(data.length());
|
| | | setLength(getBytesLength(data));
|
| | | } else {
|
| | | setLength(0);
|
| | | }
|
| | |
| | | "\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());
|
| | | // byte[] dataBytes = AESUtils.encryptAES(stringToBytes(data), AuthenticateConfig.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);
|
| | | return sendBytes;
|
| | | } else {
|
| | | return this.toString().getBytes("utf-8");
|
| | | }
|
| | |
|
| | | } catch (Exception e) {
|
| | | return new byte[]{};
|
| | | }
|
| | | }
|
| | | }
|