2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-06-10 15:16:11 -07:00
|
|
|
type JSONDevice struct {
|
2014-07-24 16:39:27 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Driver string `json:"driver"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
Commands []string `json:"commands"`
|
2014-05-15 11:50:45 -07:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:12:13 -07:00
|
|
|
type Device DriverInterface
|
|
|
|
|
2014-07-09 09:38:43 -07:00
|
|
|
type devices []Device
|
2014-06-23 20:33:59 -07:00
|
|
|
|
|
|
|
func (d *devices) Len() int {
|
2014-07-09 09:38:43 -07:00
|
|
|
return len(*d)
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:12:13 -07:00
|
|
|
func (d *devices) Each(f func(Device)) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *d {
|
2014-06-23 20:33:59 -07:00
|
|
|
f(device)
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
// Start() starts all the devices.
|
2014-07-09 09:38:43 -07:00
|
|
|
func (d *devices) Start() error {
|
2014-04-29 13:20:32 -07:00
|
|
|
var err error
|
|
|
|
log.Println("Starting devices...")
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *d {
|
|
|
|
info := "Starting device " + device.Name()
|
|
|
|
if device.Pin() != "" {
|
|
|
|
info = info + " on pin " + device.Pin()
|
|
|
|
}
|
|
|
|
log.Println(info + "...")
|
2014-04-29 13:20:32 -07:00
|
|
|
if device.Start() == false {
|
2014-04-30 08:10:44 -07:00
|
|
|
err = errors.New("Could not start device")
|
2014-04-29 13:20:32 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt() stop all the devices.
|
2014-07-09 09:38:43 -07:00
|
|
|
func (d *devices) Halt() {
|
|
|
|
for _, device := range *d {
|
2014-04-29 13:20:32 -07:00
|
|
|
device.Halt()
|
|
|
|
}
|
|
|
|
}
|