Merge pull request #320 from cyberman54/development

small fixes
This commit is contained in:
Verkehrsrot 2019-03-18 00:51:19 +01:00 committed by GitHub
commit 38b4225a8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 56 deletions

View File

@ -1,30 +1,4 @@
[ [
{
"id": "46ce842a.614d5c",
"type": "ui_gauge",
"z": "449c1517.e25f4c",
"name": "Timeserver offset",
"group": "edb7cc8d.a3817",
"order": 1,
"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": 690,
"y": 360,
"wires": []
},
{ {
"id": "49e3c067.e782e", "id": "49e3c067.e782e",
"type": "change", "type": "change",
@ -75,7 +49,7 @@
"to": "", "to": "",
"reg": false, "reg": false,
"x": 240, "x": 240,
"y": 420, "y": 464,
"wires": [ "wires": [
[ [
"84f1cda2.069e7" "84f1cda2.069e7"
@ -108,7 +82,7 @@
"retain": "", "retain": "",
"broker": "2a15ab6f.ab2244", "broker": "2a15ab6f.ab2244",
"x": 730, "x": 730,
"y": 420, "y": 464,
"wires": [] "wires": []
}, },
{ {
@ -161,7 +135,7 @@
"action": "", "action": "",
"pretty": false, "pretty": false,
"x": 580, "x": 580,
"y": 420, "y": 464,
"wires": [ "wires": [
[ [
"72d5e7ee.d1eba8" "72d5e7ee.d1eba8"
@ -191,7 +165,7 @@
"action": "", "action": "",
"property": "payload.payload_raw", "property": "payload.payload_raw",
"x": 420, "x": 420,
"y": 420, "y": 464,
"wires": [ "wires": [
[ [
"dac8aafa.389298" "dac8aafa.389298"
@ -238,27 +212,28 @@
"type": "function", "type": "function",
"z": "449c1517.e25f4c", "z": "449c1517.e25f4c",
"name": "Generate Time Answer", "name": "Generate Time Answer",
"func": "/* LoRaWAN Timeserver\n\nconstruct 6 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n0 sequence number (taken from node's time_sync_req)\n1..4 current second (from epoch time 1970)\n5 1/250ths fractions of current second\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 gateways = msg.payload.metadata.gateways;\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) return null;\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;\nvar seqno = msg.payload.payload_raw[0];\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));\nvar infoMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [infoMsg, msg, offsetMsg];", "func": "/* LoRaWAN Timeserver\n\nconstruct 6 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n0 sequence number (taken from node's time_sync_req)\n1..4 current second (from epoch time 1970)\n5 1/250ths fractions of current second\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 gateways = msg.payload.metadata.gateways;\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) return null;\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;\nvar seqno = msg.payload.payload_raw[0];\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));\nvar infoMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [infoMsg, offsetMsg, msg];",
"outputs": 3, "outputs": 3,
"noerr": 0, "noerr": 0,
"x": 420, "x": 360,
"y": 300, "y": 320,
"wires": [ "wires": [
[ [
"37722d4b.08e3c2", "37722d4b.08e3c2",
"8712a5ac.ed18e8" "a8a04c7a.c5fbd",
], "a15454a9.fa0948"
[
"49e3c067.e782e"
], ],
[ [
"46ce842a.614d5c" "46ce842a.614d5c"
],
[
"49e3c067.e782e"
] ]
], ],
"outputLabels": [ "outputLabels": [
"gw_eui", "gw_eui",
"time_sync_ans", "offset_ms",
"offset_ms" "time_sync_ans"
] ]
}, },
{ {
@ -272,7 +247,7 @@
"tostatus": true, "tostatus": true,
"complete": "payload", "complete": "payload",
"x": 700, "x": 700,
"y": 260, "y": 280,
"wires": [], "wires": [],
"icon": "node-red/bridge.png" "icon": "node-red/bridge.png"
}, },
@ -281,27 +256,74 @@
"type": "ui_text", "type": "ui_text",
"z": "449c1517.e25f4c", "z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817", "group": "edb7cc8d.a3817",
"order": 0, "order": 3,
"width": 0, "width": 0,
"height": 0, "height": 0,
"name": "Timeserver", "name": "Recent time",
"label": "Recent timeserver was:", "label": "Last answer at:",
"format": "{{msg.payload}}", "format": "{{msg.payload}}",
"layout": "col-center", "layout": "col-center",
"x": 710, "x": 810,
"y": 320, "y": 340,
"wires": [] "wires": []
}, },
{ {
"id": "edb7cc8d.a3817", "id": "46ce842a.614d5c",
"type": "ui_group", "type": "ui_gauge",
"z": "", "z": "449c1517.e25f4c",
"name": "Timeserver", "name": "Timeserver offset",
"tab": "d525a5d.0832858", "group": "edb7cc8d.a3817",
"order": 4, "order": 2,
"disp": true, "width": 0,
"width": "6", "height": 0,
"collapse": false "gtype": "gage",
"title": "Offset gateway to server",
"label": "milliseconds",
"format": "{{value}}",
"min": 0,
"max": "2000",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"x": 710,
"y": 420,
"wires": []
},
{
"id": "a8a04c7a.c5fbd",
"type": "ui_text",
"z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817",
"order": 1,
"width": 0,
"height": 0,
"name": "Recent server",
"label": "",
"format": "{{msg.payload}}",
"layout": "col-center",
"x": 700,
"y": 380,
"wires": []
},
{
"id": "a15454a9.fa0948",
"type": "function",
"z": "449c1517.e25f4c",
"name": "Time",
"func": "msg.payload = new Date().toLocaleString('en-GB', {timeZone: 'Europe/Berlin'});\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 670,
"y": 340,
"wires": [
[
"8712a5ac.ed18e8"
]
]
}, },
{ {
"id": "2a15ab6f.ab2244", "id": "2a15ab6f.ab2244",
@ -326,6 +348,17 @@
"willQos": "0", "willQos": "0",
"willPayload": "" "willPayload": ""
}, },
{
"id": "edb7cc8d.a3817",
"type": "ui_group",
"z": "",
"name": "Timeserver",
"tab": "d525a5d.0832858",
"order": 4,
"disp": true,
"width": "6",
"collapse": false
},
{ {
"id": "d525a5d.0832858", "id": "d525a5d.0832858",
"type": "ui_tab", "type": "ui_tab",

View File

@ -18,9 +18,9 @@
#define HAS_LED LED_BUILTIN // white LED on board #define HAS_LED LED_BUILTIN // white LED on board
#define HAS_BUTTON KEY_BUILTIN // button "PROG" on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board
//#define HAS_BATTERY_PROBE ADC2_GPIO13_CHANNEL // battery probe GPIO pin #define HAS_BATTERY_PROBE ADC2_GPIO13_CHANNEL // battery probe GPIO pin
#define BATT_FACTOR 4 // voltage divider 220k/100k on board #define BATT_FACTOR 4 // voltage divider 220k/100k on board
#define HAS_LOWPOWER_SWITCH GPIO_NUM_21 // switches battery power //#define HAS_LOWPOWER_SWITCH GPIO_NUM_21 // switches battery power
// Pins for I2C interface of OLED Display // Pins for I2C interface of OLED Display
#define MY_OLED_SDA (4) #define MY_OLED_SDA (4)