-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenergenie.go
executable file
·322 lines (275 loc) · 8.46 KB
/
energenie.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
/*
Go Language Raspberry Pi Interface
(c) Copyright David Thorpe 2016-2018
All Rights Reserved
Documentation http://djthorpe.github.io/gopi/
For Licensing and Usage information, please see LICENSE.md
*/
package sensors
import (
"context"
"fmt"
"time"
// Frameworks
"github.com/djthorpe/gopi"
)
////////////////////////////////////////////////////////////////////////////////
// TYPES
type (
MiHomeMode uint
MiHomeProduct byte
MiHomeValveState byte
MiHomePowerMode byte
)
////////////////////////////////////////////////////////////////////////////////
// ENER314 AND ENER314RT
type ENER314 interface {
gopi.Driver
// Send on signal - when no sockets specified then
// sends to all sockets
On(sockets ...uint) error
// Send off signal - when no sockets specified then
// sends to all sockets
Off(sockets ...uint) error
}
type ENER314RT interface {
gopi.Driver
// Receive payloads with radio until context deadline exceeded or cancel,
// this blocks sending
Receive(ctx context.Context, mode MiHomeMode, payload chan<- []byte) error
// Send a raw payload with radio
Send(payload []byte, repeat uint, mode MiHomeMode) error
// Measure device temperature
MeasureTemperature(offset float32) (float32, error)
// Reset radio device
ResetRadio() error
}
////////////////////////////////////////////////////////////////////////////////
// MIHOME
type MiHome interface {
gopi.Driver
gopi.Publisher
// Reset the device
Reset() error
// Add a wire protocol which encodes/decodes messages
AddProto(Proto) error
// Return registered protocols
Protos() []Proto
// Measure Device Temperature
MeasureTemperature() (float32, error)
// Request Switch state for both monitor and control devices
RequestSwitchOn(MiHomeProduct, uint32) error
RequestSwitchOff(MiHomeProduct, uint32) error
// Send a join message after a report is received
SendJoin(MiHomeProduct, uint32) error
// Note the eTRV messages below should be sent very shortly
// after the temperature report is provided as that's when the
// eTRV is awake to respond to the messages
RequestIdentify(MiHomeProduct, uint32) error
RequestDiagnostics(MiHomeProduct, uint32) error
RequestExercise(MiHomeProduct, uint32) error
RequestBatteryLevel(MiHomeProduct, uint32) error
RequestTargetTemperature(MiHomeProduct, uint32, float64) error
RequestReportInterval(MiHomeProduct, uint32, time.Duration) error
RequestValveState(MiHomeProduct, uint32, MiHomeValveState) error
RequestLowPowerMode(MiHomeProduct, uint32, bool) error
}
// MiHome Client Stub
type MiHomeClient interface {
gopi.RPCClient
gopi.Publisher
// Ping the remote service instance
Ping() error
// Reset the device
Reset() error
// Send 'On' and 'Off' signals
On(MiHomeProduct, uint32) error
Off(MiHomeProduct, uint32) error
// Send a join message after a join report is received
SendJoin(MiHomeProduct, uint32) error
// Request status
RequestDiagnostics(MiHomeProduct, uint32) error
RequestIdentify(MiHomeProduct, uint32) error
RequestExercise(MiHomeProduct, uint32) error
RequestBatteryLevel(MiHomeProduct, uint32) error
// Set parameters
SendTargetTemperature(MiHomeProduct, uint32, float64) error
SendReportInterval(MiHomeProduct, uint32, time.Duration) error
SendValveState(MiHomeProduct, uint32, MiHomeValveState) error
SendPowerMode(MiHomeProduct, uint32, MiHomePowerMode) error
// Receive messages
StreamMessages(ctx context.Context) error
}
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
const (
MIHOME_MODE_NONE MiHomeMode = iota
MIHOME_MODE_MONITOR // FSK
MIHOME_MODE_CONTROL // OOK
MIHOME_MODE_MAX = MIHOME_MODE_CONTROL
)
const (
// Monitor Products (FSK)
MIHOME_PRODUCT_NONE MiHomeProduct = 0x00
MIHOME_PRODUCT_MIHO004 MiHomeProduct = 0x01 // Adaptor Monitor
MIHOME_PRODUCT_MIHO005 MiHomeProduct = 0x02 // Adaptor Plus
MIHOME_PRODUCT_MIHO013 MiHomeProduct = 0x03 // eTRV
MIHOME_PRODUCT_MIHO006 MiHomeProduct = 0x05 // House Monitor
MIHOME_PRODUCT_MIHO032 MiHomeProduct = 0x0C // Motion sensor
MIHOME_PRODUCT_MIHO033 MiHomeProduct = 0x0D // Door sensor
MIHOME_PRODUCT_MIHO089 MiHomeProduct = 0x13 // Click
// Control Products (OOK)
MIHOME_PRODUCT_CONTROL_ALL MiHomeProduct = 0xF0 // OOK Switch All
MIHOME_PRODUCT_CONTROL_ONE MiHomeProduct = 0xF1 // OOK Switch 1
MIHOME_PRODUCT_CONTROL_TWO MiHomeProduct = 0xF2 // OOK Switch 2
MIHOME_PRODUCT_CONTROL_THREE MiHomeProduct = 0xF3 // OOK Switch 3
MIHOME_PRODUCT_CONTROL_FOUR MiHomeProduct = 0xF4 // OOK Switch 4
)
const (
MIHOME_VALVE_STATE_OPEN MiHomeValveState = 0x00 // Valve fully open
MIHOME_VALVE_STATE_CLOSED MiHomeValveState = 0x01 // Valve fully closed
MIHOME_VALVE_STATE_NORMAL MiHomeValveState = 0x02 // Valve in normal state
)
const (
MIHOME_POWER_NONE MiHomePowerMode = iota
MIHOME_POWER_NORMAL
MIHOME_POWER_LOW
)
////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
// Mode returns the mode for a product
func (p MiHomeProduct) Mode() MiHomeMode {
switch p {
case MIHOME_PRODUCT_MIHO004:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO005:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO013:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO006:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO032:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO033:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_MIHO089:
return MIHOME_MODE_MONITOR
case MIHOME_PRODUCT_CONTROL_ALL:
return MIHOME_MODE_CONTROL
case MIHOME_PRODUCT_CONTROL_ONE:
return MIHOME_MODE_CONTROL
case MIHOME_PRODUCT_CONTROL_TWO:
return MIHOME_MODE_CONTROL
case MIHOME_PRODUCT_CONTROL_THREE:
return MIHOME_MODE_CONTROL
case MIHOME_PRODUCT_CONTROL_FOUR:
return MIHOME_MODE_CONTROL
default:
return MIHOME_MODE_NONE
}
}
// Socket returns the socket number of a control product
func (p MiHomeProduct) Socket() uint {
switch p {
case MIHOME_PRODUCT_CONTROL_ALL:
return 0
case MIHOME_PRODUCT_CONTROL_ONE:
return 1
case MIHOME_PRODUCT_CONTROL_TWO:
return 2
case MIHOME_PRODUCT_CONTROL_THREE:
return 3
case MIHOME_PRODUCT_CONTROL_FOUR:
return 4
default:
// Return 0 otherwise
return 0
}
}
// SocketProduct maps from socket number to control product
func SocketProduct(socket uint) MiHomeProduct {
switch socket {
case 0:
return MIHOME_PRODUCT_CONTROL_ALL
case 1:
return MIHOME_PRODUCT_CONTROL_ONE
case 2:
return MIHOME_PRODUCT_CONTROL_TWO
case 3:
return MIHOME_PRODUCT_CONTROL_THREE
case 4:
return MIHOME_PRODUCT_CONTROL_FOUR
default:
return MIHOME_PRODUCT_NONE
}
}
////////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (m MiHomeMode) String() string {
switch m {
case MIHOME_MODE_NONE:
return "MIHOME_MODE_NONE"
case MIHOME_MODE_MONITOR:
return "MIHOME_MODE_MONITOR"
case MIHOME_MODE_CONTROL:
return "MIHOME_MODE_CONTROL"
default:
return "[?? Invalid MiHomeMode value]"
}
}
func (p MiHomePowerMode) String() string {
switch p {
case MIHOME_POWER_NONE:
return "MIHOME_POWER_NONE"
case MIHOME_POWER_NORMAL:
return "MIHOME_POWER_NORMAL"
case MIHOME_POWER_LOW:
return "MIHOME_POWER_LOW"
default:
return "[?? Invalid MiHomePowerMode value]"
}
}
func (p MiHomeProduct) String() string {
switch p {
case MIHOME_PRODUCT_NONE:
return "MIHOME_PRODUCT_NONE"
case MIHOME_PRODUCT_MIHO004:
return "MIHOME_PRODUCT_MIHO004"
case MIHOME_PRODUCT_MIHO005:
return "MIHOME_PRODUCT_MIHO005"
case MIHOME_PRODUCT_MIHO013:
return "MIHOME_PRODUCT_MIHO013"
case MIHOME_PRODUCT_MIHO006:
return "MIHOME_PRODUCT_MIHO006"
case MIHOME_PRODUCT_MIHO032:
return "MIHOME_PRODUCT_MIHO032"
case MIHOME_PRODUCT_MIHO033:
return "MIHOME_PRODUCT_MIHO033"
case MIHOME_PRODUCT_MIHO089:
return "MIHOME_PRODUCT_MIHO089"
case MIHOME_PRODUCT_CONTROL_ALL:
return "MIHOME_PRODUCT_CONTROL_ALL"
case MIHOME_PRODUCT_CONTROL_ONE:
return "MIHOME_PRODUCT_CONTROL_ONE"
case MIHOME_PRODUCT_CONTROL_TWO:
return "MIHOME_PRODUCT_CONTROL_TWO"
case MIHOME_PRODUCT_CONTROL_THREE:
return "MIHOME_PRODUCT_CONTROL_THREE"
case MIHOME_PRODUCT_CONTROL_FOUR:
return "MIHOME_PRODUCT_CONTROL_FOUR"
default:
return fmt.Sprintf("[?? Invalid MiHomeProduct value: 0x%02X]", uint(p))
}
}
func (s MiHomeValveState) String() string {
switch s {
case MIHOME_VALVE_STATE_OPEN:
return "MIHOME_VALVE_STATE_OPEN"
case MIHOME_VALVE_STATE_CLOSED:
return "MIHOME_VALVE_STATE_CLOSED"
case MIHOME_VALVE_STATE_NORMAL:
return "MIHOME_VALVE_STATE_NORMAL"
default:
return "[?? Invalid MiHomeValveState value]"
}
}