-
Notifications
You must be signed in to change notification settings - Fork 0
/
esproto_device.krl
141 lines (117 loc) · 4.17 KB
/
esproto_device.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
ruleset esproto_device {
meta {
name "esproto_device"
author "PJW"
description "General rules for ESProto system devices"
use module b507199x5 alias wrangler
logging on
sharing on
provides thresholds
}
global {
// public
thresholds = function(threshold_type) {
threshold_type.isnull() => ent:thresholds
| ent:thresholds{threshold_type}
}
//private
event_map = {
"new_temperature_reading" : "temperature",
"new_humidity_reading" : "humidity",
"new_pressure_reading" : "pressure"
};
reading_map = {
"temperature": "temperatureF",
"humidity": "humidity",
"pressure": "pressure"
};
collectionSubscriptions = function () {
raw_subs = wrangler:subscriptions().pick("$..subscribed[0]", true).klog(">>> All Channels >>> ");
subs = raw_subs[0].filter(function(k,v){v{"name_space"} eq "esproto-meta" && v{"relationship"} eq "Device"});
subs.klog("Subscriptions: ").defaultsTo({})
};
}
// rule to save thresholds
rule save_threshold {
select when esproto new_threshold
pre {
threshold_type = event:attr("threshold_type");
threshold_value = {"limits": {"upper": event:attr("upper_limit"),
"lower": event:attr("lower_limit")
}};
}
if(not threshold_type.isnull()) then noop();
fired {
log "Setting threshold value for #{threshold_type}";
set ent:thresholds{threshold_type} threshold_value;
}
}
rule check_threshold {
select when esproto new_temperature_reading
or esproto new_humidity_reading
or esproto new_pressure_reading
foreach event:attr("readings") setting (reading)
pre {
event_type = event:type().klog("Event type: ");
// thresholds
threshold_type = event_map{event_type};
threshold_map = thresholds(threshold_type).klog("Thresholds: ");
lower_threshold = threshold_map{["limits","lower"]}.klog("Lower threshold: ");
upper_threshold = threshold_map{["limits","upper"]};
// sensor readings
data = reading.klog("Reading from #{threshold_type}: ");
reading_value = data{reading_map{threshold_type}}.klog("Reading value for #{threshold_type}: ");
sensor_name = data{"name"}.klog("Name of sensor: ");
// decide
under = reading_value < lower_threshold;
over = upper_threshold < reading_value;
msg = under => "#{threshold_type} is under threshold of #{lower_threshold}"
| over => "#{threshold_type} is over threshold of #{upper_threshold}"
| "";
}
if( under || over ) then noop();
fired {
raise esproto event "threshold_violation" attributes
{"reading": reading.encode(),
"threshold": under => lower_threshold | upper_threshold,
"message": "threshold violation: #{msg} for #{sensor_name}"
}
}
}
// route events to all collections I'm a member of
// change eventex to expand routed events.
rule route_to_collections {
select when esproto threshold_violation
or esproto battery_level_low
foreach collectionSubscriptions() setting (sub_name, sub_value)
pre {
eci = sub_value{"event_eci"};
}
event:send({"cid": eci}, "esproto", event:type())
with attrs = event:attrs();
}
rule auto_approve_pending_subscriptions {
select when wrangler inbound_pending_subscription_added
//name_space re/esproto-meta/gi
pre{
attributes = event:attrs().klog("subcription attributes :");
subscriptions = wrangler:subscriptions()
.pick("$.subscriptions")
.klog(">>> current subscriptions >>>>")
;
declared_relationship = "device_collection";
relationship = event:attr("relationship").klog(">>> subscription relationship >>>>");
}
if ( not relationship like declared_relationship
|| subscriptions.length() == 0
) then // only auto approve the first subscription request
{
noop();
}
fired {
log ">>> auto approving subscription: #{relationship}";
raise wrangler event 'pending_subscription_approval'
attributes attributes;
}
}
}