Skip to content

Commit

Permalink
Clean up devices and connections type
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 9, 2014
1 parent 7eec236 commit c86123c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 59 deletions.
9 changes: 5 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
23 changes: 8 additions & 15 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
27 changes: 12 additions & 15 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}
2 changes: 1 addition & 1 deletion examples/ardrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
3 changes: 2 additions & 1 deletion examples/sphero.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions gobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
28 changes: 12 additions & 16 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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():
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c86123c

Please sign in to comment.