forked from sworisbreathing/go-ibbq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibbq.go
386 lines (355 loc) · 11.1 KB
/
ibbq.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
/*
Copyright 2018 the original author or authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ibbq
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"strings"
"time"
"github.com/go-ble/ble"
)
// Ibbq is an instance of the thermometer
type Ibbq struct {
ctx context.Context
config Configuration
device ble.Device
disconnectedHandler DisconnectedHandler
temperatureReceivedHandler TemperatureReceivedHandler
batteryLevelReceivedHandler BatteryLevelReceivedHandler
statusUpdatedHandler StatusUpdatedHandler
client ble.Client
profile *ble.Profile
disconnected chan struct{}
status Status
}
// TemperatureReceivedHandler is a callback for temperature readings.
// All temperature readings are returned in celsius.
type TemperatureReceivedHandler func([]float64)
// BatteryLevelReceivedHandler is a callback for battery readings.
// All battery readings are returned as percentages.
type BatteryLevelReceivedHandler func(int)
// DisconnectedHandler handles disconnection events
type DisconnectedHandler func()
// StatusUpdatedHandler is a callback for status updates.
type StatusUpdatedHandler func(Status)
// NewIbbq creates a new Ibbq
func NewIbbq(ctx context.Context, config Configuration, disconnectedHandler DisconnectedHandler, temperatureReceivedHandler TemperatureReceivedHandler, batteryLevelReceivedHandler BatteryLevelReceivedHandler, statusUpdatedHandler StatusUpdatedHandler) (ibbq Ibbq, err error) {
d, err := NewDevice("default")
ble.SetDefaultDevice(d)
return Ibbq{ctx, config, d, disconnectedHandler, temperatureReceivedHandler, batteryLevelReceivedHandler, statusUpdatedHandler, nil, nil, nil, Disconnected}, err
}
func (ibbq *Ibbq) handleDisconnects() {
logger.Debug("waiting for disconnect")
<-ibbq.client.Disconnected()
logger.Info("Disconnected", "addr", ibbq.client.Addr().String())
ibbq.client = nil
ibbq.profile = nil
ibbq.device.Stop()
ibbq.updateStatus(Disconnected)
go ibbq.disconnectedHandler()
}
func (ibbq *Ibbq) handleContextClosed() {
logger.Debug("waiting for context to close")
<-ibbq.ctx.Done()
ibbq.Disconnect(false)
}
// Connect connects to an ibbq
func (ibbq *Ibbq) Connect() error {
var client ble.Client
var err error
timeoutContext, cancel := context.WithTimeout(ibbq.ctx, ibbq.config.ConnectTimeout)
defer cancel()
c := make(chan interface{})
logger.Info("Connecting to device")
go func() {
ibbq.updateStatus(Connecting)
if client, err = ble.Connect(timeoutContext, filter()); err == nil {
logger.Info("Connected to device", "addr", client.Addr())
ibbq.client = client
logger.Debug("Setting up disconnect handler")
go ibbq.handleDisconnects()
logger.Debug("Setting up context closed handler")
go ibbq.handleContextClosed()
err = ibbq.discoverProfile()
}
if err == nil {
err = ibbq.login()
}
if err == nil {
err = ibbq.subscribeToSettingResults()
}
if err == nil {
err = ibbq.ConfigureTemperatureCelsius()
}
if err == nil {
err = ibbq.subscribeToRealTimeData()
}
if err == nil {
err = ibbq.subscribeToHistoryData()
}
if err == nil {
err = ibbq.enableRealTimeData()
}
if err == nil {
err = ibbq.enableBatteryData()
}
c <- err
close(c)
}()
select {
case <-timeoutContext.Done():
logger.Error("timeout while connecting")
err = timeoutContext.Err()
ibbq.updateStatus(Disconnected)
case err := <-c:
if err != nil {
logger.Error("Error received while connecting", "err", err)
ibbq.updateStatus(Disconnected)
} else {
ibbq.updateStatus(Connected)
}
}
return err
}
func (ibbq *Ibbq) discoverProfile() error {
var profile *ble.Profile
var err error
if profile, err = ibbq.client.DiscoverProfile(true); err == nil {
ibbq.profile = profile
}
return err
}
func (ibbq *Ibbq) login() error {
var err error
var uuid ble.UUID
if uuid, err = ble.Parse(AccountAndVerify); err == nil {
logger.Debug("logging in to device", "addr", ibbq.client.Addr(), "uuid", uuid)
characteristic := ble.NewCharacteristic(uuid)
if c := ibbq.profile.FindCharacteristic(characteristic); c != nil {
err = ibbq.client.WriteCharacteristic(c, Credentials, false)
logger.Debug("credentials written")
}
}
return err
}
func (ibbq *Ibbq) updateStatus(status Status) {
ibbq.status = status
if ibbq.statusUpdatedHandler != nil {
go ibbq.statusUpdatedHandler(status)
}
}
func (ibbq *Ibbq) subscribeToRealTimeData() error {
var err error
var uuid ble.UUID
logger.Info("Subscribing to real-time data")
if uuid, err = ble.Parse(RealTimeData); err == nil {
characteristic := ble.NewCharacteristic(uuid)
if c := ibbq.profile.FindCharacteristic(characteristic); c != nil {
err = ibbq.client.Subscribe(c, false, ibbq.realTimeDataReceived())
if err == nil {
logger.Info("Subscribed to real-time data")
} else {
logger.Error("Error subscribing to real-time data", "err", err)
}
} else {
err = errors.New("can't find characteristic for real-time data")
}
}
return err
}
func (ibbq *Ibbq) realTimeDataReceived() ble.NotificationHandler {
return func(data []byte) {
logger.Debug("received real-time data", hex.EncodeToString(data))
probeCount := len(data) / 2
probeData := make([]float64, probeCount)
for i := range data {
if i%2 == 0 {
probeData[i/2] = float64(binary.LittleEndian.Uint16(data[i:i+2])) / 10
}
}
go ibbq.temperatureReceivedHandler(probeData)
}
}
func (ibbq *Ibbq) subscribeToHistoryData() error {
var err error
var uuid ble.UUID
logger.Info("Subscribing to history data")
if uuid, err = ble.Parse(HistoryData); err == nil {
characteristic := ble.NewCharacteristic(uuid)
if c := ibbq.profile.FindCharacteristic(characteristic); c != nil {
err = ibbq.client.Subscribe(c, false, ibbq.historyDataReceived())
if err == nil {
logger.Info("Subscribed to history data")
} else {
logger.Error("Error subscribing to history data", "err", err)
}
} else {
err = errors.New("Can't find characteristic for history data")
}
}
return err
}
func (ibbq *Ibbq) historyDataReceived() ble.NotificationHandler {
return func(data []byte) {
logger.Debug("received history data", hex.EncodeToString(data))
}
}
func (ibbq *Ibbq) subscribeToSettingResults() error {
var err error
var uuid ble.UUID
logger.Info("Subscribing to setting results")
if uuid, err = ble.Parse(SettingResult); err == nil {
characteristic := ble.NewCharacteristic(uuid)
if c := ibbq.profile.FindCharacteristic(characteristic); c != nil {
err = ibbq.client.Subscribe(c, false, ibbq.settingResultReceived())
if err == nil {
logger.Info("Subscribed to setting results")
} else {
logger.Error("Error subscribing to setting results", "err", err)
}
} else {
err = errors.New("Can't find characteristic for setting results")
}
}
return err
}
func (ibbq *Ibbq) settingResultReceived() ble.NotificationHandler {
return func(data []byte) {
logger.Debug("Received setting result", "data", hex.EncodeToString(data))
switch data[0] {
case 0x24:
// battery
currentVoltage := int(binary.BigEndian.Uint16(data[1:3]))
maxVoltage := int(binary.BigEndian.Uint16(data[3:5]))
if maxVoltage == 0 {
maxVoltage = 65535
}
batteryPct := 100 * currentVoltage / maxVoltage
go ibbq.batteryLevelReceivedHandler(batteryPct)
}
}
}
func (ibbq *Ibbq) enableRealTimeData() error {
logger.Info("Enabling real-time data sending")
err := ibbq.writeSetting(realTimeDataEnable)
if err == nil {
logger.Info("Enabled real-time data sending")
}
return err
}
func (ibbq *Ibbq) enableBatteryData() error {
if ibbq.config.BatteryPollingInterval > 0 {
logger.Info("Enabling battery data sending")
var err error
if err = ibbq.writeSetting(batteryLevel); err == nil {
ticker := time.NewTicker(ibbq.config.BatteryPollingInterval)
go func() {
for {
select {
case <-ticker.C:
logger.Debug("Requesting battery data")
err := ibbq.writeSetting(batteryLevel)
if err != nil {
logger.Error("Unable to request battery level", "err", err)
ticker.Stop()
return
}
case <-ibbq.client.Disconnected():
ticker.Stop()
return
}
}
}()
}
return err
}
logger.Debug("Battery level polling was not enabled in configuration")
return nil
}
// ConfigureTemperatureCelsius changes the device to display temperatures in Celsius on the screen.
// It does not change the units sent back over the wire, however, which are always in Celsius.
func (ibbq *Ibbq) ConfigureTemperatureCelsius() error {
logger.Info("Configuring temperature for Celsius")
err := ibbq.writeSetting(unitsCelsius)
if err == nil {
logger.Info("Configured temperature for Celsius")
}
return err
}
// ConfigureTemperatureFahrenheit changes the device to display temperatures in Fahrenheit on the screen.
// It does not change the units sent back over the wire, however, which are always in Celsius.
func (ibbq *Ibbq) ConfigureTemperatureFahrenheit() error {
logger.Info("Configuring temperature for Fahrenheit")
err := ibbq.writeSetting(unitsFahrenheit)
if err == nil {
logger.Info("Configured temperature for Fahrenheit")
}
return err
}
func (ibbq *Ibbq) writeSetting(settingValue []byte) error {
var err error
var uuid ble.UUID
if uuid, err = ble.Parse(SettingData); err == nil {
characteristic := ble.NewCharacteristic(uuid)
if c := ibbq.profile.FindCharacteristic(characteristic); c != nil {
err = ibbq.client.WriteCharacteristic(c, settingValue, false)
} else {
err = errors.New("Can't find characteristic for settings data")
}
}
return err
}
// Disconnect disconnects from an ibbq
func (ibbq *Ibbq) Disconnect(force bool) error {
var err error
if ibbq.client == nil {
err = errors.New("Not connected")
if ibbq.device != nil && force {
ibbq.client = nil
ibbq.profile = nil
err = ibbq.device.Stop()
ibbq.updateStatus(Disconnected)
go ibbq.disconnectedHandler()
}
} else {
logger.Info("Disconnecting")
ibbq.updateStatus(Disconnecting)
err = ibbq.client.CancelConnection()
if ibbq.device != nil && force {
ibbq.client = nil
ibbq.profile = nil
err = ibbq.device.Stop()
ibbq.updateStatus(Disconnected)
go ibbq.disconnectedHandler()
}
}
return err
}
func filter() ble.AdvFilter {
return func(a ble.Advertisement) bool {
return strings.ToLower(a.LocalName()) == strings.ToLower(DeviceName) && a.Connectable()
}
}
func advHandler() ble.AdvHandler {
return func(a ble.Advertisement) {
logger.Debug("Found advertisement",
"address", a.Addr(),
"connectable", a.Connectable(),
"rssi", a.RSSI(),
"name", a.LocalName(),
"svcs", a.Services(),
"manufacturerData", a.ManufacturerData())
}
}