-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.picolabs.wl03a_lb.router.krl
149 lines (121 loc) · 4.2 KB
/
io.picolabs.wl03a_lb.router.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
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
ruleset io.picolabs.wl03a_lb.router {
meta {
name "WL03A-LB_device"
description <<
Received and decodes heartbeat information from a Dragino WL03A-LB Leak Detector
>>
author "PJW"
use module io.picolabs.wrangler alias wrangler
use module io.picolabs.dragino alias dragino
share lastHeartbeat
}
global {
channels = [
{"tags": ["wl03a_lb", "sensor"],
"eventPolicy": {
"allow": [ { "domain": "wl03a_lb", "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 wl03a_lb 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 wl03a_lb heartbeat
pre {
// Payload array for WL03A-LB
// Device Status (7 bytes)
// Array index 0 1 2 3 4
// Size(bytes) 1 2 1 1 2
// Value Mode Firmware Freq Sub-band Battery
//
// Leak Status (11 bytes)
// Array index 0 1 2 3
// Size(bytes) 1 3 3 4
// Value Status Total Leak Last Leak Unix
// Alarm Evemts Duration Timestamp
//
// Manual: http://wiki.dragino.com/xwiki/bin/view/Main/User%20Manual%20for%20LoRaWAN%20End%20Nodes/WL03A-LB_LoRaWAN_None-Position_Rope_Type_Water_Leak_Controller_User_Manual/
//
payload = event:attrs{["payload"]};
payload_size = event:attrs{["payload_size"]} //.klog("Payload size: ");
payload_array = (payload_size == 7) => dragino:get_payload("wl03a_lb_status", payload).klog("Payload status: ")
| (payload_size == 11) => dragino:get_payload("wl03a_lb_data", payload).klog("Payload data: ")
| []
}
always {
raise sensor event "new_status" attributes
{"status": payload_array,
"reported_at": event:attr("reported_at")} if payload_size == 7
raise sensor event "new_data" attributes
{"data": payload_array,
"reported_at": event:attr("reported_at")} if payload_size == 11
}
}
rule process_leak_data {
select when sensor new_data
pre {
data_array = event:attrs{["data"]} //.klog("data array")
status = data_array[0].as("Hex")
sensor_data = { "leak": status.band("1"),
"alarm": status.band("2"),
"tdc": status.band("4"),
"leak_events": data_array[1],
"leak_duration": data_array[2] // sec
}.klog("Sensor data: ");
readings = {"readings": sensor_data,
"timestamp": event:attr("reported_at")
}
}
always {
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
}
}