From 31505b265f7090fa57f44fddb2ff7ea07f6c050f Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sun, 22 Mar 2020 17:42:37 -0700 Subject: [PATCH] Add Plc.IsEnabled() method and refactor places that were checking settings directly to use it instead. --- field/arena.go | 2 +- field/arena_test.go | 4 ++-- plc/plc.go | 11 ++++++++--- templates/match_play.html | 2 +- templates/scoring_panel.html | 2 +- web/match_play.go | 3 ++- web/scoring_panel.go | 5 +++-- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/field/arena.go b/field/arena.go index 588035b..3af3b28 100644 --- a/field/arena.go +++ b/field/arena.go @@ -647,7 +647,7 @@ func (arena *Arena) checkCanStartMatch() error { return err } - if arena.EventSettings.PlcAddress != "" { + if arena.Plc.IsEnabled() { if !arena.Plc.IsHealthy { return fmt.Errorf("Cannot start match while PLC is not healthy.") } diff --git a/field/arena_test.go b/field/arena_test.go index aecd3ca..b80a7f1 100644 --- a/field/arena_test.go +++ b/field/arena_test.go @@ -73,12 +73,12 @@ func TestArenaCheckCanStartMatch(t *testing.T) { assert.Nil(t, arena.checkCanStartMatch()) // Check PLC constraints. - arena.EventSettings.PlcAddress = "1.2.3.4" + arena.Plc.SetAddress("1.2.3.4") err = arena.checkCanStartMatch() if assert.NotNil(t, err) { assert.Contains(t, err.Error(), "Cannot start match while PLC is not healthy") } - arena.Plc.IsHealthy = true + arena.Plc.SetAddress("") assert.Nil(t, arena.checkCanStartMatch()) } diff --git a/plc/plc.go b/plc/plc.go index fb010b2..e649c2c 100644 --- a/plc/plc.go +++ b/plc/plc.go @@ -87,11 +87,16 @@ func (plc *Plc) SetAddress(address string) { } } +// Returns true if the PLC is enabled in the configurations. +func (plc *Plc) IsEnabled() bool { + return plc.address != "" +} + // Loops indefinitely to read inputs from and write outputs to PLC. func (plc *Plc) Run() { for { if plc.handler == nil { - if plc.address == "" { + if !plc.IsEnabled() { // No PLC is configured; just allow the loop to continue to simulate inputs and outputs. plc.IsHealthy = false } else { @@ -137,13 +142,13 @@ func (plc *Plc) Run() { // Returns the state of the field emergency stop button (true if e-stop is active). func (plc *Plc) GetFieldEstop() bool { - return plc.address != "" && !plc.inputs[fieldEstop] + return plc.IsEnabled() && !plc.inputs[fieldEstop] } // Returns the state of the red and blue driver station emergency stop buttons (true if e-stop is active). func (plc *Plc) GetTeamEstops() ([3]bool, [3]bool) { var redEstops, blueEstops [3]bool - if plc.address != "" { + if plc.IsEnabled() { redEstops[0] = !plc.inputs[redEstop1] redEstops[1] = !plc.inputs[redEstop2] redEstops[2] = !plc.inputs[redEstop3] diff --git a/templates/match_play.html b/templates/match_play.html index d60f19d..a7c48f2 100644 --- a/templates/match_play.html +++ b/templates/match_play.html @@ -118,7 +118,7 @@

Referee

- {{if .EventSettings.PlcAddress}} + {{if .PlcIsEnabled}}

PLC Status


diff --git a/templates/scoring_panel.html b/templates/scoring_panel.html index c90d0fe..94761e7 100644 --- a/templates/scoring_panel.html +++ b/templates/scoring_panel.html @@ -30,7 +30,7 @@ {{end}} - {{if eq .EventSettings.PlcAddress ""}} + {{if not .PlcIsEnabled}}

diff --git a/web/match_play.go b/web/match_play.go index c1d4a40..5d6abc0 100644 --- a/web/match_play.go +++ b/web/match_play.go @@ -71,12 +71,13 @@ func (web *Web) matchPlayHandler(w http.ResponseWriter, r *http.Request) { isReplay := matchResult != nil data := struct { *model.EventSettings + PlcIsEnabled bool MatchesByType map[string]MatchPlayList CurrentMatchType string Match *model.Match AllowSubstitution bool IsReplay bool - }{web.arena.EventSettings, matchesByType, currentMatchType, web.arena.CurrentMatch, + }{web.arena.EventSettings, web.arena.Plc.IsEnabled(), matchesByType, currentMatchType, web.arena.CurrentMatch, web.arena.CurrentMatch.ShouldAllowSubstitution(), isReplay} err = template.ExecuteTemplate(w, "base", data) if err != nil { diff --git a/web/scoring_panel.go b/web/scoring_panel.go index 0199351..e97e4b9 100644 --- a/web/scoring_panel.go +++ b/web/scoring_panel.go @@ -39,8 +39,9 @@ func (web *Web) scoringPanelHandler(w http.ResponseWriter, r *http.Request) { } data := struct { *model.EventSettings - Alliance string - }{web.arena.EventSettings, alliance} + PlcIsEnabled bool + Alliance string + }{web.arena.EventSettings, web.arena.Plc.IsEnabled(), alliance} err = template.ExecuteTemplate(w, "base_no_navbar", data) if err != nil { handleWebErr(w, err)