2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2013-10-22 16:45:31 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-19 23:21:19 -08:00
|
|
|
"errors"
|
2013-11-13 20:44:54 -08:00
|
|
|
"fmt"
|
2013-12-30 22:04:23 -08:00
|
|
|
"log"
|
2013-10-22 16:45:31 -07:00
|
|
|
)
|
|
|
|
|
2014-07-18 14:18:01 -06:00
|
|
|
// JSONRobot a JSON representation of a robot.
|
2014-06-10 15:16:11 -07:00
|
|
|
type JSONRobot struct {
|
2014-05-15 11:50:45 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Commands []string `json:"commands"`
|
2014-06-10 15:16:11 -07:00
|
|
|
Connections []*JSONConnection `json:"connections"`
|
|
|
|
Devices []*JSONDevice `json:"devices"`
|
2014-05-15 11:50:45 -07:00
|
|
|
}
|
|
|
|
|
2014-11-21 11:57:26 -08:00
|
|
|
// NewJSONRobot returns a JSON representation of the robot.
|
|
|
|
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-13 11: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-22 16:45:31 -07:00
|
|
|
type Robot struct {
|
2014-06-15 17:22:50 -07:00
|
|
|
Name string
|
|
|
|
Work func()
|
2014-06-23 20:33:59 -07:00
|
|
|
connections *connections
|
|
|
|
devices *devices
|
2014-11-20 18:00:32 -08:00
|
|
|
Commander
|
|
|
|
Eventer
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-07-09 09:38:43 -07:00
|
|
|
type robots []*Robot
|
2014-06-23 20:33:59 -07:00
|
|
|
|
2014-07-18 14:18:01 -06:00
|
|
|
// Len counts the robots associated with this instance.
|
2014-06-23 20:33:59 -07:00
|
|
|
func (r *robots) Len() int {
|
2014-07-09 09:38:43 -07:00
|
|
|
return len(*r)
|
2014-06-23 20:33:59 -07:00
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
|
2014-07-18 14:18:01 -06:00
|
|
|
// Start initialises the event loop. All robots that were added will
|
|
|
|
// be automtically started as a result of this call.
|
2014-11-19 23:21:19 -08:00
|
|
|
func (r *robots) Start() (errs []error) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, robot := range *r {
|
2014-11-19 23:21:19 -08:00
|
|
|
if errs = robot.Start(); len(errs) > 0 {
|
|
|
|
for i, err := range errs {
|
|
|
|
errs[i] = errors.New(fmt.Sprintf("Robot %q: %v", robot.Name, err))
|
|
|
|
}
|
2014-11-17 16:23:19 -08:00
|
|
|
return
|
2014-11-12 11:21:50 -08:00
|
|
|
}
|
2014-04-26 09:44:26 -07:00
|
|
|
}
|
2014-11-17 16:23:19 -08:00
|
|
|
return
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-04-26 09:44:26 -07:00
|
|
|
|
2014-11-12 11:21:50 -08:00
|
|
|
// Each enumerates thru the robots and calls specified function
|
2014-06-23 20:33:59 -07:00
|
|
|
func (r *robots) Each(f func(*Robot)) {
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, robot := range *r {
|
2014-04-29 13:20:32 -07:00
|
|
|
f(robot)
|
|
|
|
}
|
2013-12-18 23:50:42 -08:00
|
|
|
}
|
|
|
|
|
2014-11-13 11: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-06-12 21:32:38 -07:00
|
|
|
func NewRobot(name string, v ...interface{}) *Robot {
|
2014-06-14 18:49:02 -07:00
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
|
|
|
|
}
|
2014-07-03 19:52:31 -07:00
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
r := &Robot{
|
2014-06-13 09:37:34 -07:00
|
|
|
Name: name,
|
2014-06-23 20:33:59 -07:00
|
|
|
connections: &connections{},
|
|
|
|
devices: &devices{},
|
2014-07-03 19:52:31 -07:00
|
|
|
Work: nil,
|
2014-11-20 18:00:32 -08:00
|
|
|
Eventer: NewEventer(),
|
|
|
|
Commander: NewCommander(),
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-06-14 18:49:02 -07:00
|
|
|
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Initializing Robot", r.Name, "...")
|
2014-07-03 19:52:31 -07:00
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
switch v[i].(type) {
|
|
|
|
case []Connection:
|
|
|
|
log.Println("Initializing connections...")
|
|
|
|
for _, connection := range v[i].([]Connection) {
|
2014-07-09 09:38:43 -07:00
|
|
|
c := r.AddConnection(connection)
|
2014-07-03 19:52:31 -07:00
|
|
|
log.Println("Initializing connection", c.Name(), "...")
|
|
|
|
}
|
|
|
|
case []Device:
|
|
|
|
log.Println("Initializing devices...")
|
|
|
|
for _, device := range v[i].([]Device) {
|
2014-07-09 09:38:43 -07:00
|
|
|
d := r.AddDevice(device)
|
2014-07-03 19:52:31 -07:00
|
|
|
log.Println("Initializing device", d.Name(), "...")
|
|
|
|
}
|
|
|
|
case func():
|
|
|
|
r.Work = v[i].(func())
|
2014-06-12 21:32:38 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 19:52:31 -07:00
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-07-18 14:18:01 -06:00
|
|
|
// Start a robot instance and runs it's work function if any. You should not
|
|
|
|
// need to manually start a robot if already part of a Gobot application as the
|
|
|
|
// robot will be automatically started for you.
|
2014-11-19 23:21:19 -08:00
|
|
|
func (r *Robot) Start() (errs []error) {
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Starting Robot", r.Name, "...")
|
2014-11-19 23:21:19 -08:00
|
|
|
if cerrs := r.Connections().Start(); len(cerrs) > 0 {
|
|
|
|
errs = append(errs, cerrs...)
|
2014-11-17 16:23:19 -08:00
|
|
|
return
|
2013-12-30 16:51:21 -08:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
if derrs := r.Devices().Start(); len(derrs) > 0 {
|
|
|
|
errs = append(errs, derrs...)
|
|
|
|
return
|
2013-12-30 16:51:21 -08:00
|
|
|
}
|
2013-12-03 15:51:17 -08:00
|
|
|
if r.Work != nil {
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Starting work...")
|
2013-12-03 15:51:17 -08:00
|
|
|
r.Work()
|
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// Devices returns all devices associated with this robot.
|
2014-06-23 20:33:59 -07:00
|
|
|
func (r *Robot) Devices() *devices {
|
|
|
|
return r.devices
|
2013-11-23 10:36:08 -08:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// AddDevice adds a new device to the robots collection of devices. Returns the
|
|
|
|
// added device.
|
2014-07-07 21:45:36 -07:00
|
|
|
func (r *Robot) AddDevice(d Device) Device {
|
2014-07-09 09:38:43 -07:00
|
|
|
*r.devices = append(*r.Devices(), d)
|
|
|
|
return d
|
2014-07-07 21:45:36 -07:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// Device returns a device given a name. Returns nil on no device.
|
2014-07-02 18:08:44 -07:00
|
|
|
func (r *Robot) Device(name string) Device {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, device := range *r.devices {
|
2014-07-03 19:14:04 -07:00
|
|
|
if device.Name() == name {
|
2014-01-02 15:12:41 -08:00
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// Connections returns all connections associated with this robot.
|
2014-06-23 20:33:59 -07:00
|
|
|
func (r *Robot) Connections() *connections {
|
|
|
|
return r.connections
|
2014-01-02 15:12:41 -08:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// AddConnection adds a new connection to the robots collection of connections.
|
|
|
|
// Returns the added connection.
|
2014-07-07 21:45:36 -07:00
|
|
|
func (r *Robot) AddConnection(c Connection) Connection {
|
2014-07-09 09:38:43 -07:00
|
|
|
*r.connections = append(*r.Connections(), c)
|
|
|
|
return c
|
2014-07-07 21:45:36 -07:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:06:57 -08:00
|
|
|
// Connection returns a connection given a name. Returns nil on no connection.
|
2014-07-02 18:08:44 -07:00
|
|
|
func (r *Robot) Connection(name string) Connection {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-09 09:38:43 -07:00
|
|
|
for _, connection := range *r.connections {
|
2014-07-03 19:52:31 -07:00
|
|
|
if connection.Name() == name {
|
2014-01-02 15:12:41 -08:00
|
|
|
return connection
|
2013-11-23 09:12:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|