wjc
2023-06-28 989b4cf5a84e898e9682f8d9723a8ba1ff20c23b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
    }
 
}