Created
March 19, 2021 03:44
-
-
Save dannyrandall/706e4271ddaefee90cb8e3ca0e0da7cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruleset wovyn_base { | |
meta { | |
name "Wovyn Base" | |
author "Daniel Randall" | |
use module io.picolabs.subscription alias subscription | |
use module sensor_profile alias profile | |
shares sensor_managers | |
} | |
global { | |
sensor_managers = function() { | |
subscription:established("Tx_role", "sensor_manager").map(function(v) { | |
v.get(["Tx"]) | |
}) | |
} | |
temperature = function() { | |
ent:temperature.defaultsTo(0) | |
} | |
getTxFromRx = function(rx) { | |
subscription:established("Tx_role", "sensor_manager").filter(function(v) { | |
v.get(["Rx"]) == rx | |
}).defaultsTo([{}]).head().get(["Tx"]) | |
} | |
} | |
rule process_heartbeat { | |
select when wovyn heartbeat where event:attrs{"genericThing"} | |
pre { | |
temperature = event:attrs{"genericThing"}{["data", "temperature", 0, "temperatureF"]} | |
} | |
send_directive("temperatureF", temperature) | |
fired { | |
ent:temperature := temperature | |
raise wovyn event "new_temperature_reading" attributes { | |
"timestamp": time:now(), | |
"temperature": temperature | |
} | |
} | |
} | |
rule find_high_temps { | |
select when wovyn new_temperature_reading where event:attrs{"temperature"} > profile:temperature_threshold() | |
fired { | |
raise wovyn event "threshold_violation" attributes event:attrs | |
} | |
} | |
rule threshold_notification { | |
select when wovyn threshold_violation | |
foreach sensor_managers() setting (eci) | |
event:send({ | |
"eci": eci, | |
"domain": "sensor_manager", | |
"type": "sub_sensor_threshold_violation", | |
"attrs": event:attrs | |
}) | |
} | |
rule accept_sensor_manager_sub { | |
select when wrangler inbound_pending_subscription_added where event:attrs{"Rx_role"} == "sensor" | |
fired { | |
raise wrangler event "pending_subscription_approval" attributes event:attrs | |
} | |
} | |
rule report_temperature { | |
select when wovyn report_temperature | |
pre { | |
sensorID = event:attrs{"sensorID"} | |
eci = getTxFromRx(sensorID) | |
reportID = event:attrs{"reportID"} | |
} | |
event:send({ | |
"eci": eci, | |
"domain": "sensor_manager", | |
"type": "temperature_report", | |
"attrs": { | |
"sensorID": sensorID, | |
"reportID": reportID, | |
"temperature": temperature() | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment