forked from cyberman54/ESP32-Paxcounter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Nodered-Timeserver.json
402 lines (402 loc) · 12 KB
/
Nodered-Timeserver.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
[
{
"id": "9b4f492d.fbfd18",
"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": 220,
"y": 520,
"wires": [
[
"53a85e2c.2728d"
]
]
},
{
"id": "9c105726.613a58",
"type": "mqtt in",
"z": "449c1517.e25f4c",
"name": "listen",
"topic": "+/devices/+/up",
"qos": "2",
"broker": "2a15ab6f.ab2244",
"x": 90,
"y": 127,
"wires": [
[
"113ef524.57edeb"
]
]
},
{
"id": "1c9a7438.6e38ec",
"type": "mqtt out",
"z": "449c1517.e25f4c",
"name": "send",
"topic": "",
"qos": "",
"retain": "",
"broker": "2a15ab6f.ab2244",
"x": 710,
"y": 520,
"wires": []
},
{
"id": "113ef524.57edeb",
"type": "json",
"z": "449c1517.e25f4c",
"name": "Convert",
"property": "payload",
"action": "",
"pretty": false,
"x": 240,
"y": 127,
"wires": [
[
"120561a.088359e"
]
]
},
{
"id": "120561a.088359e",
"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": 400,
"y": 127,
"wires": [
[
"d6f27e8e.93242"
]
]
},
{
"id": "90e76b02.6298f8",
"type": "json",
"z": "449c1517.e25f4c",
"name": "Convert",
"property": "payload",
"action": "",
"pretty": false,
"x": 560,
"y": 520,
"wires": [
[
"1c9a7438.6e38ec"
]
]
},
{
"id": "d6f27e8e.93242",
"type": "base64",
"z": "449c1517.e25f4c",
"name": "Decode",
"action": "",
"property": "payload.payload_raw",
"x": 560,
"y": 127,
"wires": [
[
"b8bd33fd.61caa",
"cc245719.3c4cd8"
]
]
},
{
"id": "53a85e2c.2728d",
"type": "base64",
"z": "449c1517.e25f4c",
"name": "Encode",
"action": "",
"property": "payload.payload_raw",
"x": 400,
"y": 520,
"wires": [
[
"90e76b02.6298f8"
]
]
},
{
"id": "15980d22.6f4663",
"type": "comment",
"z": "449c1517.e25f4c",
"name": "LoRaWAN Timeserver v1.21",
"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": 160,
"y": 47,
"wires": []
},
{
"id": "b8bd33fd.61caa",
"type": "function",
"z": "449c1517.e25f4c",
"name": "Timeserver Logic",
"func": "/* LoRaWAN Timeserver\n\nconstruct 7 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n1 sequence number (taken from node's time_sync_req)\n2 timezone in 15 minutes steps\n3..6 current second (from epoch time 1970)\n7 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\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 msg.port = 9; // Paxcounter TIMEPORT\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(7);\nnew DataView(buf).setUint8(0, seqNo);\n// Timezone (in 15min steps)\nvar timezone = 8; // CET = UTC+2h\nnew DataView(buf).setUint8(1, timezone);\nnew DataView(buf).setUint32(2, seconds);\nnew DataView(buf).setUint8(6, fractions);\n\nmsg.payload = new Buffer(new Uint8Array(buf));\nmsg.port = 9; // Paxcounter TIMEPORT\nvar euiMsg = { payload: eui };\nvar offsetMsg = { payload: offset };\n\nreturn [euiMsg, offsetMsg, deviceMsg, seqNoMsg, msg];",
"outputs": 5,
"noerr": 0,
"x": 330,
"y": 327,
"wires": [
[
"c9a83ac9.50fd18",
"6aeb3720.a89618",
"6ac55bbe.12ac54"
],
[
"de908e66.b6fd3"
],
[
"d5a35bab.44cb18"
],
[
"3a661f0a.c61b1"
],
[
"9b4f492d.fbfd18"
]
],
"outputLabels": [
"gw_eui",
"offset_ms",
"device",
"seq_no",
"time_sync_ans"
]
},
{
"id": "c9a83ac9.50fd18",
"type": "debug",
"z": "449c1517.e25f4c",
"name": "Timeserver Gw",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "payload",
"x": 680,
"y": 247,
"wires": [],
"icon": "node-red/bridge.png"
},
{
"id": "247204ab.a9f83c",
"type": "ui_text",
"z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817",
"order": 3,
"width": 0,
"height": 0,
"name": "Recent time",
"label": "Last answer at:",
"format": "{{msg.payload}}",
"layout": "col-center",
"x": 790,
"y": 307,
"wires": []
},
{
"id": "de908e66.b6fd3",
"type": "ui_gauge",
"z": "449c1517.e25f4c",
"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": 690,
"y": 387,
"wires": []
},
{
"id": "6aeb3720.a89618",
"type": "ui_text",
"z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817",
"order": 1,
"width": 0,
"height": 0,
"name": "Recent server",
"label": "Gateway",
"format": "{{msg.payload}}",
"layout": "col-center",
"x": 680,
"y": 347,
"wires": []
},
{
"id": "6ac55bbe.12ac54",
"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": 650,
"y": 307,
"wires": [
[
"247204ab.a9f83c"
]
]
},
{
"id": "d5a35bab.44cb18",
"type": "ui_text",
"z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817",
"order": 1,
"width": 0,
"height": 0,
"name": "Recent Device",
"label": "Device",
"format": "{{msg.payload}}",
"layout": "col-center",
"x": 680,
"y": 427,
"wires": []
},
{
"id": "3a661f0a.c61b1",
"type": "ui_text",
"z": "449c1517.e25f4c",
"group": "edb7cc8d.a3817",
"order": 1,
"width": 0,
"height": 0,
"name": "Sequence No",
"label": "Sequence",
"format": "{{msg.payload}}",
"layout": "col-center",
"x": 680,
"y": 467,
"wires": []
},
{
"id": "cc245719.3c4cd8",
"type": "debug",
"z": "449c1517.e25f4c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 860,
"y": 140,
"wires": []
},
{
"id": "2a15ab6f.ab2244",
"type": "mqtt-broker",
"z": "",
"name": "eu.thethings.network:1883",
"broker": "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
}
]