forked from jimjibone/goopenzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueid.go
500 lines (445 loc) · 15.4 KB
/
valueid.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package goopenzwave
// #include "gzw_valueid.h"
// #include <stdlib.h>
import "C"
import (
"fmt"
)
// ValueIDGenre defines a type for the valueid genre enum.
type ValueIDGenre int
const (
ValueIDGenreBasic ValueIDGenre = iota
ValueIDGenreUser
ValueIDGenreConfig
ValueIDGenreSystem
ValueIDGenreCount
)
func (v ValueIDGenre) String() string {
switch v {
case ValueIDGenreBasic:
return "Basic"
case ValueIDGenreUser:
return "User"
case ValueIDGenreConfig:
return "Config"
case ValueIDGenreSystem:
return "System"
case ValueIDGenreCount:
return "Count"
}
return "UNKNOWN"
}
// ValueIDType defines a type for the valueid type enum.
type ValueIDType int
const (
ValueIDTypeBool ValueIDType = iota
ValueIDTypeByte
ValueIDTypeDecimal
ValueIDTypeInt
ValueIDTypeList
ValueIDTypeSchedule
ValueIDTypeShort
ValueIDTypeString
ValueIDTypeButton
ValueIDTypeRaw
ValueIDTypeMax
)
func (v ValueIDType) String() string {
switch v {
case ValueIDTypeBool:
return "Bool"
case ValueIDTypeByte:
return "Byte"
case ValueIDTypeDecimal:
return "Decimal"
case ValueIDTypeInt:
return "Int"
case ValueIDTypeList:
return "List"
case ValueIDTypeSchedule:
return "Schedule"
case ValueIDTypeShort:
return "Short"
case ValueIDTypeString:
return "String"
case ValueIDTypeButton:
return "Button"
case ValueIDTypeRaw: // also ValueIDTypeMax
return "Raw/Max"
}
return "UNKNOWN"
}
// ValueID contains all appropriate information available for a ValueID from the
// OpenZWave library. You should not create a new ValueID manually, but receive
// it from the goopenzwave package after a Notification has been received from
// the OpenZWave library.
type ValueID struct {
HomeID uint32
NodeID uint8
Genre ValueIDGenre
CommandClassID uint8
Instance uint8
Index uint8
Type ValueIDType
ID uint64
}
// buildValueID creates a new valueid.ValueID from the C valueid_t (and
// therefore C++ ValueID) value.
func buildValueID(v C.valueid_t) *ValueID {
vid := &ValueID{
HomeID: uint32(C.valueid_getHomeId(v)),
NodeID: uint8(C.valueid_getNodeId(v)),
CommandClassID: uint8(C.valueid_getCommandClassId(v)),
Instance: uint8(C.valueid_getInstance(v)),
Index: uint8(C.valueid_getIndex(v)),
ID: uint64(C.valueid_getId(v)),
}
switch C.valueid_getGenre(v) {
case C.valueid_genre_basic:
vid.Genre = ValueIDGenreBasic
case C.valueid_genre_user:
vid.Genre = ValueIDGenreUser
case C.valueid_genre_config:
vid.Genre = ValueIDGenreConfig
case C.valueid_genre_system:
vid.Genre = ValueIDGenreSystem
case C.valueid_genre_count:
vid.Genre = ValueIDGenreCount
}
switch C.valueid_getType(v) {
case C.valueid_type_bool:
vid.Type = ValueIDTypeBool
case C.valueid_type_byte:
vid.Type = ValueIDTypeByte
case C.valueid_type_decimal:
vid.Type = ValueIDTypeDecimal
case C.valueid_type_int:
vid.Type = ValueIDTypeInt
case C.valueid_type_list:
vid.Type = ValueIDTypeList
case C.valueid_type_schedule:
vid.Type = ValueIDTypeSchedule
case C.valueid_type_short:
vid.Type = ValueIDTypeShort
case C.valueid_type_string:
vid.Type = ValueIDTypeString
case C.valueid_type_button:
vid.Type = ValueIDTypeButton
case C.valueid_type_raw:
vid.Type = ValueIDTypeRaw
// case C.valueid_type_max:
// vid.Type = ValueIDTypeMax
}
return vid
}
// IDString will create a string representation of the ID for use as a key.
func (v *ValueID) IDString() string {
return fmt.Sprintf("%d", v.ID)
}
func (v *ValueID) StringFull() string {
return fmt.Sprintf("{Label: %s, Value: %s, Units: %q, Min: %d, Max: %d, RO: %t, WO: %t, Genre: %s, CommandClassID: %d, Instance: %d, Index: %d, Type: %s, Help: %s, HomeID: 0x%x, ID: 0x%x}",
v.GetLabel(),
v.GetAsString(),
v.GetUnits(),
v.GetMin(),
v.GetMax(),
v.IsReadOnly(),
v.IsWriteOnly(),
v.Genre,
v.CommandClassID,
v.Instance,
v.Index,
v.Type,
v.GetHelp(),
v.HomeID,
v.ID)
}
func (v *ValueID) String() string {
units := ""
minmax := ""
ro := ""
wo := ""
help := ""
if v.GetUnits() != "" {
units = " (" + v.GetUnits() + ")"
}
if v.GetMin() != 0 || v.GetMax() != 0 {
minmax = fmt.Sprintf(" [%d -> %d]", v.GetMin(), v.GetMax())
}
if v.IsReadOnly() {
ro = ", readonly"
}
if v.IsWriteOnly() {
wo = ", writeonly"
}
if v.GetHelp() != "" {
help = ", help: " + v.GetHelp()
}
return fmt.Sprintf("{%s (%s): %s%s%s%s%s, genre: %s%s}",
v.GetLabel(),
v.Type,
v.GetAsString(),
units,
minmax,
ro,
wo,
v.Genre,
help,
)
}
// GetLabel returns the user-friendly label for the value.
func (v *ValueID) GetLabel() string {
return GetValueLabel(v.HomeID, v.ID)
}
// SetLabel sets the user-friendly label for the value.
func (v *ValueID) SetLabel(label string) {
SetValueLabel(v.HomeID, v.ID, label)
}
// GetUnits returns the units that the value is measured in.
func (v *ValueID) GetUnits() string {
return GetValueUnits(v.HomeID, v.ID)
}
// SetUnits sets the units that the value is measured in.
func (v *ValueID) SetUnits(units string) {
SetValueUnits(v.HomeID, v.ID, units)
}
// GetHelp returns a help string describing the value's purpose and usage.
func (v *ValueID) GetHelp() string {
return GetValueHelp(v.HomeID, v.ID)
}
// SetHelp sets a help string describing the value's purpose and usage.
func (v *ValueID) SetHelp(help string) {
SetValueHelp(v.HomeID, v.ID, help)
}
// GetMin returns the minimum that this value may contain.
func (v *ValueID) GetMin() int32 {
return GetValueMin(v.HomeID, v.ID)
}
// GetMax returns the maximum that this value may contain.
func (v *ValueID) GetMax() int32 {
return GetValueMax(v.HomeID, v.ID)
}
// IsReadOnly returns true if the value is read-only.
func (v *ValueID) IsReadOnly() bool {
return IsValueReadOnly(v.HomeID, v.ID)
}
// IsWriteOnly returns true if the value is write-only.
func (v *ValueID) IsWriteOnly() bool {
return IsValueWriteOnly(v.HomeID, v.ID)
}
// IsSet returns true if the value has been set.
func (v *ValueID) IsSet() bool {
return IsValueSet(v.HomeID, v.ID)
}
// IsPolled returns true if the value is currently being polled.
func (v *ValueID) IsPolled() bool {
return IsValuePolled(v.HomeID, v.ID)
}
// GetAsBool returns the value as a bool. It will also return an error if the
// value is not a bool type.
func (v *ValueID) GetAsBool() (bool, error) {
return GetValueAsBool(v.HomeID, v.ID)
}
// GetAsByte returns the value as an 8-bit unsigned integer. It will also
// return an error if the value is not of byte type.
func (v *ValueID) GetAsByte() (byte, error) {
return GetValueAsByte(v.HomeID, v.ID)
}
// GetAsFloat returns the value as a float. It will also return an error if
// the value is not a decimal type.
func (v *ValueID) GetAsFloat() (float32, error) {
return GetValueAsFloat(v.HomeID, v.ID)
}
// GetAsInt returns the value as a 32-bit signed integer. It will also
// return an error if the value is not of 32-bit signed integer type.
func (v *ValueID) GetAsInt() (int32, error) {
return GetValueAsInt(v.HomeID, v.ID)
}
// GetAsShort returns the value as a 16-bit signed integer. It will also
// return an error if the value is not of 16-bit signed integer type.
func (v *ValueID) GetAsShort() (int16, error) {
return GetValueAsShort(v.HomeID, v.ID)
}
// GetAsString returns the value as a string, regardless of its actual
// type.
func (v *ValueID) GetAsString() string {
return GetValueAsString(v.HomeID, v.ID)
}
// GetAsRaw returns the value as a raw byte slice. It will also return an
// error if the value is not of raw type.
func (v *ValueID) GetAsRaw() ([]byte, error) {
return GetValueAsRaw(v.HomeID, v.ID)
}
// GetListSelectionAsString returns selected item from a list as a string.
// It will also return an error if the value is not of list type.
func (v *ValueID) GetListSelectionAsString() (string, error) {
return GetValueListSelectionAsString(v.HomeID, v.ID)
}
// GetListSelectionAsInt32 returns selected item from a list as an integer.
// It will also return an error if the value is not of list type.
func (v *ValueID) GetListSelectionAsInt32() (int32, error) {
return GetValueListSelectionAsInt32(v.HomeID, v.ID)
}
// GetListItems returns the list of items from a list value. It will also
// return an error if the value is not of list type.
func (v *ValueID) GetListItems() ([]string, error) {
return GetValueListItems(v.HomeID, v.ID)
}
// GetFloatPrecision returns the float value's precision. It will also
// return an error if the value is not of decimal type.
func (v *ValueID) GetFloatPrecision() (uint8, error) {
return GetValueFloatPrecision(v.HomeID, v.ID)
}
// SetBool sets the state of a bool. It will return an error if the value
// is not of bool type.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetBool(value bool) error {
return SetValueBool(v.HomeID, v.ID, value)
}
// SetUint8 sets the value of a byte. It will return an error if the value
// is not of byte type.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetUint8(value uint8) error {
return SetValueUint8(v.HomeID, v.ID, value)
}
// SetFloat sets the value of a decimal. It will return an error if the
// value is not of decimal type.
//
// It is usually better to handle decimal values using strings rather than
// floats, to avoid floating point accuracy issues. Due to the possibility of a
// device being asleep, the command is assumed to succeed, and the value held by
// the node is updated directly. This will be reverted by a future status
// message from the device if the Z-Wave message actually failed to get through.
// Notification callbacks will be sent in both cases.
func (v *ValueID) SetFloat(value float32) error {
return SetValueFloat(v.HomeID, v.ID, value)
}
// SetInt32 sets the value of a 32-bit signed integer. It will return an
// error if the value is not of 32-bit signed integer type.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetInt32(value int32) error {
return SetValueInt32(v.HomeID, v.ID, value)
}
// SetInt16 sets the value of a 16-bit signed integer. It will return an
// error if the value is not of 16-bit signed integer type.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetInt16(value int16) error {
return SetValueInt16(v.HomeID, v.ID, value)
}
// SetBytes sets the value of a raw value. It will return an error if the
// value is not of raw type.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetBytes(value []byte) error {
return SetValueBytes(v.HomeID, v.ID, value)
}
// SetString sets the value from a string, regardless of type. It will
// return an error if the value could not be parsed into the correct type for
// the value.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetString(value string) error {
return SetValueString(v.HomeID, v.ID, value)
}
// SetListSelection sets the selected item in a list. It will return an
// error if the value is not of list type or if the selection is not in the
// list.
//
// Due to the possibility of a device being asleep, the command is assumed to
// succeed, and the value held by the node is updated directly. This will be
// reverted by a future status message from the device if the Z-Wave message
// actually failed to get through. Notification callbacks will be sent in both
// cases.
func (v *ValueID) SetListSelection(selectedItem string) error {
return SetValueListSelection(v.HomeID, v.ID, selectedItem)
}
// Refresh refreshes the specified value from the Z-Wave network. It will
// return true if the driver and node were found, otherwise false.
//
// A call to this function causes the library to send a message to the network
// to retrieve the current value of the specified ValueID (just like a poll,
// except only one-time, not recurring).
func (v *ValueID) Refresh() bool {
return RefreshValue(v.HomeID, v.ID)
}
// SetChangeVerified sets a flag indicating whether value changes noted upon a
// refresh should be verified. If so, the library will immediately refresh the
// value a second time whenever a change is observed. This helps to filter out
// spurious data reported occasionally by some devices.
func (v *ValueID) SetChangeVerified(verify bool) {
SetChangeVerified(v.HomeID, v.ID, verify)
}
// GetChangeVerified returns true if value changes upon a refresh should be
// verified. If so, the library will immediately refresh the value a second time
// whenever a change is observed. This helps to filter out spurious data
// reported occasionally by some devices.
func (v *ValueID) GetChangeVerified() bool {
return GetChangeVerified(v.HomeID, v.ID)
}
// PressButton starts an activity in a device. It will return an error if the
// value is not of button type.
//
// Since buttons are write-only values that do not report a state, no
// notification callbacks are sent.
func (v *ValueID) PressButton() error {
return PressButton(v.HomeID, v.ID)
}
// ReleaseButton stops an activity in a device. It will return an error if the
// value is not of button type.
//
// Since buttons are write-only values that do not report a state, no
// notification callbacks are sent.
func (v *ValueID) ReleaseButton() error {
return ReleaseButton(v.HomeID, v.ID)
}
// EnablePoll enables the polling of a device's state. Returns true if polling
// was enabled.
func (v *ValueID) EnablePoll(intensity uint8) bool {
return EnablePoll(v.HomeID, v.ID, intensity)
}
// DisablePoll disables the polling of a device's state. Returns true if polling
// was disabled.
func (v *ValueID) DisablePoll() bool {
return DisablePoll(v.HomeID, v.ID)
}
// SetPollIntensity sets the frequency of polling.
//
// - 0 = none
// - 1 = every time through the list
// - 2 = every other time
// - etc.
func (v *ValueID) SetPollIntensity(intensity uint8) {
SetPollIntensity(v.HomeID, v.ID, intensity)
}
// GetPollIntensity returns the polling intensity of a device's state.
func (v *ValueID) GetPollIntensity() uint8 {
return GetPollIntensity(v.HomeID, v.ID)
}