Optimization Of Buffer Conversion

#1

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"]]}]
#2

This seems better...
It is possible to stack the split nodes.

[{"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":[["ff4af11.b3b281"]]},{"id":"ff4af11.b3b281","type":"split","z":"6b4e8eca.2e2a","name":"Split The Buffer At Each Line Feed (ASCII 10)","splt":"[10]","spltType":"bin","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"","x":460,"y":240,"wires":[["13605354.0ba34d"]]},{"id":"b73748b1.7579f8","type":"debug","z":"6b4e8eca.2e2a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":800,"y":370,"wires":[]},{"id":"6d6087d7.16f498","type":"function","z":"6b4e8eca.2e2a","name":"Grab Needed Data From Array","func":"var t = msg.payload[3]\nvar c = msg.payload[5]\nvar temp = parseFloat(t.toString('utf8'))\nvar count = Number(c.toString('utf8'))\nmsg = {};\nmsg.payload = {\"AverageTemp\": temp, \"SnapShot\": count}\n\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":370,"wires":[["b73748b1.7579f8"]]},{"id":"13605354.0ba34d","type":"split","z":"6b4e8eca.2e2a","name":"Split Each Payload At TAB (ASCII 09)","splt":"[9]","spltType":"bin","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":430,"y":290,"wires":[["ebe72a3a.764d68"]]},{"id":"ebe72a3a.764d68","type":"join","z":"6b4e8eca.2e2a","name":"Combine Each Individual Payload To Form An Array","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"9","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":450,"y":330,"wires":[["6d6087d7.16f498"]]}]
#3

Or maybe

[{"id":"713a572b.ef8d68","type":"inject","z":"ff27ff95.cc8d5","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":120,"y":1200,"wires":[["79ef1c4d.957724"]]},{"id":"79ef1c4d.957724","type":"split","z":"ff27ff95.cc8d5","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":290,"y":1200,"wires":[["6671d931.e841b8"]]},{"id":"6671d931.e841b8","type":"switch","z":"ff27ff95.cc8d5","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Zone","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":430,"y":1200,"wires":[["53cc3cc4.f66854"]]},{"id":"53cc3cc4.f66854","type":"function","z":"ff27ff95.cc8d5","name":"","func":"var m = msg.payload.toString().split(\"\\t\");\nmsg.topic = m[0];\nmsg.payload = parseFloat(m[1]);\ndelete msg.parts;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":1200,"wires":[["c8bfc284.77be6"]]},{"id":"c8bfc284.77be6","type":"debug","z":"ff27ff95.cc8d5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":710,"y":1200,"wires":[]}]
1 Like
#4

Thank you for your time dceejay.
Always nice to see another way of doing things.
I was not thinking about filtering out the garbage data or using the split inside of a function node.