-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhallwaylightlate.go
68 lines (56 loc) · 1.3 KB
/
hallwaylightlate.go
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
package main
import (
"log"
"time"
"golang.org/x/time/rate"
)
type hallwayLightLate struct {
statusLoop
lastHallOn time.Time
sometimes *rate.Limiter
lastTurnedOff time.Time
}
func newHallwayLightLate() *hallwayLightLate {
const disableBursting = 1
return &hallwayLightLate{
sometimes: rate.NewLimiter(rate.Every(1*time.Minute), disableBursting),
}
}
func (l *hallwayLightLate) ProcessEvent(ev MQTTEvent) []MQTTPublish {
switch ev.Topic {
case "regelwerk/ticker/1s":
default:
return nil
}
// TODO: factor out “midnaUnlocked || phoneHome” from avrpower into another
// separate piece of code
now := ev.Timestamp
hour := now.Hour()
threshold := 17
if now.Month() == time.May ||
now.Month() == time.June ||
now.Month() == time.July ||
now.Month() == time.August {
threshold = 19
}
on := hour >= threshold
l.statusf("%v == (hour=%v > %v)", on, hour, threshold)
if !on {
return nil
}
// turn on only once a day based on time
if ev.Topic == "regelwerk/ticker/1s" &&
l.lastHallOn.After(now.Truncate(24*time.Hour)) {
if l.sometimes.Allow() {
log.Printf("already turned light on today (lastHallOn=%v)", l.lastHallOn)
}
return nil
}
l.lastHallOn = now
return []MQTTPublish{
{
Topic: "github.com/stapelberg/hue2mqtt/cmd/light/hall/on",
Payload: "{}",
},
}
}