-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalue.go
224 lines (187 loc) · 5.49 KB
/
value.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
package openzwave
// #cgo LDFLAGS: -lopenzwave -Lgo/src/github.com/ninjasphere/go-openzwave/openzwave
// #cgo CPPFLAGS: -Iopenzwave/cpp/src/platform -Iopenzwave/cpp/src -Iopenzwave/cpp/src/value_classes
//
// #include "api.h"
import "C"
import (
"fmt"
"unsafe"
"github.com/ninjasphere/go-openzwave/CC"
"github.com/ninjasphere/go-openzwave/VT"
)
type ValueID struct {
CommandClassId uint8
Instance uint8
Index uint8
}
type Value interface {
Id() ValueID
SetUint8(value uint8) bool
GetUint8() (uint8, bool)
SetBool(value bool) bool
GetBool() (bool, bool)
SetInt(value int) bool
GetInt() (int, bool)
SetFloat(value float64) bool
GetFloat() (float64, bool)
SetString(value string) bool
GetString() (string, bool)
Refresh() bool
SetPollingState(bool) bool
}
type value struct {
cRef *C.Value
}
type missingValue struct {
}
func (v value) String() string {
return fmt.Sprintf(
"Value["+
"type=%v, "+
"commandClassId=%v, "+
"instance=%d, "+
"index=%d, "+
"value='%s', "+
"label='%s', "+
"units='%s', "+
"help='%s', "+
"min=%d, "+
"max=%d, "+
"isSet=%v]",
VT.ToEnum(int(v.cRef.valueId.valueType)),
CC.ToEnum(int(v.cRef.valueId.commandClassId)),
uint(v.cRef.valueId.instance),
uint(v.cRef.valueId.index),
C.GoString(v.cRef.value),
C.GoString(v.cRef.label),
C.GoString(v.cRef.units),
C.GoString(v.cRef.help),
(int32)(v.cRef.min),
(int32)(v.cRef.max),
(bool)(v.cRef.isSet))
}
func newGoValue(cRef *C.Value) *value {
if cRef != nil {
return &value{cRef}
}
return nil
}
func (v *value) notify(api *api, nt *notification) {
// TODO
}
func (v *value) Id() ValueID {
return ValueID{
CommandClassId: uint8(v.cRef.valueId.commandClassId),
Instance: uint8(v.cRef.valueId.instance),
Index: uint8(v.cRef.valueId.index),
}
}
func (v *value) SetUint8(value uint8) bool {
return (bool)(C.setUint8Value(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), C.uint8(value)))
}
func (v *value) GetUint8() (uint8, bool) {
var value C.uint8
ok := (bool)(C.getUint8Value(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), (*C.uint8)(&value)))
return (uint8)(value), ok
}
func (v *value) SetBool(value bool) bool {
return (bool)(C.setBoolValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), C._Bool(value)))
}
func (v *value) GetBool() (bool, bool) {
var value C._Bool
ok := (bool)(C.getBoolValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), (*C._Bool)(&value)))
return (bool)(value), ok
}
func (v *value) SetInt(value int) bool {
return (bool)(C.setIntValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), C.int(value)))
}
func (v *value) GetInt() (int, bool) {
var value C.int
ok := (bool)(C.getIntValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), (*C.int)(&value)))
return (int)(value), ok
}
func (v *value) SetFloat(value float64) bool {
return (bool)(C.setFloatValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), C.float(value)))
}
func (v *value) GetFloat() (float64, bool) {
var value C.float
ok := (bool)(C.getFloatValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), (*C.float)(&value)))
return (float64)(value), ok
}
// for a missing value, the get operation always fails
func (v *value) GetString() (string, bool) {
var value *C.char
ok := (bool)(C.getStringValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), (**C.char)(&value)))
if ok && value != nil {
result := C.GoString(value)
C.free(unsafe.Pointer(value))
return result, true
} else {
return "", false
}
}
// for a missing value, the set operation always fails
func (v *value) SetString(value string) bool {
tmp := C.CString(value)
return (bool)(C.setStringValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), tmp))
}
func (v *value) Refresh() bool {
return (bool)(C.refreshValue(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id)))
}
func (v *value) free() {
C.freeValue(v.cRef)
}
func (v *value) SetPollingState(state bool) bool {
return (bool)(C.setPollingState(C.uint32(v.cRef.homeId), C.uint64(v.cRef.valueId.id), C._Bool(state)))
}
// for a missing value, the set operation always fails
func (v *missingValue) SetUint8(value uint8) bool {
return false
}
// for a missing value, the get operation always fails
func (v *missingValue) GetUint8() (uint8, bool) {
return 0, false
}
// for a missing value, the set operation always fails
func (v *missingValue) SetBool(value bool) bool {
return false
}
// for a missing value, the get operation always fails
func (v *missingValue) GetBool() (bool, bool) {
return false, false
}
// for a missing value, the get operation always fails
func (v *missingValue) GetInt() (int, bool) {
return 0, false
}
// for a missing value, the set operation always fails
func (v *missingValue) SetInt(value int) bool {
return false
}
// for a missing value, the get operation always fails
func (v *missingValue) GetFloat() (float64, bool) {
return 0.0, false
}
// for a missing value, the set operation always fails
func (v *missingValue) SetFloat(value float64) bool {
return false
}
// for a missing value, the get operation always fails
func (v *missingValue) GetString() (string, bool) {
return "", false
}
// for a missing value, the set operation always fails
func (v *missingValue) SetString(value string) bool {
return false
}
// for a missing value, the get operation always fails
func (v *missingValue) Refresh() bool {
return false
}
func (v *missingValue) SetPollingState(state bool) bool {
return false
}
func (v *missingValue) Id() ValueID {
return ValueID{0, 0, 0}
}