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-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
|
|
|
}
|
|
|
|
|
2013-10-23 07:45:31 +08:00
|
|
|
type Robot struct {
|
2014-06-16 08:22:50 +08:00
|
|
|
Name string
|
|
|
|
Commands map[string]func(map[string]interface{}) interface{}
|
|
|
|
Work func()
|
|
|
|
connections connections
|
|
|
|
devices devices
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
type Robots []*Robot
|
|
|
|
|
|
|
|
func (r Robots) Start() {
|
|
|
|
for _, robot := range r {
|
|
|
|
robot.Start()
|
2014-04-27 00:44:26 +08:00
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
2014-04-27 00:44:26 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
func (r Robots) Each(f func(*Robot)) {
|
|
|
|
for _, robot := range r {
|
|
|
|
f(robot)
|
|
|
|
}
|
2013-12-19 15:50:42 +08:00
|
|
|
}
|
|
|
|
|
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-04-30 04:20:32 +08:00
|
|
|
r := &Robot{
|
2014-06-14 00:37:34 +08:00
|
|
|
Name: name,
|
|
|
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
|
|
|
connections: connections{},
|
|
|
|
devices: devices{},
|
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-06-13 12:32:38 +08:00
|
|
|
if len(v) > 0 {
|
|
|
|
if v[0] == nil {
|
|
|
|
v[0] = []Connection{}
|
|
|
|
}
|
2014-06-15 09:49:02 +08:00
|
|
|
log.Println("Initializing connections...")
|
|
|
|
for _, connection := range v[0].([]Connection) {
|
|
|
|
c := r.AddConnection(connection)
|
|
|
|
log.Println("Initializing connection", c.name(), "...")
|
|
|
|
}
|
2014-06-13 12:32:38 +08:00
|
|
|
}
|
|
|
|
if len(v) > 1 {
|
|
|
|
if v[1] == nil {
|
|
|
|
v[1] = []Device{}
|
|
|
|
}
|
2014-06-15 09:49:02 +08:00
|
|
|
log.Println("Initializing devices...")
|
|
|
|
for _, device := range v[1].([]Device) {
|
|
|
|
d := r.AddDevice(device)
|
|
|
|
log.Println("Initializing device", d.name(), "...")
|
|
|
|
}
|
2014-06-13 12:32:38 +08:00
|
|
|
}
|
|
|
|
if len(v) > 2 {
|
|
|
|
if v[2] == nil {
|
|
|
|
v[2] = func() {}
|
|
|
|
}
|
|
|
|
r.Work = v[2].(func())
|
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-14 00:37:34 +08:00
|
|
|
func (r *Robot) AddDevice(d Device) *device {
|
|
|
|
device := NewDevice(d, r)
|
|
|
|
r.devices = append(r.devices, device)
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Robot) AddConnection(c Connection) *connection {
|
|
|
|
connection := NewConnection(c, r)
|
|
|
|
r.connections = append(r.connections, connection)
|
|
|
|
return connection
|
|
|
|
}
|
|
|
|
|
2014-06-12 07:44:23 +08:00
|
|
|
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
|
|
|
|
r.Commands[name] = f
|
|
|
|
}
|
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
func (r *Robot) Start() {
|
2014-04-30 23:10:44 +08:00
|
|
|
log.Println("Starting Robot", r.Name, "...")
|
|
|
|
if err := r.Connections().Start(); err != nil {
|
2013-12-31 08:51:21 +08:00
|
|
|
panic("Could not start connections")
|
|
|
|
}
|
2014-04-30 23:10:44 +08:00
|
|
|
if err := r.Devices().Start(); err != nil {
|
2013-12-31 08:51:21 +08:00
|
|
|
panic("Could not start devices")
|
|
|
|
}
|
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()
|
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 23:10:44 +08:00
|
|
|
func (r *Robot) Devices() devices {
|
2014-04-27 12:52:10 +08:00
|
|
|
return devices(r.devices)
|
2013-11-24 02:36:08 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
func (r *Robot) Device(name string) *device {
|
2014-04-27 00:13:33 +08:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-03 07:12:41 +08:00
|
|
|
for _, device := range r.devices {
|
|
|
|
if device.Name == name {
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:10:44 +08:00
|
|
|
func (r *Robot) Connections() connections {
|
2014-04-30 04:20:32 +08:00
|
|
|
return connections(r.connections)
|
2014-01-03 07:12:41 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
func (r *Robot) Connection(name string) *connection {
|
2014-04-27 00:13:33 +08:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-03 07:12:41 +08:00
|
|
|
for _, connection := range r.connections {
|
|
|
|
if connection.Name == name {
|
|
|
|
return connection
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-16 02:50:45 +08:00
|
|
|
|
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-12 07:44:23 +08:00
|
|
|
for command := range r.Commands {
|
|
|
|
jsonRobot.Commands = append(jsonRobot.Commands, command)
|
|
|
|
}
|
2014-05-16 02:50:45 +08:00
|
|
|
for _, device := range r.Devices() {
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonDevice := device.ToJSON()
|
2014-05-16 02:50:45 +08:00
|
|
|
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
|
|
|
|
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
|
|
|
|
}
|
|
|
|
return jsonRobot
|
|
|
|
}
|