diff --git a/index.js b/index.js index d06a073..2a74e8b 100644 --- a/index.js +++ b/index.js @@ -7,4 +7,4 @@ exports.Connection = Connection; exports.Parser = Parser; exports.createMessage = createMessage; exports.str2addr = tools.str2addr; -exports.addr2str = tools.adr2str; +exports.addr2str = tools.addr2str; diff --git a/lib/parser.js b/lib/parser.js index e9deb50..4c1d6ec 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -3,6 +3,13 @@ var Readable = require('stream').Readable, Decoder = require('./decoder'), tools = require('./tools'); + +var isModernBuffer = ( + typeof Buffer.alloc === 'function' && + typeof Buffer.allocUnsafe === 'function' && + typeof Buffer.from === 'function' + ); + /** * Parser */ @@ -71,12 +78,18 @@ Parser.prototype.parseTelegram = function(telegram) { // value var val = null; - - val = telegram[telegram.length-1]; - if(len > 8) { + var valbuffer; + if (len<=8) { + val = telegram[telegram.length-1]; + valbuffer = isModernBuffer ? Buffer.from(val) : new Buffer([val]); + } else { //if(len > 8) val = telegram.slice(10, telegram.length); + valbuffer = isModernBuffer ? Buffer.from(val) : new Buffer(val); } - + + // emit raw telegram event, pass the addresses and the buffer, no DPT guessing, copy of data buffer + self.emit('telegram', event, tools.addr2str(src, false), tools.addr2str(dest, true), valbuffer); + self.decoder.decode(len, val, function(err, type, value) { // emit action event diff --git a/package.json b/package.json index 50effcc..1e02b33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eibd", - "version": "0.3.7", + "version": "0.3.8", "description": "node eibd client", "main": "./index", "repository": {