-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_data.go
171 lines (141 loc) · 4.13 KB
/
api_data.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
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
// Copyright 2021-22 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teonet api data module
package teonet
import (
"bytes"
"encoding/binary"
"github.com/kirill-scherba/bslice"
)
// APIData is teonet API interface builder data
type APIData struct {
name string
short string
long string
usage string
ret string
cmd byte
connectMode APIconnectMode
answerMode APIanswerMode
reader func(c *Channel, p *Packet, data []byte) bool
reader2 func(data []byte, answer func(data []byte)) bool
bslice.ByteSlice
}
// SetName set APIData name
func (a *APIData) SetName(name string) *APIData {
a.name = name
return a
}
// SetShort set APIData short name
func (a *APIData) SetShort(short string) *APIData {
a.short = short
return a
}
// SetLong set APIData long name (like description)
func (a *APIData) SetLong(long string) *APIData {
a.long = long
return a
}
// SetUsage set APIData usage text
func (a *APIData) SetUsage(usage string) *APIData {
a.usage = usage
return a
}
// SetReturn set APIData return description
func (a *APIData) SetReturn(ret string) *APIData {
a.ret = ret
return a
}
// SetCmd set APIData command number
func (a *APIData) SetCmd(cmd byte) *APIData {
a.cmd = cmd
return a
}
// SetConnectMode set APIData connect mode ( server|client|client&server )
func (a *APIData) SetConnectMode(connectMode APIconnectMode) *APIData {
a.connectMode = connectMode
return a
}
// SetAnswerMode set APIData answer mode (data|cmd|packet|none)
func (a *APIData) SetAnswerMode(answerMode APIanswerMode) *APIData {
a.answerMode = answerMode
return a
}
// SetReader set APIData reader
func (a *APIData) SetReader(reader func(c *Channel, p *Packet, data []byte) bool) *APIData {
a.reader = reader
return a
}
// SetReader2 set APIData second reader
func (a *APIData) SetReader2(reader2 func(data []byte, answer func(data []byte)) bool) *APIData {
a.reader2 = reader2
return a
}
// Name return APIData name
func (a APIData) Name() string { return a.name }
// Short return APIData short name
func (a APIData) Short() string { return a.short }
// Long return APIData long name
func (a APIData) Long() string { return a.long }
// Usage return APIData usage text
func (a APIData) Usage() string { return a.usage }
// Ret return APIData return mode
func (a APIData) Ret() string { return a.ret }
// Cmd return APIData cmd number
func (a APIData) Cmd() byte { return a.cmd }
// ExecMode return APIData exec mode
func (a APIData) ExecMode() (APIconnectMode, APIanswerMode) {
return a.connectMode, a.answerMode
}
// Reader return APIData reader
func (a APIData) Reader(c *Channel, p *Packet, data []byte) bool {
return a.reader(c, p, data)
}
// Reader2 return APIData second reader
func (a APIData) Reader2(data []byte, answer func(data []byte)) bool {
return a.reader2(data, answer)
}
// MarshalBinary binary marshal APIData
func (a APIData) MarshalBinary() (data []byte, err error) {
buf := new(bytes.Buffer)
a.WriteSlice(buf, []byte(a.name))
a.WriteSlice(buf, []byte(a.short))
a.WriteSlice(buf, []byte(a.long))
a.WriteSlice(buf, []byte(a.usage))
a.WriteSlice(buf, []byte(a.ret))
binary.Write(buf, binary.LittleEndian, a.cmd)
binary.Write(buf, binary.LittleEndian, a.connectMode)
binary.Write(buf, binary.LittleEndian, a.answerMode)
data = buf.Bytes()
return
}
// UnmarshalBinary binary unmarshal APIData
func (a *APIData) UnmarshalBinary(buf *bytes.Buffer /*data []byte*/) (err error) {
// var buf = bytes.NewBuffer(data)
if a.name, err = a.ReadString(buf); err != nil {
return
}
if a.short, err = a.ReadString(buf); err != nil {
return
}
if a.long, err = a.ReadString(buf); err != nil {
return
}
if a.usage, err = a.ReadString(buf); err != nil {
return
}
if a.ret, err = a.ReadString(buf); err != nil {
return
}
if err = binary.Read(buf, binary.LittleEndian, &a.cmd); err != nil {
return
}
if err = binary.Read(buf, binary.LittleEndian, &a.connectMode); err != nil {
return
}
if err = binary.Read(buf, binary.LittleEndian, &a.answerMode); err != nil {
return
}
return
}