-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bluetooth fixes & improvements (#921)
* Fix read and logs + add getCharacteristics * Bluetooth device creation/deletion * Fix BLE load devices Co-authored-by: Pierre-Gilles Leymarie <pierregilles.leymarie@gmail.com>
- Loading branch information
1 parent
a821cfa
commit 6f92880
Showing
27 changed files
with
627 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../lib/utils/bluetooth.getCharacteristic.js → ...b/commands/bluetooth.getCharacteristic.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const Promise = require('bluebird'); | ||
|
||
const logger = require('../../../../utils/logger'); | ||
const { EVENTS } = require('../../../../utils/constants'); | ||
|
||
const { decodeValue } = require('../device/bluetooth.information'); | ||
|
||
/** | ||
* @description Poll value of a Bluetooth device | ||
* @param {Object} device - The device to control. | ||
* @returns {Promise} Promise of all read values. | ||
* @example | ||
* await bluetooth.poll({ external_id: 'bluetooth:uuid'}); | ||
*/ | ||
async function poll(device) { | ||
const [, peripheralUuid] = device.external_id.split(':'); | ||
|
||
const readFeature = (feature, peripheral) => { | ||
const featureExternalId = feature.external_id; | ||
const [, , serviceUuid, characteristicUuid] = featureExternalId.split(':'); | ||
|
||
return this.readDevice(peripheral, serviceUuid, characteristicUuid) | ||
.then((value) => { | ||
const state = decodeValue(serviceUuid, characteristicUuid, feature, value); | ||
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, { | ||
device_feature_external_id: featureExternalId, | ||
state, | ||
}); | ||
return state; | ||
}) | ||
.catch((e) => { | ||
logger.warn(e.message); | ||
return Promise.resolve(); | ||
}); | ||
}; | ||
|
||
const readFeatures = (peripheral) => { | ||
return Promise.map(device.features, (feature) => readFeature(feature, peripheral), { concurrency: 1 }); | ||
}; | ||
|
||
return this.applyOnPeripheral(peripheralUuid, readFeatures); | ||
} | ||
|
||
module.exports = { | ||
poll, | ||
}; |
47 changes: 47 additions & 0 deletions
47
server/services/bluetooth/lib/commands/bluetooth.postCreate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const Promise = require('bluebird'); | ||
|
||
const logger = require('../../../../utils/logger'); | ||
const { EVENTS } = require('../../../../utils/constants'); | ||
const { decodeValue } = require('../device/bluetooth.information'); | ||
|
||
/** | ||
* @description Subscribe to peripheral notification on device creation. | ||
* @param {Object} device - Newly created Gladys device. | ||
* @returns {Promise} All subscription promises. | ||
* @example | ||
* await bluetooth.postCreate(device); | ||
*/ | ||
async function postCreate(device) { | ||
const [, peripheralUuid] = device.external_id.split(':'); | ||
|
||
const subscribe = (peripheral) => { | ||
return Promise.map( | ||
device.features, | ||
(feature) => { | ||
const [, , serviceUuid, characteristicUuid] = feature.external_id.split(':'); | ||
|
||
const onNotify = (value) => { | ||
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, { | ||
device_feature_external_id: feature.external_id, | ||
state: decodeValue(serviceUuid, characteristicUuid, feature, value), | ||
}); | ||
}; | ||
|
||
return this.subscribeDevice(peripheral, serviceUuid, characteristicUuid, onNotify); | ||
}, | ||
{ concurrency: 1 }, | ||
).catch((e) => { | ||
logger.error(e.message); | ||
return Promise.resolve(); | ||
}); | ||
}; | ||
|
||
return this.applyOnPeripheral(peripheralUuid, subscribe, true).catch((e) => { | ||
logger.error(e.message); | ||
return Promise.resolve(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
postCreate, | ||
}; |
37 changes: 37 additions & 0 deletions
37
server/services/bluetooth/lib/commands/bluetooth.postDelete.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const Promise = require('bluebird'); | ||
|
||
const logger = require('../../../../utils/logger'); | ||
|
||
/** | ||
* @description Unsubscribe to peripheral notification on device delete. | ||
* @param {Object} device - Newly created Gladys device. | ||
* @returns {Promise} All subscription promises. | ||
* @example | ||
* await bluetooth.postDelete(device); | ||
*/ | ||
async function postDelete(device) { | ||
const [, peripheralUuid] = device.external_id.split(':'); | ||
|
||
const unsubscribe = (peripheral) => { | ||
return Promise.map( | ||
device.features, | ||
(feature) => { | ||
const [, , serviceUuid, characteristicUuid] = feature.external_id.split(':'); | ||
return this.unsubscribeDevice(peripheral, serviceUuid, characteristicUuid); | ||
}, | ||
{ concurrency: 1 }, | ||
).catch((e) => { | ||
logger.error(e.message); | ||
return Promise.resolve(); | ||
}); | ||
}; | ||
|
||
return this.applyOnPeripheral(peripheralUuid, unsubscribe, true).catch((e) => { | ||
logger.error(e.message); | ||
return Promise.resolve(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
postDelete, | ||
}; |
45 changes: 8 additions & 37 deletions
45
server/services/bluetooth/lib/commands/bluetooth.readDevice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
server/services/bluetooth/lib/commands/bluetooth.subscribeDevice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Promise = require('bluebird'); | ||
|
||
const { subscribe } = require('../utils/characteristic/bluetooth.subscribe'); | ||
const { read } = require('../utils/characteristic/bluetooth.read'); | ||
|
||
/** | ||
* @description Subscribes to peripheral characteristic. | ||
* @param {Object} peripheral - Connected Noble peripheral. | ||
* @param {string} serviceUuid - Service UUID. | ||
* @param {string} characteristicUuid - Characteristic UUID. | ||
* @param {Object} onNotify - Value callback. | ||
* @returns {Promise<Object>} The write value. | ||
* @example | ||
* await subscribeDevice({ uuid: 'peripheral' }, 'service1', 'char1', () => console.log('done')) | ||
*/ | ||
async function subscribeDevice(peripheral, serviceUuid, characteristicUuid, onNotify) { | ||
return this.getCharacteristic(peripheral, serviceUuid, characteristicUuid).then((characteristic) => | ||
subscribe(characteristic, onNotify).then(() => read(characteristic).then((value) => onNotify(value))), | ||
); | ||
} | ||
|
||
module.exports = { | ||
subscribeDevice, | ||
}; |
35 changes: 0 additions & 35 deletions
35
server/services/bluetooth/lib/commands/bluetooth.subscribePeripheral.js
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
server/services/bluetooth/lib/commands/bluetooth.unsubscribeDevice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const Promise = require('bluebird'); | ||
|
||
const { unsubscribe } = require('../utils/characteristic/bluetooth.unsubscribe'); | ||
|
||
/** | ||
* @description Unsubscribes to peripheral characteristic. | ||
* @param {Object} peripheral - Connected Noble peripheral. | ||
* @param {string} serviceUuid - Service UUID. | ||
* @param {string} characteristicUuid - Characteristic UUID. | ||
* @returns {Promise<Object>} Unscription status. | ||
* @example | ||
* await subscribeDevice({ uuid: 'peripheral' }, 'service1', 'char1') | ||
*/ | ||
async function unsubscribeDevice(peripheral, serviceUuid, characteristicUuid) { | ||
return this.getCharacteristic(peripheral, serviceUuid, characteristicUuid).then((characteristic) => | ||
unsubscribe(characteristic), | ||
); | ||
} | ||
|
||
module.exports = { | ||
unsubscribeDevice, | ||
}; |
10 changes: 4 additions & 6 deletions
10
server/services/bluetooth/lib/commands/bluetooth.writeDevice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.