cleaning up event listeners and updated noble
This commit is contained in:
parent
7918165a65
commit
a8683fc513
4 changed files with 26 additions and 17 deletions
37
plejd/ble.js
37
plejd/ble.js
|
|
@ -57,6 +57,7 @@ class PlejdService extends EventEmitter {
|
|||
this.writeQueue = [];
|
||||
|
||||
this.plejdDevices = {};
|
||||
this.connectEventHooked = false;
|
||||
|
||||
// Holds a reference to all characteristics
|
||||
this.characteristicState = STATE_UNINITIALIZED;
|
||||
|
|
@ -226,9 +227,7 @@ class PlejdService extends EventEmitter {
|
|||
if (self.deviceIdx < Object.keys(self.devices).length) {
|
||||
logger('connection timed out after 10 s. cleaning up and trying next.');
|
||||
|
||||
self.device.removeAllListeners('servicesDiscover');
|
||||
self.device.removeAllListeners('connect');
|
||||
self.device.removeAllListeners('disconnect');
|
||||
self.disconnect();
|
||||
|
||||
self.deviceIdx++;
|
||||
self.connect();
|
||||
|
|
@ -237,27 +236,36 @@ class PlejdService extends EventEmitter {
|
|||
}, 10 * 1000);
|
||||
|
||||
this.state = STATE_CONNECTING;
|
||||
this.device.connect((err) => {
|
||||
self.onDeviceConnected(err);
|
||||
});
|
||||
}
|
||||
|
||||
reset() {
|
||||
console.log('reset()');
|
||||
this.state = STATE_IDLE;
|
||||
if (!this.connectEventHooked) {
|
||||
this.device.once('connect', (state) => {
|
||||
self.onDeviceConnected(state);
|
||||
this.connectEventHooked = false;
|
||||
});
|
||||
this.connectEventHooked = true;
|
||||
}
|
||||
|
||||
this.device.connect();
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
console.log('disconnect()');
|
||||
if (this.state !== STATE_CONNECTED && this.state !== STATE_AUTHENTICATED) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearInterval(this.pingRef);
|
||||
|
||||
this.device.removeAllListeners('servicesDiscover');
|
||||
this.device.removeAllListeners('connect');
|
||||
this.device.removeAllListeners('disconnect');
|
||||
this.characteristics.auth.removeAllListeners('read');
|
||||
this.characteristics.auth.removeAllListeners('write');
|
||||
this.characteristics.data.removeAllListeners('read');
|
||||
this.characteristics.data.removeAllListeners('write');
|
||||
this.characteristics.lastData.removeAllListeners('read');
|
||||
this.characteristics.lastData.removeAllListeners('write');
|
||||
this.characteristics.ping.removeAllListeners('read');
|
||||
this.characteristics.ping.removeAllListeners('write');
|
||||
|
||||
this.connectEventHooked = false;
|
||||
|
||||
this.unsubscribeCharacteristics();
|
||||
this.device.disconnect();
|
||||
|
|
@ -405,7 +413,7 @@ class PlejdService extends EventEmitter {
|
|||
if (err) {
|
||||
console.log('error: failed to connect to device: ' + err + '. picking next.');
|
||||
this.deviceIdx++;
|
||||
this.reset();
|
||||
this.disconnect();
|
||||
this.connect();
|
||||
return;
|
||||
}
|
||||
|
|
@ -489,6 +497,7 @@ class PlejdService extends EventEmitter {
|
|||
|
||||
onDeviceDisconnected() {
|
||||
logger('onDeviceDisconnected()');
|
||||
this.disconnect();
|
||||
|
||||
if (!this.device) {
|
||||
console.log('warning: reconnect will not be performed.');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "Plejd",
|
||||
"version": "0.2.1",
|
||||
"version": "0.2.2",
|
||||
"slug": "plejd",
|
||||
"description": "Adds support for the Swedish home automation devices from Plejd.",
|
||||
"url": "https://github.com/icanos/hassio-plejd/",
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ const mqtt = require('./mqtt');
|
|||
const fs = require('fs');
|
||||
const PlejdService = require('./ble');
|
||||
|
||||
const version = "0.2.0";
|
||||
const version = "0.2.2";
|
||||
|
||||
async function main() {
|
||||
console.log('starting Plejd add-on v. ' + version);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"@abandonware/bluetooth-hci-socket": "0.5.3-3",
|
||||
"@icanos/noble": "^1.9.2-6",
|
||||
"@icanos/noble": "^1.9.2-9",
|
||||
"axios": "^0.19.0",
|
||||
"buffer-xor": "^2.0.2",
|
||||
"fs": "0.0.1-security",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue