-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
386 lines (370 loc) · 11.8 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"time"
"flag"
"fmt"
"strconv"
"net/http"
"encoding/json"
"github.com/gobuffalo/packr"
table "boardgametable/table"
)
const restPort = 8080
var sp108e *table.Sp108e
func handleSuccess(w *http.ResponseWriter, result interface{}) {
writer := *w
marshalled, err := json.Marshal(result)
if err != nil {
handleError(w, 500, "Internal Server Error:", "Error marshalling response JSON:", err)
return
}
writer.Header().Add("Content-Type", "application/json")
writer.Header().Add("Access-Control-Allow-Origin", "*")
writer.WriteHeader(200)
writer.Write(marshalled)
}
func handleError(w *http.ResponseWriter, code int, responseText string, logMessage string, err error) {
errorMessage := ""
writer := *w
if err != nil {
errorMessage = err.Error()
}
fmt.Println(logMessage, errorMessage)
writer.WriteHeader(code)
writer.Write([]byte(responseText))
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
fmt.Println("incoming request:", r.URL)
switch r.Method {
case http.MethodGet:
doRequest(w, r)
break
default:
handleError(&w, 405, "Method not allowed", "Method not allowed", nil)
break
}
}
func doRequest(w http.ResponseWriter, r *http.Request) {
keys := r.URL.Query()
command, ok := keys["command"]
if !ok || len(command) != 1 {
handleError(&w, 500, "command not given", "command not given", nil)
return;
}
switch command[0] {
case "brightness":
value, ok := keys["value"]
if !ok || len(value) != 1 {
handleError(&w, 500, "value not given", "value not given", nil)
return;
}
intValue, err := strconv.Atoi(value[0])
if err != nil {
handleError(&w, 500, "invalid value given", "invalid value given", nil)
return;
}
err = sp108e.SetBrightness(byte(intValue))
if err != nil {
handleError(&w, 500, "error setting brightness:", "error setting brightness:", err)
return;
}
handleSuccess(&w, "success")
break
case "startcolormap":
colormap, ok := keys["map"]
if !ok || len(colormap) != 1 {
handleError(&w, 500, "colormap not given", "colormap not given", nil)
return;
}
brightness, ok := keys["brightness"]
if !ok || len(brightness) != 1 {
handleError(&w, 500, "brightness not given", "brightness not given", nil)
return;
}
intBrightness, err := strconv.Atoi(brightness[0])
if err != nil {
handleError(&w, 500, "invalid brightness given", "invalid brightness given", nil)
return;
}
err = sp108e.StopAnimation()
if err != nil {
handleError(&w, 500, "error stopping animation:", "error stopping animation:", err)
return;
}
err = sp108e.SetBrightness(byte(intBrightness))
if err != nil {
handleError(&w, 500, "error setting brightness:", "error setting brightness:", err)
return;
}
animation := table.NewAnimationPlayTable(sp108e.GetFrameBuffer())
err = animation.SetPlayerColorFromString(colormap[0])
if err != nil {
handleError(&w, 500, "error setting up animation:", "error setting up animation:", err)
return;
}
err = sp108e.StartAnimation(animation)
if err != nil {
handleError(&w, 500, "error starting animation:", "error starting animation:", err)
return;
}
handleSuccess(&w, "success")
break
case "stopcolormap":
err := sp108e.StopAnimation()
if err != nil {
handleError(&w, 500, "error stopping animation:", "error stopping animation:", err)
return;
}
handleSuccess(&w, "success")
break
case "tablecolors":
l, ok := keys["left"]
if !ok || len(l) != 1 {
handleError(&w, 500, "left not given", "left not given", nil)
return;
}
r, ok := keys["right"]
if !ok || len(r) != 1 {
handleError(&w, 500, "right not given", "right not given", nil)
return;
}
t, ok := keys["top"]
if !ok || len(t) != 1 {
handleError(&w, 500, "top not given", "top not given", nil)
return;
}
b, ok := keys["bottom"]
if !ok || len(b) != 1 {
handleError(&w, 500, "bottom not given", "bottom not given", nil)
return;
}
brightness, ok := keys["brightness"]
if !ok || len(brightness) != 1 {
handleError(&w, 500, "brightness not given", "brightness not given", nil)
return;
}
intBrightness, err := strconv.Atoi(brightness[0])
if err != nil {
handleError(&w, 500, "invalid brightness given", "invalid brightness given", nil)
return;
}
err = sp108e.StopAnimation()
if err != nil {
handleError(&w, 500, "error stopping animation:", "error stopping animation:", err)
return;
}
err = sp108e.SetBrightness(byte(intBrightness))
if err != nil {
handleError(&w, 500, "error setting brightness:", "error setting brightness:", err)
return;
}
animation := table.NewAnimationPlayTable(sp108e.GetFrameBuffer())
err = animation.SetPlayerColor(table.Directions["right"], table.Colors[r[0]])
if err != nil {
handleError(&w, 500, "error creating player color for right:", "error creating player color for right:", err)
return;
}
animation.SetPlayerColor(table.Directions["bottom"], table.Colors[b[0]])
if err != nil {
handleError(&w, 500, "error creating player color for bottom:", "error creating player color for bottom:", err)
return;
}
animation.SetPlayerColor(table.Directions["left"], table.Colors[l[0]])
if err != nil {
handleError(&w, 500, "error creating player color for left:", "error creating player color for left:", err)
return;
}
animation.SetPlayerColor(table.Directions["top"], table.Colors[t[0]])
if err != nil {
handleError(&w, 500, "error creating player color for top:", "error creating player color for top:", err)
return;
}
err = sp108e.StartAnimation(animation)
if err != nil {
handleError(&w, 500, "error starting animation:", "error starting animation:", err)
return;
}
handleSuccess(&w, "success")
break
case "active":
d, ok := keys["direction"]
if !ok || len(d) != 1 {
handleError(&w, 500, "direction not given", "direction not given", nil)
return;
}
if d[0] != "left" && d[0] != "right" && d[0] != "top" && d[0] != "bottom" {
handleError(&w, 500, "unknown direction", "unknown direction", nil)
return;
}
currentAnimation := sp108e.GetCurrentAnimation()
if currentAnimation == nil {
handleError(&w, 500, "no current animation", "no current animation", nil)
return;
}
currentPlayTableAnimation, ok := currentAnimation.(*table.AnimationPlayTable)
if !ok {
handleError(&w, 500, "current animation does not support active direction", "current animation does not support active direction", nil)
return;
}
err := currentPlayTableAnimation.SetActiveDirection(table.Directions[d[0]])
if err != nil {
handleError(&w, 500, "error setting active direction:", "error setting active direction:", err)
return;
}
handleSuccess(&w, "success")
break
case "nextactive":
currentAnimation := sp108e.GetCurrentAnimation()
if currentAnimation == nil {
handleError(&w, 500, "no current animation", "no current animation", nil)
return;
}
currentPlayTableAnimation, ok := currentAnimation.(*table.AnimationPlayTable)
if !ok {
handleError(&w, 500, "current animation does not support active direction", "current animation does not support active direction", nil)
return;
}
err := currentPlayTableAnimation.ActiveDirectionNext()
if err != nil {
handleError(&w, 500, "error setting active direction:", "error setting active direction:", err)
return;
}
handleSuccess(&w, "success")
break
case "activeoff":
currentAnimation := sp108e.GetCurrentAnimation()
if currentAnimation == nil {
handleError(&w, 500, "no current animation", "no current animation", nil)
return;
}
currentPlayTableAnimation, ok := currentAnimation.(*table.AnimationPlayTable)
if !ok {
handleError(&w, 500, "current animation does not support active direction", "current animation does not support active direction", nil)
return;
}
err := currentPlayTableAnimation.ActiveDirectionOff()
if err != nil {
handleError(&w, 500, "error stopping active direction:", "error stopping active direction:", err)
return;
}
handleSuccess(&w, "success")
break
case "reconnect":
err := sp108e.Reconnect(true)
if err != nil {
handleError(&w, 500, "error reconnecting:", "error reconnecting:", err)
return
}
handleSuccess(&w, "success")
break
default:
handleError(&w, 405, "unknown command", "unknown command", nil)
break
}
}
func timerTask() {
fmt.Println("performing scheduled reconnect..")
err := sp108e.Reconnect(true)
if err != nil {
fmt.Println("error performing scheduled reconnect:", err)
return
}
}
func main() {
serverPtr := flag.Bool("server", false, "start rest server")
hostPtr := flag.String("host", "192.168.178.83", "controller host")
portPtr := flag.Int("port", 8189, "port number")
brightnessPtr := flag.Int("brightness", -1, "brightness value")
colormapPtr := flag.String("colormap", "0,100,ff,00,00-101,200,00,ff,00", "colormap definition")
colorRightPtr := flag.String("right", "", "color right")
colorLeftPtr := flag.String("left", "", "color left")
colorTopPtr := flag.String("top", "", "color top")
colorBottomPtr := flag.String("bottom", "", "color bottom")
reconnectIntervalPtr := flag.Int("reconnect", 300, "reconnect interval in seconds")
flag.Parse()
fmt.Println("Boardgame Table Control")
fmt.Println("using sp108e host:", *hostPtr)
fmt.Println("using sp108e port:", *portPtr)
// connect to the sp108e
var err error
sp108e, err = table.NewSp108e(*hostPtr, *portPtr)
if err != nil {
fmt.Println("error connecting to sp108", err)
return
}
if *serverPtr {
// server mode, start rest service
fmt.Printf("starting rest service on port %d, terminate with ctrl-c\n", restPort)
// start timer that reconnects every 5 minutes
ticker := time.NewTicker(time.Duration(*reconnectIntervalPtr) * time.Second)
quit := make(chan struct{})
// end timer by closing the quit channel: close(quit)
go func() {
for {
select {
case <- ticker.C:
timerTask()
case <- quit:
ticker.Stop()
return
}
}
}()
// setup web service
staticResources := packr.NewBox("./static")
http.Handle("/", http.FileServer(staticResources))
http.HandleFunc("/api", handleRequest)
var err = http.ListenAndServe(":"+strconv.Itoa(restPort), nil)
if err != nil {
fmt.Println("server failed starting:", err)
}
} else {
// cli mode, parse the available cli params
if *brightnessPtr!=-1 {
sp108e.SetBrightness(byte(*brightnessPtr))
}
// directions OR colormap
if *colorRightPtr != "" && *colorLeftPtr != "" && *colorTopPtr != "" && *colorBottomPtr != "" {
animation := table.NewAnimationPlayTable(sp108e.GetFrameBuffer())
err := animation.SetPlayerColor(table.Directions["right"], table.Colors[*colorRightPtr])
if err != nil {
fmt.Println("error creating player color for right:", err)
return;
}
animation.SetPlayerColor(table.Directions["bottom"], table.Colors[*colorBottomPtr])
if err != nil {
fmt.Println("error creating player color for bottom:", err)
return;
}
animation.SetPlayerColor(table.Directions["left"], table.Colors[*colorLeftPtr])
if err != nil {
fmt.Println("error creating player color for left:", err)
return;
}
animation.SetPlayerColor(table.Directions["top"], table.Colors[*colorTopPtr])
if err != nil {
fmt.Println("error creating player color for top:", err)
return;
}
fmt.Println("directional colors given, starting display loop, terminate with ctrl-c")
err = sp108e.StartAnimation(animation)
if err != nil {
fmt.Println("error starting animation:", err)
return;
}
} else if *colormapPtr != "" {
animation := new(table.AnimationPlayTable)
err := animation.SetPlayerColorFromString(*colormapPtr)
if err != nil {
fmt.Println("error creating player color for right:", err)
return;
}
fmt.Println("colormap given, starting display loop, terminate with ctrl-c")
err = sp108e.StartAnimation(animation)
if err != nil {
fmt.Println("error starting animation:", err)
return;
}
}
}
}