-
Notifications
You must be signed in to change notification settings - Fork 2
/
SleepyTimeMrB.txt.groovy
87 lines (78 loc) · 2.06 KB
/
SleepyTimeMrB.txt.groovy
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
/**
* Sleepy Time Mr. B
*
* Author: oneaccttorulethehouse@gmail.com
* Date: 2014-02-10
*
* Flash the lights when its time to stop playing games and go to bed.
*
*/
preferences {
section("When the clock strikes"){
input "time1", "time", title: "When?", required: true
}
section("Then flash..."){
input "switches", "capability.switch", title: "These lights", multiple: true
input "numFlashes", "number", title: "This number of times (default 3)", required: false
}
section("Time settings in milliseconds (optional)..."){
input "onFor", "number", title: "On for (default 1000)", required: false
input "offFor", "number", title: "Off for (default 1000)", required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
schedule(time1, "scheduleCheck")
}
def updated() {
log.debug "Updated with settings: ${settings}"
unschedule()
schedule(time1, "scheduleCheck")
}
def scheduleCheck()
{
log.trace "scheduledCheck"
flashLights()
}
private flashLights() {
def doFlash = true
def onFor = onFor ?: 1000
def offFor = offFor ?: 1000
def numFlashes = numFlashes ?: 3
log.debug "LAST ACTIVATED IS: ${state.lastActivated}"
if (state.lastActivated) {
def elapsed = now() - state.lastActivated
def sequenceTime = (numFlashes + 1) * (onFor + offFor)
doFlash = elapsed > sequenceTime
log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${state.lastActivated}"
}
if (doFlash) {
log.debug "FLASHING $numFlashes times"
state.lastActivated = now()
log.debug "LAST ACTIVATED SET TO: ${state.lastActivated}"
def initialActionOn = switches.collect{it.currentSwitch != "on"}
def delay = 1L
numFlashes.times {
log.trace "Switch on after $delay msec"
switches.eachWithIndex {s, i ->
if (initialActionOn[i]) {
s.on(delay: delay)
}
else {
s.off(delay:delay)
}
}
delay += onFor
log.trace "Switch off after $delay msec"
switches.eachWithIndex {s, i ->
if (initialActionOn[i]) {
s.off(delay: delay)
}
else {
s.on(delay:delay)
}
}
delay += offFor
}
}
}