/
chairmsg.go
235 lines (202 loc) · 5.36 KB
/
chairmsg.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
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
package main
import (
"errors"
"fmt"
"math"
"github.com/ugorji/go/codec"
)
type ChairMessage map[string]interface{}
var SAMPLE_MESSAGE = map[string]interface{}{
"seqno": 0,
"seqmax": 255,
"timestamp_type": "rel",
"timestamp": 123,
"temperature": 1,
"humidity": 1,
"occupancy": false,
"bottom_heat": 0,
"bottom_fan": 0,
"back_heat": 0,
"back_fan": 0,
}
func NewChairMessage() ChairMessage {
return make(map[string]interface{})
}
func NewChairMessageFrom(dec *codec.Decoder) (cm ChairMessage, err error) {
cm = NewChairMessage()
err = cm.DecodeFrom(dec)
return
}
func (cm ChairMessage) DecodeFrom(dec *codec.Decoder) error {
return dec.Decode(&cm)
}
func (cm ChairMessage) SanityCheck() error {
var seqmax uint64
var val interface{}
var ok bool
/* Check required fields. */
if val, ok = cm["seqmax"]; ok {
if seqmax, ok = val.(uint64); !ok {
return errors.New(fmt.Sprintf("required field seqmax is not a uint64: %v", val))
}
} else {
return errors.New("missing required field seqmax")
}
if _, ok = cm["seqno"]; ok {
if seqno, ok := val.(uint64); ok {
if seqno >= seqmax {
return errors.New(fmt.Sprintf("seqno is not less than seqmax: %v >= %v", seqno, seqmax))
}
} else {
return errors.New(fmt.Sprintf("required field seqno is not a uint64: %v", val))
}
} else {
return errors.New("missing required field seqno")
}
if val, ok = cm["timestamp"]; ok {
if _, ok := val.(int64); !ok {
return errors.New(fmt.Sprintf("required field timestamp is not an int64: %v", val))
}
} else {
return errors.New("missing required field timestamp")
}
if val, ok = cm["timestamp_type"]; ok {
if tt, ok := val.(string); ok {
if tt != "rel" && tt != "abs" {
return errors.New(fmt.Sprintf("required field timestamp_type is invalid: %v", tt))
}
} else {
return errors.New(fmt.Sprintf("required field timestamp_type is not a string: %v", val))
}
} else {
return errors.New("missing required field timestamp_type")
}
/* Check optional fields. */
if val, ok = cm["temperature"]; ok {
if temp, ok := val.(float64); ok {
if math.IsNaN(temp) || math.IsInf(temp, 0) || temp < 0 || temp >= 100 {
return errors.New(fmt.Sprintf("optional field temperature is invalid: %v", temp))
}
} else {
return errors.New(fmt.Sprintf("optional field temperature is not a float64: %v", val))
}
}
if val, ok = cm["humidity"]; ok {
if hum, ok := val.(float64); ok {
if math.IsNaN(hum) || math.IsInf(hum, 0) || hum < 0 || hum >= 100 {
return errors.New(fmt.Sprintf("optional field humidity is invalid: %v", hum))
}
} else {
return errors.New(fmt.Sprintf("optional field humidity is not a float64: %v", val))
}
}
if val, ok = cm["occupancy"]; ok {
if _, ok := val.(bool); !ok {
return errors.New(fmt.Sprintf("optional field occupancy is not a bool: %v", val))
}
}
if val, ok = cm["bottom_heat"]; ok {
if bottomh, ok := val.(uint8); ok {
if bottomh > 100 {
return errors.New(fmt.Sprintf("optional field bottom_heat is invalid: %v", bottomh))
}
} else {
return errors.New(fmt.Sprintf("optional field bottom_heat is not a uint8: %v", val))
}
}
if val, ok = cm["bottom_fan"]; ok {
if bottomf, ok := val.(uint8); ok {
if bottomf > 100 {
return errors.New(fmt.Sprintf("optional field bottom_fan is invalid: %v", bottomf))
}
} else {
return errors.New(fmt.Sprintf("optional field bottom_fan is not a uint8: %v", val))
}
}
if val, ok = cm["back_heat"]; ok {
if backh, ok := val.(uint8); ok {
if backh > 100 {
return errors.New(fmt.Sprintf("optional field back_heat is invalid: %v", backh))
}
} else {
return errors.New(fmt.Sprintf("optional field back_heat is not a uint8: %v", val))
}
}
if val, ok = cm["back_fan"]; ok {
if backf, ok := val.(uint8); ok {
if backf > 100 {
return errors.New(fmt.Sprintf("optional field back_fan is invalid: %v", backf))
}
} else {
return errors.New(fmt.Sprintf("optional field back_fan is not a uint8: %v", val))
}
}
/* Check extra fields. */
for key := range cm {
if _, ok = SAMPLE_MESSAGE[key]; !ok {
return errors.New(fmt.Sprintf("received extra key: %v", key))
}
}
return nil
}
func (cm ChairMessage) SeqMax() uint64 {
return cm["seqmax"].(uint64)
}
func (cm ChairMessage) SeqNo() uint64 {
return cm["seqno"].(uint64)
}
func (cm ChairMessage) TimestampType() string {
return cm["timestamp_type"].(string)
}
func (cm ChairMessage) Timestamp() int64 {
return cm["timestamp"].(int64)
}
func (cm ChairMessage) Temperature() (temp float64, ok bool) {
val, ok := cm["temperature"]
if ok {
temp = val.(float64)
}
return
}
func (cm ChairMessage) Humidity() (hum float64, ok bool) {
val, ok := cm["humidity"]
if ok {
hum = val.(float64)
}
return
}
func (cm ChairMessage) Occupancy() (occ bool, ok bool) {
val, ok := cm["occupancy"]
if ok {
occ = val.(bool)
}
return
}
func (cm ChairMessage) BottomHeat() (bottomh uint8, ok bool) {
val, ok := cm["bottom_heat"]
if ok {
bottomh = val.(uint8)
}
return
}
func (cm ChairMessage) BottomFan() (bottomf uint8, ok bool) {
val, ok := cm["bottom_fan"]
if ok {
bottomf = val.(uint8)
}
return
}
func (cm ChairMessage) BackHeat() (backh uint8, ok bool) {
val, ok := cm["back_heater"]
if ok {
backh = val.(uint8)
}
return
}
func (cm ChairMessage) BackFan() (backf uint8, ok bool) {
val, ok := cm["back_fan"]
if ok {
backf = val.(uint8)
}
return
}