forked from saml-dev/gome-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners.go
125 lines (110 loc) · 2.94 KB
/
listeners.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
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
package gomeassistant
import (
"time"
"github.com/golang-module/carbon"
"saml.dev/gome-assistant/internal"
)
type conditionCheck struct {
fail bool
}
func checkWithinTimeRange(startTime, endTime string) conditionCheck {
cc := conditionCheck{fail: false}
// if betweenStart and betweenEnd both set, first account for midnight
// overlap, then check if between those times.
if startTime != "" && endTime != "" {
parsedStart := internal.ParseTime(startTime)
parsedEnd := internal.ParseTime(endTime)
// check for midnight overlap
if parsedEnd.Lt(parsedStart) { // example turn on night lights when motion from 23:00 to 07:00
if parsedEnd.IsPast() { // such as at 15:00, 22:00
parsedEnd = parsedEnd.AddDay()
} else {
parsedStart = parsedStart.SubDay() // such as at 03:00, 05:00
}
}
// skip callback if not inside the range
if !carbon.Now().BetweenIncludedStart(parsedStart, parsedEnd) {
cc.fail = true
}
// otherwise just check individual before/after
} else if startTime != "" && internal.ParseTime(startTime).IsFuture() {
cc.fail = true
} else if endTime != "" && internal.ParseTime(endTime).IsPast() {
cc.fail = true
}
return cc
}
func checkStatesMatch(listenerState, s string) conditionCheck {
cc := conditionCheck{fail: false}
// check if fromState or toState are set and don't match
if listenerState != "" && listenerState != s {
cc.fail = true
}
return cc
}
func checkThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck {
cc := conditionCheck{fail: false}
// check if Throttle is set and that duration hasn't passed since lastRan
if throttle.Seconds() > 0 &&
lastRan.DiffAbsInSeconds(carbon.Now()) < int64(throttle.Seconds()) {
cc.fail = true
}
return cc
}
func checkExceptionDates(eList []time.Time) conditionCheck {
cc := conditionCheck{fail: false}
for _, e := range eList {
y1, m1, d1 := e.Date()
y2, m2, d2 := time.Now().Date()
if y1 == y2 && m1 == m2 && d1 == d2 {
cc.fail = true
break
}
}
return cc
}
func checkExceptionRanges(eList []timeRange) conditionCheck {
cc := conditionCheck{fail: false}
now := time.Now()
for _, eRange := range eList {
if now.After(eRange.start) && now.Before(eRange.end) {
cc.fail = true
break
}
}
return cc
}
func checkAllowlistDates(eList []time.Time) conditionCheck {
if len(eList) == 0 {
return conditionCheck{fail: false}
}
cc := conditionCheck{fail: true}
for _, e := range eList {
y1, m1, d1 := e.Date()
y2, m2, d2 := time.Now().Date()
if y1 == y2 && m1 == m2 && d1 == d2 {
cc.fail = false
break
}
}
return cc
}
func checkStartEndTime(s TimeString, isStart bool) conditionCheck {
cc := conditionCheck{fail: false}
// pass immediately if default
if s == "00:00" {
return cc
}
now := time.Now()
parsedTime := internal.ParseTime(string(s)).Carbon2Time()
if isStart {
if parsedTime.After(now) {
cc.fail = true
}
} else {
if parsedTime.Before(now) {
cc.fail = true
}
}
return cc
}