forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fta_display.go
126 lines (115 loc) · 3.05 KB
/
fta_display.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web handlers for the FTA diagnostic display.
package main
import (
"io"
"log"
"net/http"
"text/template"
)
// Renders the FTA diagnostic display.
func FtaDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
// Retrieve the next few matches to show which defenses they will require.
numUpcomingMatches := 3
matches, err := db.GetMatchesByType(mainArena.currentMatch.Type)
if err != nil {
handleWebErr(w, err)
return
}
var upcomingMatches []Match
for _, match := range matches {
if match.Status != "complete" {
upcomingMatches = append(upcomingMatches, match)
if len(upcomingMatches) == numUpcomingMatches {
break
}
}
}
template := template.New("").Funcs(templateHelpers)
_, err = template.ParseFiles("templates/fta_display.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
UpcomingMatches []Match
DefenseNames map[string]string
}{eventSettings, upcomingMatches, defenseNames}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// The websocket endpoint for the FTA display client to receive status updates.
func FtaDisplayWebsocketHandler(w http.ResponseWriter, r *http.Request) {
// TODO(patrick): Enable authentication once Safari (for iPad) supports it over Websocket.
websocket, err := NewWebsocket(w, r)
if err != nil {
handleWebErr(w, err)
return
}
defer websocket.Close()
robotStatusListener := mainArena.robotStatusNotifier.Listen()
defer close(robotStatusListener)
defenseSelectionListener := mainArena.defenseSelectionNotifier.Listen()
defer close(defenseSelectionListener)
reloadDisplaysListener := mainArena.reloadDisplaysNotifier.Listen()
defer close(reloadDisplaysListener)
// Send the various notifications immediately upon connection.
err = websocket.Write("status", mainArena)
if err != nil {
log.Printf("Websocket error: %s", err)
return
}
// Spin off a goroutine to listen for notifications and pass them on through the websocket.
go func() {
for {
var messageType string
var message interface{}
select {
case _, ok := <-robotStatusListener:
if !ok {
return
}
messageType = "status"
message = mainArena
case _, ok := <-defenseSelectionListener:
if !ok {
return
}
messageType = "reload"
message = nil
case _, ok := <-reloadDisplaysListener:
if !ok {
return
}
messageType = "reload"
message = nil
}
err = websocket.Write(messageType, message)
if err != nil {
// The client has probably closed the connection; nothing to do here.
return
}
}
}()
// Loop, waiting for commands and responding to them, until the client closes the connection.
for {
_, _, err := websocket.Read()
if err != nil {
if err == io.EOF {
// Client has closed the connection; nothing to do here.
return
}
log.Printf("Websocket error: %s", err)
return
}
}
}