Skip to content

Commit 425978e

Browse files
Merge pull request #90 from Rightech/lorawan-ex
upd lorawan example
2 parents dee76f1 + c578845 commit 425978e

File tree

3 files changed

+146
-140
lines changed

3 files changed

+146
-140
lines changed

docs_examples/6 - LoRaWAN/Modem model.json

Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": 1,
33
"origin": "dev.rightech.io",
4-
"time": "2021-12-23T14:04:32.309Z",
4+
"time": "2022-02-18T13:33:15.474Z",
55
"user": "613645025dab3700105ae872",
66
"group": "613645025dab3700105ae871",
77
"store": "models",
@@ -196,129 +196,123 @@
196196
]
197197
},
198198
{
199-
"id": "temperature_value",
200-
"name": "Temperature",
199+
"id": "type",
200+
"name": "Packet type",
201201
"active": true,
202202
"type": "argument",
203203
"dataType": "number",
204204
"factor": 1,
205-
"linear": true,
206-
"unit": "temperature-celsius"
205+
"linear": true
207206
},
208207
{
209-
"id": "humidity_value",
210-
"name": "Humidity",
208+
"id": "bat",
209+
"name": "Battery charge",
211210
"active": true,
212211
"type": "argument",
213212
"dataType": "number",
214213
"factor": 1,
215214
"linear": true,
215+
"_parentId": "root",
216+
"children": [
217+
{
218+
"id": "bat.levels.0",
219+
"name": "-Infinity",
220+
"active": true,
221+
"type": "level",
222+
"index": 0,
223+
"value": "-Infinity",
224+
"color": "#f16059"
225+
},
226+
{
227+
"id": "bat.levels.1",
228+
"name": "Min",
229+
"active": true,
230+
"type": "level",
231+
"index": 1,
232+
"value": 30,
233+
"color": "#f16059"
234+
},
235+
{
236+
"id": "bat.levels.2",
237+
"name": "Mid",
238+
"active": true,
239+
"type": "level",
240+
"index": 5,
241+
"value": 50,
242+
"color": "#ffae62"
243+
},
244+
{
245+
"id": "bat.levels.3",
246+
"name": "Max",
247+
"active": true,
248+
"type": "level",
249+
"index": 9,
250+
"value": 80,
251+
"color": "#a2ce4b"
252+
}
253+
],
254+
"display": {
255+
"svg": "<svg viewBox=\"0 0 16 16\"><path d=\"M 1.585938 4 C 0.71875 4 0 4.71875 0 5.585938 L 0 10.410156 C 0 11.28125 0.71875 12 1.585938 12 L 12.410156 12 C 13.28125 12 14 11.28125 14 10.410156 L 14 10 L 15 10 C 15.546875 10 16 9.546875 16 9 L 16 7 C 16 6.453125 15.546875 6 15 6 L 14 6 L 14 5.585938 C 14 4.71875 13.28125 4 12.410156 4 Z M 1.585938 5 L 12.410156 5 C 12.742188 5 13 5.257813 13 5.585938 L 13 10.410156 C 13 10.742188 12.742188 11 12.410156 11 L 1.585938 11 C 1.257813 11 1 10.742188 1 10.410156 L 1 5.585938 C 1 5.257813 1.257813 5 1.585938 5 Z M 2 6 L 2 10 L 9 10 L 9 6 Z M 14 7 L 15 7 L 15 9 L 14 9 Z\" /></svg>"
256+
},
216257
"unit": "percent"
217258
},
218259
{
219-
"id": "light_value",
220-
"name": "Light",
260+
"id": "cfg",
261+
"name": "Configuration",
262+
"active": true,
263+
"type": "argument",
264+
"dataType": "object",
265+
"factor": 1,
266+
"linear": true,
267+
"unit": null
268+
},
269+
{
270+
"id": "timestamp",
271+
"name": "Time",
221272
"active": true,
222273
"type": "argument",
223274
"dataType": "number",
224-
"display": {
225-
"svg": null
226-
},
227275
"factor": 1,
228276
"linear": true,
229-
"unit": "light-lux"
277+
"unit": "duration-millisecond",
278+
"_parentId": "root",
279+
"children": []
230280
},
231281
{
232-
"id": "switch_detected",
233-
"name": "Reed switch",
282+
"id": "temp",
283+
"name": "Temperature",
234284
"active": true,
235285
"type": "argument",
236-
"dataType": "boolean",
286+
"dataType": "number",
237287
"factor": 1,
238288
"linear": true,
239-
"unit": null
289+
"unit": "temperature-celsius",
290+
"_parentId": "root",
291+
"children": []
240292
},
241293
{
242-
"id": "leak_detected",
243-
"name": "Leakage",
294+
"id": "inputs",
295+
"name": "Inputs",
244296
"active": true,
245297
"type": "argument",
246-
"dataType": "boolean",
298+
"dataType": "number[]",
247299
"factor": 1,
248300
"linear": true,
249-
"unit": null
301+
"unit": null,
302+
"_parentId": "root",
303+
"children": []
250304
},
251305
{
252-
"id": "accelerometer",
253-
"name": "Accelerometer",
306+
"id": "evin",
307+
"name": "Alarm input",
254308
"active": true,
255-
"type": "subsystem",
256-
"children": [
257-
{
258-
"id": "hit_detected",
259-
"name": "Hit",
260-
"active": true,
261-
"type": "argument",
262-
"dataType": "boolean",
263-
"factor": 1,
264-
"linear": true,
265-
"unit": null
266-
},
267-
{
268-
"id": "axis_1",
269-
"name": "Tilt angle. Axis 1",
270-
"active": true,
271-
"type": "argument",
272-
"dataType": "number",
273-
"factor": 1,
274-
"linear": true,
275-
"unit": "angle-degree"
276-
},
277-
{
278-
"id": "axis_2",
279-
"name": "Tilt angle. Axis 2",
280-
"active": true,
281-
"type": "argument",
282-
"dataType": "number",
283-
"factor": 1,
284-
"linear": true,
285-
"unit": "angle-degree",
286-
"children": []
287-
},
288-
{
289-
"id": "axis_3",
290-
"name": "Tilt angle. Axis 3",
291-
"active": true,
292-
"type": "argument",
293-
"dataType": "number",
294-
"factor": 1,
295-
"linear": true,
296-
"unit": "angle-degree",
297-
"children": []
298-
},
299-
{
300-
"id": "current_acceleration",
301-
"name": "Current acceleration",
302-
"active": true,
303-
"type": "argument",
304-
"dataType": "number",
305-
"factor": 1,
306-
"linear": true,
307-
"unit": "acceleration-meter-per-second-squared",
308-
"children": []
309-
},
310-
{
311-
"id": "max_acceleration",
312-
"name": "Max acceleration",
313-
"active": true,
314-
"type": "argument",
315-
"dataType": "number",
316-
"factor": 1,
317-
"linear": true,
318-
"unit": "acceleration-meter-per-second-squared",
319-
"children": []
320-
}
321-
]
309+
"type": "argument",
310+
"dataType": "number",
311+
"factor": 1,
312+
"linear": true,
313+
"unit": null,
314+
"_parentId": "root",
315+
"children": []
322316
}
323317
],
324318
"_isRoot": true,

docs_examples/6 - LoRaWAN/Modem.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": 1,
33
"origin": "dev.rightech.io",
4-
"time": "2021-12-23T13:56:36.134Z",
4+
"time": "2022-02-18T13:38:25.030Z",
55
"user": "613645025dab3700105ae872",
66
"group": "613645025dab3700105ae871",
77
"store": "objects",

docs_examples/6 - LoRaWAN/Parse payload.js

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,92 @@
22
//@handler : Parse payload
33
//@author : Kristina Goldinova
44

5+
const EVENT_PACKET = 1;
6+
const EVENT_ALARM = 2;
7+
58
/**
6-
* @param {string} payload base64 sensors data
9+
* Check if n-th bit set for number
10+
* @param value {number} number value
11+
* @param bit {number} bit position
712
*/
13+
function bit(value, bit) {
14+
return (value & (1 << bit)) !== 0;
15+
}
816

9-
// Types codes
10-
var temp_and_humid_sensor = 0x01;
11-
var light_sensor = 0x02;
12-
var accelerometer = 0x03;
13-
var switch_sensor = 0x04;
14-
var leakage_sensor = 0x05;
15-
17+
/**
18+
* Parse Vega SI-11 Payload
19+
* https://en.iotvega.com/product/si11
20+
*
21+
* ```
22+
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8-11 | 12-15 | 16-19 | 20-23
23+
* 1 - EVENT_PACKET | type | bat | cfg | timestamp | t | in[0] | in[1] | in[2] | in[3]
24+
* 2 - EVENT_ALARM | type | bat | cfg | i | timestamp | in[0] | in[1] | in[2] | in[3]
25+
*
26+
* ```
27+
* @param payload {string} base64 encoded payload
28+
*/
1629
function process(payload) {
17-
const buf = ric.base64.decode(payload); // decode
30+
const data = ric.base64.decode(payload);
1831
let offset = 0;
1932

20-
let sensor_type = buf.getUint8(offset++);
21-
switch (sensor_type) {
22-
case temp_and_humid_sensor:
23-
let temperature = buf.getUint16(offset, true) * 0.01; offset += 2;
24-
let humidity = buf.getUint16(offset, true) * 0.01;
25-
return { temperature, humidity };
26-
27-
case light_sensor:
28-
let light = buf.getUint16(offset, true) * 0.01;
29-
return { light };
33+
/* - option 1 - parse binary payload from raw byte array */
34+
const bytes = new Uint8Array(data.buffer);
3035

31-
case accelerometer:
32-
let hit_detected = buf.getUint16(offset, true) == 1; offset += 2;
33-
let axis1 = buf.getUint16(offset, true); offset += 2;
34-
let axis2 = buf.getUint16(offset, true); offset += 2;
35-
let axis3 = buf.getUint16(offset, true); offset += 2;
36-
let current_accel = buf.getUint16(offset, true); offset += 2;
37-
let max_accel = buf.getUint16(offset, true);
38-
return { hit_detected, axis1, axis2, axis3, current_accel, max_accel };
36+
const evtype = bytes[offset++];
37+
const battery = bytes[offset++];
38+
const configByte = bytes[offset++];
39+
const config = {
40+
activation: bit(configByte, 0) ? "abp": "otaa",
41+
inputs: [
42+
+bit(configByte, 4),
43+
+bit(configByte, 5),
44+
+bit(configByte, 6),
45+
+bit(configByte, 7)
46+
],
47+
};
48+
const evin = evtype === EVENT_ALARM
49+
? bytes[offset++]
50+
: undefined;
3951

40-
case switch_sensor:
41-
let switch_detected = buf.getUint8(offset) == 1;
42-
return { switch_detected };
52+
/* - option 2 - use JS `DataView` interface (like Node's `Buffer`) */
53+
const evtime = data.getUint32(offset, true) * 1000;
54+
offset += 4;
4355

44-
case leakage_sensor:
45-
let leak_detected = buf.getUint8(offset) == 1;
46-
return { leak_detected };
56+
const temperature = evtype === EVENT_PACKET
57+
? data.getInt8(offset++)
58+
: undefined;
4759

48-
default:
49-
break;
60+
const inputs = [];
61+
for (let i = 0; i < 4; i++) {
62+
inputs[i] = data.getUint32(offset, true);
63+
offset += 4;
5064
}
65+
66+
return { evtype, battery, config, evin, evtime, temperature, inputs };
5167
}
5268

5369
/* ↑ here ends original handler code */
5470
/* ↓ here goes generated debug code */
5571

56-
/* 01. define test values for model "Modem model" */
72+
/* 01. define test values */
5773
const config = {};
5874
const packet = {
5975
"payload": "string"
6076
};
6177

6278
/* 02. run handler code */
6379
const result = process(
64-
packet["payload"]
80+
packet["payload"]
6581
);
6682

67-
/* 03. log handler results converted back to "Modem model" */
83+
/* 03. log handler results */
6884
console.log({
69-
"temperature_value": result["temperature"],
70-
"humidity_value": result["humidity"],
71-
"light_value": result["light"],
72-
"hit_detected": result["hit_detected"],
73-
"axis_1": result["axis1"],
74-
"axis_2": result["axis2"],
75-
"axis_3": result["axis3"],
76-
"current_acceleration": result["current_accel"],
77-
"max_acceleration": result["max_accel"],
78-
"switch_detected": result["switch_detected"],
79-
"leak_detected": result["leak_detected"]
85+
"evtype": result["evtype"],
86+
"battery": result["battery"],
87+
"config": result["config"],
88+
"evin": result["evin"],
89+
"evtime": result["evtime"],
90+
"temperature": result["temperature"],
91+
"inputs": result["inputs"]
8092
});
8193

0 commit comments

Comments
 (0)