diff --git a/plejd/config.json b/plejd/config.json index 30a6620..1fb98a4 100644 --- a/plejd/config.json +++ b/plejd/config.json @@ -1,6 +1,6 @@ { "name": "Plejd", - "version": "0.0.6", + "version": "0.0.7", "slug": "plejd", "description": "Adds support for the Swedish home automation devices from Plejd.", "url": "https://github.com/icanos/hassio-plejd/", diff --git a/plejd/plejd.js b/plejd/plejd.js index a7bc99c..17d90c6 100644 --- a/plejd/plejd.js +++ b/plejd/plejd.js @@ -137,8 +137,18 @@ class Controller extends EventEmitter { self.peripheral_address = self._reverseBuffer(Buffer.from(String(self.peripheral.address).replace(/\-/g, '').replace(/\:/g, ''), 'hex')); + let successfullyReadCharacteristics = false; 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.discoverAllServicesAndCharacteristics(async (err, services, characteristics) => { if (err) { console.log('error: failed to discover services: ' + err); return; @@ -168,6 +178,8 @@ class Controller extends EventEmitter { && this.authCharacteristic && this.pingCharacteristic) { + successfullyReadCharacteristics = true; + this.once('authenticated', () => { logger('Plejd is connected and authenticated.'); this.connectedIndex = idx; @@ -200,7 +212,6 @@ class Controller extends EventEmitter { } return Promise.resolve(true); - //}); }); }); } @@ -349,10 +360,10 @@ class Controller extends EventEmitter { try { // make sure we're connected, otherwise, return false and reconnect - if (this.peripheral.state != 'connected') { - callback(false); - return; - } + // if (this.peripheral.state != 'connected') { + // callback(false); + // return; + // } this.pingCharacteristic.write(ping, false, (err) => { if (err) { @@ -426,6 +437,7 @@ class Controller extends EventEmitter { await this.connect(); } + logger('writing ' + data + ' to ' + this.peripheral.address); this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, data), false); this.flush();