-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
demo.rules
164 lines (146 loc) · 4.3 KB
/
demo.rules
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Random
var Timer timer = null
val resList = newArrayList("640/480", "320/240", "480/360")
val urlList = newArrayList("https://www.fillmurray.com", "https://www.fillmurray.com/g",
"https://www.placecage.com", "https://www.placecage.com/c", "https://www.placecage.com/g")
val Random random = new Random()
/**
* This is a demo rule which simulates a real dimmer by reacting to increase/decrease commands
* and posting an updated state on the bus
*/
rule "Dimmed Light"
when
Item DimmedLight received command
then
if ((receivedCommand == INCREASE) || (receivedCommand == DECREASE)) {
var Number percent = 0
if (DimmedLight.state instanceof DecimalType) percent = DimmedLight.state as DecimalType
if (receivedCommand == INCREASE) percent = percent + 5
if (receivedCommand == DECREASE) percent = percent - 5
if (percent < 0) percent = 0
if (percent > 100) percent = 100
postUpdate(DimmedLight, percent);
}
end
rule "Timer Demo"
when
Item Light_GF_Corridor_Ceiling received command
then
if (receivedCommand == ON) {
if (timer === null) {
// first ON command, so create a timer to turn the light off again
timer = createTimer(now.plusSeconds(10)) [|
sendCommand(Light_GF_Corridor_Ceiling, OFF)
]
} else {
// subsequent ON command, so reschedule the existing timer
timer.reschedule(now.plusSeconds(10))
}
} else if (receivedCommand == OFF) {
// remove any previously scheduled timer
if (timer !== null) {
timer.cancel
timer = null
}
}
end
/**
* The following rules help initializing the demo items with some helpful states.
*/
rule "Initialize light states"
when
System started
then
Lights?.members.forEach(light|
postUpdate(light, if (Math::random > 0.7) ON else OFF)
)
end
rule "Initialize heating states"
when
System started
then
Heating?.members.forEach(heating|
postUpdate(heating, if (Math::random > 0.8) ON else OFF)
)
postUpdate(Temperature_Setpoint, 22)
end
rule "Initialize contact states"
when
System started
then
Windows?.members.forEach(window|
postUpdate(window, if (Math::random > 0.5) OPEN else CLOSED)
)
end
rule "Initialize Location"
when
System started
then
DemoLocation.postUpdate(new PointType("52.5200066,13.4049540"))
end
rule "Set random room temperatures"
when
System started or
Time cron "0 0/5 * * * ?"
then
Temperature?.members.forEach(temperature|
postUpdate(temperature, 20.0 + (25.0 - (Math::random * 50.0).intValue) / 10.0)
)
end
rule "Set daily max and min temperature"
when
Item Weather_Temperature changed or
Time cron "0 0 0 * * ?" or
System started
then
val max = Weather_Temperature.maximumSince(now.withTimeAtStartOfDay)
val min = Weather_Temperature.minimumSince(now.withTimeAtStartOfDay)
if (max !== null && min !== null) {
postUpdate(Weather_Temp_Max, max.state)
postUpdate(Weather_Temp_Min, min.state)
}
end
// Creates an item that stores the last update time of this item
rule "Records last weather update time"
when
Item Weather_Temperature received update
then
postUpdate(Weather_LastUpdate, new DateTimeType())
end
rule "Set random wifi variations"
when
System started or
Time cron "/20 * * * * ?"
then
postUpdate(Wifi_Level, (Math::random * 4.0).intValue)
end
rule "Set random image URLs"
when
Time cron "/10 * * * * ?"
then
val url = urlList.get(random.nextInt(urlList.length))
val res = resList.get(random.nextInt(resList.length))
postUpdate(ImageURL, url + "/" + res)
end
rule "Volume"
when
Item Volume received command
then
if (receivedCommand instanceof PercentType) {
setMasterVolume(receivedCommand)
} else {
if (receivedCommand == INCREASE) increaseMasterVolume(20)
if (receivedCommand == DECREASE) decreaseMasterVolume(20)
}
end
rule "Select Radio Station"
when
Item Radio_Station received command
then
switch(receivedCommand) {
case 0 : playStream(null)
case 1 : playStream("http://metafiles.gl-systemhaus.de/hr/hr3_2.m3u")
case 2 : playStream("http://mp3-live.swr3.de/swr3_m.m3u")
}
end
// vim: syntax=Xtend