Skip to content

Commit

Permalink
Add additional cmd parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Oct 4, 2019
1 parent 52f263d commit fb76685
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
20 changes: 19 additions & 1 deletion cmd/ssl-game-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package main

import (
"flag"
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/controller"
"github.com/gobuffalo/packr"
"log"
"net/http"
)

var address = flag.String("address", "localhost:8081", "The address on which the UI and API is served")
var visionAddress = flag.String("visionAddress", "", "The address (ip+port) from which vision packages are received")
var publishAddress = flag.String("publishAddress", "", "The address (ip+port) to which referee command should be sent")
var timeAcquisitionMode = flag.String("timeAcquisitionMode", "", "The time acquisitionMode to use (system, ci, vision)")

const configFileName = "config/ssl-game-controller.yaml"

func main() {
flag.Parse()
Expand All @@ -23,7 +29,19 @@ func main() {
}

func setupGameController() {
gameController := controller.NewGameController()
cfg := config.LoadConfig(configFileName)

if visionAddress != nil && *visionAddress != "" {
cfg.Network.VisionAddress = *visionAddress
}
if publishAddress != nil && *publishAddress != "" {
cfg.Network.PublishAddress = *publishAddress
}
if timeAcquisitionMode != nil && *timeAcquisitionMode != "" {
cfg.TimeAcquisitionMode = config.TimeAcquisitionMode(*timeAcquisitionMode)
}

gameController := controller.NewGameController(cfg)
gameController.Run()
// serve the bidirectional web socket
http.HandleFunc("/api/control", gameController.ApiServer.WsHandler)
Expand Down
16 changes: 16 additions & 0 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -196,3 +197,18 @@ func DefaultControllerConfig() (c Controller) {

return
}

// loadConfig loads the controller config
func LoadConfig(configFileName string) Controller {
cfg, err := LoadControllerConfig(configFileName)
if err != nil {
log.Printf("Could not load config: %v", err)
err = cfg.WriteTo(configFileName)
if err != nil {
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
} else {
log.Println("New default config has been written to ", configFileName)
}
}
return cfg
}
23 changes: 3 additions & 20 deletions internal/app/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"time"
)

const configFileName = "config/ssl-game-controller.yaml"

// GameController controls a game
type GameController struct {
Config config.Controller
Expand All @@ -33,11 +31,11 @@ type GameController struct {
VisionReceiver *vision.Receiver
}

// NewGameController creates a new RefBox
func NewGameController() (c *GameController) {
// NewGameController creates a new GameController
func NewGameController(cfg config.Controller) (c *GameController) {

c = new(GameController)
c.Config = loadConfig()
c.Config = cfg
c.Publisher = NewPublisher(c.Config.Network.PublishAddress)
c.ApiServer = ApiServer{}
c.ApiServer.Consumer = c
Expand Down Expand Up @@ -218,18 +216,3 @@ func (c *GameController) OnNewEvent(event Event) {
c.publish()
}
}

// loadConfig loads the controller config
func loadConfig() config.Controller {
cfg, err := config.LoadControllerConfig(configFileName)
if err != nil {
log.Printf("Could not load config: %v", err)
err = cfg.WriteTo(configFileName)
if err != nil {
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
} else {
log.Println("New default config has been written to ", configFileName)
}
}
return cfg
}

0 comments on commit fb76685

Please sign in to comment.