wjc
2023-06-28 99bc815e07e39354f51421b77f4012ffd35594d8
HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/bean/LinkRequest.java
New file
@@ -0,0 +1,150 @@
package com.hdl.sdk.link.core.bean;
import android.text.TextUtils;
import com.hdl.sdk.link.common.utils.ByteUtils;
/**
 * Created by Tong on 2021/9/29.
 */
public class LinkRequest {
    private String topic;
    private String replyTopic;
    protected byte []data;
    private int length=0;
    private boolean encrypt;
    public String getCloudTopic() {
        if(TextUtils.isEmpty(cloudTopic)){
            return topic;
        }
        return cloudTopic;
    }
    /**
     * 平台topic
     * @param cloudTopic
     */
    public void setCloudTopic(String cloudTopic) {
        this.cloudTopic = cloudTopic;
    }
    private String cloudTopic;
    public LinkRequest(String topic,String replyTopic, String data, boolean encrypt) {
        this.topic = topic;
        this.replyTopic=replyTopic;
        setData(data);
        this.encrypt = encrypt;
    }
    public LinkRequest(String topic, String data, boolean encrypt) {
        this(topic,null,data,encrypt);
    }
    public LinkRequest(String topic, byte []data, boolean encrypt) {
        this.topic = topic;
        setData(data);
        this.encrypt = encrypt;
    }
    protected LinkRequest(){}
    public String getTopic() {
        return topic;
    }
    public void setTopic(String topic) {
        this.topic = topic;
    }
    public byte[] getData() {
        return data;
    }
    public void setData(String data) {
        if (!TextUtils.isEmpty(data)) {
            this.data = ByteUtils.stringToBytes(data);
            setLength(this.data.length);
        }
    }
    public void setData(byte []data) {
        this.data = data;
        if(data!=null){
            setLength(data.length);
        }
    }
    public int getLength() {
        return length;
    }
    private void setLength(int length) {
        this.length = length;
    }
    /**
     * 加密标识
     *
     * @return
     */
    public boolean isEncrypt() {
        return encrypt;
    }
    /**
     * 加密标识
     *
     * @param encrypt
     */
    public void setEncrypt(boolean encrypt) {
        this.encrypt = encrypt;
    }
    public String getReplyTopic() {
        if(TextUtils.isEmpty(replyTopic)){
            return topic+"_reply";
        }
        return replyTopic;
    }
    public void setReplyTopic(String replyTopic) {
        this.replyTopic = replyTopic;
    }
    public byte [] getSendBytes() {
        try {
            String header = "Topic:" +
                    getTopic() +
                    "\r\n" +
                    "Length:" +
                    getLength() +
                    "\r\n\r\n";
            return ByteUtils.concatBytes(header.getBytes("utf-8"), getData());
        } catch (Exception e) {
            return new byte[]{};
        }
    }
    public byte [] getCloudSendBytes() {
        try {
            //link透传命令,内容需要包含主题,长度
            if(getCloudTopic().contains("/native/a/down/slaveoid/")){
                return getSendBytes();
            }else {
                return getData();
            }
        } catch (Exception e) {
            return new byte[]{};
        }
    }
    private int getBytesLength(String str) {
        return ByteUtils.stringToBytes(str).length;
    }
}