phillipsnick / unipi-evok

Node JS wrapper for UniPi API
MIT License
1 stars 2 forks source link

Crash on 1wire messages #12

Closed majorcs closed 6 years ago

majorcs commented 6 years ago

Thanks for your effort to create this lib and the NodeRed extension as it makes the UniPi a really good player with NodeRed. I managed to implement some similar functionality within NodeRed but it's quite difficult to maintain, your library and nodes makes it really easy.

There is a small issue. My UniPi (Neuron L203) has a OneWire sensor and it generates payload on the websocket with a little bit different structure than the other I/Os.

{"interval":15,"value":24.05,"circuit":"28409D1F0000801E","address":"28409D1F0000801E","time":1525949750.997278,"typ":"DS18B20","lost":true,"dev":"temp"}

I think the issue is the payload in this case is just a hash not an array. This results the following traceback:

/home/major/unipi-evok/lib/api.js:142
        message.forEach((device) => {
                ^

TypeError: message.forEach is not a function
    at evok.parse (/home/major/unipi-evok/lib/api.js:142:17)
    at emitOne (events.js:116:13)
    at evok.emit (events.js:211:7)
    at evok.receive (/home/major/unipi-evok/lib/client.js:87:14)
    at emitOne (events.js:116:13)
    at WebSocketConnection.emit (events.js:211:7)
    at WebSocketConnection.processFrame (/home/major/node_modules/websocket/lib/WebSocketConnection.js:552:26)
    at /home/major/node_modules/websocket/lib/WebSocketConnection.js:321:40
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
majorcs commented 6 years ago

I did a quick test, and seems this dirty hack can be a workaround

diff --git a/lib/api.js b/lib/api.js
index ed4a0bd..3d44769 100644
--- a/lib/api.js
+++ b/lib/api.js
@@ -139,6 +139,9 @@ class evok extends client {

     parse(message) {
         // handle devices
+        if (!Array.isArray(message)) {
+            message = [message]
+        }
         message.forEach((device) => {
             let previous = this.device(device.dev, device.circuit)
phillipsnick commented 6 years ago

Hey, I haven't managed to wire any 1wire devices up just yet so never tested them (https://github.com/phillipsnick/unipi-evok/issues/6).

If that fix resolves the issue, feel free to submit a PR and I'll run a quick test later and release.

phillipsnick commented 6 years ago

Fixed under #13