-
Notifications
You must be signed in to change notification settings - Fork 1
/
hallwaylightdoor.go
51 lines (43 loc) · 1.13 KB
/
hallwaylightdoor.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
package main
import (
"bytes"
"fmt"
"time"
)
type hallwayLightDoor struct {
statusLoop
lastTurnedOff time.Time
}
func (l *hallwayLightDoor) ProcessEvent(ev MQTTEvent) []MQTTPublish {
switch ev.Topic {
case "shellies/shellycolorbulb-98CDAC1FB736/color/0",
"shellies/shellycolorbulb-98CDAC1E5C3E/color/0":
if bytes.Equal(ev.Payload.([]byte), []byte("off")) {
l.lastTurnedOff = ev.Timestamp
l.statusf("floor lights last turned off: %v", l.lastTurnedOff)
}
return nil
case "github.com/stapelberg/shelly2mqtt/door/hall":
// TODO: parse JSON
if fmt.Sprintf("%s", ev.Payload) != `{"onoff":true}` {
return nil // apartment door closed
}
// apartment door opened
if hour := ev.Timestamp.Hour(); hour > 7 && hour < 17 {
l.statusf("leaving lights off during daytime")
return nil // leave lights off during daytime
}
if ago := time.Since(l.lastTurnedOff); ago < 1*time.Minute {
l.statusf("lights were turned off %v ago, not turning on", ago)
return nil
}
return []MQTTPublish{
{
Topic: "github.com/stapelberg/hue2mqtt/cmd/light/hall/on",
Payload: "{}",
},
}
default:
return nil
}
}