-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbulb-adapter.js
316 lines (287 loc) · 9.81 KB
/
playbulb-adapter.js
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
/**
* playbulb-adapter.js - MiPow Playbulb adapter.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
'use strict';
let Adapter, Device, Property;
try {
Adapter = require('../adapter');
Device = require('../device');
Property = require('../property');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
const gwa = require('gateway-addon');
Adapter = gwa.Adapter;
Device = gwa.Device;
Property = gwa.Property;
}
const noble = require('noble');
class PlaybulbProperty extends Property {
constructor(device, name, propertyDescription) {
super(device, name, propertyDescription);
this.setCachedValue(propertyDescription.value);
this.device.notifyPropertyChanged(this);
}
/**
* Set the value of the property.
*
* @param {*} value The new value to set
* @returns a promise which resolves to the updated value.
*
* @note it is possible that the updated value doesn't match
* the value passed in.
*/
setValue(value) {
return new Promise((resolve, reject) => {
super.setValue(value).then((updatedValue) => {
resolve(updatedValue);
this.device.notifyPropertyChanged(this);
}).catch((err) => {
reject(err);
});
});
}
}
class PlaybulbDevice extends Device {
constructor(adapter, id, deviceDescription) {
super(adapter, id);
this.name = deviceDescription.name;
this.type = deviceDescription.type;
this['@type'] = deviceDescription['@type'];
this.description = deviceDescription.description;
for (const propertyName in deviceDescription.properties) {
const propertyDescription = deviceDescription.properties[propertyName];
const property = new PlaybulbProperty(this, propertyName,
propertyDescription);
this.properties.set(propertyName, property);
}
}
}
class PlaybulbAdapter extends Adapter {
constructor(addonManager, packageName) {
super(addonManager, 'PlaybulbAdapter', packageName);
addonManager.addAdapter(this);
// Bind to |this| as they get called on the noble object.
this.handleStateChange = this._handleStateChange.bind(this);
this.handleDiscover = this._handleDiscover.bind(this);
this.handleScanStart = this._handleScanStart.bind(this);
this.handleScanStop = this._handleScanStop.bind(this);
// Now we are ready to start discovery.
noble.on('warning', (e) => console.warn('noble warning:', e));
this.scanEnabled = false;
this._startBLEDiscovery();
}
/**
* Start discovering BLE devices.
*/
_startBLEDiscovery() {
this.scanEnabled = true;
noble.on('stateChange', this.handleStateChange);
noble.on('scanStart', this.handleScanStart);
noble.on('scanStop', this.handleScanStop);
noble.on('discover', this.handleDiscover);
// Only manually start if powered on already. Otherwise, wait for state
// change and handle it there.
if (noble._state === 'poweredOn') {
this._startNobleScanning();
}
}
/**
* Stop discovering BLE devices.
*/
_stopBLEDiscovery() {
this._stopNobleScanning();
noble.removeListener('stateChange', this.handleStateChange);
noble.removeListener('scanStart', this.handleScanStart);
noble.removeListener('scanStop', this.handleScanStop);
noble.removeListener('discover', this.handleDiscover);
}
/**
* handle scanEnabled flag. Also, note that those functions will be called on the noble object, so we need to bind then to |this| in the constructor.
* <mrstegeman> those are event callbacks. i found that noble doesn't do well when trying to both scan and communicate with an individual device, so when doing comms, i set a flag.
* <mrstegeman> so, if a scan was started right before comms start, i just shut down the scan in the callback
*/
_handleScanStart() {
if (!this.scanEnabled) {
this._stopNobleScanning();
}
}
_handleScanStop() {
if (this.scanEnabled) {
this._startNobleScanning();
}
}
_handleStateChange(state) {
if (state === 'poweredOn' && this.scanEnabled) {
this._startNobleScanning();
} else {
this._stopNobleScanning();
}
}
/**
* We discovered a BLE device! Let's see if it's a Playbulb and add it.
*/
_handleDiscover(peripheral) {
if (peripheral.advertisement.localName &&
peripheral.advertisement.localName.startsWith('PLAYBULB ') &&
peripheral.advertisement.manufacturerData.toString('hex').startsWith('4d49') &&
peripheral.connectable) {
// The localName says it's a Playbulb, the manufacturerData starts with
// the MiPow identifier, and it's connectable, so we should be good. :)
console.log('Playbulb device discovered: ' + peripheral.advertisement.localName);
console.log(' address: ' + peripheral.address + ', RSSI ' + peripheral.rssi);
console.log(' manufacturer data: ' + peripheral.advertisement.manufacturerData.toString('hex'));
console.log(' advertised services: ' + JSON.stringify(peripheral.advertisement.serviceUuids));
/*
peripheral.connect(function(error) {
peripheral.discoverServices(null, function(error, services) {
for (var si in services) {
var deviceInformationService = services[si];
console.log('discovered device information service ' + services[si].uuid);
deviceInformationService.discoverCharacteristics(null, function(error, characteristics) {
console.log('discovered the following characteristics:');
for (var ci in characteristics) {
console.log(' ' + ci + ' uuid: ' + characteristics[ci].uuid);
}
});
}
});
peripheral.disconnect();
});
*/
console.log();
// Actually construct and add device.
var device = new PlaybulbDevice(this, 'playbulb-' + peripheral.address, {
name: peripheral.advertisement.localName,
'@type': ['Light'],
type: 'light',
description: peripheral.advertisement.localName + ' ' + peripheral.address,
properties: {
on: {
'@type': 'OnOffProperty',
label: 'On/Off',
name: 'on',
type: 'boolean',
value: false,
},
color: {
'@type': 'ColorProperty',
label: 'Color',
name: 'color',
type: 'string',
value: '#FFFFFF',
},
},
});
this.handleDeviceAdded(device);
}
else {
// In production, we want to be silent here. For now, emit debug info.
console.log('Ignoring non-Playbulb device: ' + peripheral.advertisement.localName + ' (' + peripheral.address +')');
}
}
/**
* Pure helper function to start noble scanning.
*/
_startNobleScanning() {
console.log('Start scanning for Playbulb devices...');
noble.startScanning();
}
/**
* Pure helper function to stop noble scanning.
*/
_stopNobleScanning() {
console.log('Stop scanning for Playbulb devices.');
noble.stopScanning();
}
/**
* Process to add a new device to the adapter.
*
* The important part is to call: `this.handleDeviceAdded(device)`
*
* @param {String} deviceId ID of the device to add.
* @param {String} deviceDescription Description of the device to add.
* @return {Promise} which resolves to the device added.
*/
addDevice(deviceId, deviceDescription) {
return new Promise((resolve, reject) => {
if (deviceId in this.devices) {
reject(`Device: ${deviceId} already exists.`);
} else {
const device = new PlaybulbDevice(this, deviceId, deviceDescription);
this.handleDeviceAdded(device);
resolve(device);
}
});
}
/**
* Process to remove a device from the adapter.
*
* The important part is to call: `this.handleDeviceRemoved(device)`
*
* @param {String} deviceId ID of the device to remove.
* @return {Promise} which resolves to the device removed.
*/
removeDevice(deviceId) {
return new Promise((resolve, reject) => {
const device = this.devices[deviceId];
if (device) {
this.handleDeviceRemoved(device);
resolve(device);
} else {
reject(`Device: ${deviceId} not found.`);
}
});
}
/**
* Start the pairing/discovery process.
*
* @param {Number} timeoutSeconds Number of seconds to run before timeout
*/
startPairing(_timeoutSeconds) {
console.log('PlaybulbAdapter:', this.name,
'id', this.id, 'pairing started');
this._startBLEDiscovery();
}
/**
* Cancel the pairing/discovery process.
*/
cancelPairing() {
console.log('PlaybulbAdapter:', this.name, 'id', this.id,
'pairing cancelled');
this._stopBLEDiscovery();
}
/**
* Unpair the provided the device from the adapter.
*
* @param {Object} device Device to unpair with
*/
removeThing(device) {
console.log('PlaybulbAdapter:', this.name, 'id', this.id,
'removeThing(', device.id, ') started');
this.removeDevice(device.id).then(() => {
console.log('PlaybulbAdapter: device:', device.id, 'was unpaired.');
}).catch((err) => {
console.error('PlaybulbAdapter: unpairing', device.id, 'failed');
console.error(err);
});
}
/**
* Cancel unpairing process.
*
* @param {Object} device Device that is currently being paired
*/
cancelRemoveThing(device) {
console.log('PlaybulbAdapter:', this.name, 'id', this.id,
'cancelRemoveThing(', device.id, ')');
}
}
function loadPlaybulbAdapter(addonManager, manifest, _errorCallback) {
const adapter = new PlaybulbAdapter(addonManager, manifest.name);
}
module.exports = loadPlaybulbAdapter;