/
main.go
153 lines (127 loc) · 4.6 KB
/
main.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
package main
import (
"castcontrol"
"fmt"
"hueControl"
"log"
"math"
"plexControl"
"ps4control"
"sunsetControl"
"time"
)
const (
BrightnessDim = 0.2
BrightnessNominal = 0.6
BrightnessFull = 0.8
BrightnessTick = 0.02
)
func closeTo(a float64, b float64) bool {
return math.Abs(a-b) < BrightnessTick*1.5
}
func animationLoop(brightnessValues chan float64) {
// loops forever, if there are no new brightness values coming in and no animations to do then it
// blocks on brightnessValues waiting for data.
var targetBrightness float64
for ; ; targetBrightness = <-brightnessValues { // outer loop blocks
for ; ; time.Sleep(250 * time.Millisecond) { // inner loop does not block
select {
case targetBrightness = <-brightnessValues:
default:
}
log.Printf("glux: targetBrightness: %v\n", targetBrightness)
hueBridge := hueControl.GetHueBridge()
lightsState, _ := hueControl.AreLightsOn(hueBridge)
lightsBrightness, _ := hueControl.GetLightsBrightness(hueBridge)
log.Printf("glux: actualbrightnes: %v\n", lightsBrightness)
if lightsState == true && closeTo(lightsBrightness, targetBrightness) == false {
change := BrightnessTick
if targetBrightness < lightsBrightness {
change = change - change - change // inverts positive to negative
}
hueControl.SetBrightness(hueBridge, lightsBrightness+change)
} else {
// either lights are off or we reached our target, either way stop doing things
// and bubble out to the blocking loop
break
}
}
}
}
func mainLoopFunc() {
brightnessMessages := make(chan float64)
go animationLoop(brightnessMessages)
for ; ; time.Sleep(5 * time.Second) {
hueBridge := hueControl.GetHueBridge()
lightsState, _ := hueControl.AreLightsOn(hueBridge)
secondsUntilSunsetEvent := sunsetControl.SecondsUntilSunsetEvent(time.Now())
secondsUntilSunriseEvent := sunsetControl.SecondsUntilSunriseEvent(time.Now())
plexState := plexControl.GetPlexState()
ps4State := ps4control.GetPs4State()
castState := castcontrol.GetCastState()
fmt.Printf("Lights state is %v\n", lightsState)
fmt.Printf("seconds until sunset event: %v\n", secondsUntilSunsetEvent)
fmt.Printf("seconds until sunrise event: %v\n", secondsUntilSunriseEvent)
fmt.Printf("plex state: %v\n", plexState)
fmt.Printf("ps4 state: %v\n", ps4State)
fmt.Printf("cast state: %v\n", castState)
// start of actual logic for lights
// if lights are off, do nothing. single exception is if it is close to sunset event
if secondsUntilSunsetEvent < 120 && secondsUntilSunsetEvent >= 0 && lightsState == false {
log.Printf("only %v seconds until sunset event, turning lights on", secondsUntilSunsetEvent)
hueControl.TurnLightsOn(hueBridge)
lightsState, _ = hueControl.AreLightsOn(hueBridge)
}
// if the lights are on, we probably want to turn them off at sunrise, we don't do this in a clever way, they just turn off instantly
if secondsUntilSunriseEvent < 120 && secondsUntilSunriseEvent >= 0 {
log.Printf("only %v seconds until sunrise event, turning lights off")
hueControl.TurnLightsOff(hueBridge)
lightsState, _ = hueControl.AreLightsOn(hueBridge)
}
if lightsState == false {
continue
}
// if we get here then the lights are on and we should start doing clever things
timeOfDayBrightness := sunsetControl.TimeOfDayBrightnessCalc(time.Now())
var plexBrightness float64
switch plexControl.GetPlexState() {
case plexControl.StatePlaying:
plexBrightness = 0.0
case plexControl.StatePaused:
plexBrightness = 0.5
case plexControl.StateStopped:
plexBrightness = 1.0
}
var ps4Brightness float64
switch ps4control.GetPs4State() {
case ps4control.StatePlaying:
ps4Brightness = 0.0
case ps4control.StatePaused:
ps4Brightness = 0.5
case ps4control.StateStopped:
ps4Brightness = 1.0
}
var castBrightness float64
switch castcontrol.GetCastState() {
case castcontrol.StatePlaying:
castBrightness = 0.0
case castcontrol.StatePaused:
castBrightness = 0.5
case castcontrol.StateStopped:
castBrightness = 1.0
}
fmt.Printf("timeOfDayBrightness: %v\n", timeOfDayBrightness)
fmt.Printf("plexBrightness: %v\n", plexBrightness)
fmt.Printf("ps4Brightness: %v\n", ps4Brightness)
fmt.Printf("castBrightness: %v\n", castBrightness)
var combBrightness = math.Min(plexBrightness, ps4Brightness)
combBrightness = math.Min(combBrightness, castBrightness)
totalBrightness := timeOfDayBrightness * combBrightness
brightnessMessages <- totalBrightness * BrightnessFull
fmt.Printf("total brightness: %v\n", totalBrightness*BrightnessFull)
}
}
func main() {
fmt.Printf("Starting glux-go...\n")
mainLoopFunc()
}