Skip to content

Commit

Permalink
Fix #2
Browse files Browse the repository at this point in the history
Retry mechanism + one connection at a time
  • Loading branch information
binsentsu committed Jan 18, 2020
1 parent 299bf88 commit 32f6bad
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ if (expressPort) {
new WebBinding(devices, expressPort, debugLog);
}

noble.on('warning', (message) => {debugLog(message)});

noble.on('discover', peripheral => {
let id = peripheral.address !== undefined ? peripheral.address.replace(/:/g, '') : undefined;

Expand Down
100 changes: 69 additions & 31 deletions src/am43.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const HEX_KEY_STOP_BLINDS = "00ff00009a0a01cc5d";


class am43 extends EventEmitter {
static busyDevice = null;

constructor(id, peripheral, noble) {
super();
this.log = require('debug')(`am43:${id}`);
Expand All @@ -20,63 +22,99 @@ class am43 extends EventEmitter {
this.connecttime = null;
this.lastaction = null;
this.state = null;
this.currentRetry = 0;
this.maxRetries = 30;
this.success = false;

Object.defineProperty(this, '_init', {set: function(state) {
Object.defineProperty(this, '_init', {
set: function (state) {
this.emit('initPerformed', this.getState());
}});
}
});
}

writeLog(pLogLine) {
this.log(pLogLine);
}

writeKey(handle, key) {
this.peripheral.connect((error) => {
if (error) {
this.log('ERROR' + error);
this.disconnect();
return;
}
this.connecttime = new Date();
this.log('AM43 connected');
this.peripheral.writeHandle(handle, Buffer.from(key, "hex"), true, (error) => {
if(error)
if (am43.busyDevice != null && am43.busyDevice.id !== this.id) {
this.writeLog('Connection busy for other device, waiting...');
setTimeout(() => {
this.writeKey(handle, key)
}, 1000);
return;
}
this.success = false;
am43.busyDevice = this;
this.peripheral.connect();
this.peripheral.once('connect', handleDeviceConnected);
this.peripheral.once('disconnect', disconnectMe);
const self = this;

function handleDeviceConnected() {
self.connecttime = new Date();
self.writeLog('AM43 connected');
self.peripheral.writeHandle(handle, Buffer.from(key, "hex"), true, handleWriteDone);
}

function disconnectMe() {
self.writeLog('disconnected');
if (self.success === false) {
if (self.currentRetry < self.maxRetries) {
self.writeLog("Writing unsuccessful, retrying in 1 second...");
self.currentRetry = self.currentRetry + 1;
setTimeout(() => {
self.writeKey(handle, key)
}, 1000);
}
else
{
this.log('ERROR' + error);
self.writeLog("Writing unsuccessful, giving up...");
am43.busyDevice = null;
self.currentRetry = 0;
}
this.log('key written');
setTimeout(() => {
this.disconnect();
this.emit('stateChanged', this.getState());
}, 1000);
} else {
self.writeLog("Writing was successful");
am43.busyDevice = null;
self.currentRetry = 0;
self.emit('stateChanged', self.getState());
}
}

});
});
}
function handleWriteDone(error) {
if (error) {
self.writeLog('ERROR' + error);
} else {
self.writeLog('key written');
self.success = true;
}

disconnect()
{
this.peripheral.disconnect(() => {
this.log('disconnected');
});
setTimeout(() => {
self.peripheral.disconnect();
}, 1000);
}
}

am43Init()
{
am43Init() {
this._init = true;
}

am43Open() {
this.writeKey(AM43HANDLE, HEX_KEY_OPEN_BLINDS);
this.lastaction='OPEN';
this.lastaction = 'OPEN';
this.state = 'OPEN';
}

am43Close() {
this.writeKey(AM43HANDLE, HEX_KEY_CLOSE_BLINDS);
this.lastaction='CLOSE';
this.lastaction = 'CLOSE';
this.state = 'CLOSED';
}

am43Stop() {
this.writeKey(AM43HANDLE, HEX_KEY_STOP_BLINDS);
this.lastaction='STOP';
this.lastaction = 'STOP';
this.state = 'OPEN';
}

Expand Down

0 comments on commit 32f6bad

Please sign in to comment.