Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
101 changes: 48 additions & 53 deletions src/MIDParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,68 @@
"use strict";
/*jshint esversion: 6, node: true*/

const util = require('util');
const { Transform } = require('stream');
const util = require("util");
const { Transform } = require("stream");

const helpers = require("./helpers.js");
const mids = helpers.getMids();

const constants = require("./constants.json");
const encodingOP = constants.defaultEncoder;

var debug = util.debuglog('open-protocol');
var debug = util.debuglog("open-protocol");

class MIDParser extends Transform {
/**
* @class MIDParser
* @description This class performs the parsing of a MID body.
* This transforms MID.payload (Buffer) in a MID.payload (Object).
* This class uses the implemented MIDs in 'node-open-protocol/src/mid' for parsing MIDs.
* In case of a not implemented MID, MID.payload is converted in to a String.
* @param opts parameters to Transform stream
*/
constructor(opts) {
debug("new MIDParser");

opts = opts || {};

opts.writableObjectMode = true;
opts.readableObjectMode = true;

super(opts);
}

_transform(chunk, encoding, cb) {
debug("new MIDParser _transform", chunk);

if (mids[chunk.mid]) {
mids[chunk.mid].parser(chunk, null, (err, data) => {
if (err) {
cb(new Error(`Error on parser [${err}]`));
debug("new MIDParser _transform err-parser", chunk, err);
return;
}

/**
* @class MIDParser
* @description This class performs the parsing of a MID body.
* This transforms MID.payload (Buffer) in a MID.payload (Object).
* This class uses the implemented MIDs in 'node-open-protocol/src/mid' for parsing MIDs.
* In case of a not implemented MID, MID.payload is converted in to a String.
* @param opts parameters to Transform stream
*/
constructor(opts) {
debug("new MIDParser");

opts = opts || {};

opts.writableObjectMode = true;
opts.readableObjectMode = true;

super(opts);
}

_transform(chunk, encoding, cb) {
debug("new MIDParser _transform", chunk);

if(mids[chunk.mid]){

mids[chunk.mid].parser(chunk, null, (err, data) => {

if(err){
cb(new Error(`Error on parser [${err}]`));
debug("new MIDParser _transform err-parser", chunk, err);
return;
}

this.push(data);
cb();
});

}else{

if(!Buffer.isBuffer(chunk.payload)){
cb(new Error(`Error on parser - invalid payload MID [${chunk.mid}]`));
debug("new MIDParser _transform err-invalid_payload_MID", chunk);
return;
}
this.push(data);
cb();
});
} else {
if (!Buffer.isBuffer(chunk.payload)) {
cb(new Error(`Error on parser - invalid payload MID [${chunk.mid}]`));
debug("new MIDParser _transform err-invalid_payload_MID", chunk);
return;
}

chunk.payload = chunk.payload.toString(encodingOP);
chunk.payload = chunk.payload.toString(encodingOP);

this.push(chunk);
cb();
}
this.push(chunk);
cb();
}
}

_destroy() {
//no-op, needed to handle older node versions
}
_destroy() {
//no-op, needed to handle older node versions
}
}

module.exports = MIDParser;
8 changes: 4 additions & 4 deletions src/MIDSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ class MIDSerializer extends Transform {
debug("MIDSerializer _transform", chunk);

if(mids[chunk.mid]){

mids[chunk.mid].serializer(chunk, null, (err, data) => {

if(err){
cb(new Error(`Error on serializer [${err}]`));
debug('MIDSerializer _transform err-serializer', chunk, err);
return;
}

this.push(data);
cb();
cb();
});

}else{

if(chunk.payload === undefined){
Expand Down
18 changes: 13 additions & 5 deletions src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@
},
"UNIT": {
"000": "No unit",
"001": "N-m",
"001": "N·m",
"002": "ft lbf",
"003": "cN-m",
"004": "kN-m",
"005": "MN-m",
"003": "cN·m",
"004": "kN·m",
"005": "MN·m",
"006": "in lbf",
"007": "Kpm",
"008": "Kfcnm",
Expand Down Expand Up @@ -337,6 +337,14 @@
"924": "ozf / ms",
"925": "MN / ms"
},
"TRACE_TYPE": {
"1": "Angle",
"2": "Torque",
"3": "Current",
"4": "Gradient",
"5": "Stroke",
"6": "Force"
},
"ERROR": {
"00": "No Error",
"01": "Invalid data",
Expand Down Expand Up @@ -435,4 +443,4 @@
"INCONSISTENCY_MESSAGE_NUMBER": 4
},
"defaultEncoder" : "ascii"
}
}
Loading