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 (
|
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-18 21:01:42 -07:00
|
|
|
"log"
|
2014-08-04 00:52:46 -07:00
|
|
|
"net/http"
|
2018-04-15 14:59:16 -07:00
|
|
|
"strconv"
|
2014-08-04 00:52:46 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
CurrentLedMode led.Mode
|
|
|
|
|
LedModeNames map[led.Mode]string
|
2018-08-19 02:00:50 -07:00
|
|
|
CurrentVaultLedMode vaultled.Mode
|
|
|
|
|
VaultLedModeNames map[vaultled.Mode]string
|
2018-08-18 21:01:42 -07:00
|
|
|
}{web.arena.EventSettings, web.arena.AllianceStationDisplays, plc.GetInputNames(), plc.GetRegisterNames(),
|
2018-08-19 02:00:50 -07:00
|
|
|
plc.GetCoilNames(), web.arena.ScaleLeds.GetCurrentMode(), led.ModeNames,
|
|
|
|
|
web.arena.RedVaultLeds.CurrentForceMode, 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
|
|
|
|
|
web.arena.MatchLoadTeamsNotifier.Notify(nil)
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.ReloadDisplaysNotifier.Notify(nil)
|
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
|
|
|
|
|
|
|
|
// Controls the field LEDs for testing or effect.
|
|
|
|
|
func (web *Web) fieldTestPostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !web.userIsAdmin(w, r) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if web.arena.MatchState != field.PreMatch {
|
|
|
|
|
http.Error(w, "Arena must be in pre-match state", 400)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 14:59:16 -07:00
|
|
|
mode, _ := strconv.Atoi(r.PostFormValue("mode"))
|
2018-05-19 21:42:32 -07:00
|
|
|
ledMode := led.Mode(mode)
|
|
|
|
|
web.arena.ScaleLeds.SetMode(ledMode, ledMode)
|
|
|
|
|
web.arena.RedSwitchLeds.SetMode(ledMode, ledMode)
|
|
|
|
|
web.arena.BlueSwitchLeds.SetMode(ledMode, ledMode)
|
2017-09-03 20:51:20 -07:00
|
|
|
|
2018-08-19 02:00:50 -07:00
|
|
|
vaultMode, _ := strconv.Atoi(r.PostFormValue("vaultMode"))
|
|
|
|
|
vaultLedMode := vaultled.Mode(vaultMode)
|
|
|
|
|
web.arena.RedVaultLeds.SetAllModes(vaultLedMode)
|
|
|
|
|
web.arena.BlueVaultLeds.SetAllModes(vaultLedMode)
|
|
|
|
|
|
2017-09-03 20:51:20 -07:00
|
|
|
http.Redirect(w, r, "/setup/field", 303)
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
websocket, err := NewWebsocket(w, r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer websocket.Close()
|
|
|
|
|
|
|
|
|
|
plcIoChangeListener := web.arena.Plc.IoChangeNotifier.Listen()
|
|
|
|
|
defer close(plcIoChangeListener)
|
|
|
|
|
|
|
|
|
|
// Send the PLC status immediately upon connection.
|
|
|
|
|
data := struct {
|
|
|
|
|
Inputs []bool
|
|
|
|
|
Registers []uint16
|
|
|
|
|
Coils []bool
|
|
|
|
|
}{web.arena.Plc.Inputs[:], web.arena.Plc.Registers[:], web.arena.Plc.Coils[:]}
|
|
|
|
|
err = websocket.Write("plcIoChange", data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Websocket error: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
var messageType string
|
|
|
|
|
var message interface{}
|
|
|
|
|
select {
|
|
|
|
|
case _, ok := <-plcIoChangeListener:
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
messageType = "plcIoChange"
|
|
|
|
|
message = struct {
|
|
|
|
|
Inputs []bool
|
|
|
|
|
Registers []uint16
|
|
|
|
|
Coils []bool
|
|
|
|
|
}{web.arena.Plc.Inputs[:], web.arena.Plc.Registers[:], web.arena.Plc.Coils[:]}
|
|
|
|
|
}
|
|
|
|
|
err = websocket.Write(messageType, message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// The client has probably closed the connection; nothing to do here.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|