codmpm / node-red-contrib-loxone

Connect the Loxone Miniserver to node-red via the Websocket API
MIT License
74 stars 24 forks source link

webservice node can crash node-red startup, but easy to work around #35

Closed emes closed 4 years ago

emes commented 4 years ago

Sending a request with the Webservice node immediately after starting Node-RED consistently results in a crash. This happens because structureData.msInfo is not there yet. Probably related to https://github.com/codmpm/node-red-contrib-loxone/issues/12

11 Dec 13:39:20 - [warn] [function:delay] lox not ready, queue messages
11 Dec 13:39:20 - [red] Uncaught Exception:
11 Dec 13:39:20 - TypeError: Cannot read property 'msInfo' of null
    at API.<anonymous> (/data/node_modules/node-red-contrib-loxone/loxone/loxone.js:241:61)
    at API.emit (events.js:198:13)
    at API.EventEmitter.emit (domain.js:448:20)
    at TokenEnc.<anonymous> (/data/node_modules/node-lox-ws-api/lib/API.js:256:14)
    at TokenEnc.emit (events.js:198:13)
    at TokenEnc.EventEmitter.emit (domain.js:448:20)
    at Object.callback (/data/node_modules/node-lox-ws-api/lib/Auth/Token-Enc.js:204:22)
    at API._message_text (/data/node_modules/node-lox-ws-api/lib/API.js:210:18)
    at Connection.<anonymous> (/data/node_modules/node-lox-ws-api/lib/API.js:114:14)
    at Connection.emit (events.js:198:13)
    at Connection.EventEmitter.emit (domain.js:448:20)
    at Connection.handle_message (/data/node_modules/node-lox-ws-api/lib/Connection.js:136:18)
    at WebSocketConnection.<anonymous> (/data/node_modules/node-lox-ws-api/lib/Connection.js:103:14)
    at WebSocketConnection.emit (events.js:198:13)
    at WebSocketConnection.EventEmitter.emit (domain.js:448:20)
    at WebSocketConnection.processFrame (/data/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /data/node_modules/websocket/lib/WebSocketConnection.js:323:40
    at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-red-docker@1.0.3 start: `node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS "--userDir" "/data"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-red-docker@1.0.3 start script.

Same on non-Docker hosts.

Easy workaround is to put a delay node in front of the webservice node, use a status node to monitor the webservice node, and override the delay when the webservice node is not connected. Something like:

if (!msg.status.hasOwnProperty("text") || msg.status.text === "connected") {
    msg.delay = 0;
    msg.flush = true;
} else {
    msg.delay = 60 * 1000;
}

Miniserver is running 10.3.11.25 but first noticed this shortly after msInfo was added. Apologies for not reporting at the time.

codmpm commented 4 years ago

Hey @emes. Thanks for #34, already merged.

I will have a look into this, as I'm currently working on #28. I forked node-lox-ws-api and added error messages.

Regarding your issue I don't think it will be possible to "wait" for the connection to come up, as this could be never (e.g. on erorrs). But I have to fix that node-red-contrib-loxone will not crash and simply not answer the request. Thus the delay-node workaround will still be needed.

Open to better solutions, though.

Cheers, Patrik

codmpm commented 4 years ago

fixed in 0.10.3

codmpm commented 4 years ago

I've also added a log entry, when a webservice is called and structureData(msInfo) is not there. The request will be processed as long as a connection to the miniserver is established.