added entities as devices and stability in ble

This commit is contained in:
Marcus Westin 2020-01-21 14:24:02 +00:00
parent 85a194b794
commit ff1952cd88
3 changed files with 29 additions and 26 deletions

View file

@ -144,7 +144,9 @@ class PlejdApi extends EventEmitter {
name: device.title, name: device.title,
type: type, type: type,
typeName: name, typeName: name,
dimmable: dimmable dimmable: dimmable,
version: plejdDevice.firmware.version,
serialNumber: plejdDevice.deviceId
}; };
logger(JSON.stringify(newDevice)); logger(JSON.stringify(newDevice));

View file

@ -72,6 +72,13 @@ class PlejdService extends EventEmitter {
} }
this.bleDevices = []; this.bleDevices = [];
this.characteristics = {
data: null,
lastData: null,
lastDataProperties: null,
auth: null,
ping: null
};
clearInterval(this.pingRef); clearInterval(this.pingRef);
const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/'); 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)); const response = this._createChallengeResponse(this.cryptoKey, Buffer.from(challenge));
//logger('responding to authenticate'); //logger('responding to authenticate');
await this.characteristics.auth.WriteValue([...response], {}); await this.characteristics.auth.WriteValue([...response], {});
this.emit('authenticated');
} }
catch (err) { catch (err) {
console.log('plejd-ble: error: failed to authenticate: ' + 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) { 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() { async startPing() {
console.log('startPing()'); console.log('startPing()');
clearInterval(this.pingRef); clearInterval(this.pingRef);
@ -347,8 +349,6 @@ class PlejdService extends EventEmitter {
logger('ping'); logger('ping');
await this.ping(); await this.ping();
}, 3000); }, 3000);
await this.ping();
} }
onPingSuccess(nr) { onPingSuccess(nr) {
@ -480,12 +480,7 @@ class PlejdService extends EventEmitter {
return; return;
} }
this.emit('deviceCharacteristicsComplete'); await this.authenticate();
}
onDeviceCharacteristicsComplete() {
logger('onDeviceCharacteristicsComplete()');
this.authenticate();
} }
async onLastDataUpdated(iface, properties, invalidated) { async onLastDataUpdated(iface, properties, invalidated) {
@ -549,8 +544,6 @@ class PlejdService extends EventEmitter {
console.log('wireEvents()'); console.log('wireEvents()');
const self = this; 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('pingFailed', this.onPingFailed.bind(self));
this.on('pingSuccess', this.onPingSuccess.bind(self)); this.on('pingSuccess', this.onPingSuccess.bind(self));
} }

View file

@ -35,11 +35,19 @@ const getSettingsTopic = () => `plejd/settings`;
const getDiscoveryPayload = device => ({ const getDiscoveryPayload = device => ({
schema: 'json', schema: 'json',
name: device.name, name: device.name,
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, unique_id: device.serialNumber,
state_topic: getStateTopic(device), state_topic: getStateTopic(device),
command_topic: getCommandTopic(device), command_topic: getCommandTopic(device),
optimistic: false, 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 // #endregion