Merge pull request #9 from icanos/dev

more stability improvements
This commit is contained in:
Marcus Westin 2019-12-14 15:52:29 +01:00 committed by GitHub
commit 9117dc1d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "Plejd", "name": "Plejd",
"version": "0.0.6", "version": "0.0.7",
"slug": "plejd", "slug": "plejd",
"description": "Adds support for the Swedish home automation devices from Plejd.", "description": "Adds support for the Swedish home automation devices from Plejd.",
"url": "https://github.com/icanos/hassio-plejd/", "url": "https://github.com/icanos/hassio-plejd/",

View file

@ -137,8 +137,18 @@ class Controller extends EventEmitter {
self.peripheral_address = self._reverseBuffer(Buffer.from(String(self.peripheral.address).replace(/\-/g, '').replace(/\:/g, ''), 'hex')); self.peripheral_address = self._reverseBuffer(Buffer.from(String(self.peripheral.address).replace(/\-/g, '').replace(/\:/g, ''), 'hex'));
let successfullyReadCharacteristics = false;
logger('discovering services and characteristics'); logger('discovering services and characteristics');
setTimeout(async () => {
if (!successfullyReadCharacteristics) {
logger('error: timed out when reading characteristics. moving on to next device.');
return await self._internalConnect(idx + 1);
}
}, 10000);
await self.peripheral.discoverSomeServicesAndCharacteristics([PLEJD_SERVICE], [], async (err, services, characteristics) => { await self.peripheral.discoverSomeServicesAndCharacteristics([PLEJD_SERVICE], [], async (err, services, characteristics) => {
//await self.peripheral.discoverAllServicesAndCharacteristics(async (err, services, characteristics) => {
if (err) { if (err) {
console.log('error: failed to discover services: ' + err); console.log('error: failed to discover services: ' + err);
return; return;
@ -168,6 +178,8 @@ class Controller extends EventEmitter {
&& this.authCharacteristic && this.authCharacteristic
&& this.pingCharacteristic) { && this.pingCharacteristic) {
successfullyReadCharacteristics = true;
this.once('authenticated', () => { this.once('authenticated', () => {
logger('Plejd is connected and authenticated.'); logger('Plejd is connected and authenticated.');
this.connectedIndex = idx; this.connectedIndex = idx;
@ -200,7 +212,6 @@ class Controller extends EventEmitter {
} }
return Promise.resolve(true); return Promise.resolve(true);
//});
}); });
}); });
} }
@ -349,10 +360,10 @@ class Controller extends EventEmitter {
try { try {
// make sure we're connected, otherwise, return false and reconnect // make sure we're connected, otherwise, return false and reconnect
if (this.peripheral.state != 'connected') { // if (this.peripheral.state != 'connected') {
callback(false); // callback(false);
return; // return;
} // }
this.pingCharacteristic.write(ping, false, (err) => { this.pingCharacteristic.write(ping, false, (err) => {
if (err) { if (err) {
@ -426,6 +437,7 @@ class Controller extends EventEmitter {
await this.connect(); await this.connect();
} }
logger('writing ' + data + ' to ' + this.peripheral.address);
this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, data), false); this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, data), false);
this.flush(); this.flush();