Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
- var _serialport = require('serialport');
- var _fs = require('fs');
- var fs = _interopRequireWildcard(_fs);
- var _relaxedJson = require('relaxed-json');
- var reportsPath = './reports/';
- /*
- * The idea is to give command through the serial port and then
- * listen to answers via a listener.
- * To avoid accumulation of listeners, every atomic command
- * start with an attempt to open the serial port and end by closing it.
- */
- var MeteorConnection = (function () {
- function MeteorConnection() {
- _classCallCheck(this, MeteorConnection);
- this.port = '/dev/ttyACM0';
- this.baudrate = 115200;
- this.isConnected = false;
- // Init empty fileList.
- this.fileList = [];
- // Check that the directory to stock reports exists.
- try {
- // TODO: in the future check here if there are files to upload.
- fs.readdirSync(reportsPath);
- } catch (e) {
- fs.mkdirSync(reportsPath);
- }
- }
- _createClass(MeteorConnection, [{
- key: 'open',
- value: function open() {
- var _this = this;
- return new Promise(function (resolve, reject) {
- // If already opened, do nothing.
- if (_this.isConnected) {
- resolve();
- } else if (_this.serialPort) {
- // Attributed, but not yet opened.
- _this.serialPort.open(function (error) {
- _this.isConnected = !error && _this.serialPort.isOpen();
- if (_this.isConnected) {
- resolve();
- } else {
- reject();
- }
- });
- } else {
- // Attribute, and open immediatly.
- _this.serialPort = new _serialport.SerialPort(_this.port, {
- baudrate: _this.baudrate,
- parser: _serialport.parsers.raw
- }, true, function (error) {
- _this.isConnected = !error && _this.serialPort.isOpen();
- if (_this.isConnected) {
- resolve();
- } else {
- reject();
- }
- });
- }
- });
- }
- }, {
- key: 'close',
- value: function close() {
- var _this2 = this;
- return new Promise(function (resolve, reject) {
- if (_this2.isConnected) {
- _this2.serialPort.close(function (error) {
- if (error) {
- reject();
- } else {
- _this2.isConnected = false;
- resolve();
- }
- });
- }
- });
- }
- }, {
- key: '_performSpeedTest',
- value: function _performSpeedTest() {
- var _this3 = this;
- return new Promise(function (resolve) {
- var targetPath = './speedTest.log';
- fs.open(targetPath, 'w', function (err, fd) {
- if (!err) {
- _this3.serialPort.write('\r');
- _this3.serialPort.on('data', function (data) {
- fs.write(fd, data.toString());
- });
- _this3.serialPort.write('write\r', function () {
- // Stop logging timeDelta seconds after.
- var timeDelta = 1;
- setTimeout(function () {
- _this3.serialPort.write('\r');
- var speed = fs.fstatSync(fd).size / (timeDelta * 1000);
- _this3.close().then(function () {
- fs.closeSync(fd);
- fs.unlink(targetPath);
- resolve(speed);
- });
- }, timeDelta * 1000);
- });
- }
- });
- });
- }
- }, {
- key: '_getFile',
- value: function _getFile(index) {
- var _this4 = this;
- return new Promise(function (resolve) {
- var targetPath = '' + reportsPath + 'test' + index + '.MTR';
- // TODO: Set the good creation date to the file.
- fs.open(targetPath, 'w', function (err, fd) {
- if (!err) {
- _this4.serialPort.on('data', function (data) {
- if (data.indexOf('file') !== -1) {
- // Removed command. ex "file 0\n\r"
- data = data.slice(Math.max(data.indexOf('\n'), data.indexOf('\r')) + 1);
- }
- // End of transmission is signaled by a buffer of 16 bytes
- // filled with 0xff.
- if (data.slice(-20, -4).compare(new Buffer(Array(16).fill(255))) === 0) {
- // Write the good data.
- data = data.slice(0, -20);
- fs.write(fd, data, 0, data.length);
- _this4.close().then(function () {
- fs.closeSync(fd);
- resolve(index);
- });
- } else {
- fs.write(fd, data, 0, data.length);
- }
- });
- _this4.serialPort.write('file ' + index + '\r');
- }
- });
- });
- }
- }, {
- key: 'listFiles',
- value: function listFiles() {
- var _this5 = this;
- return new Promise(function (resolve) {
- _this5.open().then(function () {
- _this5.serialPort.on('data', function (data) {
- // Remove strings added by serial prompt.
- if (data.indexOf('ls') !== -1) {
- data = data.slice(4);
- }
- if (data.indexOf('ch> ') !== -1) {
- data = data.slice(0, -4);
- }
- try {
- _this5.fileList = JSON.parse((0, _relaxedJson.transform)(data.toString()));
- _this5.close().then(resolve);
- } catch (e) {console.log(e)}
- });
- _this5.serialPort.write('ls\r');
- });
- });
- }
- }, {
- key: 'getFile',
- value: function getFile(index) {
- var _this6 = this;
- return new Promise(function (resolve) {
- _this6.open().then(function () {
- return _this6._getFile(index);
- }).then(resolve);
- });
- }
- }, {
- key: 'getSequentialFilesFrom',
- value: function getSequentialFilesFrom(index) {
- var _this7 = this;
- return new Promise(function (resolve) {
- // Recursive method to get all files > index sequencially.
- if (typeof _this7.fileList[index] === 'undefined') {
- // Last file has been fetched.
- resolve();
- } else {
- // There is an other file to fetch.
- _this7.getFile(_this7.fileList[index].id).then(function (index) {
- return _this7.getSequentialFilesFrom(index + 1);
- }).then(resolve);
- }
- });
- }
- }, {
- key: 'launchSpeedTest',
- value: function launchSpeedTest() {
- var _this8 = this;
- return new Promise(function (resolve) {
- _this8.open().then(function () {
- return _this8._performSpeedTest();
- }, function () {
- return _this8.launchSpeedTest();
- }).then(resolve);
- });
- }
- }, {
- key: 'eraseMemory',
- value: function eraseMemory() {
- var _this9 = this;
- return new Promise(function (resolve) {
- _this9.open().then(function () {
- _this9.serialPort.on('data', function (data) {
- if (data.slice(-16).compare(new Buffer(Array(16).fill(255))) === 0) {
- _this9.close().then(resolve);
- }
- });
- _this9.serialPort.write('erase\r');
- });
- });
- }
- }]);
- return MeteorConnection;
- })();
- exports.MeteorConnection = MeteorConnection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement