[{"id":"49e3c067.e782e","type":"change","z":"449c1517.e25f4c","name":"Payload","rules":[{"t":"change","p":"topic","pt":"msg","from":"up","fromt":"str","to":"down","tot":"str"},{"t":"set","p":"payload.confirmed","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"payload.schedule","pt":"msg","to":"replace","tot":"str"},{"t":"move","p":"payload","pt":"msg","to":"payload.payload_raw","tot":"msg"},{"t":"move","p":"port","pt":"msg","to":"payload.port","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":513,"wires":[["84f1cda2.069e7"]]},{"id":"cc140589.dea168","type":"mqtt in","z":"449c1517.e25f4c","name":"listen","topic":"+/devices/+/up","qos":"2","broker":"2a15ab6f.ab2244","x":110,"y":120,"wires":[["4f97d75.6c87528"]]},{"id":"72d5e7ee.d1eba8","type":"mqtt out","z":"449c1517.e25f4c","name":"send","topic":"","qos":"","retain":"","broker":"2a15ab6f.ab2244","x":730,"y":513,"wires":[]},{"id":"4f97d75.6c87528","type":"json","z":"449c1517.e25f4c","name":"Convert","property":"payload","action":"","pretty":false,"x":260,"y":120,"wires":[["9f4b8dd3.2f0d2"]]},{"id":"9f4b8dd3.2f0d2","type":"switch","z":"449c1517.e25f4c","name":"Timeport","property":"payload.port","propertyType":"msg","rules":[{"t":"eq","v":"9","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":420,"y":120,"wires":[["8ed813a9.a9319"]]},{"id":"dac8aafa.389298","type":"json","z":"449c1517.e25f4c","name":"Convert","property":"payload","action":"","pretty":false,"x":580,"y":513,"wires":[["72d5e7ee.d1eba8"]]},{"id":"8ed813a9.a9319","type":"base64","z":"449c1517.e25f4c","name":"Decode","action":"","property":"payload.payload_raw","x":420,"y":200,"wires":[["831ab883.d6a238"]]},{"id":"84f1cda2.069e7","type":"base64","z":"449c1517.e25f4c","name":"Encode","action":"","property":"payload.payload_raw","x":420,"y":513,"wires":[["dac8aafa.389298"]]},{"id":"6190967b.01f758","type":"comment","z":"449c1517.e25f4c","name":"LoRaWAN Timeserver v1.1","info":"PLEASE NOTE: There is a patent filed for the time sync algorithm used in the\ncode of this file. The shown implementation example is covered by the\nrepository's licencse, but you may not be eligible to deploy the applied\nalgorithm in applications without granted license by the patent holder.","x":170,"y":40,"wires":[]},{"id":"831ab883.d6a238","type":"function","z":"449c1517.e25f4c","name":"Generate Time Answer","func":"/* LoRaWAN Timeserver\n\nconstruct 5 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n1..4 current second (from epoch time 1970)\n5 1/250ths fractions of current second\n\nFPort = sequence number (taken from node's time_sync_req)\n\n*/\n\nfunction timecompare(a, b) {\n \n const timeA = a.time;\n const timeB = b.time;\n\n let comparison = 0;\n if (timeA > timeB) {\n comparison = 1;\n } else if (timeA < timeB) {\n comparison = -1;\n }\n return comparison;\n}\n\nlet confidence = 2000; // max millisecond diff gateway time to server time\n\nvar deviceMsg = { payload: msg.payload.dev_id };\nvar seqNo = msg.payload.payload_raw[0];\nvar seqNoMsg = { payload: seqNo };\nvar gateway_list = msg.payload.metadata.gateways;\n\n// filter all gateway timestamps that have milliseconds part (which we assume have a \".\")\nvar gateways = gateway_list.filter(function (element) {\n return (element.time.includes(\".\"));\n});\n\nvar gateway_time = gateways.map(gw => {\n return {\n time: new Date(gw.time),\n eui: gw.gtw_id,\n }\n });\nvar server_time = new Date(msg.payload.metadata.time);\n\n// validate all gateway timestamps against lorawan server_time (which is assumed to be recent)\nvar gw_timestamps = gateway_time.filter(function (element) {\n return ((element.time > (server_time - confidence) && element.time <= server_time));\n});\n\n// if no timestamp left, we have no valid one and exit\nif (gw_timestamps.length === 0) {\n var notavailMsg = { payload: \"n/a\"};\nvarnotimeMsg={payload:0xff};\nvarbuf2=Buffer.alloc(1);\nmsg.payload=newBuffer(buf2.fill(0xff));\nreturn[notavailMsg,