Function with switch statememt

9,665 views
Skip to first unread message

Dave C-J

unread,
Aug 17, 2015, 3:30:58 PM8/17/15
to Node-RED
A quick example of a function with a switch statement inside to provide separate outputs...

[{"id":"c474f6e8.3b8b08","type":"inject","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":399,"y":547,"z":"ef78dde5.10872","wires":[["339a460c.cc65ba"]]},{"id":"339a460c.cc65ba","type":"function","name":"function with switch","func":"\nvar day;\nvar m = msg.payload % 5;\nnode.warn(\"number = \" + m);\nswitch (m) {\n    case 0:\n        return[{payload:0},null,null,null,null];\n    case 1:\n        return[null,{payload:1},null,null,null];\n    case 2:\n        return[null,null,{payload:2},null,null];\n    case 3:\n        return[null,null,null,{payload:3},null];\n    case 4:\n        return[null,null,null,null,{payload:4}];\n}\n\nreturn null;","outputs":"5","noerr":0,"x":604,"y":546,"z":"ef78dde5.10872","wires":[["9a42f2d8.65bd1"],["7e380634.81c7f8"],["77151e17.88eae"],["1376d06c.ec893"],["c1774d45.3e88b"]]},{"id":"9a42f2d8.65bd1","type":"debug","name":"","active":true,"console":"false","complete":"false","x":852,"y":429,"z":"ef78dde5.10872","wires":[]},{"id":"7e380634.81c7f8","type":"debug","name":"","active":true,"console":"false","complete":"false","x":854,"y":472,"z":"ef78dde5.10872","wires":[]},{"id":"77151e17.88eae","type":"debug","name":"","active":true,"console":"false","complete":"false","x":854,"y":516,"z":"ef78dde5.10872","wires":[]},{"id":"1376d06c.ec893","type":"debug","name":"","active":true,"console":"false","complete":"false","x":854,"y":561,"z":"ef78dde5.10872","wires":[]},{"id":"c1774d45.3e88b","type":"debug","name":"","active":true,"console":"false","complete":"false","x":856,"y":607,"z":"ef78dde5.10872","wires":[]}]

Simon John

unread,
Aug 17, 2015, 3:47:23 PM8/17/15
to Node-RED
in my case, msg.payload is a string like "5;3;34.0", which i then split:

if (msg.payload)
{
    msg
.node = msg.payload.split(';')[0];
    msg
.type = msg.payload.split(';')[1];
    msg
.reading = msg.payload.split(';')[2];
}

return msg;



but in my switch statement, my "cases" are integers, so a quick call to parseInt() fixed it:


switch(parseInt(msg.type))
{
   
case 1:
        msg
.output = "shed DS-temp";
       
break;
   
case 2:
        msg
.output = "shed BMP-temp";
       
break;
   
case 3:
        msg
.output = "shed BMP-pressure";
       
break;  
   
default:
        msg
.output = "shed unknown sensor";
}

return msg;




 

Simon John

unread,
Aug 17, 2015, 4:52:37 PM8/17/15
to Node-RED
in fact change that to:

msg.reading = msg.payload.split(';')[2].trim();


or there will be other types of problems with trailing newlines

Julian Knight

unread,
Aug 18, 2015, 4:14:20 AM8/18/15
to Node-RED
You might also want some additional checks on the incoming data in case you get a missing or blank entry. You should also restrict the length of your input string so that it can't be "fuzzed" by a rogue transmission.

Simon John

unread,
Aug 18, 2015, 6:49:57 AM8/18/15
to Node-RED
oh certainly yes, but this is day1, security comes in at day3 and exception handling i've got to look into also

Julian Knight

unread,
Aug 18, 2015, 12:10:47 PM8/18/15
to Node-RED
;)
Reply all
Reply to author
Forward
0 new messages