Skip to content

Commit

Permalink
defer promise resolve when giop driver is disconnected, updated gener…
Browse files Browse the repository at this point in the history
…ated JS files
  • Loading branch information
mwittig committed Mar 2, 2019
1 parent 4d0cd1e commit 5dfb89a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/driver/gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ GpioDriver = (function(superClass) {

GpioDriver.prototype.disconnect = function() {
this.vhduino.kill();
return Promise.resolve();
return Promise.delay(1000);
};

GpioDriver.prototype.write = function(data) {
Expand Down
94 changes: 56 additions & 38 deletions lib/driver/serialport.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
var Promise, SerialPort, SerialPortDriver, events,
var Delimiter, Promise, SerialPort, SerialPortDriver, events,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;

events = require('events');

SerialPort = require("serialport");

Delimiter = SerialPort.parsers.Delimiter;

Promise = require('bluebird');

Promise.promisifyAll(SerialPort.prototype);
Expand All @@ -14,14 +16,24 @@ SerialPortDriver = (function(superClass) {
extend(SerialPortDriver, superClass);

function SerialPortDriver(protocolOptions) {
SerialPortDriver.__super__.constructor.call(this);
this.serialPort = new SerialPort(protocolOptions.serialDevice, {
baudrate: protocolOptions.baudrate,
parser: SerialPort.parsers.readline("\r\n"),
baudRate: protocolOptions.baudrate,
autoOpen: false
});
}

SerialPortDriver.prototype._open = function() {
if (!this.serialPort.isOpen) {
return this.serialPort.openAsync();
} else {
return Promise.resolve();
}
};

SerialPortDriver.prototype.connect = function(timeout, retries) {
var resolver;
resolver = null;
this.ready = false;
this.serialPort.removeAllListeners('error');
this.serialPort.removeAllListeners('data');
Expand All @@ -38,48 +50,54 @@ SerialPortDriver = (function(superClass) {
return _this.emit('close');
};
})(this));
return this.serialPort.openAsync().then((function(_this) {
return function() {
var resolver;
resolver = null;
_this.serialPort.on("data", function(data) {
var line, readyLine;
line = data.replace(/\0/g, '').trim();
_this.emit('data', line);
readyLine = line.match(/ready(?: ([a-z]+)-([0-9]+\.[0-9]+\.[0-9]+))?/);
if (readyLine != null) {
_this.ready = true;
_this.emit('ready', {
tag: readyLine[1],
version: readyLine[2]
});
return;
}
this.parser = this.serialPort.pipe(new Delimiter({
delimiter: '\r\n',
encoding: 'ascii'
}));
this.parser.on("data", (function(_this) {
return function(data) {
var line, readyLine;
line = data.replace(/\0/g, '').trim();
_this.emit('data', line);
readyLine = line.match(/ready(?: ([a-z]+)-([0-9]+\.[0-9]+\.[0-9]+))?/);
if (readyLine != null) {
_this.ready = true;
return _this.emit('ready', {
tag: readyLine[1],
version: readyLine[2]
});
} else {
if (!_this.ready) {
_this.serialPort.writeAsync("RESET\n")["catch"](function(error) {
return _this.serialPort.writeAsync("RESET\n")["catch"](function(error) {
return this.emit("error", error);
});
return;
}
return _this.emit('line', line);
});
return new Promise(function(resolve, reject) {
Promise.delay(1000).then(function() {
return _this.serialPort.writeAsync("PING\n")["catch"](reject);
}).done();
resolver = resolve;
return _this.once("ready", resolver);
}).timeout(timeout)["catch"](function(err) {
_this.removeListener("ready", resolver);
_this.serialPort.removeAllListeners('data');
if (err.name === "TimeoutError" && retries > 0) {
_this.emit('reconnect', err);
return _this.connect(timeout, retries - 1);
} else {
throw err;
return _this.emit('line', line);
}
}
};
})(this));
return new Promise((function(_this) {
return function(resolve, reject) {
resolver = resolve;
return _this._open().then(function() {
return Promise.delay(1000).then(function() {
_this.serialPort.writeAsync("PING\n")["catch"](reject);
return _this.once("ready", resolver);
});
});
};
})(this)).timeout(timeout)["catch"]((function(_this) {
return function(err) {
_this.removeListener("ready", resolver);
_this.serialPort.removeAllListeners('data');
if (err.name === "TimeoutError" && retries > 0) {
_this.emit('reconnect', err);
return _this.connect(timeout, retries - 1);
} else {
throw err;
}
};
})(this));
};

Expand Down
3 changes: 1 addition & 2 deletions src/driver/gpio.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ class GpioDriver extends events.EventEmitter
@vhduino.on('close', (code) => @emit 'close' )
@vhduino.on('error', (error) => @emit('error', error) )

# return Promise.resolve()
return new Promise( (resolve, reject) =>
@once("ready", resolve)
@once("error", reject)
)

disconnect: ->
@vhduino.kill()
return Promise.resolve()
return Promise.delay(1000)

write: (data) ->
return new Promise( (resolve, reject) =>
Expand Down

0 comments on commit 5dfb89a

Please sign in to comment.