Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. 'use strict';
  2.  
  3. Object.defineProperty(exports, '__esModule', {
  4. value: true
  5. });
  6.  
  7. 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; }; })();
  8.  
  9. 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; } }
  10.  
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  12.  
  13. var _serialport = require('serialport');
  14.  
  15. var _fs = require('fs');
  16.  
  17. var fs = _interopRequireWildcard(_fs);
  18.  
  19. var _relaxedJson = require('relaxed-json');
  20.  
  21. var reportsPath = './reports/';
  22.  
  23.  
  24. /*
  25. * The idea is to give command through the serial port and then
  26. * listen to answers via a listener.
  27. * To avoid accumulation of listeners, every atomic command
  28. * start with an attempt to open the serial port and end by closing it.
  29. */
  30.  
  31. var MeteorConnection = (function () {
  32. function MeteorConnection() {
  33. _classCallCheck(this, MeteorConnection);
  34.  
  35. this.port = '/dev/ttyACM0';
  36. this.baudrate = 115200;
  37. this.isConnected = false;
  38.  
  39. // Init empty fileList.
  40. this.fileList = [];
  41.  
  42. // Check that the directory to stock reports exists.
  43. try {
  44. // TODO: in the future check here if there are files to upload.
  45. fs.readdirSync(reportsPath);
  46. } catch (e) {
  47. fs.mkdirSync(reportsPath);
  48. }
  49. }
  50.  
  51. _createClass(MeteorConnection, [{
  52. key: 'open',
  53. value: function open() {
  54. var _this = this;
  55.  
  56. return new Promise(function (resolve, reject) {
  57. // If already opened, do nothing.
  58. if (_this.isConnected) {
  59. resolve();
  60. } else if (_this.serialPort) {
  61. // Attributed, but not yet opened.
  62. _this.serialPort.open(function (error) {
  63. _this.isConnected = !error && _this.serialPort.isOpen();
  64. if (_this.isConnected) {
  65. resolve();
  66. } else {
  67. reject();
  68. }
  69. });
  70. } else {
  71. // Attribute, and open immediatly.
  72. _this.serialPort = new _serialport.SerialPort(_this.port, {
  73. baudrate: _this.baudrate,
  74. parser: _serialport.parsers.raw
  75. }, true, function (error) {
  76. _this.isConnected = !error && _this.serialPort.isOpen();
  77. if (_this.isConnected) {
  78. resolve();
  79. } else {
  80. reject();
  81. }
  82. });
  83. }
  84. });
  85. }
  86. }, {
  87. key: 'close',
  88. value: function close() {
  89. var _this2 = this;
  90.  
  91. return new Promise(function (resolve, reject) {
  92. if (_this2.isConnected) {
  93. _this2.serialPort.close(function (error) {
  94. if (error) {
  95. reject();
  96. } else {
  97. _this2.isConnected = false;
  98. resolve();
  99. }
  100. });
  101. }
  102. });
  103. }
  104. }, {
  105. key: '_performSpeedTest',
  106. value: function _performSpeedTest() {
  107. var _this3 = this;
  108.  
  109. return new Promise(function (resolve) {
  110. var targetPath = './speedTest.log';
  111. fs.open(targetPath, 'w', function (err, fd) {
  112. if (!err) {
  113. _this3.serialPort.write('\r');
  114. _this3.serialPort.on('data', function (data) {
  115. fs.write(fd, data.toString());
  116. });
  117. _this3.serialPort.write('write\r', function () {
  118. // Stop logging timeDelta seconds after.
  119. var timeDelta = 1;
  120. setTimeout(function () {
  121. _this3.serialPort.write('\r');
  122. var speed = fs.fstatSync(fd).size / (timeDelta * 1000);
  123. _this3.close().then(function () {
  124. fs.closeSync(fd);
  125. fs.unlink(targetPath);
  126. resolve(speed);
  127. });
  128. }, timeDelta * 1000);
  129. });
  130. }
  131. });
  132. });
  133. }
  134. }, {
  135. key: '_getFile',
  136. value: function _getFile(index) {
  137. var _this4 = this;
  138.  
  139. return new Promise(function (resolve) {
  140. var targetPath = '' + reportsPath + 'test' + index + '.MTR';
  141. // TODO: Set the good creation date to the file.
  142. fs.open(targetPath, 'w', function (err, fd) {
  143. if (!err) {
  144. _this4.serialPort.on('data', function (data) {
  145. if (data.indexOf('file') !== -1) {
  146. // Removed command. ex "file 0\n\r"
  147. data = data.slice(Math.max(data.indexOf('\n'), data.indexOf('\r')) + 1);
  148. }
  149. // End of transmission is signaled by a buffer of 16 bytes
  150. // filled with 0xff.
  151. if (data.slice(-20, -4).compare(new Buffer(Array(16).fill(255))) === 0) {
  152. // Write the good data.
  153. data = data.slice(0, -20);
  154. fs.write(fd, data, 0, data.length);
  155. _this4.close().then(function () {
  156. fs.closeSync(fd);
  157. resolve(index);
  158. });
  159. } else {
  160. fs.write(fd, data, 0, data.length);
  161. }
  162. });
  163. _this4.serialPort.write('file ' + index + '\r');
  164. }
  165. });
  166. });
  167. }
  168. }, {
  169. key: 'listFiles',
  170. value: function listFiles() {
  171. var _this5 = this;
  172.  
  173. return new Promise(function (resolve) {
  174. _this5.open().then(function () {
  175. _this5.serialPort.on('data', function (data) {
  176. // Remove strings added by serial prompt.
  177. if (data.indexOf('ls') !== -1) {
  178. data = data.slice(4);
  179. }
  180. if (data.indexOf('ch> ') !== -1) {
  181. data = data.slice(0, -4);
  182. }
  183. try {
  184. _this5.fileList = JSON.parse((0, _relaxedJson.transform)(data.toString()));
  185. _this5.close().then(resolve);
  186. } catch (e) {console.log(e)}
  187. });
  188. _this5.serialPort.write('ls\r');
  189. });
  190. });
  191. }
  192. }, {
  193. key: 'getFile',
  194. value: function getFile(index) {
  195. var _this6 = this;
  196.  
  197. return new Promise(function (resolve) {
  198. _this6.open().then(function () {
  199. return _this6._getFile(index);
  200. }).then(resolve);
  201. });
  202. }
  203. }, {
  204. key: 'getSequentialFilesFrom',
  205. value: function getSequentialFilesFrom(index) {
  206. var _this7 = this;
  207.  
  208. return new Promise(function (resolve) {
  209. // Recursive method to get all files > index sequencially.
  210. if (typeof _this7.fileList[index] === 'undefined') {
  211. // Last file has been fetched.
  212. resolve();
  213. } else {
  214. // There is an other file to fetch.
  215. _this7.getFile(_this7.fileList[index].id).then(function (index) {
  216. return _this7.getSequentialFilesFrom(index + 1);
  217. }).then(resolve);
  218. }
  219. });
  220. }
  221. }, {
  222. key: 'launchSpeedTest',
  223. value: function launchSpeedTest() {
  224. var _this8 = this;
  225.  
  226. return new Promise(function (resolve) {
  227. _this8.open().then(function () {
  228. return _this8._performSpeedTest();
  229. }, function () {
  230. return _this8.launchSpeedTest();
  231. }).then(resolve);
  232. });
  233. }
  234. }, {
  235. key: 'eraseMemory',
  236. value: function eraseMemory() {
  237. var _this9 = this;
  238.  
  239. return new Promise(function (resolve) {
  240. _this9.open().then(function () {
  241. _this9.serialPort.on('data', function (data) {
  242. if (data.slice(-16).compare(new Buffer(Array(16).fill(255))) === 0) {
  243. _this9.close().then(resolve);
  244. }
  245. });
  246. _this9.serialPort.write('erase\r');
  247. });
  248. });
  249. }
  250. }]);
  251.  
  252. return MeteorConnection;
  253. })();
  254.  
  255. exports.MeteorConnection = MeteorConnection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement