-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.go
More file actions
195 lines (178 loc) · 4.64 KB
/
parameters.go
File metadata and controls
195 lines (178 loc) · 4.64 KB
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
package deviceparameters
import "fmt"
import "errors"
import "encoding/hex"
import "encoding/binary"
import "bytes"
import "strconv"
type DeviceParameterType uint8
const (
DP_TYPE_RAW DeviceParameterType = 0x00
DP_TYPE_UINT8 DeviceParameterType = 0x01
DP_TYPE_UINT16 DeviceParameterType = 0x02
DP_TYPE_UINT32 DeviceParameterType = 0x04
DP_TYPE_UINT64 DeviceParameterType = 0x08
DP_TYPE_STRING DeviceParameterType = 0x80
DP_TYPE_INT8 DeviceParameterType = 0x81
DP_TYPE_INT16 DeviceParameterType = 0x82
DP_TYPE_INT32 DeviceParameterType = 0x84
DP_TYPE_INT64 DeviceParameterType = 0x88
DP_TYPE_NIL DeviceParameterType = 0xFF
)
var DeviceParameterTypeToString = map[DeviceParameterType]string{
DP_TYPE_RAW: "raw",
DP_TYPE_UINT8: "u8",
DP_TYPE_UINT16: "u16",
DP_TYPE_UINT32: "u32",
DP_TYPE_UINT64: "u64",
DP_TYPE_STRING: "str",
DP_TYPE_INT8: "i8",
DP_TYPE_INT16: "i16",
DP_TYPE_INT32: "i32",
DP_TYPE_INT64: "i64",
DP_TYPE_NIL: "nil",
}
var DeviceParameterStringToType = map[string]DeviceParameterType{}
func init() {
for k, v := range DeviceParameterTypeToString {
DeviceParameterStringToType[v] = k
}
}
func (dpt DeviceParameterType) String() string {
return DeviceParameterTypeToString[dpt]
}
func ParseDeviceParameterType(name string) (DeviceParameterType, error) {
v, ok := DeviceParameterStringToType[name]
if ok {
return v, nil
}
return DP_TYPE_NIL, errors.New(fmt.Sprintf("%s is not a valid parameter type!", name))
}
func ParseParameterValue(tpt DeviceParameterType, tval string) ([]byte, error) {
var value []byte
var err error
var t interface{}
switch tpt {
case DP_TYPE_RAW:
return hex.DecodeString(tval)
case DP_TYPE_STRING:
return []byte(tval), nil
case DP_TYPE_NIL:
return nil, nil
case DP_TYPE_UINT8:
if v, err := strconv.ParseUint(tval, 10, 8); err == nil {
t = uint8(v)
} else {
return nil, err
}
case DP_TYPE_UINT16:
if v, err := strconv.ParseUint(tval, 10, 16); err == nil {
t = uint16(v)
} else {
return nil, err
}
case DP_TYPE_UINT32:
if v, err := strconv.ParseUint(tval, 10, 32); err == nil {
t = uint32(v)
} else {
return nil, err
}
case DP_TYPE_UINT64:
if v, err := strconv.ParseUint(tval, 10, 64); err == nil {
t = uint64(v)
} else {
return nil, err
}
case DP_TYPE_INT8:
if v, err := strconv.ParseInt(tval, 10, 8); err == nil {
t = int8(v)
} else {
return nil, err
}
case DP_TYPE_INT16:
if v, err := strconv.ParseInt(tval, 10, 16); err == nil {
t = int16(v)
} else {
return nil, err
}
case DP_TYPE_INT32:
if v, err := strconv.ParseInt(tval, 10, 32); err == nil {
t = int32(v)
} else {
return nil, err
}
case DP_TYPE_INT64:
if v, err := strconv.ParseInt(tval, 10, 64); err == nil {
t = int64(v)
} else {
return nil, err
}
}
if t != nil {
switch t := t.(type) {
case uint8, uint16, uint32, uint64, int8, int16, int32, int64:
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, t); err != nil {
return nil, err
}
value = buf.Bytes()
}
}
return value, err
}
func ParameterValueString(dpt DeviceParameterType, value []byte) (string, error) {
if dpt == DP_TYPE_NIL {
return fmt.Sprintf("%X", value), nil
} else if dpt == DP_TYPE_RAW {
return fmt.Sprintf("%X", value), nil
} else if dpt == DP_TYPE_STRING {
return string(value), nil
} else {
s := fmt.Sprintf("%v", value)
buf := bytes.NewBuffer(value)
if dpt == DP_TYPE_UINT8 {
var v uint8
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_UINT16 {
var v uint16
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_UINT32 {
var v uint32
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_UINT64 {
var v uint64
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_INT8 {
var v int8
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_INT16 {
var v int16
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_INT32 {
var v int32
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else if dpt == DP_TYPE_INT64 {
var v int64
if err := binary.Read(buf, binary.BigEndian, &v); err == nil {
s = fmt.Sprintf("%d", v)
}
} else {
return s, errors.New("Unrecognized parameter type!")
}
return s, nil
}
}