added Halt() to a collection of devices

This commit is contained in:
Matt Aimonetti 2014-04-26 21:52:10 -07:00
parent 82b8fd5224
commit 852a7699c1
3 changed files with 16 additions and 13 deletions

View File

@ -5,6 +5,12 @@ import (
"reflect" "reflect"
) )
type Device interface {
Init() bool
Start() bool
Halt() bool
}
type device struct { type device struct {
Name string `json:"name"` Name string `json:"name"`
Type string `json:"driver"` Type string `json:"driver"`
@ -13,10 +19,13 @@ type device struct {
Driver DriverInterface `json:"-"` Driver DriverInterface `json:"-"`
} }
type Device interface { type devices []*device
Init() bool
Start() bool // Halt() stop all the devices.
Halt() bool func (d devices) Halt() {
for _, device := range d {
device.Halt()
}
} }
func NewDevice(driver DriverInterface, r *Robot) *device { func NewDevice(driver DriverInterface, r *Robot) *device {

View File

@ -42,7 +42,7 @@ func (m *Master) Start() {
// waiting on something coming on the channel // waiting on something coming on the channel
_ = <-c _ = <-c
for _, r := range m.Robots { for _, r := range m.Robots {
r.haltDevices() r.GetDevices().Halt()
r.finalizeConnections() r.finalizeConnections()
} }

View File

@ -113,20 +113,14 @@ func (r *Robot) startDevices() bool {
return success return success
} }
func (r *Robot) haltDevices() {
for _, device := range r.devices {
device.Halt()
}
}
func (r *Robot) finalizeConnections() { func (r *Robot) finalizeConnections() {
for _, connection := range r.connections { for _, connection := range r.connections {
connection.Finalize() connection.Finalize()
} }
} }
func (r *Robot) GetDevices() []*device { func (r *Robot) GetDevices() devices {
return r.devices return devices(r.devices)
} }
func (r *Robot) GetDevice(name string) *device { func (r *Robot) GetDevice(name string) *device {