Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cctalk parsers #1342

Merged
merged 28 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
de63a9b
Added Working CCTalk Parser
frank-dspeed Sep 22, 2017
7e6f6e8
Added Working CCTalk Parser
frank-dspeed Sep 22, 2017
50c7ec9
Apply ESLint Rule SET
frank-dspeed Sep 22, 2017
e498b84
Improved Const Position and Name for Reading
frank-dspeed Sep 22, 2017
c590021
chore(packages): upgrade eslint and sinon (#1343)
reconbot Sep 22, 2017
401bd30
Remove not needed debug
frank-dspeed Sep 23, 2017
b41b161
add basic test
frank-dspeed Sep 23, 2017
21b5ba3
Added Working CCTalk Parser
frank-dspeed Sep 22, 2017
d99dcdd
Added Working CCTalk Parser
frank-dspeed Sep 22, 2017
0659493
Apply ESLint Rule SET
frank-dspeed Sep 22, 2017
00e25bb
Improved Const Position and Name for Reading
frank-dspeed Sep 22, 2017
604198d
Remove not needed debug
frank-dspeed Sep 23, 2017
897d530
add basic test
frank-dspeed Sep 23, 2017
6ce2e77
Merge branch 'add_cctalk_parsers' of https://github.com/direktspeed/n…
frank-dspeed Sep 24, 2017
d421232
Deprecating buffer.set Hope God will allow that
frank-dspeed Sep 24, 2017
2b00ea9
New Version array based
frank-dspeed Sep 24, 2017
fcdd6d6
New Version array based faster
frank-dspeed Sep 24, 2017
cf972f4
Disable tests
frank-dspeed Sep 24, 2017
b0f9311
Fix Flatten Array
frank-dspeed Sep 24, 2017
9fa9817
Fixed: parser and Tests also updated to use array
frank-dspeed Sep 25, 2017
cd5d899
Fixed: parser and Tests also updated to use array
frank-dspeed Sep 25, 2017
e43809c
Fixed: parser and Tests also updated to use array
frank-dspeed Sep 25, 2017
72f078a
Fixed: parser and Tests also updated to use array
frank-dspeed Sep 25, 2017
a48513c
Fixed: parser and Tests also updated to use array
frank-dspeed Sep 25, 2017
926f893
Fixed: node < 6
frank-dspeed Sep 25, 2017
d0fb367
Finish: written basic documentation
frank-dspeed Sep 25, 2017
d5bfcfa
Fix: written basic documentation
frank-dspeed Sep 25, 2017
7382fe4
compiled docs for cctalk parser
frank-dspeed Sep 27, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/parsers/cctalk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const Transform = require('stream').Transform;
const Buffer = require('safe-buffer').Buffer;

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
// ES7 allows directly push [...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
const FullMsgLength = this.array[1] + 5;

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;
this.push(frame);
}
cb();
}
};
10 changes: 10 additions & 0 deletions lib/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,21 @@ 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);
```
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some docs up here ☝️

module.exports = {
ByteLength: require('./byte-length'),
CCTalk: require('./cctalk'),
Delimiter: require('./delimiter'),
Readline: require('./readline'),
Ready: require('./ready'),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so weird, we totally have this in master

"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.0",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/arduinoTest/stress.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 41 additions & 0 deletions test/parser-cctalk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'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 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.getCall(0).args[0], 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([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]));
});
});
// 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]));