hybridgroup.gobot/device.go

54 lines
1012 B
Go
Raw Normal View History

2014-04-30 23:10:44 +08:00
package gobot
2014-04-30 04:20:32 +08:00
import (
"errors"
"log"
)
2014-06-11 06:16:11 +08:00
type JSONDevice struct {
2014-05-16 02:50:45 +08:00
Name string `json:"name"`
Driver string `json:"driver"`
2014-06-11 06:16:11 +08:00
Connection *JSONConnection `json:"connection"`
2014-05-16 02:50:45 +08:00
Commands []string `json:"commands"`
}
2014-06-24 11:33:59 +08:00
type devices struct {
devices []DriverInterface
}
func (d *devices) Len() int {
return len(d.devices)
}
func (d *devices) Add(dev DriverInterface) DriverInterface {
d.devices = append(d.devices, dev)
return dev
2014-04-30 04:20:32 +08:00
}
2014-06-24 11:33:59 +08:00
func (d *devices) Each(f func(DriverInterface)) {
for _, device := range d.devices {
f(device)
}
}
2014-04-30 04:20:32 +08:00
// Start() starts all the devices.
func (d devices) Start() error {
var err error
log.Println("Starting devices...")
2014-06-24 11:33:59 +08:00
for _, device := range d.devices {
log.Println("Starting device " + device.name() + "...")
2014-04-30 04:20:32 +08:00
if device.Start() == false {
2014-04-30 23:10:44 +08:00
err = errors.New("Could not start device")
2014-04-30 04:20:32 +08:00
break
}
}
return err
}
// Halt() stop all the devices.
func (d devices) Halt() {
2014-06-24 11:33:59 +08:00
for _, device := range d.devices {
2014-04-30 04:20:32 +08:00
device.Halt()
}
}