As a kind of follow on from the other (recent) post, I am still stuck but with a new problem.
I am sending commands to remote machines and monitoring their reply.
When a command is received I get this:
{"topic":"COMMAND_REPLY/BedPi","payload":"Command received","qos":2,"retain":false,"_msgid":"71d39c0b.57bfe4"}
All being good, I get this back:
{"topic":"COMMAND_REPLY/BedPi","payload":"{\"code\":0}","qos":2,"retain":false,"_msgid":"a395b354.35cdf"}
In bad situations I get this back (as example)
{"topic":"COMMAND_REPLY/BedPi","payload":"{\"code\":127,\"message\":\"Command failed: ityut\\n/bin/sh: 1: ityut: not found\\n\",\"result\":\"FAIL\"}","qos":2,"retain":false,"_msgid":"4ec683cd.3abe1c"}
(Not rocket science.)
But the problem now happens.
These come back on COMMAND_REPLY/(devicename)
All good there.
But I then (now?) want to farm this off to another process which will alert me with real hardware.
This is the flow I have - which doesn't work.
[{"id":"c4be6f7.a9f739","type":"mqtt in","z":"9c1e5490.5d70b","name":"Return Codes from remote commands","topic":"COMMAND_REPLY/#","qos":"2","broker":"8d56c01f.a5662","x":210,"y":820,"wires":[["8cc09199.3899c8","40e0b56c.e338fc"]]},{"id":"8cc09199.3899c8","type":"function","z":"9c1e5490.5d70b","name":"Command spliter","func":"var result_ = msg.payload;\nvar fcommand = msg.payload.message;\nnode.warn(\"Fcommand\");\nnode.warn(fcommand);\nif (result_.indexOf('code\":1') !== -1)\n{\n //\n node.status({fill:\"yellow\",shape:\"dot\",text:\"Oops\"});\n msg.payload = \"Oops\";\n // more code here for the second output.\n var msg2 = {payload: \"yellow\"};\n}\n\nif (result_.indexOf('code\":127') !== -1)\n{\n node.status({fill:\"red\",shape:\"dot\",text:\"Fail\"});\n msg = {payload:\"Command fail\", command:fcommand};\n node.warn(msg);\n// msg.payload = \"Command fail\";\n // more code here for the second output.\n msg2 = {payload: \"red\"};\n}\nreturn [msg,msg2];\n","outputs":2,"noerr":0,"x":490,"y":820,"wires":[["53a1e649.584bd8","e3691ea6.85f748"],["fa9d0647.0bcf7"]]},{"id":"40e0b56c.e338fc","type":"debug","z":"9c1e5490.5d70b","name":"RAW message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":420,"y":740,"wires":[]},{"id":"fa9d0647.0bcf7","type":"debug","z":"9c1e5490.5d70b","name":"Alarm colour","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":680,"y":870,"wires":[]},{"id":"53a1e649.584bd8","type":"debug","z":"9c1e5490.5d70b","name":"Alarm message","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":740,"wires":[]},{"id":"e3691ea6.85f748","type":"switch","z":"9c1e5490.5d70b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Command successful.","vt":"str"},{"t":"eq","v":"Oops.","vt":"str"},{"t":"eq","v":"Command fail.","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":680,"y":790,"wires":[[],[],["1d82abad.643b34","2ef7273c.d9f948"]]},{"id":"1d82abad.643b34","type":"debug","z":"9c1e5490.5d70b","name":"URGENT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":890,"y":820,"wires":[]},{"id":"2ef7273c.d9f948","type":"mqtt out","z":"9c1e5490.5d70b","name":"","topic":"TEST","qos":"","retain":"","broker":"8a2e80be.f7c928","x":880,"y":780,"wires":[]},{"id":"8d56c01f.a5662","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"TimePi Comms Up","closeTopic":"EOM","closePayload":"TimePi shutting down","willTopic":"EOM","willQos":"0","willPayload":"TimePi Comms Failure"},{"id":"8a2e80be.f7c928","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"ARDUINO_STATUS","birthQos":"2","birthPayload":"connected","willTopic":"ARDUINO_STATUS","willQos":"0","willPayload":"disconnected"}]
The problem is it works for the "command received" and "command good".
But when I get a "command error" and the extra data the function
node isn't splitting / dealing with the payload (?).
I put a JSON
node in to get that formatted (?) correctly and it works, but it spits the dummy when it gets the "command received" and "command ok" messages.
unexpected character at position 0
I think.
Or it is TypeError: result_.indexOf is not a function
because of the msg.payload structure.
(That is from the function
node.)
I've been going around in circles because I got it working with the error coming back, but forgot the check what happens with the "received" and "ok" messages.
To get them working, it then falls over with an error message.
I am wanting/trying to construct a message which can be sent with MQTT to another device to then decode the error and indicate the needed information.
That (in this example) is:
The device name - BedPi in this case.
The command - Command failed: ityut/bin/sh: 1: ityut: not found in this case.
Does that make sense?
How can I get around the problem?
I'm open to maybe handling the original messages being re-parsed rather than going through an intermediate flow.
If the flow I posted, the MQTT OUT
node at the end is the input to the next stage where the real world I/O happens. (Indicators etc)
It is publishing to TEST
for now as I am still working on that part too.
I'll stop typing/digging now. Hope someone can see what I am trying to do and can offer a solution.