Skip to content

Commit fbe4dfe

Browse files
committed
Sample API
1 parent 30b59ed commit fbe4dfe

File tree

4 files changed

+102
-48
lines changed

4 files changed

+102
-48
lines changed

plc/plc.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ type Plc interface {
1818
SetAddress(address string)
1919
IsEnabled() bool
2020
IsHealthy() bool
21-
SetAlternateIOStopState(input int, state bool)
22-
ResetEstops()
2321
IoChangeNotifier() *websocket.Notifier
2422
Run()
2523
GetArmorBlockStatuses() map[string]bool
@@ -42,6 +40,9 @@ type Plc interface {
4240
SetSubwooferCountdown(redState, blueState bool)
4341
SetAmpLights(redLow, redHigh, redCoop, blueLow, blueHigh, blueCoop bool)
4442
SetPostMatchSubwooferLights(state bool)
43+
//Freezy Arena
44+
SetAlternateIOStopState(input int, state bool)
45+
ResetEstops()
4546
}
4647

4748
type ModbusPlc struct {
@@ -166,25 +167,6 @@ func (plc *ModbusPlc) SetAddress(address string) {
166167
}
167168
}
168169

169-
func (plc *ModbusPlc) ResetEstops(){
170-
plc.inputs[red1EStop] = true
171-
plc.inputs[red2EStop] = true
172-
plc.inputs[red3EStop] = true
173-
plc.inputs[blue1EStop] = true
174-
plc.inputs[blue2EStop] = true
175-
plc.inputs[blue3EStop] = true
176-
plc.inputs[red1AStop] = true
177-
plc.inputs[red2AStop] = true
178-
plc.inputs[red3AStop] = true
179-
plc.inputs[blue1AStop] = true
180-
plc.inputs[blue2AStop] = true
181-
plc.inputs[blue3AStop] = true
182-
}
183-
184-
// used for Alternate IO stops
185-
func (plc *ModbusPlc) SetAlternateIOStopState(input int, state bool){
186-
plc.inputs[input] = state
187-
}
188170
// Returns true if the PLC is enabled in the configurations.
189171
func (plc *ModbusPlc) IsEnabled() bool {
190172
return plc.address != ""
@@ -527,3 +509,24 @@ func boolToByte(bools []bool) []byte {
527509
}
528510
return bytes
529511
}
512+
513+
func (plc *ModbusPlc) ResetEstops(){
514+
plc.inputs[fieldEStop] = true
515+
plc.inputs[red1EStop] = true
516+
plc.inputs[red2EStop] = true
517+
plc.inputs[red3EStop] = true
518+
plc.inputs[blue1EStop] = true
519+
plc.inputs[blue2EStop] = true
520+
plc.inputs[blue3EStop] = true
521+
plc.inputs[red1AStop] = true
522+
plc.inputs[red2AStop] = true
523+
plc.inputs[red3AStop] = true
524+
plc.inputs[blue1AStop] = true
525+
plc.inputs[blue2AStop] = true
526+
plc.inputs[blue3AStop] = true
527+
}
528+
529+
// used for Alternate IO stops
530+
func (plc *ModbusPlc) SetAlternateIOStopState(input int, state bool){
531+
plc.inputs[input] = state
532+
}

web/alternateIO.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2018 Team 254. All Rights Reserved.
2+
// Author: pat@patfairbank.com (Patrick Fairbank)
3+
//
4+
// Web handlers for the field monitor display showing robot connection status.
5+
6+
package web
7+
8+
import (
9+
//"github.com/Team254/cheesy-arena/game"
10+
//"github.com/Team254/cheesy-arena/model"
11+
"encoding/json"
12+
"net/http"
13+
14+
)
15+
16+
17+
// RequestPayload represents the structure of the incoming POST data.
18+
type RequestPayload struct {
19+
Channel int `json:"channel"`
20+
State bool `json:"state"`
21+
}
22+
23+
// Renders the field monitor display.
24+
func (web *Web) eStopStatePostHandler(w http.ResponseWriter, r *http.Request) {
25+
// Ensure the request is a POST request.
26+
if r.Method != http.MethodPost {
27+
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
28+
return
29+
}
30+
31+
// Parse the request body.
32+
var payload []RequestPayload
33+
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
34+
http.Error(w, "Invalid request payload", http.StatusBadRequest)
35+
return
36+
}
37+
38+
for _, item := range payload {
39+
web.arena.Plc.SetAlternateIOStopState(item.Channel, item.State)
40+
}
41+
42+
// Respond with success.
43+
w.WriteHeader(http.StatusOK)
44+
w.Write([]byte("eStop state updated successfully."))
45+
46+
}

web/api.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,6 @@ func (web *Web) rankingsApiHandler(w http.ResponseWriter, r *http.Request) {
180180
}
181181
}
182182

183-
184-
// Generates a JSON dump of the arenaStatus, primarily for use by the stack Lights.
185-
func (web *Web) allianceStatusApiHandler(w http.ResponseWriter, r *http.Request) {
186-
// Preload the JSON as a string
187-
var allianceStations = web.arena.AllianceStations
188-
189-
// Iterate through the slice of AllianceStation structs
190-
for i := range allianceStations {
191-
// If the struct has a Team field, remove or clear it
192-
allianceStations[i].Team = nil // Remove Team information
193-
}
194-
jsonData, err := json.Marshal(allianceStations)
195-
if err != nil {
196-
handleWebErr(w, err)
197-
return
198-
}
199-
200-
w.Header().Set("Content-Type", "application/json")
201-
_, err = w.Write(jsonData)
202-
if err != nil {
203-
handleWebErr(w, err)
204-
return
205-
}
206-
}
207-
208183
// Generates a JSON dump of the alliances.
209184
func (web *Web) alliancesApiHandler(w http.ResponseWriter, r *http.Request) {
210185
alliances, err := web.arena.Database.GetAllAlliances()
@@ -361,3 +336,31 @@ func (web *Web) generateBracketSvg(w io.Writer, activeMatch *model.Match) error
361336
}{bracketType, matchups}
362337
return template.ExecuteTemplate(w, "bracket", data)
363338
}
339+
340+
// Generates a JSON dump of the arenaStatus, primarily for use by the stack Lights.
341+
func (web *Web) allianceStatusApiHandler(w http.ResponseWriter, r *http.Request) {
342+
// Preload the JSON as a string
343+
var allianceStations = web.arena.AllianceStations
344+
345+
// Iterate through the slice of AllianceStation structs
346+
for i := range allianceStations {
347+
// If the struct has a Team field, remove or clear it
348+
allianceStations[i].Team = nil // Remove Team information
349+
}
350+
jsonData, err := json.Marshal(allianceStations)
351+
if err != nil {
352+
handleWebErr(w, err)
353+
return
354+
}
355+
356+
w.Header().Set("Content-Type", "application/json")
357+
_, err = w.Write(jsonData)
358+
if err != nil {
359+
handleWebErr(w, err)
360+
return
361+
}
362+
}
363+
364+
func (web *Web) eStopApiHandler(w http.ResponseWriter, r *http.Request) {
365+
366+
}

web/web.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ func (web *Web) newHandler() http.Handler {
136136
mux.HandleFunc("GET /api/bracket/svg", web.bracketSvgApiHandler)
137137
mux.HandleFunc("GET /api/matches/{type}", web.matchesApiHandler)
138138
mux.HandleFunc("GET /api/rankings", web.rankingsApiHandler)
139-
mux.HandleFunc("GET /api/allianceStatus", web.allianceStatusApiHandler)
140139
mux.HandleFunc("GET /api/sponsor_slides", web.sponsorSlidesApiHandler)
141140
mux.HandleFunc("GET /api/teams/{teamId}/avatar", web.teamAvatarsApiHandler)
142141
mux.HandleFunc("GET /display", web.placeholderDisplayHandler)
143-
mux.HandleFunc("GET /field_monitor_help", web.fieldMonitorDisplayHelpHandler)
144142
mux.HandleFunc("GET /display/websocket", web.placeholderDisplayWebsocketHandler)
145143
mux.HandleFunc("GET /displays/alliance_station", web.allianceStationDisplayHandler)
146144
mux.HandleFunc("GET /displays/alliance_station/websocket", web.allianceStationDisplayWebsocketHandler)
@@ -230,6 +228,10 @@ func (web *Web) newHandler() http.Handler {
230228
mux.HandleFunc("GET /setup/teams/generate_wpa_keys", web.teamsGenerateWpaKeysHandler)
231229
mux.HandleFunc("GET /setup/teams/progress", web.teamsUpdateProgressBarHandler)
232230
mux.HandleFunc("GET /setup/teams/refresh", web.teamsRefreshHandler)
231+
//Freezy Arena
232+
mux.HandleFunc("GET /api/allianceStatus", web.allianceStatusApiHandler)
233+
mux.HandleFunc("GET /field_monitor_help", web.fieldMonitorDisplayHelpHandler)
234+
mux.HandleFunc("POST /freezy/eStopState", web.eStopStatePostHandler)
233235
return mux
234236
}
235237

0 commit comments

Comments
 (0)