added error handling for ping as well

This commit is contained in:
Marcus Westin 2019-12-05 08:53:22 +01:00
parent 20c41cceeb
commit c36bea1c5c

View file

@ -294,7 +294,7 @@ class Controller extends EventEmitter {
this.pingRef = setInterval(async () => { this.pingRef = setInterval(async () => {
logger('ping'); logger('ping');
if (self.isConnected) { if (self.isConnected) {
self.plejdPing(async (pingOk) => { await self.plejdPing(async (pingOk) => {
if (!pingOk) { if (!pingOk) {
logger('ping failed'); logger('ping failed');
@ -313,9 +313,10 @@ class Controller extends EventEmitter {
}, 3000); }, 3000);
} }
plejdPing(callback) { async plejdPing(callback) {
var ping = crypto.randomBytes(1); var ping = crypto.randomBytes(1);
try {
this.pingCharacteristic.write(ping, false, (err) => { this.pingCharacteristic.write(ping, false, (err) => {
if (err) { if (err) {
console.log('error: unable to send ping: ' + err); console.log('error: unable to send ping: ' + err);
@ -337,6 +338,12 @@ class Controller extends EventEmitter {
}); });
}); });
} }
catch (error) {
console.log('error: writing to plejd: ' + error);
await self.disconnect();
await self.connect();
}
}
async authenticate() { async authenticate() {
const self = this; const self = this;
@ -383,11 +390,11 @@ class Controller extends EventEmitter {
await this.connect(); await this.connect();
} }
await this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, data), false); this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, data), false);
let writeData; let writeData;
while ((writeData = this.writeQueue.shift()) !== undefined) { while ((writeData = this.writeQueue.shift()) !== undefined) {
await this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, writeData), false); this.dataCharacteristic.write(this._encryptDecrypt(this.cryptoKey, this.peripheral_address, writeData), false);
} }
if (!this.keepAlive) { if (!this.keepAlive) {