node-red config with use of ttn nodes

This commit is contained in:
Marius Gripp 2019-08-27 15:40:33 +02:00
parent 730f35babf
commit 6894d718e6

View File

@ -1,410 +1,474 @@
[
{
"id":"656d6468.69133c",
"type":"tab",
"label":"Flow 1",
"disabled":false,
"info":""
},
{
"id":"2a15ab6f.ab2244",
"type":"mqtt-broker",
"z":"",
"name":"eu.thethings.network:1883",
"broker":"mqtt://xdot-gr:ttn-account-v2.eH0S7LsbltxCHjNTfMLcDc_mzhW2n4IN8GrtC-mKOXk@eu.thethings.network/",
"port":"1883",
"tls":"",
"clientid":"",
"usetls":false,
"compatmode":true,
"keepalive":"60",
"cleansession":true,
"birthTopic":"",
"birthQos":"0",
"birthPayload":"",
"closeTopic":"",
"closeQos":"0",
"closePayload":"",
"willTopic":"",
"willQos":"0",
"willPayload":""
},
{
"id":"edb7cc8d.a3817",
"type":"ui_group",
"z":"",
"name":"Timeserver",
"tab":"d525a5d.0832858",
"order":4,
"disp":true,
"width":"6",
"collapse":false
},
{
"id":"d525a5d.0832858",
"type":"ui_tab",
"z":"",
"name":"Timeserver",
"icon":"sync",
"order":3,
"disabled":false,
"hidden":false
},
{
"id":"49e3c067.e782e",
"type":"change",
"z":"656d6468.69133c",
"name":"Payload",
"rules":[
{
"t":"change",
"p":"topic",
"pt":"msg",
"from":"up",
"fromt":"str",
"to":"down",
"tot":"str"
{
"id":"23a9162c.cb1e6a",
"type":"tab",
"label":"Flow 1",
"disabled":false,
"info":""
},
{
"id":"2a15ab6f.ab2244",
"type":"mqtt-broker",
"z":"",
"name":"mqtt://db-clock-update-test:ttn-account-v2.etVLJSGCzS5ExuDw59HtGUIzv9Kcu8UPT_eWiz8ARs0@eu.thethings.network",
"broker":"mqtt://db-clock-update-test:ttn-account-v2.etVLJSGCzS5ExuDw59HtGUIzv9Kcu8UPT_eWiz8ARs0@eu.thethings.network",
"port":"1883",
"tls":"",
"clientid":"",
"usetls":false,
"compatmode":true,
"keepalive":"60",
"cleansession":true,
"birthTopic":"",
"birthQos":"0",
"birthPayload":"",
"closeTopic":"",
"closeQos":"0",
"closePayload":"",
"willTopic":"",
"willQos":"0",
"willPayload":""
},
{
"id":"edb7cc8d.a3817",
"type":"ui_group",
"z":"",
"name":"Timeserver",
"tab":"d525a5d.0832858",
"order":4,
"disp":true,
"width":"6",
"collapse":false
},
{
"id":"d525a5d.0832858",
"type":"ui_tab",
"z":"",
"name":"Timeserver",
"icon":"sync",
"order":3,
"disabled":false,
"hidden":false
},
{
"id":"428b4734.d61248",
"type":"ui_base",
"theme":{
"name":"theme-light",
"lightTheme":{
"default":"#0094CE",
"baseColor":"#0094CE",
"baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
"edited":true,
"reset":false
},
"darkTheme":{
"default":"#097479",
"baseColor":"#097479",
"baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
"edited":false
},
"customTheme":{
"name":"Untitled Theme 1",
"default":"#4B7930",
"baseColor":"#4B7930",
"baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
},
"themeState":{
"base-color":{
"default":"#0094CE",
"value":"#0094CE",
"edited":false
},
{
"t":"set",
"p":"payload.confirmed",
"pt":"msg",
"to":"false",
"tot":"bool"
"page-titlebar-backgroundColor":{
"value":"#0094CE",
"edited":false
},
{
"t":"set",
"p":"payload.schedule",
"pt":"msg",
"to":"replace",
"tot":"str"
"page-backgroundColor":{
"value":"#fafafa",
"edited":false
},
{
"t":"move",
"p":"payload",
"pt":"msg",
"to":"payload.payload_raw",
"tot":"msg"
"page-sidebar-backgroundColor":{
"value":"#ffffff",
"edited":false
},
{
"t":"move",
"p":"port",
"pt":"msg",
"to":"payload.port",
"tot":"msg"
"group-textColor":{
"value":"#1bbfff",
"edited":false
},
"group-borderColor":{
"value":"#ffffff",
"edited":false
},
"group-backgroundColor":{
"value":"#ffffff",
"edited":false
},
"widget-textColor":{
"value":"#111111",
"edited":false
},
"widget-backgroundColor":{
"value":"#0094ce",
"edited":false
},
"widget-borderColor":{
"value":"#ffffff",
"edited":false
},
"base-font":{
"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
}
],
"action":"",
"property":"",
"from":"",
"to":"",
"reg":false,
"x":240,
"y":513,
"wires":[
[
"84f1cda2.069e7"
]
]
},
{
"id":"cc140589.dea168",
"type":"mqtt in",
"z":"656d6468.69133c",
"name":"listen",
"topic":"+/devices/+/up",
"qos":"2",
"broker":"2a15ab6f.ab2244",
"x":110,
"y":120,
"wires":[
[
"4f97d75.6c87528"
]
]
},
{
"id":"72d5e7ee.d1eba8",
"type":"mqtt out",
"z":"656d6468.69133c",
"name":"send",
"topic":"",
"qos":"",
"retain":"",
"broker":"2a15ab6f.ab2244",
"x":730,
"y":513,
"wires":[
},
"angularTheme":{
"primary":"indigo",
"accents":"blue",
"warn":"red",
"background":"grey"
}
},
"site":{
"name":"Node-RED Dashboard",
"hideToolbar":"false",
"allowSwipe":"false",
"lockMenu":"false",
"allowTempTheme":"true",
"dateFormat":"DD/MM/YYYY",
"sizes":{
"sx":48,
"sy":48,
"gx":6,
"gy":6,
"cx":6,
"cy":6,
"px":0,
"py":0
}
}
},
{
"id":"f8f8391b.7e8728",
"type":"ttn app",
"z":"",
"appId":"db-clock-update-test",
"accessKey":"ttn-account-v2.etVLJSGCzS5ExuDw59HtGUIzv9Kcu8UPT_eWiz8ARs0",
"discovery":"discovery.thethingsnetwork.org:1900"
},
{
"id":"49e3c067.e782e",
"type":"change",
"z":"23a9162c.cb1e6a",
"name":"Payload",
"rules":[
{
"t":"change",
"p":"topic",
"pt":"msg",
"from":"up",
"fromt":"str",
"to":"down",
"tot":"str"
},
{
"t":"set",
"p":"confirmed",
"pt":"msg",
"to":"false",
"tot":"bool"
},
{
"t":"set",
"p":"schedule",
"pt":"msg",
"to":"replace",
"tot":"str"
}
],
"action":"",
"property":"",
"from":"",
"to":"",
"reg":false,
"x":360,
"y":600,
"wires":[
[
"813b9751.dba8a8",
"5aff6f87.f125a"
]
]
},
{
"id":"9f4b8dd3.2f0d2",
"type":"switch",
"z":"23a9162c.cb1e6a",
"name":"Timeport",
"property":"port",
"propertyType":"msg",
"rules":[
{
"t":"eq",
"v":"9",
"vt":"num"
}
],
"checkall":"true",
"repair":false,
"outputs":1,
"x":420,
"y":120,
"wires":[
[
"831ab883.d6a238",
"b1e45e31.61136"
]
]
},
{
"id":"6190967b.01f758",
"type":"comment",
"z":"23a9162c.cb1e6a",
"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":"4f97d75.6c87528",
"type":"json",
"z":"656d6468.69133c",
"name":"Convert",
"property":"payload",
"action":"",
"pretty":false,
"x":260,
"y":120,
"wires":[
[
"9f4b8dd3.2f0d2"
]
]
},
{
"id":"9f4b8dd3.2f0d2",
"type":"switch",
"z":"656d6468.69133c",
"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":"656d6468.69133c",
"name":"Convert",
"property":"payload",
"action":"",
"pretty":false,
"x":580,
"y":513,
"wires":[
[
"72d5e7ee.d1eba8"
]
]
},
{
"id":"8ed813a9.a9319",
"type":"base64",
"z":"656d6468.69133c",
"name":"Decode",
"action":"",
"property":"payload.payload_raw",
"x":580,
"y":120,
"wires":[
[
"831ab883.d6a238"
]
]
},
{
"id":"84f1cda2.069e7",
"type":"base64",
"z":"656d6468.69133c",
"name":"Encode",
"action":"",
"property":"payload.payload_raw",
"x":420,
"y":513,
"wires":[
[
"dac8aafa.389298"
]
]
},
{
"id":"6190967b.01f758",
"type":"comment",
"z":"656d6468.69133c",
"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":"23a9162c.cb1e6a",
"name":"Timeserver Logic",
"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\n// guess if we have received a valid time_sync_req command\nif (msg.payload_raw.length != 1)\n return;\n\nvar deviceMsg = { payload: msg.payload.dev_id };\nvar seqNo = msg.payload[\"timesync_seqno\"];\nvar seqNoMsg = { payload: seqNo };\nvar gateway_list = msg.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.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\" };\n var notimeMsg = { payload: 0xff }; \n var buf2 = Buffer.alloc(1);\n msg.payload = new Buffer(buf2.fill(0xff));\n return [notavailMsg, notavailMsg, deviceMsg, seqNoMsg, msg];}\n\n// sort time array in ascending order to find most recent timestamp for time answer\ngw_timestamps.sort(timecompare);\n\nvar timestamp = gw_timestamps[0].time;\nvar eui = gw_timestamps[0].eui;\nvar offset = server_time - timestamp;\n\nvar seconds = Math.floor(timestamp/1000);\nvar fractions = (timestamp % 1000) / 4;\n\nlet buf = new ArrayBuffer(6);\nnew DataView(buf).setUint8(0, seqNo);\nnew DataView(buf).setUint32(1, seconds);\nnew DataView(buf).setUint8(5, fractions);\n\nmsg.payload = new Buffer(new Uint8Array(buf));\nmsg.port = 9;\nvar euiMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [euiMsg, offsetMsg, deviceMsg, seqNoMsg, msg];",
"outputs":5,
"noerr":0,
"x":350,
"y":320,
"wires":[
[
"37722d4b.08e3c2",
"a8a04c7a.c5fbd",
"a15454a9.fa0948"
],
[
"46ce842a.614d5c"
],
[
"a5dbb4ef.019168"
],
[
"1cb58e7f.221362"
],
[
"49e3c067.e782e"
]
],
"outputLabels":[
"gw_eui",
"offset_ms",
"device",
"seq_no",
"time_sync_ans"
]
},
{
"id":"37722d4b.08e3c2",
"type":"debug",
"z":"23a9162c.cb1e6a",
"name":"Timeserver Gw",
"active":true,
"tosidebar":false,
"console":false,
"tostatus":true,
"complete":"payload",
"x":700,
"y":240,
"wires":[
]
},
{
"id":"831ab883.d6a238",
"type":"function",
"z":"656d6468.69133c",
"name":"Timeserver Logic",
"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\n// guess if we have received a valid time_sync_req command\nif (msg.payload.payload_raw.length != 1)\n return;\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\" };\n var notimeMsg = { payload: 0xff }; \n var buf2 = Buffer.alloc(1);\n msg.payload = new Buffer(buf2.fill(0xff));\n return [notavailMsg, notavailMsg, deviceMsg, seqNoMsg, msg];}\n\n// sort time array in ascending order to find most recent timestamp for time answer\ngw_timestamps.sort(timecompare);\n\nvar timestamp = gw_timestamps[0].time;\nvar eui = gw_timestamps[0].eui;\nvar offset = server_time - timestamp;\n\nvar seconds = Math.floor(timestamp/1000);\nvar fractions = (timestamp % 1000) / 4;\n\nlet buf = new ArrayBuffer(6);\nnew DataView(buf).setUint8(0, seqNo);\nnew DataView(buf).setUint32(1, seconds);\nnew DataView(buf).setUint8(5, fractions);\n\nmsg.payload = new Buffer(new Uint8Array(buf));\nmsg.port = 9;\nvar euiMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [euiMsg, offsetMsg, deviceMsg, seqNoMsg, msg];",
"outputs":5,
"noerr":0,
"x":350,
"y":320,
"wires":[
[
"37722d4b.08e3c2",
"a8a04c7a.c5fbd",
"a15454a9.fa0948"
],
[
"46ce842a.614d5c"
],
[
"a5dbb4ef.019168"
],
[
"1cb58e7f.221362"
],
[
"49e3c067.e782e"
]
],
"outputLabels":[
"gw_eui",
"offset_ms",
"device",
"seq_no",
"time_sync_ans"
]
},
{
"id":"37722d4b.08e3c2",
"type":"debug",
"z":"656d6468.69133c",
"name":"Timeserver Gw",
"active":true,
"tosidebar":false,
"console":false,
"tostatus":true,
"complete":"payload",
"x":700,
"y":240,
"wires":[
],
"icon":"node-red/bridge.png"
},
{
"id":"8712a5ac.ed18e8",
"type":"ui_text",
"z":"23a9162c.cb1e6a",
"group":"edb7cc8d.a3817",
"order":3,
"width":0,
"height":0,
"name":"Recent time",
"label":"Last answer at:",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":810,
"y":300,
"wires":[
],
"icon":"node-red/bridge.png"
},
{
"id":"8712a5ac.ed18e8",
"type":"ui_text",
"z":"656d6468.69133c",
"group":"edb7cc8d.a3817",
"order":3,
"width":0,
"height":0,
"name":"Recent time",
"label":"Last answer at:",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":810,
"y":300,
"wires":[
]
},
{
"id":"46ce842a.614d5c",
"type":"ui_gauge",
"z":"23a9162c.cb1e6a",
"name":"Timeserver offset",
"group":"edb7cc8d.a3817",
"order":2,
"width":0,
"height":0,
"gtype":"gage",
"title":"Offset gateway to server",
"label":"milliseconds",
"format":"{{value}}",
"min":0,
"max":"2000",
"colors":[
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1":"",
"seg2":"",
"x":710,
"y":380,
"wires":[
]
},
{
"id":"46ce842a.614d5c",
"type":"ui_gauge",
"z":"656d6468.69133c",
"name":"Timeserver offset",
"group":"edb7cc8d.a3817",
"order":2,
"width":0,
"height":0,
"gtype":"gage",
"title":"Offset gateway to server",
"label":"milliseconds",
"format":"{{value}}",
"min":0,
"max":"2000",
"colors":[
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1":"",
"seg2":"",
"x":710,
"y":380,
"wires":[
]
},
{
"id":"a8a04c7a.c5fbd",
"type":"ui_text",
"z":"23a9162c.cb1e6a",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Recent server",
"label":"Gateway",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":700,
"y":340,
"wires":[
]
},
{
"id":"a8a04c7a.c5fbd",
"type":"ui_text",
"z":"656d6468.69133c",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Recent server",
"label":"Gateway",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":700,
"y":340,
"wires":[
]
},
{
"id":"a15454a9.fa0948",
"type":"function",
"z":"23a9162c.cb1e6a",
"name":"Time",
"func":"msg.payload = new Date().toLocaleString('en-GB', {timeZone: 'Europe/Berlin'});\nreturn msg;",
"outputs":1,
"noerr":0,
"x":670,
"y":300,
"wires":[
[
"8712a5ac.ed18e8"
]
]
},
{
"id":"a5dbb4ef.019168",
"type":"ui_text",
"z":"23a9162c.cb1e6a",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Recent Device",
"label":"Device",
"format":"{msg.payload}",
"layout":"col-center",
"x":700,
"y":420,
"wires":[
]
},
{
"id":"a15454a9.fa0948",
"type":"function",
"z":"656d6468.69133c",
"name":"Time",
"func":"msg.payload = new Date().toLocaleString('en-GB', {timeZone: 'Europe/Berlin'});\nreturn msg;",
"outputs":1,
"noerr":0,
"x":670,
"y":300,
"wires":[
[
"8712a5ac.ed18e8"
]
]
},
{
"id":"a5dbb4ef.019168",
"type":"ui_text",
"z":"656d6468.69133c",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Recent Device",
"label":"Device",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":700,
"y":420,
"wires":[
]
},
{
"id":"1cb58e7f.221362",
"type":"ui_text",
"z":"23a9162c.cb1e6a",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Sequence No",
"label":"Sequence",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":700,
"y":460,
"wires":[
]
},
{
"id":"1cb58e7f.221362",
"type":"ui_text",
"z":"656d6468.69133c",
"group":"edb7cc8d.a3817",
"order":1,
"width":0,
"height":0,
"name":"Sequence No",
"label":"Sequence",
"format":"{{msg.payload}}",
"layout":"col-center",
"x":700,
"y":460,
"wires":[
]
},
{
"id":"a5356ffb.f0cab",
"type":"ttn uplink",
"z":"23a9162c.cb1e6a",
"name":"",
"app":"f8f8391b.7e8728",
"dev_id":"",
"field":"",
"x":240,
"y":120,
"wires":[
[
"9f4b8dd3.2f0d2"
]
]
},
{
"id":"813b9751.dba8a8",
"type":"debug",
"z":"23a9162c.cb1e6a",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"false",
"x":590,
"y":640,
"wires":[
]
}
]
},
{
"id":"5aff6f87.f125a",
"type":"ttn downlink",
"z":"23a9162c.cb1e6a",
"name":"",
"app":"f8f8391b.7e8728",
"dev_id":"",
"port":"",
"confirmed":false,
"schedule":"replace",
"x":590,
"y":560,
"wires":[
]
},
{
"id":"b1e45e31.61136",
"type":"debug",
"z":"23a9162c.cb1e6a",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload",
"targetType":"msg",
"x":590,
"y":100,
"wires":[
]
}
]