111
k94314517
2025-02-28 04dba6a17f836b5fbdf0eedff8a129c6785fd8a2
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package org.yzh.protocol.basics;
 
import io.github.yezhihao.netmc.core.model.Message;
import io.github.yezhihao.netmc.session.Session;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.util.ToStringBuilder;
import io.netty.buffer.ByteBuf;
import org.yzh.protocol.commons.MessageId;
 
import java.beans.Transient;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class JTMessage implements Message {
 
    @Field(length = 2, desc = "消息ID")
    protected int messageId;
    @Field(length = 2, desc = "消息体属性")
    protected int properties;
    @Field(length = 1, desc = "协议版本号", version = 1)
    protected int protocolVersion;
    @Field(length = 6, charset = "BCD", desc = "终端手机号", version = {-1, 0})
    @Field(length = 10, charset = "BCD", desc = "终端手机号", version = 1)
    protected String clientId;
    @Field(length = 2, desc = "流水号")
    protected int serialNo;
    @Field(length = 2, desc = "消息包总数")
    protected Integer packageTotal;
    @Field(length = 2, desc = "包序号")
    protected Integer packageNo;
    /** bcc校验 */
    protected boolean verified = true;
 
    protected transient Session session;
 
    protected transient ByteBuf payload;
 
 
    public JTMessage() {
    }
 
    public JTMessage(int messageId) {
        this.messageId = messageId;
    }
 
    public JTMessage copyBy(JTMessage that) {
        this.setClientId(that.getClientId());
        this.setProtocolVersion(that.getProtocolVersion());
        this.setVersion(that.isVersion());
        return this;
    }
 
    public JTMessage messageId(int messageId) {
        this.messageId = messageId;
        return this;
    }
 
    public int getMessageId() {
        return messageId;
    }
 
    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }
 
    public int getProperties() {
        return properties;
    }
 
    public void setProperties(int properties) {
        this.properties = properties;
    }
 
    public int getProtocolVersion() {
        return protocolVersion;
    }
 
    public void setProtocolVersion(int protocolVersion) {
        this.protocolVersion = protocolVersion;
    }
 
    public String getClientId() {
        return clientId;
    }
 
    public void setClientId(String clientId) {
        this.clientId = clientId;
    }
 
    public int getSerialNo() {
        return serialNo;
    }
 
    public void setSerialNo(int serialNo) {
        this.serialNo = serialNo;
    }
 
    public Integer getPackageTotal() {
        if (isSubpackage())
            return packageTotal;
        return null;
    }
 
    public void setPackageTotal(Integer packageTotal) {
        this.packageTotal = packageTotal;
    }
 
    public Integer getPackageNo() {
        if (isSubpackage())
            return packageNo;
        return null;
    }
 
    public void setPackageNo(Integer packageNo) {
        this.packageNo = packageNo;
    }
 
    public boolean isVerified() {
        return verified;
    }
 
    public void setVerified(boolean verified) {
        this.verified = verified;
    }
 
    @Transient
    public Session getSession() {
        return session;
    }
 
    public void setSession(Session session) {
        this.session = session;
    }
 
    @Transient
    public ByteBuf getPayload() {
        return payload;
    }
 
    public void setPayload(ByteBuf payload) {
        this.payload = payload;
    }
 
    public int reflectMessageId() {
        if (messageId != 0)
            return messageId;
        return reflectMessageId(this.getClass());
    }
 
    public static int reflectMessageId(Class<?> clazz) {
        io.github.yezhihao.protostar.annotation.Message messageType = clazz.getAnnotation(io.github.yezhihao.protostar.annotation.Message.class);
        if (messageType != null && messageType.value().length > 0)
            return messageType.value()[0];
        return 0;
    }
 
    public boolean transform() {
        return true;
    }
 
    public boolean noBuffer() {
        return true;
    }
 
    private static final int BODY_LENGTH = 0b0000_0011_1111_1111;
    private static final int ENCRYPTION = 0b00011_100_0000_0000;
    private static final int SUBPACKAGE = 0b0010_0000_0000_0000;
    private static final int VERSION = 0b0100_0000_0000_0000;
    private static final int RESERVED = 0b1000_0000_0000_0000;
 
    /** 消息体长度 */
    public int getBodyLength() {
        return this.properties & BODY_LENGTH;
    }
 
    public void setBodyLength(int bodyLength) {
        this.properties ^= (properties & BODY_LENGTH);
        this.properties |= bodyLength;
    }
 
    /** 加密方式 */
    public int getEncryption() {
        return (properties & ENCRYPTION) >> 10;
    }
 
    public void setEncryption(int encryption) {
        this.properties ^= (properties & ENCRYPTION);
        this.properties |= (encryption << 10);
    }
 
    /** 是否分包 */
    public boolean isSubpackage() {
        return (properties & SUBPACKAGE) == SUBPACKAGE;
    }
 
    public void setSubpackage(boolean subpackage) {
        if (subpackage)
            this.properties |= SUBPACKAGE;
        else
            this.properties ^= (properties & SUBPACKAGE);
    }
 
    /** 是否有版本 */
    public boolean isVersion() {
        return (properties & VERSION) == VERSION;
    }
 
    public void setVersion(boolean version) {
        if (version)
            this.properties |= VERSION;
        else
            this.properties ^= (properties & VERSION);
    }
 
    /** 保留位 */
    public boolean isReserved() {
        return (properties & RESERVED) == RESERVED;
    }
 
    public void setReserved(boolean reserved) {
        if (reserved)
            this.properties |= RESERVED;
        else
            this.properties ^= (properties & RESERVED);
    }
 
    protected StringBuilder toStringHead() {
        final StringBuilder sb = new StringBuilder(768);
        sb.append(MessageId.getName(messageId));
        sb.append('[');
        sb.append("cid=").append(clientId);
        sb.append(",msgId=").append(messageId);
        sb.append(",version=").append(protocolVersion);
        sb.append(",serialNo=").append(serialNo);
        sb.append(",props=").append(properties);
        sb.append(",verified=").append(verified);
        if (isSubpackage()) {
            sb.append(",pt=").append(packageTotal);
            sb.append(",pn=").append(packageNo);
        }
        sb.append(']');
        sb.append(',');
        return sb;
    }
 
    @Override
    public String toString() {
        String result = ToStringBuilder.toString(toStringHead(), this, false, "messageId", "clientId", "protocolVersion", "serialNo", "properties", "packageTotal", "packageNo");
        return result;
    }
}