From de63a9ba2e283ba7c7e421d37327401d8f07cd23 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 11:28:31 +0100 Subject: [PATCH 01/27] Added Working CCTalk Parser --- lib/parsers/cctalk.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/parsers/cctalk.js diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js new file mode 100644 index 000000000..85a4710ed --- /dev/null +++ b/lib/parsers/cctalk.js @@ -0,0 +1,38 @@ +'use strict'; +const Buffer = require('safe-buffer').Buffer; +const Transform = require('stream').Transform; + +const MAX_PACKET_LENGTH = 255 + 5; +const debug = require('debug') + +module.exports = class ccTalkParser extends Transform { + constructor() { + super(); + this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length + this.cursor = 0; + } + _transform(buffer, _, cb) { + debug('parser set')(this.buffer, buffer ,this.cursor) + debug('parser set')(this.buffer.toString('hex'), buffer.buffer ,this.cursor) + + this.buffer.set(buffer, this.cursor); + this.cursor += buffer.length; + debug('parse befor loop')(buffer, this.cursor) + while(this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { + // full frame accumulated + var length = this.buffer[1] + 5; + //console.log("length", length); + + //copy command from the buffer + var frame = new Uint8Array(length); + frame.set(this.buffer.slice(0, length)); + + // copy remaining buffer to the begin of the buffer to prepare for next command + this.buffer.set(this.buffer.slice(length, this.cursor)); + this.cursor -= length; + debug('parse push',frame,this.buffer,this.cursor) + this.push(frame); + } + cb(); + } +}; From 7e6f6e80c856b26de793cc5f726c53cecb9f0c21 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 11:30:35 +0100 Subject: [PATCH 02/27] Added Working CCTalk Parser --- lib/parsers/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/parsers/index.js b/lib/parsers/index.js index c05e05045..7e789b803 100644 --- a/lib/parsers/index.js +++ b/lib/parsers/index.js @@ -76,6 +76,7 @@ parser.on('data', console.log); module.exports = { ByteLength: require('./byte-length'), + CCTalk: require('./cctalk'), Delimiter: require('./delimiter'), Readline: require('./readline'), Ready: require('./ready'), From 50c7ec962db677886729e5df58c9963ee249a289 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 12:35:39 +0100 Subject: [PATCH 03/27] Apply ESLint Rule SET --- lib/parsers/cctalk.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 85a4710ed..5d8282bbe 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -3,35 +3,35 @@ const Buffer = require('safe-buffer').Buffer; const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -const debug = require('debug') +const debug = require('debug'); -module.exports = class ccTalkParser extends Transform { +module.exports = class ccTalkParser extends Transform { constructor() { super(); this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, buffer ,this.cursor) - debug('parser set')(this.buffer.toString('hex'), buffer.buffer ,this.cursor) + debug('parser set')(this.buffer, buffer, this.cursor); + debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); - this.buffer.set(buffer, this.cursor); - this.cursor += buffer.length; - debug('parse befor loop')(buffer, this.cursor) - while(this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { - // full frame accumulated - var length = this.buffer[1] + 5; - //console.log("length", length); + this.buffer.set(buffer, this.cursor); + this.cursor += buffer.length; + debug('parse befor loop')(buffer, this.cursor); + while (this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { + // full frame accumulated + const length = this.buffer[1] + 5; + // console.log("length", length); - //copy command from the buffer - var frame = new Uint8Array(length); - frame.set(this.buffer.slice(0, length)); + // copy command from the buffer + const frame = new Uint8Array(length); + frame.set(this.buffer.slice(0, length)); - // copy remaining buffer to the begin of the buffer to prepare for next command - this.buffer.set(this.buffer.slice(length, this.cursor)); - this.cursor -= length; - debug('parse push',frame,this.buffer,this.cursor) - this.push(frame); + // copy remaining buffer to the begin of the buffer to prepare for next command + this.buffer.set(this.buffer.slice(length, this.cursor)); + this.cursor -= length; + debug('parse push', frame, this.buffer, this.cursor); + this.push(frame); } cb(); } From e498b847dc58e437b3bf8311a5a82aca93506afe Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 12:45:01 +0100 Subject: [PATCH 04/27] Improved Const Position and Name for Reading --- lib/parsers/cctalk.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 5d8282bbe..cf1dd0e63 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -12,15 +12,16 @@ module.exports = class ccTalkParser extends Transform { this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, buffer, this.cursor); + debug('parser set')(this.buffer, this.cursor); debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); this.buffer.set(buffer, this.cursor); this.cursor += buffer.length; + // full frame accumulated + const length = this.buffer[1] + 5; debug('parse befor loop')(buffer, this.cursor); - while (this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { - // full frame accumulated - const length = this.buffer[1] + 5; + + while (this.cursor > 1 && this.cursor >= length) { // console.log("length", length); // copy command from the buffer From c590021f6289c27ef9661d4363be7b019cdf6adc Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Fri, 22 Sep 2017 09:41:44 -0400 Subject: [PATCH 05/27] chore(packages): upgrade eslint and sinon (#1343) --- package.json | 4 ++-- test/arduinoTest/stress.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 50b3c19f1..458370dd3 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "chai": "^4.0.2", "chai-subset": "^1.5.0", "conventional-changelog-cli": "^1.3.2", - "eslint": "^4.5.0", + "eslint": "^4.7.2", "eslint-config-standard": "^10.2.1", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.1.0", @@ -82,7 +82,7 @@ "mocha": "^3.4.2", "prebuild": "^6.2.1", "proxyquire": "^1.7.10", - "sinon": "^3.2.1" + "sinon": "^3.3.0" }, "engines": { "node": ">=4.0.0" diff --git a/test/arduinoTest/stress.js b/test/arduinoTest/stress.js index bf448fa43..08d73eafd 100644 --- a/test/arduinoTest/stress.js +++ b/test/arduinoTest/stress.js @@ -1,4 +1,4 @@ -/* eslint-disable node/no-missing-require */ +/* eslint-disable node/no-missing-require, node/no-extraneous-require */ 'use strict'; // `npm run stress` to run these tests From 401bd30e5c2696ced77f2c8917a0150c48548319 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sat, 23 Sep 2017 13:33:32 +0100 Subject: [PATCH 06/27] Remove not needed debug --- lib/parsers/cctalk.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index cf1dd0e63..b1525f00e 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -3,7 +3,6 @@ const Buffer = require('safe-buffer').Buffer; const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -const debug = require('debug'); module.exports = class ccTalkParser extends Transform { constructor() { @@ -12,18 +11,13 @@ module.exports = class ccTalkParser extends Transform { this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, this.cursor); - debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); - this.buffer.set(buffer, this.cursor); this.cursor += buffer.length; // full frame accumulated const length = this.buffer[1] + 5; - debug('parse befor loop')(buffer, this.cursor); + // console.log("length", length); while (this.cursor > 1 && this.cursor >= length) { - // console.log("length", length); - // copy command from the buffer const frame = new Uint8Array(length); frame.set(this.buffer.slice(0, length)); @@ -31,7 +25,6 @@ module.exports = class ccTalkParser extends Transform { // copy remaining buffer to the begin of the buffer to prepare for next command this.buffer.set(this.buffer.slice(length, this.cursor)); this.cursor -= length; - debug('parse push', frame, this.buffer, this.cursor); this.push(frame); } cb(); From b41b16138a6e25486197fb5e70c3043b451c05a5 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sat, 23 Sep 2017 13:43:33 +0100 Subject: [PATCH 07/27] add basic test --- lib/parsers/cctalk.js | 2 +- test/parser-cctalk.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/parser-cctalk.js diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index b1525f00e..f03cb9987 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -4,7 +4,7 @@ const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -module.exports = class ccTalkParser extends Transform { +module.exports = class CCTalkParser extends Transform { constructor() { super(); this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js new file mode 100644 index 000000000..c4d94cf46 --- /dev/null +++ b/test/parser-cctalk.js @@ -0,0 +1,32 @@ +'use strict'; +/* eslint-disable no-new */ + +const Buffer = require('safe-buffer').Buffer; +const sinon = require('sinon'); +const CCTalkParser = require('../lib/parsers/cctalk'); + +describe('CCTalkParser', () => { + it('emits data events every 5 bytes', () => { + const data = Buffer.from('Robots are so freaking cool!'); + const spy = sinon.spy(); + const parser = new CCTalkParser(); + parser.on('data', spy); + parser.write(data); + assert.equal(spy.callCount, 3); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2])); + assert.deepEqual(spy.getCall(1).args[0], Buffer.from([254])); + assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0])); + }); + + it('continues looking for bytes in additional writes', () => { + const parser = new CCTalkParser(); + const spy = sinon.spy(); + parser.on('data', spy); + parser.write(Buffer.from([1, 0, 2])); + parser.write(Buffer.from([254])); + assert.equal(spy.callCount, 1); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2, 254])); + }); + // TODO: case crc message got data 2 bits + // TODO: case process remaining buffer +}); From 21b5ba32d5ba7d7f8ef41cb89087aab0f028eede Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 11:28:31 +0100 Subject: [PATCH 08/27] Added Working CCTalk Parser --- lib/parsers/cctalk.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/parsers/cctalk.js diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js new file mode 100644 index 000000000..85a4710ed --- /dev/null +++ b/lib/parsers/cctalk.js @@ -0,0 +1,38 @@ +'use strict'; +const Buffer = require('safe-buffer').Buffer; +const Transform = require('stream').Transform; + +const MAX_PACKET_LENGTH = 255 + 5; +const debug = require('debug') + +module.exports = class ccTalkParser extends Transform { + constructor() { + super(); + this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length + this.cursor = 0; + } + _transform(buffer, _, cb) { + debug('parser set')(this.buffer, buffer ,this.cursor) + debug('parser set')(this.buffer.toString('hex'), buffer.buffer ,this.cursor) + + this.buffer.set(buffer, this.cursor); + this.cursor += buffer.length; + debug('parse befor loop')(buffer, this.cursor) + while(this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { + // full frame accumulated + var length = this.buffer[1] + 5; + //console.log("length", length); + + //copy command from the buffer + var frame = new Uint8Array(length); + frame.set(this.buffer.slice(0, length)); + + // copy remaining buffer to the begin of the buffer to prepare for next command + this.buffer.set(this.buffer.slice(length, this.cursor)); + this.cursor -= length; + debug('parse push',frame,this.buffer,this.cursor) + this.push(frame); + } + cb(); + } +}; From d99dcdd4735241247f11e450ff5cb7a4fff48ba3 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 11:30:35 +0100 Subject: [PATCH 09/27] Added Working CCTalk Parser --- lib/parsers/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/parsers/index.js b/lib/parsers/index.js index c05e05045..7e789b803 100644 --- a/lib/parsers/index.js +++ b/lib/parsers/index.js @@ -76,6 +76,7 @@ parser.on('data', console.log); module.exports = { ByteLength: require('./byte-length'), + CCTalk: require('./cctalk'), Delimiter: require('./delimiter'), Readline: require('./readline'), Ready: require('./ready'), From 065949340eca961d50a660cd1b2df9409af3ef07 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 12:35:39 +0100 Subject: [PATCH 10/27] Apply ESLint Rule SET --- lib/parsers/cctalk.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 85a4710ed..5d8282bbe 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -3,35 +3,35 @@ const Buffer = require('safe-buffer').Buffer; const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -const debug = require('debug') +const debug = require('debug'); -module.exports = class ccTalkParser extends Transform { +module.exports = class ccTalkParser extends Transform { constructor() { super(); this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, buffer ,this.cursor) - debug('parser set')(this.buffer.toString('hex'), buffer.buffer ,this.cursor) + debug('parser set')(this.buffer, buffer, this.cursor); + debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); - this.buffer.set(buffer, this.cursor); - this.cursor += buffer.length; - debug('parse befor loop')(buffer, this.cursor) - while(this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { - // full frame accumulated - var length = this.buffer[1] + 5; - //console.log("length", length); + this.buffer.set(buffer, this.cursor); + this.cursor += buffer.length; + debug('parse befor loop')(buffer, this.cursor); + while (this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { + // full frame accumulated + const length = this.buffer[1] + 5; + // console.log("length", length); - //copy command from the buffer - var frame = new Uint8Array(length); - frame.set(this.buffer.slice(0, length)); + // copy command from the buffer + const frame = new Uint8Array(length); + frame.set(this.buffer.slice(0, length)); - // copy remaining buffer to the begin of the buffer to prepare for next command - this.buffer.set(this.buffer.slice(length, this.cursor)); - this.cursor -= length; - debug('parse push',frame,this.buffer,this.cursor) - this.push(frame); + // copy remaining buffer to the begin of the buffer to prepare for next command + this.buffer.set(this.buffer.slice(length, this.cursor)); + this.cursor -= length; + debug('parse push', frame, this.buffer, this.cursor); + this.push(frame); } cb(); } From 00e25bb6dc5d9411313cc945cc23bad15be5073f Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Fri, 22 Sep 2017 12:45:01 +0100 Subject: [PATCH 11/27] Improved Const Position and Name for Reading --- lib/parsers/cctalk.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 5d8282bbe..cf1dd0e63 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -12,15 +12,16 @@ module.exports = class ccTalkParser extends Transform { this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, buffer, this.cursor); + debug('parser set')(this.buffer, this.cursor); debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); this.buffer.set(buffer, this.cursor); this.cursor += buffer.length; + // full frame accumulated + const length = this.buffer[1] + 5; debug('parse befor loop')(buffer, this.cursor); - while (this.cursor > 1 && this.cursor >= this.buffer[1] + 5) { - // full frame accumulated - const length = this.buffer[1] + 5; + + while (this.cursor > 1 && this.cursor >= length) { // console.log("length", length); // copy command from the buffer From 604198d18ce2444a36ee98c7514f509d97092849 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sat, 23 Sep 2017 13:33:32 +0100 Subject: [PATCH 12/27] Remove not needed debug --- lib/parsers/cctalk.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index cf1dd0e63..b1525f00e 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -3,7 +3,6 @@ const Buffer = require('safe-buffer').Buffer; const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -const debug = require('debug'); module.exports = class ccTalkParser extends Transform { constructor() { @@ -12,18 +11,13 @@ module.exports = class ccTalkParser extends Transform { this.cursor = 0; } _transform(buffer, _, cb) { - debug('parser set')(this.buffer, this.cursor); - debug('parser set')(this.buffer.toString('hex'), buffer.buffer, this.cursor); - this.buffer.set(buffer, this.cursor); this.cursor += buffer.length; // full frame accumulated const length = this.buffer[1] + 5; - debug('parse befor loop')(buffer, this.cursor); + // console.log("length", length); while (this.cursor > 1 && this.cursor >= length) { - // console.log("length", length); - // copy command from the buffer const frame = new Uint8Array(length); frame.set(this.buffer.slice(0, length)); @@ -31,7 +25,6 @@ module.exports = class ccTalkParser extends Transform { // copy remaining buffer to the begin of the buffer to prepare for next command this.buffer.set(this.buffer.slice(length, this.cursor)); this.cursor -= length; - debug('parse push', frame, this.buffer, this.cursor); this.push(frame); } cb(); From 897d5306da6d1c33d2e26d83b5e214cc9af9fbc5 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sat, 23 Sep 2017 13:43:33 +0100 Subject: [PATCH 13/27] add basic test --- lib/parsers/cctalk.js | 2 +- test/parser-cctalk.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/parser-cctalk.js diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index b1525f00e..f03cb9987 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -4,7 +4,7 @@ const Transform = require('stream').Transform; const MAX_PACKET_LENGTH = 255 + 5; -module.exports = class ccTalkParser extends Transform { +module.exports = class CCTalkParser extends Transform { constructor() { super(); this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js new file mode 100644 index 000000000..c4d94cf46 --- /dev/null +++ b/test/parser-cctalk.js @@ -0,0 +1,32 @@ +'use strict'; +/* eslint-disable no-new */ + +const Buffer = require('safe-buffer').Buffer; +const sinon = require('sinon'); +const CCTalkParser = require('../lib/parsers/cctalk'); + +describe('CCTalkParser', () => { + it('emits data events every 5 bytes', () => { + const data = Buffer.from('Robots are so freaking cool!'); + const spy = sinon.spy(); + const parser = new CCTalkParser(); + parser.on('data', spy); + parser.write(data); + assert.equal(spy.callCount, 3); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2])); + assert.deepEqual(spy.getCall(1).args[0], Buffer.from([254])); + assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0])); + }); + + it('continues looking for bytes in additional writes', () => { + const parser = new CCTalkParser(); + const spy = sinon.spy(); + parser.on('data', spy); + parser.write(Buffer.from([1, 0, 2])); + parser.write(Buffer.from([254])); + assert.equal(spy.callCount, 1); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2, 254])); + }); + // TODO: case crc message got data 2 bits + // TODO: case process remaining buffer +}); From d421232890e538bf0c5fa2c2902af8cd96f4092e Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sun, 24 Sep 2017 10:50:11 +0100 Subject: [PATCH 14/27] Deprecating buffer.set Hope God will allow that --- lib/parsers/cctalk.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index f03cb9987..e528bfd77 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -11,7 +11,7 @@ module.exports = class CCTalkParser extends Transform { this.cursor = 0; } _transform(buffer, _, cb) { - this.buffer.set(buffer, this.cursor); + this.buffer[this.cursor] = buffer; this.cursor += buffer.length; // full frame accumulated const length = this.buffer[1] + 5; @@ -23,7 +23,7 @@ module.exports = class CCTalkParser extends Transform { frame.set(this.buffer.slice(0, length)); // copy remaining buffer to the begin of the buffer to prepare for next command - this.buffer.set(this.buffer.slice(length, this.cursor)); + this.buffer = this.buffer.slice(length, this.cursor); this.cursor -= length; this.push(frame); } From 2b00ea9b21845b43256d3df20b25bf38cbd230b7 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sun, 24 Sep 2017 13:59:59 +0100 Subject: [PATCH 15/27] New Version array based --- lib/parsers/cctalk.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index e528bfd77..1bbbcc6fe 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -1,30 +1,33 @@ 'use strict'; -const Buffer = require('safe-buffer').Buffer; const Transform = require('stream').Transform; -const MAX_PACKET_LENGTH = 255 + 5; - module.exports = class CCTalkParser extends Transform { constructor() { super(); - this.buffer = Buffer.alloc(MAX_PACKET_LENGTH); // maxium ccTalkMessage length - this.cursor = 0; + this.array = []; } _transform(buffer, _, cb) { - this.buffer[this.cursor] = buffer; - this.cursor += buffer.length; + // TODO: Better Faster es7 no supported by node 4 + const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; + this.array.push(array); + // full frame accumulated - const length = this.buffer[1] + 5; - // console.log("length", length); + const FullMsgLength = this.array[1] + 5; + // console.log("FullMsgLength",FullMsgLength); + + while (this.array.length > 1 && this.array.length >= FullMsgLength) { + // copy command from the array + const frame = new Uint8Array(FullMsgLength); + frame.set(this.array.slice(0, FullMsgLength)); - while (this.cursor > 1 && this.cursor >= length) { - // copy command from the buffer - const frame = new Uint8Array(length); - frame.set(this.buffer.slice(0, length)); + if (this.array.length > FullMsgLength) { // Preserve Additional data from next Command + const AdditionalData = this.array.slice(frame.length, this.array.length); + this.array.splice(0, FullMsgLength, AdditionalData); + this.array.length = AdditionalData.length; + } else { // flush the array + this.array.length = 0; + } - // copy remaining buffer to the begin of the buffer to prepare for next command - this.buffer = this.buffer.slice(length, this.cursor); - this.cursor -= length; this.push(frame); } cb(); From fcdd6d6f769a62c952c2a47ef05fef5d833c5fe9 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sun, 24 Sep 2017 14:26:46 +0100 Subject: [PATCH 16/27] New Version array based faster --- lib/parsers/cctalk.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 1bbbcc6fe..453be3e12 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -20,10 +20,8 @@ module.exports = class CCTalkParser extends Transform { const frame = new Uint8Array(FullMsgLength); frame.set(this.array.slice(0, FullMsgLength)); - if (this.array.length > FullMsgLength) { // Preserve Additional data from next Command - const AdditionalData = this.array.slice(frame.length, this.array.length); - this.array.splice(0, FullMsgLength, AdditionalData); - this.array.length = AdditionalData.length; + if (this.array.length > FullMsgLength) { // Preserve Extra Data + this.array = this.array.slice(frame.length, this.array.length); } else { // flush the array this.array.length = 0; } From cf972f4f64013552587ef20e2e84b71b18c1fcd2 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sun, 24 Sep 2017 18:48:31 +0100 Subject: [PATCH 17/27] Disable tests --- test/parser-cctalk.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js index c4d94cf46..3f8dbec7f 100644 --- a/test/parser-cctalk.js +++ b/test/parser-cctalk.js @@ -6,26 +6,12 @@ const sinon = require('sinon'); const CCTalkParser = require('../lib/parsers/cctalk'); describe('CCTalkParser', () => { - it('emits data events every 5 bytes', () => { - const data = Buffer.from('Robots are so freaking cool!'); - const spy = sinon.spy(); - const parser = new CCTalkParser(); - parser.on('data', spy); - parser.write(data); - assert.equal(spy.callCount, 3); - assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2])); - assert.deepEqual(spy.getCall(1).args[0], Buffer.from([254])); - assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0])); - }); - it('continues looking for bytes in additional writes', () => { const parser = new CCTalkParser(); const spy = sinon.spy(); - parser.on('data', spy); - parser.write(Buffer.from([1, 0, 2])); - parser.write(Buffer.from([254])); - assert.equal(spy.callCount, 1); - assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1, 0, 2, 254])); + // parser.on('data', spy); + // parser.write(Buffer.from([1, 0, 2])); + // parser.write(Buffer.from([254, 217])); }); // TODO: case crc message got data 2 bits // TODO: case process remaining buffer From b0f9311206e40de6c272959d648edf0e28580d82 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Sun, 24 Sep 2017 21:04:52 +0100 Subject: [PATCH 18/27] Fix Flatten Array --- lib/parsers/cctalk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 453be3e12..b382362dc 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -9,7 +9,7 @@ module.exports = class CCTalkParser extends Transform { _transform(buffer, _, cb) { // TODO: Better Faster es7 no supported by node 4 const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; - this.array.push(array); + array.map((byte) => this.array.push(byte)); // full frame accumulated const FullMsgLength = this.array[1] + 5; From 9fa9817355ef9fb5e3b8396dd4e9c806cf489766 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 12:35:03 +0100 Subject: [PATCH 19/27] Fixed: parser and Tests also updated to use array --- lib/parsers/cctalk.js | 17 +++++++---------- test/parser-cctalk.js | 32 ++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index b382362dc..83d618550 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -5,27 +5,24 @@ module.exports = class CCTalkParser extends Transform { constructor() { super(); this.array = []; + this.cursor = 0; } _transform(buffer, _, cb) { + this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; array.map((byte) => this.array.push(byte)); // full frame accumulated - const FullMsgLength = this.array[1] + 5; - // console.log("FullMsgLength",FullMsgLength); - - while (this.array.length > 1 && this.array.length >= FullMsgLength) { + while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { // copy command from the array + const FullMsgLength = this.array[1] + 5; const frame = new Uint8Array(FullMsgLength); frame.set(this.array.slice(0, FullMsgLength)); - if (this.array.length > FullMsgLength) { // Preserve Extra Data - this.array = this.array.slice(frame.length, this.array.length); - } else { // flush the array - this.array.length = 0; - } - + // Preserve Extra Data + this.array = this.array.slice(frame.length, this.array.length); + this.cursor -= FullMsgLength; this.push(frame); } cb(); diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js index 3f8dbec7f..4d1e1c178 100644 --- a/test/parser-cctalk.js +++ b/test/parser-cctalk.js @@ -6,13 +6,33 @@ const sinon = require('sinon'); const CCTalkParser = require('../lib/parsers/cctalk'); describe('CCTalkParser', () => { - it('continues looking for bytes in additional writes', () => { + it('emits data for a default length message', () => { + const data = Buffer.from([2, 0, 1, 254, 217]); + const spy = sinon.spy(); + const parser = new CCTalkParser(); + parser.on('data', spy); + parser.write(data); + assert.equal(spy.callCount, 1); + // assert.deepEqual(spy.callArgWith(Buffer.from([2, 0, 1, 254, 217]))); + }); + + it('emits data for a 7 byte length message', () => { + const parser = new CCTalkParser(); + const spy = sinon.spy(); + parser.on('data', spy); + parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217])); + assert.equal(spy.callCount, 1); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217])); + }); + + it('emits 2 times data first length 7 secund length 5', () => { const parser = new CCTalkParser(); const spy = sinon.spy(); - // parser.on('data', spy); - // parser.write(Buffer.from([1, 0, 2])); - // parser.write(Buffer.from([254, 217])); + parser.on('data', spy); + parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217, 2, 0, 1, 254, 217])); + assert.equal(spy.callCount, 2); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217])); + assert.deepEqual(spy.getCall(1).args[0], Buffer.from([2, 0, 1, 254, 217])); }); - // TODO: case crc message got data 2 bits - // TODO: case process remaining buffer }); +// TODO: parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217, 2, 0, 1, 254, 217, 2, 2, 1, 251, 1, 1, 217, 2, 2, 1, 252, 1, 1, 217, 2, 0, 1, 253, 217])); From cd5d8991a2b5d7d97a4857e23416bd02007167d5 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 12:38:43 +0100 Subject: [PATCH 20/27] Fixed: parser and Tests also updated to use array --- lib/parsers/cctalk.js | 2 +- test/parser-cctalk.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 83d618550..3e0595b43 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -11,7 +11,7 @@ module.exports = class CCTalkParser extends Transform { this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; - array.map((byte) => this.array.push(byte)); + array.map((byte) => this.array.push(byte)); // ES7 allows directly push // full frame accumulated while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js index 4d1e1c178..bd677aa36 100644 --- a/test/parser-cctalk.js +++ b/test/parser-cctalk.js @@ -13,7 +13,7 @@ describe('CCTalkParser', () => { parser.on('data', spy); parser.write(data); assert.equal(spy.callCount, 1); - // assert.deepEqual(spy.callArgWith(Buffer.from([2, 0, 1, 254, 217]))); + assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 0, 1, 254, 217])); }); it('emits data for a 7 byte length message', () => { From e43809c1ac75948e8d474f22ff0bea2ec836bfa2 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 13:00:24 +0100 Subject: [PATCH 21/27] Fixed: parser and Tests also updated to use array --- lib/parsers/cctalk.js | 9 +++++---- test/parser-cctalk.js | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 3e0595b43..0cbf7c240 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -1,5 +1,6 @@ 'use strict'; const Transform = require('stream').Transform; +const Buffer = require('safe-buffer').Buffer; module.exports = class CCTalkParser extends Transform { constructor() { @@ -13,13 +14,13 @@ module.exports = class CCTalkParser extends Transform { const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; array.map((byte) => this.array.push(byte)); // ES7 allows directly push - // full frame accumulated while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { + // full frame accumulated // copy command from the array const FullMsgLength = this.array[1] + 5; - const frame = new Uint8Array(FullMsgLength); - frame.set(this.array.slice(0, FullMsgLength)); - + // const frame = new Uint8Array(FullMsgLength); + // frame.set(this.array.slice(0, FullMsgLength)); + const frame = Buffer.from(this.array.slice(0, FullMsgLength)); // Preserve Extra Data this.array = this.array.slice(frame.length, this.array.length); this.cursor -= FullMsgLength; diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js index bd677aa36..7c757a8e7 100644 --- a/test/parser-cctalk.js +++ b/test/parser-cctalk.js @@ -29,7 +29,10 @@ describe('CCTalkParser', () => { const parser = new CCTalkParser(); const spy = sinon.spy(); parser.on('data', spy); - parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217, 2, 0, 1, 254, 217])); + parser.write(Buffer.from([2, 2, 1])); + parser.write(Buffer.from([254, 1, 1])); + parser.write(Buffer.from([217, 2])); + parser.write(Buffer.from([0, 1, 254, 217])); assert.equal(spy.callCount, 2); assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217])); assert.deepEqual(spy.getCall(1).args[0], Buffer.from([2, 0, 1, 254, 217])); From 72f078a1a0e2c46bc4e43778ed84a5e25148b119 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 13:39:18 +0100 Subject: [PATCH 22/27] Fixed: parser and Tests also updated to use array --- lib/parsers/cctalk.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index 0cbf7c240..ecd6dffd3 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -11,15 +11,14 @@ module.exports = class CCTalkParser extends Transform { _transform(buffer, _, cb) { this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 - const array = Array.prototype.slice.call(buffer, 0); // [...buffer]; - array.map((byte) => this.array.push(byte)); // ES7 allows directly push + // ES7 allows directly push [...buffer] + buffer.map((byte) => this.array.push(byte)); while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { // full frame accumulated // copy command from the array const FullMsgLength = this.array[1] + 5; - // const frame = new Uint8Array(FullMsgLength); - // frame.set(this.array.slice(0, FullMsgLength)); + const frame = Buffer.from(this.array.slice(0, FullMsgLength)); // Preserve Extra Data this.array = this.array.slice(frame.length, this.array.length); From a48513c5872200290a3785f41a17e36a13a31aa4 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 13:52:54 +0100 Subject: [PATCH 23/27] Fixed: parser and Tests also updated to use array --- lib/parsers/cctalk.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index ecd6dffd3..f99d2e78b 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -12,7 +12,8 @@ module.exports = class CCTalkParser extends Transform { this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 // ES7 allows directly push [...buffer] - buffer.map((byte) => this.array.push(byte)); + Buffer.from(buffer) + .map((byte) => this.array.push(byte)); while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { // full frame accumulated From 926f893065adf98acb6271aaab8254e6963c283b Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 13:57:20 +0100 Subject: [PATCH 24/27] Fixed: node < 6 --- lib/parsers/cctalk.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index f99d2e78b..b4879ec66 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -12,8 +12,7 @@ module.exports = class CCTalkParser extends Transform { this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 // ES7 allows directly push [...buffer] - Buffer.from(buffer) - .map((byte) => this.array.push(byte)); + this.array = this.array.concat(Array.from(buffer)); while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { // full frame accumulated From d0fb3671b04b96ae408ce5e0c645744c70469406 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 22:26:36 +0100 Subject: [PATCH 25/27] Finish: written basic documentation --- lib/parsers/cctalk.js | 5 +++-- lib/parsers/index.js | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js index b4879ec66..19104522b 100644 --- a/lib/parsers/cctalk.js +++ b/lib/parsers/cctalk.js @@ -12,8 +12,9 @@ module.exports = class CCTalkParser extends Transform { this.cursor += buffer.length; // TODO: Better Faster es7 no supported by node 4 // ES7 allows directly push [...buffer] - this.array = this.array.concat(Array.from(buffer)); - + // this.array = this.array.concat(Array.from(buffer)); //Slower ?!? + Array.from(buffer) + .map((byte) => this.array.push(byte)); while (this.cursor > 1 && this.cursor >= this.array[1] + 5) { // full frame accumulated // copy command from the array diff --git a/lib/parsers/index.js b/lib/parsers/index.js index 7e789b803..ab178a661 100644 --- a/lib/parsers/index.js +++ b/lib/parsers/index.js @@ -71,6 +71,15 @@ const Regex = SerialPort.parsers.Regex; const port = new SerialPort('/dev/tty-usbserial1'); const parser = port.pipe(new Regex({ regex: /[\r\n]+/ })); parser.on('data', console.log); +``` + +To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer. +```js +const SerialPort = require('serialport'); +const Regex = SerialPort.parsers.Regex; +const port = new SerialPort('/dev/tty-usbserial1'); +const parser = port.pipe(new Regex({ regex: /[\r\n]+/ })); +parser.on('data', console.log); ``` */ From d5bfcfaf3056b51e385d1f2ef659991786c6d154 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Mon, 25 Sep 2017 22:31:11 +0100 Subject: [PATCH 26/27] Fix: written basic documentation --- lib/parsers/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/parsers/index.js b/lib/parsers/index.js index ab178a661..275ba44ba 100644 --- a/lib/parsers/index.js +++ b/lib/parsers/index.js @@ -76,9 +76,9 @@ parser.on('data', console.log); To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer. ```js const SerialPort = require('serialport'); -const Regex = SerialPort.parsers.Regex; -const port = new SerialPort('/dev/tty-usbserial1'); -const parser = port.pipe(new Regex({ regex: /[\r\n]+/ })); +const CCTalk = SerialPort.parsers.CCTalk; +const port = new SerialPort('/dev/ttyUSB0'); +const parser = port.pipe(new CCtalk()); parser.on('data', console.log); ``` */ From 7382fe40cb4c618b99802976b7206ad6b4ec42e7 Mon Sep 17 00:00:00 2001 From: Frank Lemanschik Date: Wed, 27 Sep 2017 13:16:59 +0100 Subject: [PATCH 27/27] compiled docs for cctalk parser --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index e5a767ceb..188f9b354 100644 --- a/README.md +++ b/README.md @@ -788,6 +788,15 @@ const parser = port.pipe(new Regex({ regex: /[\r\n]+/ })); parser.on('data', console.log); ``` +To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer. +```js +const SerialPort = require('serialport'); +const CCTalk = SerialPort.parsers.CCTalk; +const port = new SerialPort('/dev/ttyUSB0'); +const parser = port.pipe(new CCtalk()); +parser.on('data', console.log); +``` + * * *