2014-04-30 23:10:44 +08:00
|
|
|
package gobot
|
2013-10-23 07:45:31 +08:00
|
|
|
|
|
|
|
import (
|
2013-11-14 12:44:54 +08:00
|
|
|
"fmt"
|
2013-12-31 14:04:23 +08:00
|
|
|
"log"
|
2013-10-23 07:45:31 +08:00
|
|
|
)
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// JSONRobot a JSON representation of a Robot.
|
2014-06-11 06:16:11 +08:00
|
|
|
type JSONRobot struct {
|
2014-05-16 02:50:45 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Commands []string `json:"commands"`
|
2014-06-11 06:16:11 +08:00
|
|
|
Connections []*JSONConnection `json:"connections"`
|
|
|
|
Devices []*JSONDevice `json:"devices"`
|
2014-05-16 02:50:45 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// NewJSONRobot returns a JSONRobot given a Robot.
|
2014-11-22 03:57:26 +08:00
|
|
|
func NewJSONRobot(robot *Robot) *JSONRobot {
|
|
|
|
jsonRobot := &JSONRobot{
|
|
|
|
Name: robot.Name,
|
|
|
|
Commands: []string{},
|
|
|
|
Connections: []*JSONConnection{},
|
|
|
|
Devices: []*JSONDevice{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for command := range robot.Commands() {
|
|
|
|
jsonRobot.Commands = append(jsonRobot.Commands, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
robot.Devices().Each(func(device Device) {
|
|
|
|
jsonDevice := NewJSONDevice(device)
|
|
|
|
jsonRobot.Connections = append(jsonRobot.Connections, NewJSONConnection(robot.Connection(jsonDevice.Connection)))
|
|
|
|
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
|
|
|
|
})
|
|
|
|
return jsonRobot
|
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Robot is a named entitity that manages a collection of connections and devices.
|
|
|
|
// It containes it's own work routine and a collection of
|
|
|
|
// custom commands to control a robot remotely via the Gobot api.
|
2013-10-23 07:45:31 +08:00
|
|
|
type Robot struct {
|
2014-06-16 08:22:50 +08:00
|
|
|
Name string
|
|
|
|
Work func()
|
2014-12-31 21:15:52 +08:00
|
|
|
connections *Connections
|
|
|
|
devices *Devices
|
2014-11-21 10:00:32 +08:00
|
|
|
Commander
|
|
|
|
Eventer
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Robots is a collection of Robot
|
|
|
|
type Robots []*Robot
|
2014-06-24 11:33:59 +08:00
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Len returns the amount of Robots in the collection.
|
|
|
|
func (r *Robots) Len() int {
|
2014-07-10 00:38:43 +08:00
|
|
|
return len(*r)
|
2014-06-24 11:33:59 +08:00
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Start calls the Start method of each Robot in the collection
|
|
|
|
func (r *Robots) Start() (errs []error) {
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, robot := range *r {
|
2014-11-20 15:21:19 +08:00
|
|
|
if errs = robot.Start(); len(errs) > 0 {
|
|
|
|
for i, err := range errs {
|
2014-12-31 21:15:52 +08:00
|
|
|
errs[i] = fmt.Errorf("Robot %q: %v", robot.Name, err)
|
2014-11-20 15:21:19 +08:00
|
|
|
}
|
2014-11-18 08:23:19 +08:00
|
|
|
return
|
2014-11-13 03:21:50 +08:00
|
|
|
}
|
2014-04-27 00:44:26 +08:00
|
|
|
}
|
2014-11-18 08:23:19 +08:00
|
|
|
return
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
2014-04-27 00:44:26 +08:00
|
|
|
|
2015-07-22 04:20:02 +08:00
|
|
|
// Stop calls the Stop method of each Robot in the collection
|
|
|
|
func (r *Robots) Stop() (errs []error) {
|
|
|
|
for _, robot := range *r {
|
|
|
|
if errs = robot.Stop(); len(errs) > 0 {
|
|
|
|
for i, err := range errs {
|
|
|
|
errs[i] = fmt.Errorf("Robot %q: %v", robot.Name, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Each enumerates through the Robots and calls specified callback function.
|
|
|
|
func (r *Robots) Each(f func(*Robot)) {
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, robot := range *r {
|
2014-04-30 04:20:32 +08:00
|
|
|
f(robot)
|
|
|
|
}
|
2013-12-19 15:50:42 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// NewRobot returns a new Robot given a name and optionally accepts:
|
|
|
|
//
|
|
|
|
// []Connection: Connections which are automatically started and stopped with the robot
|
|
|
|
// []Device: Devices which are automatically started and stopped with the robot
|
|
|
|
// func(): The work routine the robot will execute once all devices and connections have been initialized and started
|
2014-12-31 21:15:52 +08:00
|
|
|
// A name will be automaically generated if no name is supplied.
|
2014-06-13 12:32:38 +08:00
|
|
|
func NewRobot(name string, v ...interface{}) *Robot {
|
2014-06-15 09:49:02 +08:00
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
|
|
|
|
}
|
2014-07-04 10:52:31 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
r := &Robot{
|
2014-06-14 00:37:34 +08:00
|
|
|
Name: name,
|
2014-12-31 21:15:52 +08:00
|
|
|
connections: &Connections{},
|
|
|
|
devices: &Devices{},
|
2014-07-04 10:52:31 +08:00
|
|
|
Work: nil,
|
2014-11-21 10:00:32 +08:00
|
|
|
Eventer: NewEventer(),
|
|
|
|
Commander: NewCommander(),
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
2014-06-15 09:49:02 +08:00
|
|
|
|
2014-04-30 23:10:44 +08:00
|
|
|
log.Println("Initializing Robot", r.Name, "...")
|
2014-07-04 10:52:31 +08:00
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
switch v[i].(type) {
|
|
|
|
case []Connection:
|
|
|
|
log.Println("Initializing connections...")
|
|
|
|
for _, connection := range v[i].([]Connection) {
|
2014-07-10 00:38:43 +08:00
|
|
|
c := r.AddConnection(connection)
|
2014-07-04 10:52:31 +08:00
|
|
|
log.Println("Initializing connection", c.Name(), "...")
|
|
|
|
}
|
|
|
|
case []Device:
|
|
|
|
log.Println("Initializing devices...")
|
|
|
|
for _, device := range v[i].([]Device) {
|
2014-07-10 00:38:43 +08:00
|
|
|
d := r.AddDevice(device)
|
2014-07-04 10:52:31 +08:00
|
|
|
log.Println("Initializing device", d.Name(), "...")
|
|
|
|
}
|
|
|
|
case func():
|
|
|
|
r.Work = v[i].(func())
|
2014-06-13 12:32:38 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-04 10:52:31 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Start a Robot's Connections, Devices, and work.
|
2014-11-20 15:21:19 +08:00
|
|
|
func (r *Robot) Start() (errs []error) {
|
2014-04-30 23:10:44 +08:00
|
|
|
log.Println("Starting Robot", r.Name, "...")
|
2014-11-20 15:21:19 +08:00
|
|
|
if cerrs := r.Connections().Start(); len(cerrs) > 0 {
|
|
|
|
errs = append(errs, cerrs...)
|
2014-11-18 08:23:19 +08:00
|
|
|
return
|
2013-12-31 08:51:21 +08:00
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
if derrs := r.Devices().Start(); len(derrs) > 0 {
|
|
|
|
errs = append(errs, derrs...)
|
|
|
|
return
|
2013-12-31 08:51:21 +08:00
|
|
|
}
|
2013-12-04 07:51:17 +08:00
|
|
|
if r.Work != nil {
|
2014-04-30 23:10:44 +08:00
|
|
|
log.Println("Starting work...")
|
2013-12-04 07:51:17 +08:00
|
|
|
r.Work()
|
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 04:20:02 +08:00
|
|
|
// Stop stops a Robot's connections and Devices
|
|
|
|
func (r *Robot) Stop() (errs []error) {
|
|
|
|
log.Println("Stopping Robot", r.Name, "...")
|
|
|
|
if heers := r.Devices().Halt(); len(heers) > 0 {
|
|
|
|
for _, err := range heers {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ceers := r.Connections().Finalize(); len(ceers) > 0 {
|
|
|
|
for _, err := range ceers {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Devices returns all devices associated with this Robot.
|
|
|
|
func (r *Robot) Devices() *Devices {
|
2014-06-24 11:33:59 +08:00
|
|
|
return r.devices
|
2013-11-24 02:36:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// AddDevice adds a new Device to the robots collection of devices. Returns the
|
2014-11-14 03:06:57 +08:00
|
|
|
// added device.
|
2014-07-08 12:45:36 +08:00
|
|
|
func (r *Robot) AddDevice(d Device) Device {
|
2014-07-10 00:38:43 +08:00
|
|
|
*r.devices = append(*r.Devices(), d)
|
|
|
|
return d
|
2014-07-08 12:45:36 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Device returns a device given a name. Returns nil if the Device does not exist.
|
2014-07-03 09:08:44 +08:00
|
|
|
func (r *Robot) Device(name string) Device {
|
2014-04-27 00:13:33 +08:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, device := range *r.devices {
|
2014-07-04 10:14:04 +08:00
|
|
|
if device.Name() == name {
|
2014-01-03 07:12:41 +08:00
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Connections returns all connections associated with this robot.
|
2014-12-31 21:15:52 +08:00
|
|
|
func (r *Robot) Connections() *Connections {
|
2014-06-24 11:33:59 +08:00
|
|
|
return r.connections
|
2014-01-03 07:12:41 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// AddConnection adds a new connection to the robots collection of connections.
|
|
|
|
// Returns the added connection.
|
2014-07-08 12:45:36 +08:00
|
|
|
func (r *Robot) AddConnection(c Connection) Connection {
|
2014-07-10 00:38:43 +08:00
|
|
|
*r.connections = append(*r.Connections(), c)
|
|
|
|
return c
|
2014-07-08 12:45:36 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Connection returns a connection given a name. Returns nil if the Connection
|
|
|
|
// does not exist.
|
2014-07-03 09:08:44 +08:00
|
|
|
func (r *Robot) Connection(name string) Connection {
|
2014-04-27 00:13:33 +08:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, connection := range *r.connections {
|
2014-07-04 10:52:31 +08:00
|
|
|
if connection.Name() == name {
|
2014-01-03 07:12:41 +08:00
|
|
|
return connection
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|