-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathlight.go
236 lines (202 loc) Β· 9.66 KB
/
light.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// MIT License
//
// # Copyright (c) 2019 Stefan Wichmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"time"
log "github.com/sirupsen/logrus"
hue "github.com/stefanwichmann/go.hue"
)
const initializationDuration = 3 * time.Second
// Light represents a light kelvin can automate in your system.
type Light struct {
ID int `json:"id"`
Name string `json:"name"`
HueLight HueLight `json:"-"`
TargetLightState LightState `json:"targetLightState,omitempty"`
Scheduled bool `json:"scheduled"`
Reachable bool `json:"reachable"`
On bool `json:"on"`
Tracking bool `json:"-"`
Automatic bool `json:"automatic"`
Initializing bool `json:"-"`
Schedule Schedule `json:"-"`
Interval Interval `json:"interval"`
Appearance time.Time `json:"-"`
}
func (light *Light) updateCurrentLightState(attr hue.LightAttributes) error {
light.HueLight.updateCurrentLightState(attr)
light.Reachable = light.HueLight.Reachable
light.On = light.HueLight.On
return nil
}
func (light *Light) update(transistionTime time.Duration) (bool, error) {
// Is the light associated to any schedule?
if !light.Scheduled {
return false, nil
}
// If the light is not reachable anymore clean up
if !light.Reachable {
if light.Tracking {
log.Printf("π‘ Light %s - Light is no longer reachable. Clearing state...", light.Name)
light.Tracking = false
light.Automatic = false
light.Initializing = false
return false, nil
}
// Ignore light because we are not tracking it.
return false, nil
}
// If the light was turned off clean up
if !light.On {
if light.Tracking {
log.Printf("π‘ Light %s - Light was turned off. Clearing state...", light.Name)
light.Tracking = false
light.Automatic = false
light.Initializing = false
return false, nil
}
// Ignore light because we are not tracking it.
return false, nil
}
// Did the light just appear?
if !light.Tracking {
log.Printf("π‘ Light %s - Light just appeared.", light.Name)
light.Tracking = true
light.Appearance = time.Now()
// Should we auto-enable Kelvin?
if light.Schedule.enableWhenLightsAppear {
log.Printf("π‘ Light %s - Initializing state to %vK at %v%% brightness.", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
log.Debugf("π‘ Light %s - Could not initialize light after %v", light.Name, time.Since(light.Appearance))
return true, err
}
light.Automatic = true
light.Initializing = true
log.Debugf("π‘ Light %s - Light was initialized to %vK at %v%% brightness", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
return true, nil
}
}
// Ignore light if it was changed manually
if !light.Automatic {
// return if we should ignore color temperature and brightness
if light.TargetLightState.ColorTemperature == -1 && light.TargetLightState.Brightness == -1 {
return false, nil
}
// if status == scene state --> Activate Kelvin
if light.HueLight.hasState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness) {
log.Printf("π‘ Light %s - Detected matching target state. Activating Kelvin...", light.Name)
light.Automatic = true
light.Initializing = true
// set correct target lightstate on HueLight
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
log.Debugf("π‘ Light %s - Updated light state to %vK at %v%% brightness (Scene detection)", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
return true, nil
}
// Light was changed manually and does not conform to scene detection
return false, nil
}
// Keep adjusting the light state for 10 seconds after the light appeared
if light.Initializing {
log.Debugf("π‘ Light %s - Light in initialization for %v (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d)", light.Name, time.Since(light.Appearance), light.HueLight.TargetColorTemperature, light.HueLight.CurrentColorTemperature, light.HueLight.TargetColor, light.HueLight.CurrentColor, light.HueLight.TargetBrightness, light.HueLight.CurrentBrightness)
hasChanged := light.HueLight.hasChanged()
// Disable initialization phase if 10 seconds have passed and the light state has been adopted
if time.Now().After(light.Appearance.Add(initializationDuration)) && !hasChanged {
log.Debugf("π‘ Light %s - Ending initialization phase after %v", light.Name, time.Since(light.Appearance))
light.Initializing = false
}
if hasChanged {
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
log.Debugf("π‘ Light %s - Adjusting light state to %vK at %v%% brightness (Initialization)", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
return true, nil
}
return false, nil
}
// Did the user manually change the light state?
if light.HueLight.hasChanged() {
if log.GetLevel() == log.DebugLevel {
log.Debugf("π‘ Light %s - Light state has been changed manually after %v (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d)", light.Name, time.Since(light.Appearance), light.HueLight.TargetColorTemperature, light.HueLight.CurrentColorTemperature, light.HueLight.TargetColor, light.HueLight.CurrentColor, light.HueLight.TargetBrightness, light.HueLight.CurrentBrightness)
} else {
log.Printf("π‘ Light %s - Light state has been changed manually. Disabling Kelvin...", light.Name)
}
light.Automatic = false
return false, nil
}
// Update of lightstate needed?
if light.HueLight.hasState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness) {
return false, nil
}
// Light is turned on and in automatic state. Set target lightstate.
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
log.Printf("π‘ Light %s - Updated light state to %vK at %v%% brightness", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
return true, nil
}
func (light *Light) updateSchedule(schedule Schedule) {
light.Schedule = schedule
light.Scheduled = true
log.Printf("π‘ Light %s - Activating schedule for %v (Sunrise: %v, Sunset: %v)", light.Name, light.Schedule.endOfDay.Format("Jan 2 2006"), light.Schedule.sunrise.Time.Format("15:04"), light.Schedule.sunset.Time.Format("15:04"))
light.updateInterval()
}
func (light *Light) updateInterval() {
if !light.Scheduled {
log.Debugf("π‘ Light %s - Light is not associated to any schedule. No interval to update...", light.Name)
return
}
newInterval, err := light.Schedule.currentInterval(time.Now())
if err != nil {
log.Warningf("π‘ Light %s - Could not determine interval for current schedule: %v", light.Name, err)
return
}
if newInterval != light.Interval {
light.Interval = newInterval
log.Printf("π‘ Light %s - Activating interval %v - %v", light.Name, light.Interval.Start.Time.Format("15:04"), light.Interval.End.Time.Format("15:04"))
}
}
func (light *Light) updateTargetLightState() bool {
if !light.Scheduled {
log.Debugf("π‘ Light %s - Light is not associated to any schedule. No target light state to update...", light.Name)
return false
}
// Calculate the target lightstate from the interval
newLightState := light.Interval.calculateLightStateInInterval(time.Now())
// Did the target light state change?
if newLightState.equals(light.TargetLightState) {
return false
}
// First initialization of the TargetLightState?
if light.TargetLightState.ColorTemperature == 0 && light.TargetLightState.Brightness == 0 {
log.Debugf("π‘ Light %s - Initialized target light state for the interval %v - %v to %+v", light.Name, light.Interval.Start.Time.Format("15:04"), light.Interval.End.Time.Format("15:04"), newLightState)
} else {
log.Debugf("π‘ Light %s - Updated target light state for the interval %v - %v from %+v to %+v", light.Name, light.Interval.Start.Time.Format("15:04"), light.Interval.End.Time.Format("15:04"), light.TargetLightState, newLightState)
}
light.TargetLightState = newLightState
return true
}