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
33
plejd/ble.js
33
plejd/ble.js
|
|
@ -57,6 +57,7 @@ class PlejdService extends EventEmitter {
|
||||||
this.writeQueue = [];
|
this.writeQueue = [];
|
||||||
|
|
||||||
this.plejdDevices = {};
|
this.plejdDevices = {};
|
||||||
|
this.connectEventHooked = false;
|
||||||
|
|
||||||
// Holds a reference to all characteristics
|
// Holds a reference to all characteristics
|
||||||
this.characteristicState = STATE_UNINITIALIZED;
|
this.characteristicState = STATE_UNINITIALIZED;
|
||||||
|
|
@ -226,9 +227,7 @@ class PlejdService extends EventEmitter {
|
||||||
if (self.deviceIdx < Object.keys(self.devices).length) {
|
if (self.deviceIdx < Object.keys(self.devices).length) {
|
||||||
logger('connection timed out after 10 s. cleaning up and trying next.');
|
logger('connection timed out after 10 s. cleaning up and trying next.');
|
||||||
|
|
||||||
self.device.removeAllListeners('servicesDiscover');
|
self.disconnect();
|
||||||
self.device.removeAllListeners('connect');
|
|
||||||
self.device.removeAllListeners('disconnect');
|
|
||||||
|
|
||||||
self.deviceIdx++;
|
self.deviceIdx++;
|
||||||
self.connect();
|
self.connect();
|
||||||
|
|
@ -237,27 +236,36 @@ class PlejdService extends EventEmitter {
|
||||||
}, 10 * 1000);
|
}, 10 * 1000);
|
||||||
|
|
||||||
this.state = STATE_CONNECTING;
|
this.state = STATE_CONNECTING;
|
||||||
this.device.connect((err) => {
|
|
||||||
self.onDeviceConnected(err);
|
if (!this.connectEventHooked) {
|
||||||
|
this.device.once('connect', (state) => {
|
||||||
|
self.onDeviceConnected(state);
|
||||||
|
this.connectEventHooked = false;
|
||||||
});
|
});
|
||||||
|
this.connectEventHooked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
this.device.connect();
|
||||||
console.log('reset()');
|
|
||||||
this.state = STATE_IDLE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
console.log('disconnect()');
|
console.log('disconnect()');
|
||||||
if (this.state !== STATE_CONNECTED && this.state !== STATE_AUTHENTICATED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
clearInterval(this.pingRef);
|
clearInterval(this.pingRef);
|
||||||
|
|
||||||
this.device.removeAllListeners('servicesDiscover');
|
this.device.removeAllListeners('servicesDiscover');
|
||||||
this.device.removeAllListeners('connect');
|
this.device.removeAllListeners('connect');
|
||||||
this.device.removeAllListeners('disconnect');
|
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.unsubscribeCharacteristics();
|
||||||
this.device.disconnect();
|
this.device.disconnect();
|
||||||
|
|
@ -405,7 +413,7 @@ class PlejdService extends EventEmitter {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('error: failed to connect to device: ' + err + '. picking next.');
|
console.log('error: failed to connect to device: ' + err + '. picking next.');
|
||||||
this.deviceIdx++;
|
this.deviceIdx++;
|
||||||
this.reset();
|
this.disconnect();
|
||||||
this.connect();
|
this.connect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -489,6 +497,7 @@ class PlejdService extends EventEmitter {
|
||||||
|
|
||||||
onDeviceDisconnected() {
|
onDeviceDisconnected() {
|
||||||
logger('onDeviceDisconnected()');
|
logger('onDeviceDisconnected()');
|
||||||
|
this.disconnect();
|
||||||
|
|
||||||
if (!this.device) {
|
if (!this.device) {
|
||||||
console.log('warning: reconnect will not be performed.');
|
console.log('warning: reconnect will not be performed.');
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "Plejd",
|
"name": "Plejd",
|
||||||
"version": "0.2.1",
|
"version": "0.2.2",
|
||||||
"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/",
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ const mqtt = require('./mqtt');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const PlejdService = require('./ble');
|
const PlejdService = require('./ble');
|
||||||
|
|
||||||
const version = "0.2.0";
|
const version = "0.2.2";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('starting Plejd add-on v. ' + version);
|
console.log('starting Plejd add-on v. ' + version);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@abandonware/bluetooth-hci-socket": "0.5.3-3",
|
"@abandonware/bluetooth-hci-socket": "0.5.3-3",
|
||||||
"@icanos/noble": "^1.9.2-6",
|
"@icanos/noble": "^1.9.2-9",
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.19.0",
|
||||||
"buffer-xor": "^2.0.2",
|
"buffer-xor": "^2.0.2",
|
||||||
"fs": "0.0.1-security",
|
"fs": "0.0.1-security",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue