From ff1952cd88e264fc26baf30270caa8e929c6fb80 Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 21 Jan 2020 14:24:02 +0000 Subject: [PATCH] added entities as devices and stability in ble --- plejd/api.js | 4 +++- plejd/ble.bluez.js | 39 ++++++++++++++++----------------------- plejd/mqtt.js | 12 ++++++++++-- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/plejd/api.js b/plejd/api.js index 0e41589..207d16a 100644 --- a/plejd/api.js +++ b/plejd/api.js @@ -144,7 +144,9 @@ class PlejdApi extends EventEmitter { name: device.title, type: type, typeName: name, - dimmable: dimmable + dimmable: dimmable, + version: plejdDevice.firmware.version, + serialNumber: plejdDevice.deviceId }; logger(JSON.stringify(newDevice)); diff --git a/plejd/ble.bluez.js b/plejd/ble.bluez.js index 4ffaff3..a25769b 100644 --- a/plejd/ble.bluez.js +++ b/plejd/ble.bluez.js @@ -72,6 +72,13 @@ class PlejdService extends EventEmitter { } this.bleDevices = []; + this.characteristics = { + data: null, + lastData: null, + lastDataProperties: null, + auth: null, + ping: null + }; clearInterval(this.pingRef); const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/'); @@ -301,12 +308,18 @@ class PlejdService extends EventEmitter { const response = this._createChallengeResponse(this.cryptoKey, Buffer.from(challenge)); //logger('responding to authenticate'); await this.characteristics.auth.WriteValue([...response], {}); - - this.emit('authenticated'); } catch (err) { console.log('plejd-ble: error: failed to authenticate: ' + err); } + + // auth done, start ping + await this.startPing(); + + // After we've authenticated, we need to hook up the event listener + // for changes to lastData. + this.characteristics.lastDataProperties.on('PropertiesChanged', this.onLastDataUpdated.bind(this)); + this.characteristics.lastData.StartNotify(); } async write(data, retry = true) { @@ -328,17 +341,6 @@ class PlejdService extends EventEmitter { } } - onAuthenticated() { - // Start ping - logger('onAuthenticated()'); - this.startPing(); - - // After we've authenticated, we need to hook up the event listener - // for changes to lastData. - this.characteristics.lastDataProperties.on('PropertiesChanged', this.onLastDataUpdated.bind(this)); - this.characteristics.lastData.StartNotify(); - } - async startPing() { console.log('startPing()'); clearInterval(this.pingRef); @@ -347,8 +349,6 @@ class PlejdService extends EventEmitter { logger('ping'); await this.ping(); }, 3000); - - await this.ping(); } onPingSuccess(nr) { @@ -480,12 +480,7 @@ class PlejdService extends EventEmitter { return; } - this.emit('deviceCharacteristicsComplete'); - } - - onDeviceCharacteristicsComplete() { - logger('onDeviceCharacteristicsComplete()'); - this.authenticate(); + await this.authenticate(); } async onLastDataUpdated(iface, properties, invalidated) { @@ -549,8 +544,6 @@ class PlejdService extends EventEmitter { console.log('wireEvents()'); const self = this; - this.on('deviceCharacteristicsComplete', this.onDeviceCharacteristicsComplete.bind(self)); - this.on('authenticated', this.onAuthenticated.bind(self)); this.on('pingFailed', this.onPingFailed.bind(self)); this.on('pingSuccess', this.onPingSuccess.bind(self)); } diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 39afbb9..d9607fa 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -35,11 +35,19 @@ const getSettingsTopic = () => `plejd/settings`; const getDiscoveryPayload = device => ({ schema: 'json', name: device.name, - unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, + unique_id: device.serialNumber, state_topic: getStateTopic(device), command_topic: getCommandTopic(device), optimistic: false, - brightness: `${device.dimmable}` + retain: true, + brightness: `${device.dimmable}`, + device: { + identifiers: device.serialNumber, + manufacturer: 'Plejd', + model: device.typeName, + name: device.name, + sw_version: device.version + } }); // #endregion