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.
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
package web
|
2014-08-04 00:52:46 -07:00
|
|
|
|
|
|
|
|
import (
|
2018-09-03 14:33:02 -07:00
|
|
|
"fmt"
|
2017-09-03 20:51:20 -07:00
|
|
|
"github.com/Team254/cheesy-arena/field"
|
2018-04-15 18:33:04 -07:00
|
|
|
"github.com/Team254/cheesy-arena/led"
|
2017-08-23 22:41:56 -07:00
|
|
|
"github.com/Team254/cheesy-arena/model"
|
2018-08-19 02:00:50 -07:00
|
|
|
"github.com/Team254/cheesy-arena/vaultled"
|
2018-08-31 22:40:08 -07:00
|
|
|
"github.com/Team254/cheesy-arena/websocket"
|
2018-09-03 14:33:02 -07:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
2014-08-04 00:52:46 -07:00
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Shows the field configuration page.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) fieldGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !web.userIsAdmin(w, r) {
|
2015-08-22 23:33:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
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 {
|
2017-08-23 22:41:56 -07:00
|
|
|
*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
|
2018-08-19 02:00:50 -07:00
|
|
|
VaultLedModeNames map[vaultled.Mode]string
|
2018-08-18 21:01:42 -07:00
|
|
|
}{web.arena.EventSettings, web.arena.AllianceStationDisplays, plc.GetInputNames(), plc.GetRegisterNames(),
|
2018-09-03 14:33:02 -07:00
|
|
|
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.
|
2017-08-28 20:14:32 -07:00
|
|
|
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")
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.AllianceStationDisplays[displayId] = allianceStation
|
2018-08-31 22:40:08 -07:00
|
|
|
web.arena.MatchLoadNotifier.Notify()
|
2017-09-02 14:08:16 -07:00
|
|
|
http.Redirect(w, r, "/setup/field", 303)
|
2014-08-04 00:52:46 -07:00
|
|
|
}
|
2014-08-23 22:04:26 -07:00
|
|
|
|
|
|
|
|
// Force-reloads all the websocket-connected displays.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) fieldReloadDisplaysHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !web.userIsAdmin(w, r) {
|
2015-08-22 23:33:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 22:40:08 -07:00
|
|
|
web.arena.ReloadDisplaysNotifier.Notify()
|
2017-09-02 14:08:16 -07:00
|
|
|
http.Redirect(w, r, "/setup/field", 303)
|
2014-08-28 00:12:45 -07:00
|
|
|
}
|
2017-09-03 20:51:20 -07:00
|
|
|
|
2018-08-18 21:01:42 -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
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 22:40:08 -07:00
|
|
|
ws, err := websocket.NewWebsocket(w, r)
|
2018-08-18 21:01:42 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-08-31 22:40:08 -07:00
|
|
|
defer ws.Close()
|
2018-08-18 21:01:42 -07:00
|
|
|
|
2018-09-03 14:33:02 -07:00
|
|
|
// 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))
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-18 21:01:42 -07:00
|
|
|
}
|