/
t808_0x8001.go
59 lines (48 loc) · 1.12 KB
/
t808_0x8001.go
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
package protocol
// 平台通用应答
type T808_0x8001 struct {
// 对应的终端消息的流水号
ReplyMsgSerialNo uint16
// 对应的终端消息的ID
ReplyMsgID MsgID
// 处理结果
Result Result
}
func (entity *T808_0x8001) MsgID() MsgID {
return MsgT808_0x8001
}
func (entity *T808_0x8001) Encode() ([]byte, error) {
writer := NewWriter()
// 写入流水号
writer.WriteUint16(entity.ReplyMsgSerialNo)
// 写入消息ID
writer.WriteUint16(uint16(entity.ReplyMsgID))
// 写入处理结果
writer.WriteByte(byte(entity.Result))
return writer.Bytes(), nil
}
func (entity *T808_0x8001) Decode(data []byte) (int, error) {
if len(data) < 5 {
return 0, ErrInvalidBody
}
reader := NewReader(data)
// 读取流水号
msgSerialNo, err := reader.ReadUint16()
if err != nil {
return 0, err
}
// 读取消息ID
msgID, err := reader.ReadUint16()
if err != nil {
return 0, err
}
// 读取处理结果
b, err := reader.ReadByte()
if err != nil {
return 0, err
}
entity.ReplyMsgID = MsgID(msgID)
entity.Result = Result(b)
entity.ReplyMsgSerialNo = msgSerialNo
return len(data) - reader.Len(), nil
}