From c159228f48ec5a0d3891ca6dd50adf1815244d5b Mon Sep 17 00:00:00 2001 From: Thomas Kohler Date: Tue, 13 Feb 2024 16:30:25 +0100 Subject: [PATCH] gobot: rename Master to Manager (#1070) --- CHANGELOG.md | 22 ++++++------ README.md | 44 +++++++++++------------ api/api.go | 36 +++++++++---------- api/api_test.go | 8 ++--- api/doc.go | 4 +-- doc.go | 12 +++---- examples/batty.go | 2 +- examples/bleclient_multiple_generic.go | 8 ++--- examples/bleclient_multiple_info.go | 8 ++--- examples/bleclient_ollie_multiple.go | 8 ++--- examples/digispark_api.go | 8 ++--- examples/edison_button_led_api.go | 8 ++--- examples/firmata_blink_api.go | 8 ++--- examples/firmata_travis.go | 10 +++--- examples/hello_api.go | 10 +++--- examples/hello_api_auth.go | 10 +++--- examples/hello_api_custom.go | 10 +++--- examples/hello_api_video.go | 8 ++--- examples/particle_api.go | 8 ++--- examples/pebble.go | 8 ++--- examples/pebble_accelerometer.go | 8 ++--- examples/serialport_sphero_api.go | 8 ++--- examples/serialport_sphero_calibration.go | 8 ++--- examples/serialport_sphero_conways.go | 6 ++-- examples/serialport_sphero_dpad.go | 8 ++--- examples/serialport_sphero_master.go | 10 +++--- examples/serialport_sphero_multiple.go | 8 ++--- examples/square.go | 8 ++--- examples/square_fire.go | 8 ++--- master.go => manager.go | 38 ++++++++++---------- master_test.go => manager_test.go | 42 +++++++++++----------- platforms/pebble/README.md | 12 +++---- platforms/pebble/doc.go | 8 ++--- 33 files changed, 206 insertions(+), 206 deletions(-) rename master.go => manager.go (67%) rename master_test.go => manager_test.go (82%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5576ac2d..45da5240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1183,7 +1183,7 @@ ### core -* Add Running() methods for Master and Robot and increase test coverage accordingly +* Add Running() methods for Manager and Robot and increase test coverage accordingly ### sysfs @@ -1647,9 +1647,9 @@ ### core * Refactoring to allow 'Metal' development using Gobot packages -* Able to run robots without being part of a Master. +* Able to run robots without being part of a Manager. * Now running all work in separate goroutines -* Rename internal name of Master type +* Rename internal name of Manager type * Refactor events to use channels all the way down. * Eliminate potential race conditions from Events and Every functions * Add Unsubscribe() to Eventer, now Once() works as expected @@ -1657,7 +1657,7 @@ * Ranges over event channels instead of using select * No longer return non-standard slices of errors, instead use hashicorp/go-multierror * Ensure that all drivers have default names -* Now both Robot and Master operate using AutoRun as expected +* Now both Robot and Manager operate using AutoRun as expected * Use canonical import domain of gobot.io for all code * Use time.Sleep unless waiting for a timeout in a select * Uses time.NewTimer() instead of time.After() to be more efficient @@ -2456,7 +2456,7 @@ * Replaced ginkgo/gomega with system testing package * Refactor gobot/robot/device commands * Added Event type -* Replaced Master type with Gobot type +* Replaced Manager type with Gobot type * Every` and `After` now accept `time.Duration` * Removed reflection helper methods @@ -2580,7 +2580,7 @@ * Finalize on SIGINT * Publish function for driver events * device test coverage -* master and robot test coverage +* manager and robot test coverage ### Clean @@ -2600,7 +2600,7 @@ ### Refactor -* robot and master +* robot and manager ### Remove @@ -2636,8 +2636,8 @@ * Travis banner to README * api commands * POST command -* master example -* robot master +* manager example +* robot manager * Sphero example * Digispark to list of supported platforms * helper functions @@ -2737,11 +2737,11 @@ ### Rename -* Gobot struct to Master +* Gobot struct to Manager ### Set -* GOMAXPROCS property in GobotMaster +* GOMAXPROCS property in GobotManager ### Skeleton diff --git a/README.md b/README.md index f4d720fc..dd3e18b3 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,8 @@ func main() { work := func() { gobot.Every(1*time.Second, func() { if err := led.Toggle(); err != nil { - fmt.Println(err) - } + fmt.Println(err) + } }) } @@ -90,8 +90,8 @@ func main() { ) if err := robot.Start(); err != nil { - panic(err) - } + panic(err) + } } ``` @@ -148,26 +148,26 @@ import ( func main() { e := edison.NewAdaptor() if err := e.Connect(); err != nil { - fmt.Println(err) - } + fmt.Println(err) + } led := gpio.NewLedDriver(e, "13") if err := led.Start(); err != nil { - fmt.Println(err) - } + fmt.Println(err) + } for { if err := led.Toggle(); err != nil { - fmt.Println(err) - } + fmt.Println(err) + } time.Sleep(1000 * time.Millisecond) } } ``` -### "Master" Gobot +### "Manager" Gobot -You can also use the full capabilities of the framework aka "Master Gobot" to control swarms of robots or other features +You can also use the full capabilities of the framework aka "Manager Gobot" to control swarms of robots or other features such as the built-in API server. For example: ```go @@ -216,8 +216,8 @@ func NewSwarmBot(port string) *gobot.Robot { } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() spheros := []string{ "/dev/rfcomm0", @@ -227,12 +227,12 @@ func main() { } for _, port := range spheros { - master.AddRobot(NewSwarmBot(port)) + manager.AddRobot(NewSwarmBot(port)) } - if err := master.Start(); err != nil { - panic(err) - } + if err := manager.Start(); err != nil { + panic(err) + } } ``` @@ -416,15 +416,15 @@ device status, and execute device commands. To activate the API, import the `gobot.io/x/gobot/v2/api` package and instantiate the `API` like this: ```go - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() ``` You can also specify the api host and port, and turn on authentication: ```go - master := gobot.NewMaster() - server := api.NewAPI(master) + manager := gobot.NewManager() + server := api.NewAPI(manager) server.Port = "4000" server.AddHandler(api.BasicAuth("gort", "klatuu")) server.Start() diff --git a/api/api.go b/api/api.go index b0de3dbb..0c369fba 100644 --- a/api/api.go +++ b/api/api.go @@ -17,7 +17,7 @@ import ( // API represents an API server type API struct { - master *gobot.Master + manager *gobot.Manager router *pat.PatternServeMux Host string Port string @@ -28,11 +28,11 @@ type API struct { } // NewAPI returns a new api instance -func NewAPI(m *gobot.Master) *API { +func NewAPI(m *gobot.Manager) *API { return &API{ - master: m, - router: pat.New(), - Port: "3000", + manager: m, + router: pat.New(), + Port: "3000", start: func(a *API) { log.Println("Initializing API on " + a.Host + ":" + a.Port + "...") http.Handle("/", a) @@ -196,20 +196,20 @@ func (a *API) robeaux(res http.ResponseWriter, req *http.Request) { // mcp returns MCP route handler. // Writes JSON with gobot representation func (a *API) mcp(res http.ResponseWriter, req *http.Request) { - a.writeJSON(map[string]interface{}{"MCP": gobot.NewJSONMaster(a.master)}, res) + a.writeJSON(map[string]interface{}{"MCP": gobot.NewJSONManager(a.manager)}, res) } // mcpCommands returns commands route handler. // Writes JSON with global commands representation func (a *API) mcpCommands(res http.ResponseWriter, req *http.Request) { - a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONMaster(a.master).Commands}, res) + a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONManager(a.manager).Commands}, res) } // robots returns route handler. // Writes JSON with robots representation func (a *API) robots(res http.ResponseWriter, req *http.Request) { jsonRobots := []*gobot.JSONRobot{} - a.master.Robots().Each(func(r *gobot.Robot) { + a.manager.Robots().Each(func(r *gobot.Robot) { jsonRobots = append(jsonRobots, gobot.NewJSONRobot(r)) }) a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res) @@ -238,7 +238,7 @@ func (a *API) robotCommands(res http.ResponseWriter, req *http.Request) { // robotDevices returns devices route handler. // Writes JSON with robot devices representation func (a *API) robotDevices(res http.ResponseWriter, req *http.Request) { - if robot := a.master.Robot(req.URL.Query().Get(":robot")); robot != nil { + if robot := a.manager.Robot(req.URL.Query().Get(":robot")); robot != nil { jsonDevices := []*gobot.JSONDevice{} robot.Devices().Each(func(d gobot.Device) { jsonDevices = append(jsonDevices, gobot.NewJSONDevice(d)) @@ -268,11 +268,11 @@ func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) { res.Header().Set("Cache-Control", "no-cache") res.Header().Set("Connection", "keep-alive") - device := a.master.Robot(req.URL.Query().Get(":robot")). + device := a.manager.Robot(req.URL.Query().Get(":robot")). Device(req.URL.Query().Get(":device")) //nolint:forcetypeassert // no error return value, so there is no better way - if event := a.master.Robot(req.URL.Query().Get(":robot")). + if event := a.manager.Robot(req.URL.Query().Get(":robot")). Device(req.URL.Query().Get(":device")).(gobot.Eventer). Event(req.URL.Query().Get(":event")); len(event) > 0 { //nolint:forcetypeassert // no error return value, so there is no better way @@ -314,7 +314,7 @@ func (a *API) robotDeviceCommands(res http.ResponseWriter, req *http.Request) { // writes JSON with robot connections representation func (a *API) robotConnections(res http.ResponseWriter, req *http.Request) { jsonConnections := []*gobot.JSONConnection{} - if robot := a.master.Robot(req.URL.Query().Get(":robot")); robot != nil { + if robot := a.manager.Robot(req.URL.Query().Get(":robot")); robot != nil { robot.Connections().Each(func(c gobot.Connection) { jsonConnections = append(jsonConnections, gobot.NewJSONConnection(c)) }) @@ -336,7 +336,7 @@ func (a *API) robotConnection(res http.ResponseWriter, req *http.Request) { // executeMcpCommand calls a global command associated to requested route func (a *API) executeMcpCommand(res http.ResponseWriter, req *http.Request) { - a.executeCommand(a.master.Command(req.URL.Query().Get(":command")), + a.executeCommand(a.manager.Command(req.URL.Query().Get(":command")), res, req, ) @@ -350,7 +350,7 @@ func (a *API) executeRobotDeviceCommand(res http.ResponseWriter, req *http.Reque } else { a.executeCommand( //nolint:forcetypeassert // no error return value, so there is no better way - a.master.Robot(req.URL.Query().Get(":robot")). + a.manager.Robot(req.URL.Query().Get(":robot")). Device(req.URL.Query().Get(":device")).(gobot.Commander). Command(req.URL.Query().Get(":command")), res, @@ -365,7 +365,7 @@ func (a *API) executeRobotCommand(res http.ResponseWriter, req *http.Request) { a.writeJSON(map[string]interface{}{"error": err.Error()}, res) } else { a.executeCommand( - a.master.Robot(req.URL.Query().Get(":robot")). + a.manager.Robot(req.URL.Query().Get(":robot")). Command(req.URL.Query().Get(":command")), res, req, @@ -410,14 +410,14 @@ func (a *API) Debug() { } func (a *API) jsonRobotFor(name string) (*gobot.JSONRobot, error) { - if robot := a.master.Robot(name); robot != nil { + if robot := a.manager.Robot(name); robot != nil { return gobot.NewJSONRobot(robot), nil } return nil, fmt.Errorf("No Robot found with the name %s", name) } func (a *API) jsonDeviceFor(robot string, name string) (*gobot.JSONDevice, error) { - if device := a.master.Robot(robot).Device(name); device != nil { + if device := a.manager.Robot(robot).Device(name); device != nil { return gobot.NewJSONDevice(device), nil } @@ -425,7 +425,7 @@ func (a *API) jsonDeviceFor(robot string, name string) (*gobot.JSONDevice, error } func (a *API) jsonConnectionFor(robot string, name string) (*gobot.JSONConnection, error) { - if connection := a.master.Robot(robot).Connection(name); connection != nil { + if connection := a.manager.Robot(robot).Connection(name); connection != nil { return gobot.NewJSONConnection(connection), nil } diff --git a/api/api_test.go b/api/api_test.go index 895f9416..49ea95f6 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -19,7 +19,7 @@ import ( func initTestAPI() *API { log.SetOutput(NullReadWriteCloser{}) - g := gobot.NewMaster() + g := gobot.NewManager() a := NewAPI(g) a.start = func(m *API) {} a.Start() @@ -38,7 +38,7 @@ func initTestAPI() *API { func TestStartWithoutDefaults(t *testing.T) { log.SetOutput(NullReadWriteCloser{}) - g := gobot.NewMaster() + g := gobot.NewManager() a := NewAPI(g) a.start = func(m *API) {} @@ -396,13 +396,13 @@ func TestRobotDeviceEvent(t *testing.T) { respc <- resp }() - event := a.master.Robot("Robot1"). + event := a.manager.Robot("Robot1"). Device("Device1").(gobot.Eventer). Event("TestEvent") go func() { time.Sleep(time.Millisecond * 5) - a.master.Robot("Robot1"). + a.manager.Robot("Robot1"). Device("Device1").(gobot.Eventer).Publish(event, "event-data") }() diff --git a/api/doc.go b/api/doc.go index a8e04d3b..08fc826a 100644 --- a/api/doc.go +++ b/api/doc.go @@ -13,14 +13,14 @@ Example: ) func main() { - gbot := gobot.NewMaster() + gbot := gobot.NewManager() // Starts the API server on default port 3000 api.NewAPI(gbot).Start() // Accessible via http://localhost:3000/api/commands/say_hello gbot.AddCommand("say_hello", func(params map[string]interface{}) interface{} { - return "Master says hello!" + return "Manager says hello!" }) hello := gbot.AddRobot(gobot.NewRobot("Eve")) diff --git a/doc.go b/doc.go index b24ca673..633a086e 100644 --- a/doc.go +++ b/doc.go @@ -76,9 +76,9 @@ pure idiomatic Golang code. For example: } } -# Master Gobot +# Manager Gobot -Finally, you can use Master Gobot to add the complete Gobot API or control swarms of Robots: +Finally, you can use Manager Gobot to add the complete Gobot API or control swarms of Robots: package main @@ -125,8 +125,8 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() spheros := []string{ "/dev/rfcomm0", @@ -136,10 +136,10 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm } for _, port := range spheros { - master.AddRobot(NewSwarmBot(port)) + manager.AddRobot(NewSwarmBot(port)) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/batty.go b/examples/batty.go index b5237c9f..398d544e 100644 --- a/examples/batty.go +++ b/examples/batty.go @@ -15,7 +15,7 @@ import ( ) func main() { - gbot := gobot.NewMaster() + gbot := gobot.NewManager() api.NewAPI(gbot).Start() diff --git a/examples/bleclient_multiple_generic.go b/examples/bleclient_multiple_generic.go index c09b4e3e..cbccf564 100644 --- a/examples/bleclient_multiple_generic.go +++ b/examples/bleclient_multiple_generic.go @@ -53,15 +53,15 @@ func NewSwarmBot(port string) *gobot.Robot { } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() for _, port := range os.Args[1:] { bot := NewSwarmBot(port) - master.AddRobot(bot) + manager.AddRobot(bot) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/bleclient_multiple_info.go b/examples/bleclient_multiple_info.go index f76f5b0a..11afb275 100644 --- a/examples/bleclient_multiple_info.go +++ b/examples/bleclient_multiple_info.go @@ -71,15 +71,15 @@ func NewSwarmBot(port string) *gobot.Robot { } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() for _, port := range os.Args[1:] { bot := NewSwarmBot(port) - master.AddRobot(bot) + manager.AddRobot(bot) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/bleclient_ollie_multiple.go b/examples/bleclient_ollie_multiple.go index 90a06d71..5195e6f8 100644 --- a/examples/bleclient_ollie_multiple.go +++ b/examples/bleclient_ollie_multiple.go @@ -47,15 +47,15 @@ func NewSwarmBot(port string) *gobot.Robot { } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() for _, port := range os.Args[1:] { bot := NewSwarmBot(port) - master.AddRobot(bot) + manager.AddRobot(bot) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/digispark_api.go b/examples/digispark_api.go index b409ba8f..1930ff73 100644 --- a/examples/digispark_api.go +++ b/examples/digispark_api.go @@ -14,8 +14,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() digisparkAdaptor := digispark.NewAdaptor() led := gpio.NewLedDriver(digisparkAdaptor, "0") @@ -25,9 +25,9 @@ func main() { []gobot.Device{led}, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/edison_button_led_api.go b/examples/edison_button_led_api.go index 70dad36d..167c7974 100644 --- a/examples/edison_button_led_api.go +++ b/examples/edison_button_led_api.go @@ -16,8 +16,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() e := edison.NewAdaptor() @@ -43,9 +43,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/firmata_blink_api.go b/examples/firmata_blink_api.go index 9d8c027c..46a51cbf 100644 --- a/examples/firmata_blink_api.go +++ b/examples/firmata_blink_api.go @@ -17,8 +17,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Start() firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0") @@ -38,9 +38,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/firmata_travis.go b/examples/firmata_travis.go index c8f16ff8..1cd3dc4b 100644 --- a/examples/firmata_travis.go +++ b/examples/firmata_travis.go @@ -88,16 +88,16 @@ func checkTravis(robot *gobot.Robot) { } func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() firmataAdaptor := firmata.NewAdaptor(os.Args[1]) red := gpio.NewLedDriver(firmataAdaptor, "7", gpio.WithName("red")) green := gpio.NewLedDriver(firmataAdaptor, "6", gpio.WithName("green")) blue := gpio.NewLedDriver(firmataAdaptor, "5", gpio.WithName("blue")) work := func() { - checkTravis(master.Robot("travis")) + checkTravis(manager.Robot("travis")) gobot.Every(10*time.Second, func() { - checkTravis(master.Robot("travis")) + checkTravis(manager.Robot("travis")) }) } @@ -107,8 +107,8 @@ func main() { work, ) - master.AddRobot(robot) - if err := master.Start(); err != nil { + manager.AddRobot(robot) + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/hello_api.go b/examples/hello_api.go index 9079343b..0cb62678 100644 --- a/examples/hello_api.go +++ b/examples/hello_api.go @@ -16,27 +16,27 @@ import ( ) func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() - a := api.NewAPI(master) + a := api.NewAPI(manager) a.AddHandler(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path)) }) a.Debug() a.Start() - master.AddCommand("custom_gobot_command", + manager.AddCommand("custom_gobot_command", func(params map[string]interface{}) interface{} { return "This command is attached to the mcp!" }) - hello := master.AddRobot(gobot.NewRobot("hello")) + hello := manager.AddRobot(gobot.NewRobot("hello")) hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} { return fmt.Sprintf("This command is attached to the robot %v", hello.Name) }) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/hello_api_auth.go b/examples/hello_api_auth.go index a4fa3249..6afb7ff6 100644 --- a/examples/hello_api_auth.go +++ b/examples/hello_api_auth.go @@ -16,9 +16,9 @@ import ( ) func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() - a := api.NewAPI(master) + a := api.NewAPI(manager) a.AddHandler(api.BasicAuth("gort", "klatuu")) a.Debug() @@ -27,18 +27,18 @@ func main() { }) a.Start() - master.AddCommand("custom_gobot_command", + manager.AddCommand("custom_gobot_command", func(params map[string]interface{}) interface{} { return "This command is attached to the mcp!" }) - hello := master.AddRobot(gobot.NewRobot("hello")) + hello := manager.AddRobot(gobot.NewRobot("hello")) hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} { return fmt.Sprintf("This command is attached to the robot %v", hello.Name) }) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/hello_api_custom.go b/examples/hello_api_custom.go index 895165ec..a1eff0b9 100644 --- a/examples/hello_api_custom.go +++ b/examples/hello_api_custom.go @@ -15,9 +15,9 @@ import ( ) func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() - a := api.NewAPI(master) + a := api.NewAPI(manager) // creates routes/handlers for the custom API a.Get("/", func(res http.ResponseWriter, req *http.Request) { @@ -26,7 +26,7 @@ func main() { } }) a.Get("/api/hello", func(res http.ResponseWriter, req *http.Request) { - msg := fmt.Sprintf("This command is attached to the robot %v", master.Robot("hello").Name) + msg := fmt.Sprintf("This command is attached to the robot %v", manager.Robot("hello").Name) if _, err := res.Write([]byte(msg)); err != nil { fmt.Println(err) } @@ -35,9 +35,9 @@ func main() { // starts the API without the default C2PIO API and Robeaux web interface. a.StartWithoutDefaults() - master.AddRobot(gobot.NewRobot("hello")) + manager.AddRobot(gobot.NewRobot("hello")) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/hello_api_video.go b/examples/hello_api_video.go index dbcf73e1..469e661c 100644 --- a/examples/hello_api_video.go +++ b/examples/hello_api_video.go @@ -28,9 +28,9 @@ func main() { // parse args deviceID := os.Args[1] - master := gobot.NewMaster() + manager := gobot.NewManager() - a := api.NewAPI(master) + a := api.NewAPI(manager) // add the standard C3PIO API routes manually. a.AddC3PIORoutes() @@ -40,7 +40,7 @@ func main() { // means the REST API will be available, but not the web interface. a.StartWithoutDefaults() - hello := master.AddRobot(gobot.NewRobot("hello")) + hello := manager.AddRobot(gobot.NewRobot("hello")) hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} { return fmt.Sprintf("This command is attached to the robot %v", hello.Name) @@ -61,7 +61,7 @@ func main() { // start capturing go mjpegCapture() - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/particle_api.go b/examples/particle_api.go index ee267d07..0dbc30b7 100644 --- a/examples/particle_api.go +++ b/examples/particle_api.go @@ -25,8 +25,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() core := particle.NewAdaptor(os.Args[1], os.Args[2]) led := gpio.NewLedDriver(core, "D7") @@ -45,9 +45,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/pebble.go b/examples/pebble.go index 3bbe1bed..80a48146 100644 --- a/examples/pebble.go +++ b/examples/pebble.go @@ -15,8 +15,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api := api.NewAPI(master) + manager := gobot.NewManager() + api := api.NewAPI(manager) api.Port = "8080" api.Start() @@ -40,9 +40,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/pebble_accelerometer.go b/examples/pebble_accelerometer.go index 632989ed..4887a40e 100644 --- a/examples/pebble_accelerometer.go +++ b/examples/pebble_accelerometer.go @@ -15,8 +15,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Port = "8080" a.Start() @@ -35,9 +35,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_api.go b/examples/serialport_sphero_api.go index a6f2f3df..c12cd9f4 100644 --- a/examples/serialport_sphero_api.go +++ b/examples/serialport_sphero_api.go @@ -14,8 +14,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() spheros := map[string]string{ "Sphero-BPO": "/dev/rfcomm0", @@ -39,10 +39,10 @@ func main() { return nil }) - master.AddRobot(robot) + manager.AddRobot(robot) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_calibration.go b/examples/serialport_sphero_calibration.go index ee3f46bd..a4442fc2 100644 --- a/examples/serialport_sphero_calibration.go +++ b/examples/serialport_sphero_calibration.go @@ -15,8 +15,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Start() ballConn := serialport.NewAdaptor("/dev/rfcomm0") @@ -62,9 +62,9 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_conways.go b/examples/serialport_sphero_conways.go index 47af5996..73e97479 100644 --- a/examples/serialport_sphero_conways.go +++ b/examples/serialport_sphero_conways.go @@ -25,7 +25,7 @@ type conway struct { } func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() spheros := []string{ "/dev/rfcomm0", @@ -67,10 +67,10 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_dpad.go b/examples/serialport_sphero_dpad.go index a6206dec..111b0115 100644 --- a/examples/serialport_sphero_dpad.go +++ b/examples/serialport_sphero_dpad.go @@ -16,8 +16,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Start() conn := serialport.NewAdaptor("/dev/rfcomm0") @@ -47,9 +47,9 @@ func main() { return "ok" }) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_master.go b/examples/serialport_sphero_master.go index a4c0e42e..793858b3 100644 --- a/examples/serialport_sphero_master.go +++ b/examples/serialport_sphero_master.go @@ -15,7 +15,7 @@ import ( ) func main() { - master := gobot.NewMaster() + manager := gobot.NewManager() spheros := map[string]string{ "Sphero-BPO": "/dev/rfcomm0", @@ -35,13 +35,13 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) } robot := gobot.NewRobot("", func() { gobot.Every(1*time.Second, func() { - sphero := master.Robot("Sphero-BPO").Device("sphero").(*sphero.SpheroDriver) + sphero := manager.Robot("Sphero-BPO").Device("sphero").(*sphero.SpheroDriver) sphero.SetRGB(uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), @@ -50,9 +50,9 @@ func main() { }, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/serialport_sphero_multiple.go b/examples/serialport_sphero_multiple.go index 65fef75d..12058f56 100644 --- a/examples/serialport_sphero_multiple.go +++ b/examples/serialport_sphero_multiple.go @@ -50,8 +50,8 @@ func NewSwarmBot(port string) *gobot.Robot { } func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() spheros := []string{ "/dev/rfcomm0", @@ -61,10 +61,10 @@ func main() { } for _, port := range spheros { - master.AddRobot(NewSwarmBot(port)) + manager.AddRobot(NewSwarmBot(port)) } - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/square.go b/examples/square.go index 7a6c3fc3..5c99fb3b 100644 --- a/examples/square.go +++ b/examples/square.go @@ -17,8 +17,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Start() board := edison.NewAdaptor() @@ -103,9 +103,9 @@ func main() { return enabled }) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/examples/square_fire.go b/examples/square_fire.go index 76980069..6fa2eb5f 100644 --- a/examples/square_fire.go +++ b/examples/square_fire.go @@ -17,8 +17,8 @@ import ( ) func main() { - master := gobot.NewMaster() - a := api.NewAPI(master) + manager := gobot.NewManager() + a := api.NewAPI(manager) a.Start() board := edison.NewAdaptor() @@ -103,9 +103,9 @@ func main() { return enabled }) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } } diff --git a/master.go b/manager.go similarity index 67% rename from master.go rename to manager.go index 6119d6d1..eb0322f7 100644 --- a/master.go +++ b/manager.go @@ -6,15 +6,15 @@ import ( "sync/atomic" ) -// JSONMaster is a JSON representation of a Gobot Master. -type JSONMaster struct { +// JSONManager is a JSON representation of a Gobot Manager. +type JSONManager struct { Robots []*JSONRobot `json:"robots"` Commands []string `json:"commands"` } -// NewJSONMaster returns a JSONMaster given a Gobot Master. -func NewJSONMaster(gobot *Master) *JSONMaster { - jsonGobot := &JSONMaster{ +// NewJSONManager returns a JSONManager given a Gobot Manager. +func NewJSONManager(gobot *Manager) *JSONManager { + jsonGobot := &JSONManager{ Robots: []*JSONRobot{}, Commands: []string{}, } @@ -29,9 +29,9 @@ func NewJSONMaster(gobot *Master) *JSONMaster { return jsonGobot } -// Master is the main type of your Gobot application and contains a collection of -// Robots, API commands that apply to the Master, and Events that apply to the Master. -type Master struct { +// Manager is the main type of your Gobot application and contains a collection of +// Robots, API commands that apply to the Manager, and Events that apply to the Manager. +type Manager struct { robots *Robots trap func(chan os.Signal) AutoRun bool @@ -40,9 +40,9 @@ type Master struct { Eventer } -// NewMaster returns a new Gobot Master -func NewMaster() *Master { - m := &Master{ +// NewManager returns a new Gobot Manager +func NewManager() *Manager { + m := &Manager{ robots: &Robots{}, trap: func(c chan os.Signal) { signal.Notify(c, os.Interrupt) @@ -58,7 +58,7 @@ func NewMaster() *Master { // Start calls the Start method on each robot in its collection of robots. On // error, call Stop to ensure that all robots are returned to a sane, stopped // state. -func (g *Master) Start() error { +func (g *Manager) Start() error { if err := g.robots.Start(!g.AutoRun); err != nil { return err } @@ -80,31 +80,31 @@ func (g *Master) Start() error { } // Stop calls the Stop method on each robot in its collection of robots. -func (g *Master) Stop() error { +func (g *Manager) Stop() error { err := g.robots.Stop() g.running.Store(false) return err } -// Running returns if the Master is currently started or not -func (g *Master) Running() bool { +// Running returns if the Manager is currently started or not +func (g *Manager) Running() bool { return g.running.Load().(bool) //nolint:forcetypeassert // no error return value, so there is no better way } -// Robots returns all robots associated with this Gobot Master. -func (g *Master) Robots() *Robots { +// Robots returns all robots associated with this Gobot Manager. +func (g *Manager) Robots() *Robots { return g.robots } // AddRobot adds a new robot to the internal collection of robots. Returns the // added robot -func (g *Master) AddRobot(r *Robot) *Robot { +func (g *Manager) AddRobot(r *Robot) *Robot { *g.robots = append(*g.robots, r) return r } // Robot returns a robot given name. Returns nil if the Robot does not exist. -func (g *Master) Robot(name string) *Robot { +func (g *Manager) Robot(name string) *Robot { for _, robot := range *g.Robots() { if robot.Name == name { return robot diff --git a/master_test.go b/manager_test.go similarity index 82% rename from master_test.go rename to manager_test.go index 6ac1ddca..f9525fe3 100644 --- a/master_test.go +++ b/manager_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/require" ) -func initTestMaster() *Master { +func initTestManager() *Manager { log.SetOutput(&NullReadWriteCloser{}) - g := NewMaster() + g := NewManager() g.trap = func(c chan os.Signal) { c <- os.Interrupt } @@ -25,9 +25,9 @@ func initTestMaster() *Master { return g } -func initTestMaster1Robot() *Master { +func initTestManager1Robot() *Manager { log.SetOutput(&NullReadWriteCloser{}) - g := NewMaster() + g := NewManager() g.trap = func(c chan os.Signal) { c <- os.Interrupt } @@ -45,8 +45,8 @@ func TestNullReadWriteCloser(t *testing.T) { require.NoError(t, n.Close()) } -func TestMasterRobot(t *testing.T) { - g := initTestMaster() +func TestManagerRobot(t *testing.T) { + g := initTestManager() assert.Equal(t, "Robot1", g.Robot("Robot1").Name) assert.Equal(t, (*Robot)(nil), g.Robot("Robot4")) assert.Equal(t, (Device)(nil), g.Robot("Robot4").Device("Device1")) @@ -58,25 +58,25 @@ func TestMasterRobot(t *testing.T) { assert.Equal(t, 3, g.Robot("Robot1").Connections().Len()) } -func TestMasterToJSON(t *testing.T) { - g := initTestMaster() +func TestManagerToJSON(t *testing.T) { + g := initTestManager() g.AddCommand("test_function", func(params map[string]interface{}) interface{} { return nil }) - json := NewJSONMaster(g) + json := NewJSONManager(g) assert.Len(t, json.Robots, g.Robots().Len()) assert.Len(t, json.Commands, len(g.Commands())) } -func TestMasterStart(t *testing.T) { - g := initTestMaster() +func TestManagerStart(t *testing.T) { + g := initTestManager() require.NoError(t, g.Start()) require.NoError(t, g.Stop()) assert.False(t, g.Running()) } -func TestMasterStartAutoRun(t *testing.T) { - g := NewMaster() +func TestManagerStartAutoRun(t *testing.T) { + g := NewManager() g.AddRobot(newTestRobot("Robot99")) go func() { _ = g.Start() }() time.Sleep(10 * time.Millisecond) @@ -87,8 +87,8 @@ func TestMasterStartAutoRun(t *testing.T) { assert.False(t, g.Running()) } -func TestMasterStartDriverErrors(t *testing.T) { - g := initTestMaster1Robot() +func TestManagerStartDriverErrors(t *testing.T) { + g := initTestManager1Robot() e := errors.New("driver start error 1") testDriverStart = func() error { return e @@ -105,8 +105,8 @@ func TestMasterStartDriverErrors(t *testing.T) { testDriverStart = func() error { return nil } } -func TestMasterHaltFromRobotDriverErrors(t *testing.T) { - g := initTestMaster1Robot() +func TestManagerHaltFromRobotDriverErrors(t *testing.T) { + g := initTestManager1Robot() var ec int testDriverHalt = func() error { ec++ @@ -123,8 +123,8 @@ func TestMasterHaltFromRobotDriverErrors(t *testing.T) { assert.Equal(t, want, g.Start()) } -func TestMasterStartRobotAdaptorErrors(t *testing.T) { - g := initTestMaster1Robot() +func TestManagerStartRobotAdaptorErrors(t *testing.T) { + g := initTestManager1Robot() var ec int testAdaptorConnect = func() error { ec++ @@ -144,8 +144,8 @@ func TestMasterStartRobotAdaptorErrors(t *testing.T) { testAdaptorConnect = func() error { return nil } } -func TestMasterFinalizeErrors(t *testing.T) { - g := initTestMaster1Robot() +func TestManagerFinalizeErrors(t *testing.T) { + g := initTestManager1Robot() var ec int testAdaptorFinalize = func() error { ec++ diff --git a/platforms/pebble/README.md b/platforms/pebble/README.md index c12e3500..10f43117 100644 --- a/platforms/pebble/README.md +++ b/platforms/pebble/README.md @@ -29,8 +29,8 @@ import ( ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() pebbleAdaptor := pebble.NewAdaptor() watch := pebble.NewDriver(pebbleAdaptor) @@ -52,11 +52,11 @@ func main() { work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { - panic(err) - } + if err := manager.Start(); err != nil { + panic(err) + } } ``` diff --git a/platforms/pebble/doc.go b/platforms/pebble/doc.go index 30805653..1c95fd21 100644 --- a/platforms/pebble/doc.go +++ b/platforms/pebble/doc.go @@ -24,8 +24,8 @@ your computer IP, robot name is 'pebble' and robot api port is 8080 ) func main() { - master := gobot.NewMaster() - api.NewAPI(master).Start() + manager := gobot.NewManager() + api.NewAPI(manager).Start() pebbleAdaptor := pebble.NewAdaptor() watch := pebble.NewDriver(pebbleAdaptor) @@ -47,9 +47,9 @@ your computer IP, robot name is 'pebble' and robot api port is 8080 work, ) - master.AddRobot(robot) + manager.AddRobot(robot) - if err := master.Start(); err != nil { + if err := manager.Start(); err != nil { panic(err) } }