-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.picolabs.wovyn.emitter.krl
145 lines (121 loc) · 4.83 KB
/
io.picolabs.wovyn.emitter.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
ruleset io.picolabs.wovyn.emitter {
meta {
name "wovyn_emitter"
author "PJW"
description "Simulates a Wovyn temperature sensor"
version "1.2"
use module io.picolabs.wrangler alias wrangler
shares schedule, heartbeat_period, operating_state, my_rid
}
global {
schedule = function(){schedule:list()};
heartbeat_period = function(){ent:heartbeat_period};
operating_state = function(){ent:emitter_state};
default_heartbeat_period = 300; //seconds
my_rid = function(){meta:rid};
}
rule set_emitter_operation {
select when emitter new_state
if(event:attr("pause")) then noop();
fired {
ent:emitter_state := "paused";
} else {
ent:emitter_state := "running";
}
}
rule set_period {
select when emitter new_heartbeat_period
always {
ent:heartbeat_period := event:attr("heartbeat_period")
.klog("Heartbeat period: "); // in seconds
}
}
rule raise_emitter_event {
select when emitter new_sensor_reading
pre {
// Bounds should not be fixed, but are for now
period = ent:heartbeat_period.defaultsTo(20)
.klog("Heartbeat period: "); // in seconds
temperatureF = (random:integer(lower = 700, upper = 800)/10) // one decimal digit of precision
.klog("TemperatureF: ");
temperatureC = math:round((temperatureF - 32)/1.8,1);
healthPercent = random:integer(lower = 500, upper = 900)/10; // one decimal digit of precision
transducerGUID = ent:transducerGUID.defaultsTo(random:uuid());
emitterGUID = ent:emitterGUID.defaultsTo(random:uuid());
genericThing = {
"typeId": "2.1.2",
"typeName": "generic.simple.temperature",
"healthPercent": healthPercent,
"heartbeatSeconds": period,
"data": {
"temperature": [
{
"name": "enclosure temperature",
"transducerGUID": transducerGUID,
"units": "degrees",
"temperatureF": temperatureF,
"temperatureC": temperatureC
}
]
}
};
specificThing = {
"make": "Wovyn ESProto",
"model": "Temp2000",
"typeId": "1.1.2.2.2000",
"typeName": "enterprise.wovyn.esproto.temp.2000",
"thingGUID": emitterGUID+".1",
"firmwareVersion": "Wovyn-Temp2000-1.1-DEV",
"transducer": [
{
"name": "Maxim DS18B20 Digital Thermometer",
"transducerGUID": transducerGUID,
"transducerType": "Maxim Integrated.DS18B20",
"units": "degrees",
"temperatureC": temperatureC
}
],
"battery": {
"maximumVoltage": 3.6,
"minimumVoltage": 2.7,
"currentVoltage": 3.4
}
};
property = {
"name": "Wovyn_163A54",
"description": "Wovyn ESProto Temp2000",
"location": {
"description": "Timbuktu",
"imageURL": "http://www.wovyn.com/assets/img/wovyn-logo-small.png",
"latitude": "16.77078",
"longitude": "-3.00819"
}
};
}
if ( ent:emitter_state == "running" ) then noop();
fired {
ent:transducerGUID := transducerGUID if ent:transducerGUID.isnull();
ent:emitterGUID := emitterGUID if ent:emitterGUID.isnull();
raise wovyn event "heartbeat" attributes {
"emitterGUID": emitterGUID,
"genericThing": genericThing,
"specificThing": specificThing,
"property": property
}
}
}
rule inialize_ruleset {
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
pre {
period = ent:heartbeat_period
.defaultsTo(event:attr("heartbeat_period") || default_heartbeat_period)
.klog("Initilizing heartbeat period: "); // in seconds
}
if ( ent:heartbeat_period.isnull() && schedule:list().length() == 0) then send_directive("Initializing sensor pico");
fired {
ent:heartbeat_period := period if ent:heartbeat_period.isnull();
ent:emitter_state := "running"if ent:emitter_state.isnull();
schedule emitter event "new_sensor_reading" repeat << */#{period} * * * * * >> attributes { }
}
}
}