Files
cheesy-arena-lite/web/setup_field.go

124 lines
3.6 KiB
Go
Raw Normal View History

2014-08-04 00:52:46 -07:00
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Web routes for configuring the field components.
package web
2014-08-04 00:52:46 -07:00
import (
"fmt"
"github.com/Team254/cheesy-arena/field"
2018-04-15 18:33:04 -07:00
"github.com/Team254/cheesy-arena/led"
"github.com/Team254/cheesy-arena/model"
"github.com/Team254/cheesy-arena/vaultled"
"github.com/Team254/cheesy-arena/websocket"
"github.com/mitchellh/mapstructure"
"io"
"log"
2014-08-04 00:52:46 -07:00
"net/http"
)
// Shows the field configuration page.
func (web *Web) fieldGetHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
2015-08-22 23:33:38 -07:00
return
}
template, err := web.parseFiles("templates/setup_field.html", "templates/base.html")
2014-08-04 00:52:46 -07:00
if err != nil {
handleWebErr(w, err)
return
}
2018-04-15 18:33:04 -07:00
plc := web.arena.Plc
2014-08-04 00:52:46 -07:00
data := struct {
*model.EventSettings
2014-08-04 00:52:46 -07:00
AllianceStationDisplays map[string]string
2018-04-15 18:33:04 -07:00
InputNames []string
RegisterNames []string
CoilNames []string
LedModeNames map[led.Mode]string
VaultLedModeNames map[vaultled.Mode]string
}{web.arena.EventSettings, web.arena.AllianceStationDisplays, plc.GetInputNames(), plc.GetRegisterNames(),
plc.GetCoilNames(), led.ModeNames, vaultled.ModeNames}
2014-08-04 00:52:46 -07:00
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
// Updates the display-station mapping for a single display.
func (web *Web) fieldPostHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
2015-08-22 23:33:38 -07:00
return
}
2014-08-04 00:52:46 -07:00
displayId := r.PostFormValue("displayId")
allianceStation := r.PostFormValue("allianceStation")
web.arena.AllianceStationDisplays[displayId] = allianceStation
web.arena.MatchLoadNotifier.Notify()
http.Redirect(w, r, "/setup/field", 303)
2014-08-04 00:52:46 -07:00
}
// Force-reloads all the websocket-connected displays.
func (web *Web) fieldReloadDisplaysHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
2015-08-22 23:33:38 -07:00
return
}
web.arena.ReloadDisplaysNotifier.Notify()
http.Redirect(w, r, "/setup/field", 303)
2014-08-28 00:12:45 -07:00
}
// The websocket endpoint for sending realtime updates to the field setup page.
func (web *Web) fieldWebsocketHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
return
}
ws, err := websocket.NewWebsocket(w, r)
if err != nil {
handleWebErr(w, err)
return
}
defer ws.Close()
// Subscribe the websocket to the notifiers whose messages will be passed on to the client, in a separate goroutine.
go ws.HandleNotifiers(web.arena.LedModeNotifier, web.arena.Plc.IoChangeNotifier)
// Loop, waiting for commands and responding to them, until the client closes the connection.
for {
messageType, 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
}
switch messageType {
case "setLedMode":
if web.arena.MatchState != field.PreMatch {
ws.WriteError("Arena must be in pre-match state")
continue
}
var modeMessage field.LedModeMessage
err = mapstructure.Decode(data, &modeMessage)
if err != nil {
ws.WriteError(err.Error())
continue
}
web.arena.ScaleLeds.SetMode(modeMessage.LedMode, modeMessage.LedMode)
web.arena.RedSwitchLeds.SetMode(modeMessage.LedMode, modeMessage.LedMode)
web.arena.BlueSwitchLeds.SetMode(modeMessage.LedMode, modeMessage.LedMode)
web.arena.RedVaultLeds.SetAllModes(modeMessage.VaultLedMode)
web.arena.BlueVaultLeds.SetAllModes(modeMessage.VaultLedMode)
web.arena.LedModeNotifier.Notify()
default:
ws.WriteError(fmt.Sprintf("Invalid message type '%s'.", messageType))
}
}
}