hybridgroup.gobot/robot.go

195 lines
4.9 KiB
Go
Raw Normal View History

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
)
// 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
}
// Robot software representation of a physical board. A robot is a named
// entitity that manages multiple IO devices using a set of adaptors. Additionally
// a user can specificy custom commands to control a robot remotely.
2013-10-23 07:45:31 +08:00
type Robot struct {
Name string
commands map[string]func(map[string]interface{}) interface{}
Work func()
2014-06-24 11:33:59 +08:00
connections *connections
devices *devices
2013-10-23 07:45:31 +08:00
}
2014-07-10 00:38:43 +08:00
type robots []*Robot
2014-06-24 11:33:59 +08:00
// Len counts the robots associated with this instance.
2014-06-24 11:33:59 +08:00
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
// Start initialises the event loop. All robots that were added will
// be automtically started as a result of this call.
2014-11-18 08:23:19 +08:00
func (r *robots) Start() (err error) {
2014-07-10 00:38:43 +08:00
for _, robot := range *r {
2014-11-18 08:23:19 +08:00
err = robot.Start()
2014-11-13 03:21:50 +08:00
if err != nil {
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
2014-11-13 03:21:50 +08:00
// Each enumerates thru the robots and calls specified function
2014-06-24 11:33:59 +08:00
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
}
// NewRobot constructs a new named robot. Though a robot's name will be generated,
// we recommend that user take care of naming a robot for later access.
2014-06-13 12:32:38 +08:00
func NewRobot(name string, v ...interface{}) *Robot {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
2014-04-30 04:20:32 +08:00
r := &Robot{
Name: name,
commands: make(map[string]func(map[string]interface{}) interface{}),
2014-06-24 11:33:59 +08:00
connections: &connections{},
devices: &devices{},
Work: nil,
2014-04-30 04:20:32 +08:00
}
2014-04-30 23:10:44 +08:00
log.Println("Initializing Robot", r.Name, "...")
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)
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)
log.Println("Initializing device", d.Name(), "...")
}
case func():
r.Work = v[i].(func())
default:
2014-11-18 08:23:19 +08:00
log.Println("Unknown argument passed to NewRobot")
2014-06-13 12:32:38 +08:00
}
}
2014-04-30 04:20:32 +08:00
return r
}
// AddCommand setup a new command that we be made available via the REST api.
2014-06-12 07:44:23 +08:00
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
r.commands[name] = f
}
// Commands lists out all available commands on this robot.
func (r *Robot) Commands() map[string]func(map[string]interface{}) interface{} {
return r.commands
}
// Command fetch a named command on this robot.
func (r *Robot) Command(name string) func(map[string]interface{}) interface{} {
return r.commands[name]
2014-06-12 07:44:23 +08: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-18 08:23:19 +08:00
func (r *Robot) Start() (err error) {
2014-04-30 23:10:44 +08:00
log.Println("Starting Robot", r.Name, "...")
2014-11-18 08:23:19 +08:00
if err = r.Connections().Start(); err != nil {
return
2013-12-31 08:51:21 +08:00
}
2014-11-18 08:23:19 +08:00
if err = r.Devices().Start(); err != nil {
return err
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-13 03:21:50 +08:00
return nil
2013-10-23 07:45:31 +08:00
}
// Devices retrieves all devices associated with this robot.
2014-06-24 11:33:59 +08:00
func (r *Robot) Devices() *devices {
return r.devices
2013-11-24 02:36:08 +08:00
}
// AddDevice adds a new device on this robot.
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
}
// Device finds a device by name.
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 {
if device.Name() == name {
2014-01-03 07:12:41 +08:00
return device
}
}
return nil
}
// Connections retrieves all connections on this robot.
2014-06-24 11:33:59 +08:00
func (r *Robot) Connections() *connections {
return r.connections
2014-01-03 07:12:41 +08:00
}
// AddConnection add a new connection on this robot.
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
}
// Connection finds a connection by name.
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 {
if connection.Name() == name {
2014-01-03 07:12:41 +08:00
return connection
2013-11-24 01:12:57 +08:00
}
}
return nil
}
2014-05-16 02:50:45 +08:00
// ToJSON returns a JSON representation of the master robot.
2014-06-11 06:16:11 +08:00
func (r *Robot) ToJSON() *JSONRobot {
jsonRobot := &JSONRobot{
Name: r.Name,
2014-06-12 07:44:23 +08:00
Commands: []string{},
2014-06-11 06:16:11 +08:00
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
2014-06-24 11:33:59 +08:00
for command := range r.Commands() {
2014-06-12 07:44:23 +08:00
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
2014-06-24 11:33:59 +08:00
r.Devices().Each(func(device Device) {
2014-06-11 06:16:11 +08:00
jsonDevice := device.ToJSON()
2014-07-25 07:39:27 +08:00
jsonRobot.Connections = append(jsonRobot.Connections, r.Connection(jsonDevice.Connection).ToJSON())
2014-05-16 02:50:45 +08:00
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
2014-06-24 11:33:59 +08:00
})
2014-05-16 02:50:45 +08:00
return jsonRobot
}