hybridgroup.gobot/robot.go

144 lines
3.1 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-11-14 12:44:54 +08:00
"math/rand"
"time"
2013-10-23 07:45:31 +08:00
)
2014-05-16 02:50:45 +08:00
type JsonRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*JsonConnection `json:"connections"`
Devices []*JsonDevice `json:"devices"`
}
2013-10-23 07:45:31 +08:00
type Robot struct {
2014-05-16 02:50:45 +08:00
Name string `json:"-"`
2013-11-28 12:05:45 +08:00
Commands map[string]interface{} `json:"-"`
2014-05-16 02:50:45 +08:00
RobotCommands []string `json:"-"`
2013-12-03 03:39:10 +08:00
Work func() `json:"-"`
2014-04-30 04:20:32 +08:00
connections connections `json:"-"`
devices devices `json:"-"`
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-04-30 04:20:32 +08:00
func NewRobot(name string, c []Connection, d []Device, work func()) *Robot {
r := &Robot{
2014-04-30 23:10:44 +08:00
Name: name,
Work: work,
2014-04-30 04:20:32 +08:00
}
r.initName()
2014-04-30 23:10:44 +08:00
log.Println("Initializing Robot", r.Name, "...")
2013-12-03 10:59:28 +08:00
r.initCommands()
2014-04-30 23:10:44 +08:00
r.initConnections(c)
r.initDevices(d)
2014-04-30 04:20:32 +08:00
return r
}
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
}
func (r *Robot) initName() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
2014-04-15 14:15:54 +08:00
r.Name = fmt.Sprintf("Robot%v", i)
}
}
2013-12-03 10:59:28 +08:00
func (r *Robot) initCommands() {
2014-04-19 13:44:50 +08:00
r.RobotCommands = make([]string, 0)
2013-12-03 10:59:28 +08:00
for k, _ := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}
}
2014-04-30 23:10:44 +08:00
func (r *Robot) initConnections(c []Connection) {
r.connections = make(connections, len(c))
2013-12-31 14:04:23 +08:00
log.Println("Initializing connections...")
2014-04-30 23:10:44 +08:00
for i, connection := range c {
log.Println("Initializing connection", FieldByNamePtr(connection, "Name"), "...")
2014-01-03 07:12:41 +08:00
r.connections[i] = NewConnection(connection, r)
2013-11-14 12:44:54 +08:00
}
2013-10-23 07:45:31 +08:00
}
2014-04-30 23:10:44 +08:00
func (r *Robot) initDevices(d []Device) {
r.devices = make([]*device, len(d))
2013-12-31 14:04:23 +08:00
log.Println("Initializing devices...")
2014-04-30 23:10:44 +08:00
for i, device := range d {
log.Println("Initializing device", FieldByNamePtr(device, "Name"), "...")
2014-01-03 07:12:41 +08:00
r.devices[i] = NewDevice(device, r)
2013-11-14 12:44:54 +08:00
}
2013-10-23 07:45:31 +08:00
}
2014-04-30 23:10:44 +08:00
func (r *Robot) Devices() devices {
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
func (r *Robot) ToJson() *JsonRobot {
jsonRobot := new(JsonRobot)
jsonRobot.Name = r.Name
jsonRobot.Commands = r.RobotCommands
jsonRobot.Connections = make([]*JsonConnection, 0)
for _, device := range r.Devices() {
jsonDevice := device.ToJson()
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
}
return jsonRobot
}