Manage device data with Node Red

Andrea3 years ago

Hi,
I needed to send some events (button push on a Teltonika FMB120 device) but with Tracca I wasn't able to do it.
Tho goal was to open the office gate and the home gate (based on location) with just 1 button connected to the Traccar device and only if I'm in the car.
So I've added these to the traccar.xml configuration file to forward the Teltonika and smartphone requests to Node Red:

 
    <entry key='forward.enable'>true</entry>
    <entry key='forward.json'>true</entry>
    <entry key='forward.url'>http://172.20.0.10:1880/traccar</entry>

Where 172.20.0.10 is the Node Red docker container.
In Node Red I have these nodes:

[{"id":"3ccbc4f0.819214","type":"http in","z":"6cb9aa9b.7228dc","name":"","url":"/traccar","method":"post","upload":false,"swaggerDoc":"","x":110,"y":520,"wires":[["12f80585.4774ba","f8deb287.a52108","bd9b6cb0.abf7b8"]]},{"id":"12f80585.4774ba","type":"debug","z":"6cb9aa9b.7228dc","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":290,"y":460,"wires":[]},{"id":"4fe74345.4dbe74","type":"http response","z":"6cb9aa9b.7228dc","name":"","statusCode":"","headers":{},"x":430,"y":520,"wires":[]},{"id":"f8deb287.a52108","type":"template","z":"6cb9aa9b.7228dc","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        Ok! {{req.query.name}}!\n    </body>\n</html>","x":290,"y":520,"wires":[["4fe74345.4dbe74"]]},{"id":"bd9b6cb0.abf7b8","type":"switch","z":"6cb9aa9b.7228dc","name":"Who is it?","property":"payload.device.id","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":280,"y":580,"wires":[["e8e678df.6f7c9"],["f6ffb40a.4cf718","16804d7e.b45e3b"]]},{"id":"e28dd67d.b2bc38","type":"comment","z":"6cb9aa9b.7228dc","name":"Device IDs","info":"1 smartphone\n4 Giulietta","x":100,"y":580,"wires":[]},{"id":"e8e678df.6f7c9","type":"change","z":"6cb9aa9b.7228dc","name":"Store smartphone geofence into global var","rules":[{"t":"set","p":"smartphone-geofenceID","pt":"global","to":"payload.device.geofenceIds[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":560,"wires":[[]]},{"id":"f6ffb40a.4cf718","type":"change","z":"6cb9aa9b.7228dc","name":"Store Giulietta geofence into global var","rules":[{"t":"set","p":"giulietta-geofenceID","pt":"global","to":"payload.device.geofenceIds[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":600,"wires":[[]]},{"id":"16804d7e.b45e3b","type":"switch","z":"6cb9aa9b.7228dc","name":"Giulietta button pushed?","property":"payload.position.attributes.alarm","propertyType":"msg","rules":[{"t":"eq","v":"sos","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":640,"wires":[["60b11f27.f2cb3"]]},{"id":"7b014815.25b318","type":"comment","z":"6cb9aa9b.7228dc","name":"Geofence IDs","info":"1 office\n2 home","x":110,"y":620,"wires":[]},{"id":"60b11f27.f2cb3","type":"function","z":"6cb9aa9b.7228dc","name":"Check if smartphone is near Giulietta","func":"if (global.get(\"smartphone-geofenceID\") == global.get(\"giulietta-geofenceID\")){\n    var newMsg = {};\n    newMsg.payload = msg.payload.device.geofenceIds;\n    return newMsg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":770,"y":640,"wires":[["4b53b3f8.88b474"]]},{"id":"4b53b3f8.88b474","type":"switch","z":"6cb9aa9b.7228dc","name":"Which geofence?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":640,"wires":[["ab4f94d2.94e498"],["2728d40d.f221fc"]]},{"id":"52a2a307.6cf9ec","type":"mqtt out","z":"6cb9aa9b.7228dc","name":"","topic":"traccar/gate","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b07da909.a1626","x":1350,"y":640,"wires":[]},{"id":"ab4f94d2.94e498","type":"change","z":"6cb9aa9b.7228dc","name":"Office","rules":[{"t":"set","p":"payload","pt":"msg","to":"office","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1200,"y":620,"wires":[["52a2a307.6cf9ec"]]},{"id":"2728d40d.f221fc","type":"change","z":"6cb9aa9b.7228dc","name":"Home","rules":[{"t":"set","p":"payload","pt":"msg","to":"home","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1200,"y":660,"wires":[["52a2a307.6cf9ec"]]},{"id":"b07da909.a1626","type":"mqtt-broker","name":"Polluce 1883","broker":"172.20.0.12","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]