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

Raw telegram event #48

Merged
merged 7 commits into from
Apr 8, 2017
Merged

Raw telegram event #48

merged 7 commits into from
Apr 8, 2017

Conversation

snowdd1
Copy link
Contributor

@snowdd1 snowdd1 commented Feb 27, 2017

When using an exernal decoder for telegram data, the event system could not be used fully as the data was pre-parsed by decoder.js decode() which might not have been guessing the type right (e.g. for a lot of scaled data types)

Refers to #47

For handling unparsed data this commit adds an 'telegram' event (only
with data, otherwise use "read" event) emits
`"telegram", event, src, dest, buffer`
tools.adr2str --> tools.addr2str;
@andreek
Copy link
Owner

andreek commented Mar 30, 2017

Older versions are broken because of Buffers.from(..). Its possible to improve? Maybe building a workaround if from is undefined in Buffer using old Buffer constructor?

@snowdd1
Copy link
Contributor Author

snowdd1 commented Mar 30, 2017

I am not very proficient in node < 4.0 (that was the LTS when I started), but I'll give it a try:

Buffer.from() existed in node 4.4 - use Buffer.alloc() as test for old
node versions
@snowdd1
Copy link
Contributor Author

snowdd1 commented Mar 30, 2017

... and failed, as Buffer.from() existed in older node versions, but is neither documented nor does it work as expected. As test I now use Buffer.alloc(), which really was not existing in node 4.4 and before.

@andreek
Copy link
Owner

andreek commented Mar 31, 2017

Well, I found a npm packet that is solving the problem. I don't want node-eibd to depend on it. Because we only need to know if we are new api or old api.

See https://github.com/LinusU/buffer-from/blob/master/index.js

We could add a method to our tools like this isModern and use it to determine if we got the new buffer api in parser.js.

@snowdd1
Copy link
Contributor Author

snowdd1 commented Mar 31, 2017

Hi André, I've compared that to what I did - and it's basically the same. It uses Buffer.alloc to determine if the "modern" Buffer is available. Of course it has a lot of more features (such as determining if the parameter passed to Buffer.from() is an array or a string, but we don't need all this.

Questions: Do you want to have a central place in eibd to determine the modernity of node once and put that into a variable, and define the buffer initializer and copier in one place (better if we would need it somewhere else, too), or leave it in the parse.js where it is now (single purpose)?

@snowdd1
Copy link
Contributor Author

snowdd1 commented Mar 31, 2017

But I found that I don't seem to need to create the buffer first and then copy the data to it, there was a Buffer constructor that took a buffer, too. So it's basically down to 4 lines. Fingers crossed that travis shows good!

lib/parser.js Outdated
var valbuffer;
if (len<=8) {
val = telegram[telegram.length-1];
if (Buffer.alloc) { // use Buffer.alloc as a means of distinction, as Buffer.from existed in node 4.4 but did not work as expected
Copy link
Owner

Choose a reason for hiding this comment

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

// val is an integer, to to pass it to Buffer in old api we need to make it an Array of length 1
valbuffer = isModernBuffer ? Buffer.from(val) : new Buffer([val]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, done

lib/parser.js Outdated
val = telegram.slice(10, telegram.length);
if (Buffer.alloc) { // see above
Copy link
Owner

Choose a reason for hiding this comment

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

valbuffer = isModernBuffer ? Buffer.from(val) : new Buffer(val);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.


val = telegram[telegram.length-1];
if(len > 8) {
var valbuffer;
Copy link
Owner

Choose a reason for hiding this comment

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

Doing node api detection in one place

var isModernBuffer = (
  typeof Buffer.alloc === 'function' &&
  typeof Buffer.allocUnsafe === 'function' &&
  typeof Buffer.from === 'function'
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now in the head of the module.

@andreek
Copy link
Owner

andreek commented Apr 2, 2017

Leaved some comments about how to get the code more readable .

@snowdd1
Copy link
Contributor Author

snowdd1 commented Apr 6, 2017

HI André, if changed the lines according to the tips you've left for me. Much better readable now, and fewer lines of code, too.

@snowdd1
Copy link
Contributor Author

snowdd1 commented Apr 6, 2017

I am so confident now I have even increased the version number in package.json.

@andreek andreek merged commit 53151ca into andreek:master Apr 8, 2017
@Demonderik
Copy link

Hi, something wrong with new parser - now my home bridge-knx setup broken:

�[37m[4/12/2017, 10:33:56 PM]�[39m �[36m[Marantz]�[39m current power state is: OFF
stdout
22:34:27
�[37m[4/12/2017, 10:34:27 PM]�[39m �[36m[homebridge-knx.KNX]�[39m DEBUG got dest=518
stdout
22:34:27
buffer.js:89
stdout
22:34:27
throw new TypeError('"value" argument must not be a number');
stdout
22:34:27
^
stdout
22:34:27
stdout
22:34:27
TypeError: "value" argument must not be a number
stdout
22:34:27
at Function.Buffer.from (buffer.js:89:11)
stdout
22:34:27
at Parser.parseTelegram (/usr/lib/node_modules/homebridge-knx/node_modules/eibd/lib/parser.js:84:43)
stdout
22:34:27
at Parser.onData (/usr/lib/node_modules/homebridge-knx/node_modules/eibd/lib/parser.js:150:12)
stdout
22:34:27
at Socket.<anonymous> (/usr/lib/node_modules/homebridge-knx/node_modules/eibd/lib/parser.js:36:10)
stdout
22:34:27
at emitOne (events.js:96:13)
stdout
22:34:27
at Socket.emit (events.js:188:7)
stdout
22:34:27
at readableAddChunk (_stream_readable.js:176:18)
stdout
22:34:27
at Socket.Readable.push (_stream_readable.js:134:10)
stdout
22:34:27
at TCP.onread (net.js:543:20)

@andreek
Copy link
Owner

andreek commented Apr 12, 2017

Which node version?

@andreek
Copy link
Owner

andreek commented Apr 12, 2017

nodejs/node#4660

@Demonderik
Copy link

Hi andreek,
I have node v6.7.0 installed.

@Demonderik
Copy link

andreek, I have compatibility problems with the latest stable node.
What first version of node that contains #4660 fix?

@Demonderik
Copy link

hi andreek, tried node v6.10.0
and latest eibd v0.3.8
Unfortunately - NO success.
homebridge-knx v0.3.10 throw exception in eibd module during (any?) attempt to read the telegram from knx (knxd runs on another docker image) This behaviour begins after latest update of the homebridge v0.4.17

@andreek
Copy link
Owner

andreek commented Apr 14, 2017

@Demonderik should be fixed. See #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants