-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights_svc.go
159 lines (127 loc) · 3.45 KB
/
lights_svc.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
package main
import (
"fmt"
"log"
"time"
"github.com/cameliot/alpaca"
)
type LightsSvc struct {
Lights []Light
Cgi *LichtCgi
updateBuffer map[int]Light
}
func NewLightsSvc(lichtCgiBase string) *LightsSvc {
log.Println("Using licht.cgi @", lichtCgiBase)
// Load initial state from server
cgi := NewLichtCgi(lichtCgiBase)
lights, err := cgi.FetchLights(5)
if err != nil {
log.Println("Could not fetch lights from server:", err)
}
// Initial State
svc := &LightsSvc{
Lights: lights,
Cgi: cgi,
updateBuffer: map[int]Light{},
}
return svc
}
/*
Service main: React to incoming actions and dispatch
responses.
*/
func (self *LightsSvc) Handle(actions alpaca.Actions, dispatch alpaca.Dispatch) {
// Constantly poll server in case someone changed the
// values using the legacy web interface.
go self.watchServer(dispatch)
// Apply updates over HTTP at a constant rate
go self.applyUpdatesProc(dispatch)
// Hanlde actions
for action := range actions {
switch action.Type {
case SET_LIGHT_VALUE_REQUEST:
self.handleSetLightValue(action, dispatch)
case GET_LIGHT_VALUES_REQUEST:
dispatch(GetLightValuesSuccess(self.Lights))
}
}
}
func (self *LightsSvc) handleSetLightValue(
action alpaca.Action, dispatch alpaca.Dispatch) {
// Create new light update from
payload := LightValuePayload{}
action.DecodePayload(&payload)
// Update state
if payload.Id >= len(self.Lights) {
// Huh. This should not happen.
dispatch(SetLightValueError(501, fmt.Errorf(
"Set light id > registered lights",
)))
return
}
light := Light{payload.Id, payload.Value}
self.Lights[payload.Id] = light
// Queue update
self.updateBuffer[light.Id] = light
}
/*
Watch the server and dispatch events in case something changed
*/
func (self *LightsSvc) watchServer(dispatch alpaca.Dispatch) {
for {
nextLights, err := self.Cgi.FetchLights(10)
if err != nil {
log.Println(
"An error occured while fetching state from server:",
err,
)
// Go has sometimes an issue with the caching of the ip address or
// something else. As a quick and dirty fix let this service
// just die and let systemd restart it.
log.Fatal("Connecting to dali failed. Let's die.")
continue // This is never reached
}
// Diff with current values and dispatch
// updated event if required.
for i, nextLight := range nextLights {
if i >= len(self.Lights) {
log.Println("Interessting! A new light was installed?")
dispatch(SetLightValueSuccess(nextLight.Id, nextLight.Value))
continue
}
currentLight := self.Lights[i]
if currentLight.Id != nextLight.Id {
log.Println("Something is wrong. Skipping.")
continue
}
if currentLight.Value != nextLight.Value {
// The value has changed! Inform our fellow services.
dispatch(SetLightValueSuccess(nextLight.Id, nextLight.Value))
}
}
// Update state
self.Lights = nextLights
// Repeat after some timeout
time.Sleep(5 * time.Second)
}
}
/*
Apply updates with a constant rate
*/
func (self *LightsSvc) applyUpdatesProc(dispatch alpaca.Dispatch) {
for {
for id, light := range self.updateBuffer {
// Set light value on server
err := self.Cgi.Update(light.Id, light.Value)
if err != nil {
dispatch(SetLightValueError(500, err))
return
}
delete(self.updateBuffer, id)
// OK
dispatch(SetLightValueSuccess(light.Id, light.Value))
time.Sleep(time.Second / 15) // Limit FPS
}
time.Sleep(time.Second / 30) // Limit Updated Rate
}
}