-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.picolabs.ldds20.krl
120 lines (96 loc) · 3.14 KB
/
io.picolabs.ldds20.krl
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
ruleset io.picolabs.ldds20.router {
meta {
name "LDDS29_device"
description <<
Received and decodes heartbeat information from a Dragino LDDS20 liquid level sensor
>>
author "PJW"
use module io.picolabs.wrangler alias wrangler
use module io.picolabs.dragino alias dragino
share lastHeartbeat, lastLiquidLevel
}
global {
channels = [
{"tags": ["ldds20", "sensor"],
"eventPolicy": {
"allow": [ { "domain": "ldds20", "name": "*" }, ],
"deny": []
},
"queryPolicy": {
"allow": [ { "rid": "*", "name": "*" } ],
"deny": []
}
},
{"tags": ["sensor"],
"eventPolicy": {
"allow": [ { "domain": "sensor", "name": "*" }, ],
"deny": []
},
"queryPolicy": {
"allow": [ { "rid": "*", "name": "*" } ],
"deny": []
}
}];
// API functions
lastHeartbeat = function() {
ent:lastHeartbeat.klog("Return value ")
}
lastLiquidLevel = function() {
ent:lastLiquidLevel
}
}
// mostly for debugging; see all data from last heartbeat
rule receive_heartbeat {
select when ldds20 heartbeat
pre {
heartbeat = event:attrs
}
always {
ent:lastHeartbeat := heartbeat;
}
}
// 10g propane tank full (and warm): 557mm
// after cooking steak (20 min): 537mm
rule process_heartbeat {
select when ldds20 heartbeat
pre {
// Payload array for LDDS20
// Array index 0 1 2 3 4
// Size(bytes) 2 2 1 2 1
// Value BAT Distance Int Temp Sensor Flag
//
payload_array = dragino:get_payload("ldds20", event:attrs{["payload"]}).klog("Payload data: ")
battery_status = dragino:get_battery_status("ldds20", payload_array)
battery_voltage = dragino:get_battery_value("ldds20", payload_array)
sensor_data = {"liquid_level": payload_array[1], // in mm
"battery_status": battery_status,
"battery_voltage": battery_voltage
};
readings = {"readings": sensor_data,
"sensor_type": "dragino_ldds20",
"sensor_id": event:attrs{["uuid"]},
"timestamp": event:attrs{["reported_at"]}
}
}
always {
ent:lastLiquidLevel := sensor_data{"liquid_level"}
raise sensor event "new_readings" attributes readings;
}
}
// initialize this pico
rule create_channels {
select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
foreach channels setting(channel)
pre {
existing_channels = wrangler:channels(channel{"tags"}.join(","));
}
if existing_channels.length() == 0 then
wrangler:createChannel(channel{"tags"},
channel{"eventPolicy"},
channel{"queryPolicy"}) setting(new_channel)
}
rule inialize_ruleset {
select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
noop() // nothing to do right now
}
}