Clean up devices and connections type
This commit is contained in:
parent
7eec236b07
commit
c86123c694
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
27
device.go
27
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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
|
|
|
@ -2,9 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
5
gobot.go
5
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
28
robot.go
28
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue