2014-04-30 08:10:44 -07:00
|
|
|
package gobot
|
2013-10-22 16:45:31 -07:00
|
|
|
|
|
|
|
import (
|
2013-11-13 20:44:54 -08:00
|
|
|
"fmt"
|
2013-12-30 22:04:23 -08:00
|
|
|
"log"
|
2013-11-13 20:44:54 -08:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
2013-10-22 16:45:31 -07:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-22 16:45:31 -07:00
|
|
|
type Robot struct {
|
2014-06-11 16:44:23 -07:00
|
|
|
Name string `json:"-"`
|
|
|
|
Commands map[string]func(map[string]interface{}) interface{} `json:"-"`
|
|
|
|
Work func() `json:"-"`
|
|
|
|
connections connections `json:"-"`
|
|
|
|
devices devices `json:"-"`
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
type Robots []*Robot
|
|
|
|
|
|
|
|
func (r Robots) Start() {
|
|
|
|
for _, robot := range r {
|
|
|
|
robot.Start()
|
2014-04-26 09:44:26 -07:00
|
|
|
}
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2014-04-26 09:44:26 -07:00
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
func (r Robots) Each(f func(*Robot)) {
|
|
|
|
for _, robot := range r {
|
|
|
|
f(robot)
|
|
|
|
}
|
2013-12-18 23:50:42 -08:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
func NewRobot(name string, c []Connection, d []Device, work func()) *Robot {
|
|
|
|
r := &Robot{
|
2014-06-11 16:44:23 -07:00
|
|
|
Name: name,
|
|
|
|
Work: work,
|
|
|
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
2014-04-29 13:20:32 -07:00
|
|
|
}
|
2013-12-01 22:46:52 -08:00
|
|
|
r.initName()
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Initializing Robot", r.Name, "...")
|
|
|
|
r.initConnections(c)
|
|
|
|
r.initDevices(d)
|
2014-04-29 13:20:32 -07:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-11 16:44:23 -07:00
|
|
|
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
|
|
|
|
r.Commands[name] = f
|
|
|
|
}
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
func (r *Robot) Start() {
|
2014-04-30 08:10:44 -07:00
|
|
|
log.Println("Starting Robot", r.Name, "...")
|
|
|
|
if err := r.Connections().Start(); err != nil {
|
2013-12-30 16:51:21 -08:00
|
|
|
panic("Could not start connections")
|
|
|
|
}
|
2014-04-30 08:10:44 -07:00
|
|
|
if err := r.Devices().Start(); err != nil {
|
2013-12-30 16:51:21 -08:00
|
|
|
panic("Could not start devices")
|
|
|
|
}
|
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()
|
|
|
|
}
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:46:52 -08:00
|
|
|
func (r *Robot) initName() {
|
|
|
|
if r.Name == "" {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
i := rand.Int()
|
2014-04-14 23:15:54 -07:00
|
|
|
r.Name = fmt.Sprintf("Robot%v", i)
|
2013-12-01 22:46:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 08:10:44 -07:00
|
|
|
func (r *Robot) initConnections(c []Connection) {
|
|
|
|
r.connections = make(connections, len(c))
|
2013-12-30 22:04:23 -08:00
|
|
|
log.Println("Initializing connections...")
|
2014-04-30 08:10:44 -07:00
|
|
|
for i, connection := range c {
|
|
|
|
log.Println("Initializing connection", FieldByNamePtr(connection, "Name"), "...")
|
2014-01-02 15:12:41 -08:00
|
|
|
r.connections[i] = NewConnection(connection, r)
|
2013-11-13 20:44:54 -08:00
|
|
|
}
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-30 08:10:44 -07:00
|
|
|
func (r *Robot) initDevices(d []Device) {
|
|
|
|
r.devices = make([]*device, len(d))
|
2013-12-30 22:04:23 -08:00
|
|
|
log.Println("Initializing devices...")
|
2014-04-30 08:10:44 -07:00
|
|
|
for i, device := range d {
|
|
|
|
log.Println("Initializing device", FieldByNamePtr(device, "Name"), "...")
|
2014-01-02 15:12:41 -08:00
|
|
|
r.devices[i] = NewDevice(device, r)
|
2013-11-13 20:44:54 -08:00
|
|
|
}
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-30 08:10:44 -07:00
|
|
|
func (r *Robot) Devices() devices {
|
2014-04-26 21:52:10 -07:00
|
|
|
return devices(r.devices)
|
2013-11-23 10:36:08 -08:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
func (r *Robot) Device(name string) *device {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-02 15:12:41 -08:00
|
|
|
for _, device := range r.devices {
|
|
|
|
if device.Name == name {
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-30 08:10:44 -07:00
|
|
|
func (r *Robot) Connections() connections {
|
2014-04-29 13:20:32 -07:00
|
|
|
return connections(r.connections)
|
2014-01-02 15:12:41 -08:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:20:32 -07:00
|
|
|
func (r *Robot) Connection(name string) *connection {
|
2014-04-26 10:13:33 -06:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-02 15:12:41 -08:00
|
|
|
for _, connection := range r.connections {
|
|
|
|
if connection.Name == name {
|
|
|
|
return connection
|
2013-11-23 09:12:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-15 11:50:45 -07:00
|
|
|
|
2014-06-10 15:16:11 -07:00
|
|
|
func (r *Robot) ToJSON() *JSONRobot {
|
|
|
|
jsonRobot := &JSONRobot{
|
|
|
|
Name: r.Name,
|
2014-06-11 16:44:23 -07:00
|
|
|
Commands: []string{},
|
2014-06-10 15:16:11 -07:00
|
|
|
Connections: []*JSONConnection{},
|
|
|
|
Devices: []*JSONDevice{},
|
|
|
|
}
|
2014-06-11 16:44:23 -07:00
|
|
|
for command := range r.Commands {
|
|
|
|
jsonRobot.Commands = append(jsonRobot.Commands, command)
|
|
|
|
}
|
2014-05-15 11:50:45 -07:00
|
|
|
for _, device := range r.Devices() {
|
2014-06-10 15:16:11 -07:00
|
|
|
jsonDevice := device.ToJSON()
|
2014-05-15 11:50:45 -07:00
|
|
|
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
|
|
|
|
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
|
|
|
|
}
|
|
|
|
return jsonRobot
|
|
|
|
}
|