More of a programming topic, but I felt that the good people here could give their advice.
I have a JSONArray buffer that is received by a tcp node, like so:
[2,115,99,97,110,110,101,114,9,48,10,90,111,110,101,49,9,56,57,51,46,52,10,90,111,110,101,50,9,50,55,55,50,46,48,10,99,104,101,99,107,115,117,109,9,48,48,48,48,48,57,50,54,10,3]
This translates to readable text as:
scanner 0
Zone1 893.4
Zone2 2772.0
checksum 00000926
There are only two values I care about. The first being Zone1 893.4
and Zone2 2772.0
.
I have hacked the values out and was wondering if there was a more elegant way of doing it.
My intention was to create a key/value pair with Zone1
and Zone2
being the two keys. I ended up just writing the values to their own variable under the msg object because I did not understand how to split them.
The "keys" are represented between ASCII 10 and 9. The "values" are between ASCII 9 and 10.
It is my understanding that I can only split the buffer using one split character at a time.
[{"id":"f7f7265c.c87868","type":"inject","z":"6b4e8eca.2e2a","name":"For Testing Only","topic":"","payload":"[2,115,99,97,110,110,101,114,9,48,10,90,111,110,101,49,9,56,57,51,46,52,10,90,111,110,101,50,9,50,55,55,50,46,48,10,99,104,101,99,107,115,117,109,9,48,48,48,48,48,57,50,54,10,3]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":240,"wires":[["43443798.8ce148"]]},{"id":"43443798.8ce148","type":"function","z":"6b4e8eca.2e2a","name":"Convert Payload To JSON String","func":"var n = JSON.stringify(msg.payload.toString('utf8'))\nmsg.origBuffer = msg.payload\nmsg.payload = n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":240,"wires":[["ff4af11.b3b281"]]},{"id":"c4d108e0.ffb5b8","type":"join","z":"6b4e8eca.2e2a","name":"Combine The Split Parts Into An Array","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":490,"y":320,"wires":[["6d6087d7.16f498"]]},{"id":"ff4af11.b3b281","type":"split","z":"6b4e8eca.2e2a","name":"Split The JSON Object","splt":"\\","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":460,"y":280,"wires":[["c4d108e0.ffb5b8"]]},{"id":"b73748b1.7579f8","type":"debug","z":"6b4e8eca.2e2a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":400,"wires":[]},{"id":"6d6087d7.16f498","type":"function","z":"6b4e8eca.2e2a","name":"Grab Needed Data From Array","func":"var t = msg.payload[4]\nvar c = msg.payload[6]\nvar temp = t.slice(1,t.length)\nvar count = c.slice(1,c.length)\nmsg.averageTemp = temp\nmsg.snapShotCounter = count\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":360,"wires":[["bb810854.4d2e08"]]},{"id":"bb810854.4d2e08","type":"change","z":"6b4e8eca.2e2a","name":"Convert Data From String To Number","rules":[{"t":"set","p":"averageTemp","pt":"msg","to":"$number(averageTemp)","tot":"jsonata"},{"t":"set","p":"snapShotCounter","pt":"msg","to":"$number(snapShotCounter)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":400,"wires":[["b73748b1.7579f8"]]}]