2018-09-23 18:39:24 -07:00
|
|
|
// Copyright 2018 Team 254. All Rights Reserved.
|
2014-09-06 17:06:06 -07:00
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
//
|
2018-09-23 18:39:24 -07:00
|
|
|
// Web handlers for the field monitor display showing robot connection status.
|
2014-09-06 17:06:06 -07:00
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
package web
|
2014-09-06 17:06:06 -07:00
|
|
|
|
|
|
|
|
import (
|
2021-05-16 11:00:29 -07:00
|
|
|
"github.com/Team254/cheesy-arena-lite/model"
|
|
|
|
|
"github.com/Team254/cheesy-arena-lite/websocket"
|
2020-04-02 20:06:56 -07:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
2014-09-06 17:06:06 -07:00
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
2018-09-23 18:39:24 -07:00
|
|
|
// Renders the field monitor display.
|
|
|
|
|
func (web *Web) fieldMonitorDisplayHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-02 20:06:56 -07:00
|
|
|
if r.URL.Query().Get("fta") == "true" && !web.userIsAdmin(w, r) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !web.enforceDisplayConfiguration(w, r, map[string]string{"reversed": "false", "fta": "false"}) {
|
2018-09-09 22:42:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 18:39:24 -07:00
|
|
|
template, err := web.parseFiles("templates/field_monitor_display.html")
|
2014-09-06 17:06:06 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data := struct {
|
2017-08-23 22:41:56 -07:00
|
|
|
*model.EventSettings
|
2017-08-28 20:14:32 -07:00
|
|
|
}{web.arena.EventSettings}
|
2018-09-23 18:39:24 -07:00
|
|
|
err = template.ExecuteTemplate(w, "field_monitor_display.html", data)
|
2014-09-06 17:06:06 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-13 21:07:14 -07:00
|
|
|
|
2018-09-23 18:39:24 -07:00
|
|
|
// The websocket endpoint for the field monitor display client to receive status updates.
|
|
|
|
|
func (web *Web) fieldMonitorDisplayWebsocketHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-02 20:06:56 -07:00
|
|
|
isFta := r.URL.Query().Get("fta") == "true"
|
|
|
|
|
if isFta && !web.userIsAdmin(w, r) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 22:27:08 -07:00
|
|
|
display, err := web.registerDisplay(r)
|
2018-09-09 22:42:38 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-03-29 16:55:53 -07:00
|
|
|
defer web.arena.MarkDisplayDisconnected(display.DisplayConfiguration.Id)
|
2016-08-13 21:07:14 -07:00
|
|
|
|
2018-08-31 22:40:08 -07:00
|
|
|
ws, err := websocket.NewWebsocket(w, r)
|
2016-08-13 21:07:14 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-08-31 22:40:08 -07:00
|
|
|
defer ws.Close()
|
2016-08-13 21:07:14 -07:00
|
|
|
|
2020-04-02 20:06:56 -07:00
|
|
|
// Subscribe the websocket to the notifiers whose messages will be passed on to the client, in a separate goroutine.
|
|
|
|
|
go ws.HandleNotifiers(display.Notifier, web.arena.ArenaStatusNotifier, web.arena.EventStatusNotifier,
|
2020-03-29 18:56:34 -07:00
|
|
|
web.arena.ReloadDisplaysNotifier)
|
2020-04-02 20:06:56 -07:00
|
|
|
|
|
|
|
|
// Loop, waiting for commands and responding to them, until the client closes the connection.
|
|
|
|
|
for {
|
|
|
|
|
command, data, err := ws.Read()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
// Client has closed the connection; nothing to do here.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if command == "updateTeamNotes" {
|
|
|
|
|
if isFta {
|
|
|
|
|
args := struct {
|
|
|
|
|
Station string
|
|
|
|
|
Notes string
|
|
|
|
|
}{}
|
|
|
|
|
err = mapstructure.Decode(data, &args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ws.WriteError(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if allianceStation, ok := web.arena.AllianceStations[args.Station]; ok {
|
|
|
|
|
if allianceStation.Team != nil {
|
|
|
|
|
allianceStation.Team.FtaNotes = args.Notes
|
2021-05-12 17:49:05 -07:00
|
|
|
if err := web.arena.Database.UpdateTeam(allianceStation.Team); err != nil {
|
2020-04-02 20:06:56 -07:00
|
|
|
ws.WriteError(err.Error())
|
|
|
|
|
}
|
|
|
|
|
web.arena.ArenaStatusNotifier.Notify()
|
|
|
|
|
} else {
|
|
|
|
|
ws.WriteError("No team present")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ws.WriteError("Invalid alliance station")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ws.WriteError("Must be in FTA mode to update team notes")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-13 21:07:14 -07:00
|
|
|
}
|