2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2014-04-29 13:20:32 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2014-11-21 11:57:26 -08:00
|
|
|
"reflect"
|
2016-11-07 14:55:21 +01:00
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2014-04-29 13:20:32 -07:00
|
|
|
)
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// JSONDevice is a JSON representation of a Device.
|
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-12-31 05:15:52 -08:00
|
|
|
// NewJSONDevice returns a JSONDevice given a Device.
|
2014-11-21 11:57:26 -08:00
|
|
|
func NewJSONDevice(device Device) *JSONDevice {
|
|
|
|
jsonDevice := &JSONDevice{
|
|
|
|
Name: device.Name(),
|
|
|
|
Driver: reflect.TypeOf(device).String(),
|
|
|
|
Commands: []string{},
|
|
|
|
Connection: "",
|
|
|
|
}
|
|
|
|
if device.Connection() != nil {
|
|
|
|
jsonDevice.Connection = device.Connection().Name()
|
|
|
|
}
|
2014-12-03 10:59:19 -08:00
|
|
|
if commander, ok := device.(Commander); ok {
|
|
|
|
for command := range commander.Commands() {
|
|
|
|
jsonDevice.Commands = append(jsonDevice.Commands, command)
|
|
|
|
}
|
2014-11-21 11:57:26 -08:00
|
|
|
}
|
|
|
|
return jsonDevice
|
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// A Device is an instnace of a Driver
|
2014-11-20 18:00:32 -08:00
|
|
|
type Device Driver
|
2014-07-02 17:12:13 -07:00
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Devices represents a collection of Device
|
|
|
|
type Devices []Device
|
2014-06-23 20:33:59 -07:00
|
|
|
|
2014-10-15 12:57:07 -05:00
|
|
|
// Len returns devices length
|
2014-12-31 05:15:52 -08: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-12-31 05:15:52 -08:00
|
|
|
// Each enumerates through the Devices and calls specified callback function.
|
|
|
|
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
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Start calls Start on each Device in d
|
2016-11-07 14:55:21 +01:00
|
|
|
func (d *Devices) Start() (err error) {
|
2014-04-29 13:20:32 -07:00
|
|
|
log.Println("Starting devices...")
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *d {
|
|
|
|
info := "Starting device " + device.Name()
|
2014-11-21 19:35:01 -08:00
|
|
|
|
2014-11-28 19:04:22 -08:00
|
|
|
if pinner, ok := device.(Pinner); ok {
|
|
|
|
info = info + " on pin " + pinner.Pin()
|
2014-07-09 09:38:43 -07:00
|
|
|
}
|
2014-11-21 19:35:01 -08:00
|
|
|
|
2014-07-09 09:38:43 -07:00
|
|
|
log.Println(info + "...")
|
2016-11-07 14:55:21 +01:00
|
|
|
if derr := device.Start(); derr != nil {
|
|
|
|
err = multierror.Append(err, derr)
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 14:55:21 +01:00
|
|
|
return err
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:15:52 -08:00
|
|
|
// Halt calls Halt on each Device in d
|
2016-11-07 14:55:21 +01:00
|
|
|
func (d *Devices) Halt() (err error) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *d {
|
2016-11-07 14:55:21 +01:00
|
|
|
if derr := device.Halt(); derr != nil {
|
|
|
|
err = multierror.Append(err, derr)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2016-11-07 14:55:21 +01:00
|
|
|
return err
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|