Skip to content

Commit

Permalink
gobot: rename Master to Manager (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas committed Feb 13, 2024
1 parent 27d0b21 commit c159228
Show file tree
Hide file tree
Showing 33 changed files with 206 additions and 206 deletions.
22 changes: 11 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1647,17 +1647,17 @@
### 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
* DeleteEvent function added to Eventer interface
* 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -2600,7 +2600,7 @@

### Refactor

* robot and master
* robot and manager

### Remove

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2737,11 +2737,11 @@

### Rename

* Gobot struct to Master
* Gobot struct to Manager

### Set

* GOMAXPROCS property in GobotMaster
* GOMAXPROCS property in GobotManager

### Skeleton

Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

Expand All @@ -90,8 +90,8 @@ func main() {
)

if err := robot.Start(); err != nil {
panic(err)
}
panic(err)
}
}
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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)
}
}
```

Expand Down Expand Up @@ -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()
Expand Down
36 changes: 18 additions & 18 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
})
Expand All @@ -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,
)
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -410,22 +410,22 @@ 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
}

return nil, fmt.Errorf("No Device found with the name %s", name)
}

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
}

Expand Down
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {}

Expand Down Expand Up @@ -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")
}()

Expand Down
4 changes: 2 additions & 2 deletions api/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit c159228

Please sign in to comment.