UNPKG

node-aurora

Version:

Provides an interface to the Aurora Dreamband.

1,190 lines (873 loc) 49.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuroraSleepStages = exports.AuroraStreamOutputIds = exports.AuroraStreamIds = exports.AuroraLogTypeIds = exports.AuroraEventOutputIds = exports.AuroraEventIds = exports.AuroraConstants = exports.Aurora = undefined; var _toArray2 = require('babel-runtime/helpers/toArray'); var _toArray3 = _interopRequireDefault(_toArray2); var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _AuroraUsb = require('./AuroraUsb'); var _AuroraUsb2 = _interopRequireDefault(_AuroraUsb); var _AuroraBluetooth = require('./AuroraBluetooth'); var _AuroraBluetooth2 = _interopRequireDefault(_AuroraBluetooth); var _drivelist = require('drivelist'); var _drivelist2 = _interopRequireDefault(_drivelist); var _ejectMedia = require('eject-media'); var _ejectMedia2 = _interopRequireDefault(_ejectMedia); var _AuroraConstants = require('./AuroraConstants'); var _AuroraConstants2 = _interopRequireDefault(_AuroraConstants); var _events = require('events'); var _events2 = _interopRequireDefault(_events); var _stream = require('stream'); var _stream2 = _interopRequireDefault(_stream); var _util = require('./util'); var _usbDetection = require('usb-detection'); var _usbDetection2 = _interopRequireDefault(_usbDetection); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var MSD_DISCONNECT_RETRY_DELAY_MS = 2000; var MSD_SCAN_RETRY_DELAY_MS = 2000; var MSD_CONNECT_DELAY_SEC = 30; var Aurora = function (_EventEmitter) { (0, _inherits3.default)(Aurora, _EventEmitter); function Aurora() { var _this2 = this; (0, _classCallCheck3.default)(this, Aurora); var _this = (0, _possibleConstructorReturn3.default)(this, (Aurora.__proto__ || (0, _getPrototypeOf2.default)(Aurora)).call(this)); _this._watchUsb = function () { _this._unwatchUsb(); _usbDetection2.default.on('add:' + parseInt(_AuroraConstants2.default.AURORA_USB_VID), _this._onAuroraUsbAttached); _usbDetection2.default.on('remove:' + parseInt(_AuroraConstants2.default.AURORA_USB_VID), _this._onAuroraUsbDetached); }; _this._unwatchUsb = function () { _usbDetection2.default.removeListener('add:' + parseInt(_AuroraConstants2.default.AURORA_USB_VID), _this._onAuroraUsbAttached); _usbDetection2.default.removeListener('remove:' + parseInt(_AuroraConstants2.default.AURORA_USB_VID), _this._onAuroraUsbDetached); }; _this._onAuroraUsbAttached = function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(device) { return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (device.productId === parseInt(_AuroraConstants2.default.AURORA_USB_MSD_PID)) { _this._findMsdDrive(5).then(_this._msdSetAttached, true); } else if (device.productId === parseInt(_AuroraConstants2.default.AURORA_USB_SERIAL_PID) && _this._autoConnectUsb) { _this.autoConnectUsb(); } case 1: case 'end': return _context.stop(); } } }, _callee, _this2); })); return function (_x) { return _ref.apply(this, arguments); }; }(); _this._onAuroraUsbDetached = function (device) { if (device.productId === parseInt(_AuroraConstants2.default.AURORA_USB_MSD_PID)) { _this._findMsdDrive(5).then(_this._msdSetDetached, false); } }; _this._msdSetAttached = function (msdDrive) { if (!_this._msdDrive && msdDrive) { _this._msdAttaching = false; _this._msdDrive = msdDrive; _this.emit('msdAttachmentChange', msdDrive); } }; _this._msdSetDetached = function (msdDrive) { if (_this._msdDrive && !msdDrive) { _this._msdDrive = false; _this.emit('msdAttachmentChange', false); } }; _this._onUsbConnectionStateChange = function (connectionState, previousConnectionState) { if (connectionState == _AuroraUsb2.default.ConnectionStates.CONNECTED_IDLE && previousConnectionState == _AuroraUsb2.default.ConnectionStates.CONNECTING) { _this._getOsInfo('usb').then(function (cmd) { _this.emit(_this._isFlashing ? 'flashConnectionChange' : 'usbConnectionChange', cmd.response); }).catch(function (error) { _this.disconnectUsb(); }); } else if (connectionState == _AuroraUsb2.default.ConnectionStates.DISCONNECTED && previousConnectionState != _AuroraUsb2.default.ConnectionStates.CONNECTING) { _this.emit(_this._isFlashing ? 'flashConnectionChange' : 'usbConnectionChange', false); } }; _this._onBluetoothConnectionStateChange = function (connectionState, previousConnectionState) { if (connectionState == _AuroraBluetooth2.default.ConnectionStates.CONNECTED_IDLE && previousConnectionState == _AuroraBluetooth2.default.ConnectionStates.CONNECTING) { _this._getOsInfo('bluetooth').then(function (cmd) { _this.emit(_this._isFlashing ? 'flashConnectionChange' : 'bluetoothConnectionChange', cmd.response); }).catch(function (error) { _this.disconnectBluetooth(); }); } else if (connectionState == _AuroraBluetooth2.default.ConnectionStates.DISCONNECTED && previousConnectionState != _AuroraBluetooth2.default.ConnectionStates.CONNECTING) { _this.emit(_this._isFlashing ? 'flashConnectionChange' : 'bluetoothConnectionChange', false); if (_this._autoConnectBluetooth) { _this._auroraBluetooth.connect(0).catch(function () {}); } } }; _this._onCmdInputRequested = function () { if (!_this._cmdCurrent) return; _this.emit('cmdInputRequested', _this._cmdCurrent.inputStream); }; _this._onCmdOutputReady = function (output) { if (!_this._cmdCurrent) return; _this._cmdCurrent.outputStream.push(output); }; _this._onAuroraLog = function (log) { _this.emit('log', log); }; _this._onAuroraStreamData = function (streamData) { _this.emit('streamData', streamData); }; _this._onAuroraEvent = function (auroraEvent) { _this.emit('auroraEvent', auroraEvent); }; _this._onAuroraError = function (error) { _this.emit('auroraError', error); }; _this._auroraUsb = new _AuroraUsb2.default(); _this._auroraUsb.on('connectionStateChange', _this._onUsbConnectionStateChange); _this._auroraUsb.on('usbError', _this._onAuroraError); _this._auroraUsb.on('log', _this._onAuroraLog); _this._auroraUsb.on('streamData', _this._onAuroraStreamData); _this._auroraUsb.on('auroraEvent', _this._onAuroraEvent); _this._auroraUsb.on('cmdInputRequested', _this._onCmdInputRequested); _this._auroraUsb.on('cmdOutputReady', _this._onCmdOutputReady); _this._auroraBluetooth = new _AuroraBluetooth2.default(); _this._auroraBluetooth.on('connectionStateChange', _this._onBluetoothConnectionStateChange); _this._auroraBluetooth.on('bluetoothError', _this._onAuroraError); _this._auroraBluetooth.on('streamData', _this._onAuroraStreamData); _this._auroraBluetooth.on('auroraEvent', _this._onAuroraEvent); _this._auroraBluetooth.on('cmdInputRequested', _this._onCmdInputRequested); _this._auroraBluetooth.on('cmdOutputReady', _this._onCmdOutputReady); _this._cmdQueue = []; _this._msdDrive = false; _this._isFlashing = false; _this._info = {}; //this scans for MSD disks that could potentially be the Aurora _this._findMsdDrive().then(_this._msdSetAttached, true); _this._watchUsb(); return _this; } (0, _createClass3.default)(Aurora, [{ key: 'isConnected', value: function isConnected() { return this.isUsbConnected() || this.isBluetoothConnected(); } }, { key: 'isUsbConnected', value: function isUsbConnected() { return this._auroraUsb.isConnected(); } }, { key: 'isBluetoothConnected', value: function isBluetoothConnected() { return this._auroraBluetooth.isConnected(); } }, { key: 'isMsdAttached', value: function isMsdAttached() { return !!this._msdDrive; } }, { key: 'autoConnectUsb', value: function autoConnectUsb() { this._autoConnectUsb = true; if (!this._auroraUsb.isConnected() && !this._auroraUsb.isConnecting()) { this._auroraUsb.connect().catch(function () {}); } } }, { key: 'autoConnectBluetooth', value: function autoConnectBluetooth() { this._autoConnectBluetooth = true; if (!this._auroraBluetooth.isConnected() && !this._auroraBluetooth.isConnecting()) { this._auroraBluetooth.connect(0).catch(function () {}); } } }, { key: 'connectUsb', value: function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() { var _this3 = this; var port = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'detect'; var retryCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: if (!this._auroraUsb.isConnected()) { _context2.next = 4; break; } return _context2.abrupt('return', _promise2.default.reject('Already connected over usb.')); case 4: if (!this._auroraUsb.isConnecting()) { _context2.next = 6; break; } return _context2.abrupt('return', _promise2.default.reject('Already connecting over usb.')); case 6: return _context2.abrupt('return', new _promise2.default(function (resolve, reject) { _this3.once('usbConnectionChange', function (fwInfo) { if (!fwInfo) return reject(); resolve(fwInfo); }); _this3.detachMsd().then(function () { return _this3._auroraUsb.connect(port, retryCount); }).catch(reject); })); case 7: case 'end': return _context2.stop(); } } }, _callee2, this); })); function connectUsb() { return _ref2.apply(this, arguments); } return connectUsb; }() }, { key: 'disconnectUsb', value: function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() { return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: this._autoConnectUsb = false; if (!(!this._auroraUsb.isConnected() && !this._auroraUsb.isConnecting())) { _context3.next = 3; break; } return _context3.abrupt('return'); case 3: return _context3.abrupt('return', this._auroraUsb.disconnect()); case 4: case 'end': return _context3.stop(); } } }, _callee3, this); })); function disconnectUsb() { return _ref3.apply(this, arguments); } return disconnectUsb; }() }, { key: 'connectBluetooth', value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() { var _this4 = this; var timeoutMs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 20000; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: if (!this._auroraBluetooth.isConnected()) { _context4.next = 2; break; } return _context4.abrupt('return', _promise2.default.reject('Already connected over bluetooth.')); case 2: //is USB is already connected, lets signal to //the Aurora to start advertising aggressively if (this.isUsbConnected()) { this.queueCmd('ble-reset', 'usb'); } if (!this._auroraBluetooth.isConnecting()) { _context4.next = 5; break; } return _context4.abrupt('return', _promise2.default.reject('Already connecting over bluetooth.')); case 5: return _context4.abrupt('return', new _promise2.default(function (resolve, reject) { _this4.once('bluetoothConnectionChange', function (fwInfo) { if (!fwInfo) return reject(); resolve(fwInfo); }); _this4._auroraBluetooth.connect(timeoutMs).catch(reject); })); case 6: case 'end': return _context4.stop(); } } }, _callee4, this); })); function connectBluetooth() { return _ref4.apply(this, arguments); } return connectBluetooth; }() }, { key: 'disconnectBluetooth', value: function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() { return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: this._autoConnectBluetooth = false; if (!(!this._auroraBluetooth.isConnected() && !this._auroraBluetooth.isConnecting())) { _context5.next = 3; break; } return _context5.abrupt('return'); case 3: return _context5.abrupt('return', this._auroraBluetooth.disconnect()); case 4: case 'end': return _context5.stop(); } } }, _callee5, this); })); function disconnectBluetooth() { return _ref5.apply(this, arguments); } return disconnectBluetooth; }() }, { key: 'attachMsd', value: function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() { var i; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: if (this.isConnected()) { _context6.next = 2; break; } return _context6.abrupt('return', _promise2.default.reject('Must have a connection first.')); case 2: if (!this.isMsdAttached()) { _context6.next = 6; break; } return _context6.abrupt('return', _promise2.default.reject('MSD mode already attached.')); case 6: if (!this._msdAttaching) { _context6.next = 8; break; } return _context6.abrupt('return', _promise2.default.reject('Already attaching MSD.')); case 8: this._msdAttaching = true; _context6.prev = 9; _context6.next = 12; return this.queueCmd('usb-mode 2'); case 12: _context6.next = 18; break; case 14: _context6.prev = 14; _context6.t0 = _context6['catch'](9); this._msdAttaching = false; return _context6.abrupt('return', _promise2.default.reject('Failed enabling MSD mode: ' + _context6.t0)); case 18: i = 0; case 19: if (!(i < MSD_CONNECT_DELAY_SEC)) { _context6.next = 27; break; } _context6.next = 22; return (0, _util.sleep)(1000); case 22: if (!this._msdDrive) { _context6.next = 24; break; } return _context6.abrupt('return', this._msdDrive); case 24: i++; _context6.next = 19; break; case 27: this._msdAttaching = false; return _context6.abrupt('return', _promise2.default.reject('Timeout waiting for Aurora MSD drive to mount.')); case 29: case 'end': return _context6.stop(); } } }, _callee6, this, [[9, 14]]); })); function attachMsd() { return _ref6.apply(this, arguments); } return attachMsd; }() }, { key: 'detachMsd', value: function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8() { var _this5 = this; var retryCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5; return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: if (this._msdDrive) { _context8.next = 2; break; } return _context8.abrupt('return'); case 2: _context8.next = 4; return (0, _util.sleep)(1500); case 4: if (this._msdDrive) { _context8.next = 6; break; } return _context8.abrupt('return'); case 6: return _context8.abrupt('return', (0, _util.promisify)(_ejectMedia2.default.eject, _ejectMedia2.default)(this._msdDrive).then(function () { //we go ahead and mark the drive as removed in case //the event hasn't fired yet. _this5._msdSetDetached(); }).catch((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7() { return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: if (!retryCount) { _context7.next = 7; break; } _context7.next = 3; return (0, _util.sleep)(MSD_DISCONNECT_RETRY_DELAY_MS); case 3: if (_this5._msdDrive) { _context7.next = 6; break; } _this5._msdSetDetached(); return _context7.abrupt('return', _promise2.default.resolve()); case 6: return _context7.abrupt('return', _this5.detachMsd(retryCount - 1)); case 7: return _context7.abrupt('return', _this5._findMsdDrive().then(function (msdDrive) { if (msdDrive) return _promise2.default.reject('Failed disconnecting from MSD'); _this5._msdSetDetached(); return _promise2.default.resolve(); })); case 8: case 'end': return _context7.stop(); } } }, _callee7, _this5); })))); case 7: case 'end': return _context8.stop(); } } }, _callee8, this); })); function detachMsd() { return _ref7.apply(this, arguments); } return detachMsd; }() }, { key: 'flash', value: function () { var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(fwFile) { var _this6 = this; var fwVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var fwType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'app'; var wasUsbAutoConnectOff, wasBluetoothAutoConnectOff, wasUsbConnected, wasBluetoothConnected, flashCmd; return _regenerator2.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: if (!this._isFlashing) { _context9.next = 2; break; } return _context9.abrupt('return', _promise2.default.reject('Already flashing.')); case 2: if (this.isConnected()) { _context9.next = 4; break; } return _context9.abrupt('return', _promise2.default.reject('Must be connected to perform flash.')); case 4: //remember whether auto connect was on before flash //since we are going to secretly turn auto connect on now wasUsbAutoConnectOff = !this._autoConnectUsb; wasBluetoothAutoConnectOff = !this._autoConnectBluetooth; wasUsbConnected = this.isUsbConnected(); wasBluetoothConnected = this.isBluetoothConnected(); if (this.isUsbConnected()) { this._autoConnectUsb = true; } if (this.isBluetoothConnected()) { this._autoConnectBluetooth = true; } flashCmd = fwType == 'bootloader' || fwType == 'bootloader-and-bootstrap' ? 'os-flash-bootloader' : fwType == 'ble' ? 'ble-flash' : 'os-flash'; if (this._info.version >= 20100) { flashCmd += ' ' + fwFile + ' /'; if (fwType == 'bootloader-and-bootstrap') { flashCmd += ' 1'; } } return _context9.abrupt('return', this.queueCmd(flashCmd).then(function () { _this6._isFlashing = true; return new _promise2.default(function (resolve, reject) { var onFlashConnectionChange = void 0; var flashTimeout = void 0; var finish = function finish() { if (wasUsbAutoConnectOff && _this6._autoConnectUsb) { _this6._autoConnectUsb = false; } if (wasBluetoothAutoConnectOff && _this6._autoConnectBluetooth) { _this6._autoConnectBluetooth = false; } _this6._isFlashing = false; clearTimeout(flashTimeout); _this6.removeListener('flashConnectionChange', onFlashConnectionChange); if (wasUsbConnected && !_this6.isUsbConnected()) { _this6.emit('usbConnectionChange', false); } if (wasBluetoothConnected && !_this6.isBluetoothConnected()) { setTimeout(function () { if (!_this6.isBluetoothConnected()) { _this6.emit('bluetoothConnectionChange', false); } }, 3000); } }; onFlashConnectionChange = function onFlashConnectionChange(fwInfo) { if (fwInfo) { finish(); var version = fwType == 'bootloader' || fwType == 'bootloader-and-bootstrap' ? fwInfo.bootloaderVersion : fwType == 'ble' ? fwInfo.bleVersion : fwInfo.version; if (!fwVersion || version === fwVersion) { resolve(fwInfo); } else { reject('Flash failed. Expected ' + type + ' version ' + (0, _util.versionToString)(fwVersion) + ' but have ' + (0, _util.versionToString)(version) + '.'); } } }; _this6.on('flashConnectionChange', onFlashConnectionChange); flashTimeout = setTimeout(function () { finish(); reject('Unable to verify flash. Timeout waiting for reconnection.'); }, 50000); }); })); case 13: case 'end': return _context9.stop(); } } }, _callee9, this); })); function flash(_x6) { return _ref9.apply(this, arguments); } return flash; }() }, { key: 'queueCmd', value: function () { var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(commandStr) { var connectorType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'any'; var _this7 = this; var onCmdBegin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var onCmdEnd = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; return _regenerator2.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: if (this._getConnector(connectorType).isConnected()) { _context10.next = 2; break; } return _context10.abrupt('return', _promise2.default.reject('Not connected to Aurora over ' + (connectorType == 'any' ? 'usb or bluetooth' : connectorType) + '.')); case 2: return _context10.abrupt('return', new _promise2.default(function (resolve, reject) { _this7._cmdQueue.push({ commandStr: commandStr, connectorType: connectorType, onCmdBegin: onCmdBegin, onCmdEnd: onCmdEnd, resolve: resolve, reject: reject }); if (!_this7._cmdCurrent) { _this7._processCmdQueue(); } })); case 3: case 'end': return _context10.stop(); } } }, _callee10, this); })); function queueCmd(_x9) { return _ref10.apply(this, arguments); } return queueCmd; }() //this command is really only useful to reconcile differences between //the old version of os-info and new ones //TODO: remove once all in-field units are upgraded to firmware >= 2.1.0 }, { key: '_getOsInfo', value: function () { var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(connectorType) { var _this8 = this; return _regenerator2.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: return _context11.abrupt('return', this.queueCmd('os-info 1', connectorType).catch(function (cmdWithResponse) { //if the "too many arguments" error, then we'll reissue the command without params if (cmdWithResponse.response.error === 3) { return _this8.queueCmd('os-info', connectorType); } return _promise2.default.reject(cmdWithResponse); }).then(function (cmdWithResponse) { if (cmdWithResponse.response.error === 3) { return _this8.queueCmd('os-info', connectorType); } return cmdWithResponse; }).then(function (cmdWithResponse) { if (typeof cmdWithResponse.response.version == 'string') { cmdWithResponse.response.version = (0, _util.stringToVersion)(cmdWithResponse.response.version); } _this8._info = cmdWithResponse.response; return cmdWithResponse; })); case 1: case 'end': return _context11.stop(); } } }, _callee11, this); })); function _getOsInfo(_x13) { return _ref11.apply(this, arguments); } return _getOsInfo; }() }, { key: '_processCmdQueue', value: function _processCmdQueue() { var _this9 = this; this._cmdCurrent = this._cmdQueue.shift(); if (!this._cmdCurrent) { return; } this._cmdCurrent.connector = this._getConnector(this._cmdCurrent.connectorType); if (!this._cmdCurrent.connector.isConnected()) { this._cmdCurrent.reject('No longer connected to Aurora over ' + (this._cmdCurrent.connectorType == 'any' ? 'usb or bluetooth' : this._cmdCurrent.connectorType) + '.'); return; } var _cmdCurrent$commandSt = this._cmdCurrent.commandStr.split(' '), _cmdCurrent$commandSt2 = (0, _toArray3.default)(_cmdCurrent$commandSt), command = _cmdCurrent$commandSt2[0], args = _cmdCurrent$commandSt2.slice(1); this._cmdCurrent.inputStream = new _stream2.default.Writable(); this._cmdCurrent.inputStream._write = function (data, encoding, done) { _this9._cmdCurrent.connector.writeCmdInput(data).then(function () { return done(); }); }; this._cmdCurrent.outputStream = new _stream2.default.Readable(); this._cmdCurrent.outputStream._read = function () {}; var cmd = { command: command, args: args, connectorType: this._cmdCurrent.connectorType, outputStream: this._cmdCurrent.outputStream, beginTime: Date.now() }; this.emit('cmdBegin', cmd); if (this._cmdCurrent.onCmdBegin) { this._cmdCurrent.onCmdBegin(cmd); } this._cmdCurrent.connector.writeCmd(this._cmdCurrent.commandStr).then(function (cmdWithResponse) { cmd.endTime = Date.now(); cmd.origin = cmdWithResponse.origin; cmd.error = cmdWithResponse.error; cmd.response = cmdWithResponse.response; return cmd; }).catch(function (error) { cmd.origin = _this9._cmdCurrent.connectorType == 'any' ? 'unknown' : _this9._cmdCurrent.connectorType; cmd.error = true; cmd.response = { error: -99, message: 'Fatal error: ' + error }; _this9._cmdQueue = []; return cmd; }).then(function () { var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(cmd) { return _regenerator2.default.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: cmd.outputStream.push(null); if (cmd.error) { _this9._cmdCurrent.reject(cmd); } else { _this9._cmdCurrent.resolve(cmd); } if (_this9._cmdCurrent.onCmdEnd) { _this9._cmdCurrent.onCmdEnd(cmd); } _this9.emit('cmdEnd', cmd); //todo this shouldn't be necessary!! //figure out WTF is going on setTimeout(function () { _this9._cmdCurrent = null; _this9._processCmdQueue(); }, 200); case 5: case 'end': return _context12.stop(); } } }, _callee12, _this9); })); return function (_x14) { return _ref12.apply(this, arguments); }; }()); } }, { key: '_getConnector', value: function _getConnector(connector) { switch (connector) { case 'usb': return this._auroraUsb; case 'bluetooth': return this._auroraBluetooth; case 'any': default: return this._auroraUsb.isConnected() ? this._auroraUsb : this._auroraBluetooth; } } }, { key: '_findMsdDrive', value: function () { var _ref13 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee14() { var _this10 = this; var retryCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var successOnFound = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return _regenerator2.default.wrap(function _callee14$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: return _context14.abrupt('return', (0, _util.promisify)(_drivelist2.default.list, _drivelist2.default)().then(function () { var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13(drives) { var drive; return _regenerator2.default.wrap(function _callee13$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: drive = drives.find(function (drive) { return drive.description == _AuroraConstants2.default.MSD_DRIVE_NAME; }); if (!(!drive || !drive.mountpoints.length || !drive.mountpoints[0].path)) { _context13.next = 6; break; } if (!(!retryCount || !successOnFound)) { _context13.next = 4; break; } return _context13.abrupt('return', false); case 4: _context13.next = 8; break; case 6: if (!successOnFound) { _context13.next = 8; break; } return _context13.abrupt('return', drive.mountpoints[0].path); case 8: _context13.next = 10; return (0, _util.sleep)(MSD_SCAN_RETRY_DELAY_MS); case 10: return _context13.abrupt('return', _this10._findMsdDrive(retryCount - 1, successOnFound)); case 11: case 'end': return _context13.stop(); } } }, _callee13, _this10); })); return function (_x17) { return _ref14.apply(this, arguments); }; }())); case 1: case 'end': return _context14.stop(); } } }, _callee14, this); })); function _findMsdDrive() { return _ref13.apply(this, arguments); } return _findMsdDrive; }() }]); return Aurora; }(_events2.default); var AuroraEventIds = _AuroraConstants2.default.EventIds; var AuroraEventOutputIds = _AuroraConstants2.default.EventOutputsIds; var AuroraLogTypeIds = _AuroraConstants2.default.LogTypeIds; var AuroraStreamIds = _AuroraConstants2.default.StreamIds; var AuroraStreamOutputIds = _AuroraConstants2.default.StreamOutputIds; var AuroraSleepStages = _AuroraConstants2.default.SleepStages; exports.Aurora = Aurora; exports.AuroraConstants = _AuroraConstants2.default; exports.AuroraEventIds = AuroraEventIds; exports.AuroraEventOutputIds = AuroraEventOutputIds; exports.AuroraLogTypeIds = AuroraLogTypeIds; exports.AuroraStreamIds = AuroraStreamIds; exports.AuroraStreamOutputIds = AuroraStreamOutputIds; exports.AuroraSleepStages = AuroraSleepStages; Object.defineProperty(Aurora.prototype, 'syncTime', { value: require('./AuroraCmdSyncTime') }); Object.defineProperty(Aurora.prototype, 'readFile', { value: require('./AuroraCmdReadFile') }); Object.defineProperty(Aurora.prototype, 'writeFile', { value: require('./AuroraCmdWriteFile') }); Object.defineProperty(Aurora.prototype, 'downloadFile', { value: require('./AuroraCmdDownloadFile') }); Object.defineProperty(Aurora.prototype, 'uploadFile', { value: require('./AuroraCmdUploadFile') }); Object.defineProperty(Aurora.prototype, 'flashFile', { value: require('./AuroraCmdFlashFile') }); Object.defineProperty(Aurora.prototype, 'getProfiles', { value: require('./AuroraCmdGetProfiles') }); Object.defineProperty(Aurora.prototype, 'setProfiles', { value: require('./AuroraCmdSetProfiles') }); Object.defineProperty(Aurora.prototype, 'getSessions', { value: require('./AuroraCmdGetSessions') }); Object.defineProperty(Aurora.prototype, 'downloadStream', { value: require('./AuroraCmdDownloadStream') }); Object.defineProperty(Aurora.prototype, 'playBuzzSong', { value: require('./AuroraCmdPlayBuzzSong') }); Object.defineProperty(Aurora.prototype, 'playLedEffect', { value: require('./AuroraCmdPlayLedEffect') }); exports.default = new Aurora();