serial-nci-scale
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -146,3 +146,3 @@ "use strict"; | ||
}).catch(function (e) { | ||
console.log(e); | ||
console.error(e); | ||
@@ -165,3 +165,4 @@ _this2.disconnect(); | ||
return initPort; | ||
}() | ||
}() // Disconnect gracefully | ||
}, { | ||
@@ -171,2 +172,4 @@ key: "disconnect", | ||
var _disconnect = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var _this3 = this; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -177,22 +180,20 @@ while (1) { | ||
if (!this.isConnected) { | ||
_context2.next = 10; | ||
_context2.next = 3; | ||
break; | ||
} | ||
this.isDisconnecting = true; | ||
_context2.next = 4; | ||
return this.getWeight(); | ||
this.isDisconnecting = true; // One last poll loop to unlock the reader before disconnecting | ||
case 4: | ||
// Await one last poll to complete before disconnecting | ||
this.reader.releaseLock(); | ||
this.writer.releaseLock(); | ||
_context2.next = 8; | ||
return this.port.close(); | ||
return _context2.abrupt("return", this.getWeight().then(function () { | ||
_this3.reader.releaseLock(); | ||
case 8: | ||
this.isDisconnecting = false; | ||
this.isConnected = false; | ||
_this3.writer.releaseLock(); | ||
case 10: | ||
return _this3.port.close(); | ||
}).finally(function () { | ||
_this3.isDisconnecting = false; | ||
_this3.isConnected = false; | ||
})); | ||
case 3: | ||
case "end": | ||
@@ -212,11 +213,21 @@ return _context2.stop(); | ||
}, { | ||
key: "readWriteError", | ||
value: function readWriteError(e) { | ||
console.error(e); // Don't bother to disconnect gracefully | ||
this.isConnected = false; | ||
this.isDisconnecting = false; // Try to close the port, but this will likely throw an error | ||
return this.port.close().catch(function (e) { | ||
return console.error(e); | ||
}); | ||
} | ||
}, { | ||
key: "send", | ||
value: function send(data) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
return this.initPort().then(function () { | ||
return _this3.writer.write(data).catch(function (e) { | ||
console.error(e); | ||
_this3.disconnect(); | ||
return _this4.writer.write(data).catch(function (e) { | ||
return _this4.readWriteError(e); | ||
}); | ||
@@ -310,3 +321,3 @@ }); | ||
value: function readLoop() { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -318,12 +329,11 @@ return this.reader.read().then(function (_ref) { | ||
// Keep read loop open while connected | ||
if (_this4.isConnected && !_this4.isDisconnecting) { | ||
_this4.readLoop(); | ||
if (_this5.isConnected && !_this5.isDisconnecting) { | ||
_this5.readLoop(); | ||
} | ||
_this4.responseBuffer = Uint8Array.from([].concat(_toConsumableArray(_this4.responseBuffer), _toConsumableArray(value))); | ||
_this5.responseBuffer = Uint8Array.from([].concat(_toConsumableArray(_this5.responseBuffer), _toConsumableArray(value))); | ||
_this4.flushResponseBuffer(); | ||
_this5.flushResponseBuffer(); | ||
}).catch(function (e) { | ||
console.error(e); | ||
return _this4.disconnect(); | ||
return _this5.readWriteError(e); | ||
}); | ||
@@ -334,13 +344,13 @@ } | ||
value: function startPolling() { | ||
var _this5 = this; | ||
var _this6 = this; | ||
if (!this.isPolling) { | ||
this.initPort().then(function () { | ||
_this5.isPolling = true; | ||
_this6.isPolling = true; | ||
var poll = setInterval(function () { | ||
if (_this5.isConnected && _this5.isPolling && !_this5.isDisconnecting) { | ||
_this5.getWeight(); | ||
if (_this6.isConnected && _this6.isPolling && !_this6.isDisconnecting) { | ||
_this6.getWeight(); | ||
} else { | ||
clearInterval(poll); | ||
_this5.isPolling = false; | ||
_this6.isPolling = false; | ||
} | ||
@@ -359,3 +369,3 @@ }, 250); | ||
value: function getWeight() { | ||
var _this6 = this; | ||
var _this7 = this; | ||
@@ -367,3 +377,3 @@ return new Promise(function (resolve) { | ||
var onWeight = function onWeight(e) { | ||
_this6.removeEventListener('weight', onWeight); | ||
_this7.removeEventListener('weight', onWeight); | ||
@@ -373,5 +383,5 @@ resolve(e.detail); | ||
_this6.addEventListener('weight', onWeight); | ||
_this7.addEventListener('weight', onWeight); | ||
_this6.send(new Uint8Array([W, CR])); | ||
_this7.send(new Uint8Array([W, CR])); | ||
}); | ||
@@ -382,3 +392,3 @@ } | ||
value: function getStatus() { | ||
var _this7 = this; | ||
var _this8 = this; | ||
@@ -392,3 +402,3 @@ return new Promise(function (resolve) { | ||
_this7.removeEventListener('status', onStatus); | ||
_this8.removeEventListener('status', onStatus); | ||
@@ -398,5 +408,5 @@ resolve(detail); | ||
_this7.addEventListener('status', onStatus); | ||
_this8.addEventListener('status', onStatus); | ||
_this7.send(new Uint8Array([S, CR])); | ||
_this8.send(new Uint8Array([S, CR])); | ||
}); | ||
@@ -403,0 +413,0 @@ } |
{ | ||
"name": "serial-nci-scale", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Serial interface for Salter Brecknell PS-USB scales using NCI protocol and SCP-12 layout", | ||
@@ -5,0 +5,0 @@ "main": "dist/serial-nci-scale.js", |
24801
341