From c86123c69454f841469df76c390185f109687c2a Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 9 Jul 2014 09:38:43 -0700 Subject: [PATCH] Clean up devices and connections type --- api/api_test.go | 9 +++++---- connection.go | 23 ++++++++--------------- device.go | 27 ++++++++++++--------------- examples/ardrone.go | 2 +- examples/sphero.go | 3 ++- gobot.go | 5 +++-- gobot_test.go | 10 +++++----- robot.go | 28 ++++++++++++---------------- test_helper.go | 1 + 9 files changed, 49 insertions(+), 59 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 57317a5d3..8c081f8af 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -3,12 +3,13 @@ package api import ( "bytes" "encoding/json" - "github.com/hybridgroup/gobot" "io/ioutil" "log" "net/http" "net/http/httptest" "testing" + + "github.com/hybridgroup/gobot" ) func initTestAPI() *api { @@ -18,9 +19,9 @@ func initTestAPI() *api { a.start = func(m *api) {} a.Start() - g.Robots().Add(gobot.NewTestRobot("Robot 1")) - g.Robots().Add(gobot.NewTestRobot("Robot 2")) - g.Robots().Add(gobot.NewTestRobot("Robot 3")) + g.AddRobot(gobot.NewTestRobot("Robot 1")) + g.AddRobot(gobot.NewTestRobot("Robot 2")) + g.AddRobot(gobot.NewTestRobot("Robot 3")) return a } diff --git a/connection.go b/connection.go index 8c4fd797a..eba6ecda4 100644 --- a/connection.go +++ b/connection.go @@ -13,33 +13,26 @@ type JSONConnection struct { type Connection AdaptorInterface -type connections struct { - connections []Connection -} +type connections []Connection func (c *connections) Len() int { - return len(c.connections) -} - -func (c *connections) Add(a Connection) Connection { - c.connections = append(c.connections, a) - return a + return len(*c) } func (c *connections) Each(f func(Connection)) { - for _, connection := range c.connections { + for _, connection := range *c { f(connection) } } // Start() starts all the connections. -func (c connections) Start() error { +func (c *connections) Start() error { var err error log.Println("Starting connections...") - for _, connection := range c.connections { + for _, connection := range *c { info := "Starting connection " + connection.Name() if connection.Port() != "" { - info = info + " on Port " + connection.Port() + info = info + " on port " + connection.Port() } log.Println(info + "...") if connection.Connect() == false { @@ -51,8 +44,8 @@ func (c connections) Start() error { } // Filanize() finalizes all the connections. -func (c connections) Finalize() { - for _, connection := range c.connections { +func (c *connections) Finalize() { + for _, connection := range *c { connection.Finalize() } } diff --git a/device.go b/device.go index bce0c73f3..ee76c36d2 100644 --- a/device.go +++ b/device.go @@ -14,31 +14,28 @@ type JSONDevice struct { type Device DriverInterface -type devices struct { - devices []Device -} +type devices []Device func (d *devices) Len() int { - return len(d.devices) -} - -func (d *devices) Add(dev Device) Device { - d.devices = append(d.devices, dev) - return dev + return len(*d) } func (d *devices) Each(f func(Device)) { - for _, device := range d.devices { + for _, device := range *d { f(device) } } // Start() starts all the devices. -func (d devices) Start() error { +func (d *devices) Start() error { var err error log.Println("Starting devices...") - for _, device := range d.devices { - log.Println("Starting device " + device.Name() + "...") + for _, device := range *d { + info := "Starting device " + device.Name() + if device.Pin() != "" { + info = info + " on pin " + device.Pin() + } + log.Println(info + "...") if device.Start() == false { err = errors.New("Could not start device") break @@ -48,8 +45,8 @@ func (d devices) Start() error { } // Halt() stop all the devices. -func (d devices) Halt() { - for _, device := range d.devices { +func (d *devices) Halt() { + for _, device := range *d { device.Halt() } } diff --git a/examples/ardrone.go b/examples/ardrone.go index 687e75359..7c13f4497 100644 --- a/examples/ardrone.go +++ b/examples/ardrone.go @@ -13,7 +13,7 @@ func main() { work := func() { drone.TakeOff() - gobot.On(drone.Event("Flying"), func(data interface{}) { + gobot.On(drone.Event("flying"), func(data interface{}) { gobot.After(3*time.Second, func() { drone.Land() }) diff --git a/examples/sphero.go b/examples/sphero.go index b9743b0ec..65b35b428 100644 --- a/examples/sphero.go +++ b/examples/sphero.go @@ -2,9 +2,10 @@ package main import ( "fmt" + "time" + "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/platforms/sphero" - "time" ) func main() { diff --git a/gobot.go b/gobot.go index 93f49c530..1774ab84d 100644 --- a/gobot.go +++ b/gobot.go @@ -59,11 +59,12 @@ func (g *Gobot) Robots() *robots { } func (g *Gobot) AddRobot(r *Robot) *Robot { - return g.Robots().Add(r) + *g.robots = append(*g.robots, r) + return r } func (g *Gobot) Robot(name string) *Robot { - for _, robot := range g.Robots().robots { + for _, robot := range *g.Robots() { if robot.Name == name { return robot } diff --git a/gobot_test.go b/gobot_test.go index 99fa9f245..ca3c80428 100644 --- a/gobot_test.go +++ b/gobot_test.go @@ -12,9 +12,9 @@ func initTestGobot() *Gobot { g.trap = func(c chan os.Signal) { c <- os.Interrupt } - g.Robots().Add(NewTestRobot("Robot 1")) - g.Robots().Add(NewTestRobot("Robot 2")) - g.Robots().Add(NewTestRobot("Robot 3")) + g.AddRobot(NewTestRobot("Robot 1")) + g.AddRobot(NewTestRobot("Robot 2")) + g.AddRobot(NewTestRobot("Robot 3")) return g } @@ -27,9 +27,9 @@ func TestGobotRobot(t *testing.T) { g := initTestGobot() Expect(t, g.Robot("Robot 1").Name, "Robot 1") Expect(t, g.Robot("Robot 4"), (*Robot)(nil)) - Expect(t, g.Robot("Robot 1").Device("Device 4"), (DriverInterface)(nil)) + Expect(t, g.Robot("Robot 1").Device("Device 4"), (Device)(nil)) Expect(t, g.Robot("Robot 1").Device("Device 1").Name(), "Device 1") Expect(t, g.Robot("Robot 1").Devices().Len(), 3) - Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (AdaptorInterface)(nil)) + Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (Connection)(nil)) Expect(t, g.Robot("Robot 1").Connections().Len(), 3) } diff --git a/robot.go b/robot.go index 3728a5427..2c1f994f1 100644 --- a/robot.go +++ b/robot.go @@ -20,26 +20,20 @@ type Robot struct { devices *devices } -type robots struct { - robots []*Robot -} +type robots []*Robot func (r *robots) Len() int { - return len(r.robots) -} -func (r *robots) Add(robot *Robot) *Robot { - r.robots = append(r.robots, robot) - return robot + return len(*r) } func (r *robots) Start() { - for _, robot := range r.robots { + for _, robot := range *r { robot.Start() } } func (r *robots) Each(f func(*Robot)) { - for _, robot := range r.robots { + for _, robot := range *r { f(robot) } } @@ -64,13 +58,13 @@ func NewRobot(name string, v ...interface{}) *Robot { case []Connection: log.Println("Initializing connections...") for _, connection := range v[i].([]Connection) { - c := r.Connections().Add(connection) + c := r.AddConnection(connection) log.Println("Initializing connection", c.Name(), "...") } case []Device: log.Println("Initializing devices...") for _, device := range v[i].([]Device) { - d := r.Devices().Add(device) + d := r.AddDevice(device) log.Println("Initializing device", d.Name(), "...") } case func(): @@ -114,14 +108,15 @@ func (r *Robot) Devices() *devices { } func (r *Robot) AddDevice(d Device) Device { - return r.Devices().Add(d) + *r.devices = append(*r.Devices(), d) + return d } func (r *Robot) Device(name string) Device { if r == nil { return nil } - for _, device := range r.devices.devices { + for _, device := range *r.devices { if device.Name() == name { return device } @@ -134,14 +129,15 @@ func (r *Robot) Connections() *connections { } func (r *Robot) AddConnection(c Connection) Connection { - return r.Connections().Add(c) + *r.connections = append(*r.Connections(), c) + return c } func (r *Robot) Connection(name string) Connection { if r == nil { return nil } - for _, connection := range r.connections.connections { + for _, connection := range *r.connections { if connection.Name() == name { return connection } diff --git a/test_helper.go b/test_helper.go index f4d2c519d..acc4800ce 100644 --- a/test_helper.go +++ b/test_helper.go @@ -41,6 +41,7 @@ func (NullReadWriteCloser) Write(p []byte) (int, error) { func (NullReadWriteCloser) Read(b []byte) (int, error) { return len(b), nil } + func (NullReadWriteCloser) Close() error { return nil }