hybridgroup.gobot/robot.go

153 lines
3.2 KiB
Go
Raw Normal View History

2013-10-23 07:45:31 +08:00
package gobot
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
)
type Robot struct {
2014-01-27 10:55:27 +08:00
Connections []Connection `json:"connections"`
Devices []Device `json:"devices"`
Name string `json:"name"`
2013-11-28 12:05:45 +08:00
Commands map[string]interface{} `json:"-"`
2014-01-27 10:55:27 +08:00
RobotCommands []string `json:"commands"`
2013-12-03 03:39:10 +08:00
Work func() `json:"-"`
connections []*connection `json:"-"`
devices []*device `json:"-"`
2014-04-27 00:44:26 +08:00
master *Master `json:"-"`
2013-10-23 07:45:31 +08:00
}
2014-04-15 14:15:54 +08:00
func (r *Robot) Start() {
2014-04-27 00:44:26 +08:00
if r.master == nil {
r.master = NewMaster()
}
r.master.Robots = []*Robot{r}
r.master.Start()
2013-12-19 15:50:42 +08:00
}
func (r *Robot) startRobot() {
r.initName()
2013-12-03 10:59:28 +08:00
r.initCommands()
2013-11-14 12:44:54 +08:00
r.initConnections()
2013-12-31 08:51:21 +08:00
if r.startConnections() != true {
panic("Could not start connections")
}
2014-04-01 06:26:35 +08:00
if r.initDevices() != true {
panic("Could not initialize devices")
}
2013-12-31 08:51:21 +08:00
if r.startDevices() != true {
panic("Could not start devices")
}
2013-12-04 07:51:17 +08:00
if r.Work != nil {
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)
}
}
2013-10-25 13:04:58 +08:00
func (r *Robot) initConnections() {
r.connections = make([]*connection, len(r.Connections))
2013-12-31 14:04:23 +08:00
log.Println("Initializing connections...")
2014-01-03 07:12:41 +08:00
for i, connection := range r.Connections {
log.Println("Initializing connection ", FieldByNamePtr(connection, "Name"), "...")
r.connections[i] = NewConnection(connection, r)
2013-11-14 12:44:54 +08:00
}
2013-10-23 07:45:31 +08:00
}
2014-04-01 06:26:35 +08:00
func (r *Robot) initDevices() bool {
r.devices = make([]*device, len(r.Devices))
2013-12-31 14:04:23 +08:00
log.Println("Initializing devices...")
2014-01-03 07:12:41 +08:00
for i, device := range r.Devices {
r.devices[i] = NewDevice(device, r)
2013-11-14 12:44:54 +08:00
}
2014-04-01 06:26:35 +08:00
success := true
for _, device := range r.devices {
log.Println("Initializing device " + device.Name + "...")
if device.Init() == false {
success = false
break
}
}
return success
2013-10-23 07:45:31 +08:00
}
2013-12-31 08:51:21 +08:00
func (r *Robot) startConnections() bool {
2013-12-31 14:04:23 +08:00
log.Println("Starting connections...")
2013-12-31 08:51:21 +08:00
success := true
2014-01-03 07:12:41 +08:00
for _, connection := range r.connections {
log.Println("Starting connection " + connection.Name + "...")
if connection.Connect() == false {
2013-12-31 08:51:21 +08:00
success = false
break
}
2013-11-14 12:44:54 +08:00
}
2013-12-31 08:51:21 +08:00
return success
2013-10-23 07:45:31 +08:00
}
2013-12-31 08:51:21 +08:00
func (r *Robot) startDevices() bool {
2013-12-31 14:04:23 +08:00
log.Println("Starting devices...")
2013-12-31 08:51:21 +08:00
success := true
2014-01-03 07:12:41 +08:00
for _, device := range r.devices {
log.Println("Starting device " + device.Name + "...")
if device.Start() == false {
2013-12-31 08:51:21 +08:00
success = false
break
}
2013-11-14 12:44:54 +08:00
}
2013-12-31 08:51:21 +08:00
return success
2013-10-23 07:45:31 +08:00
}
2013-11-24 01:12:57 +08:00
2013-12-31 12:38:14 +08:00
func (r *Robot) finalizeConnections() {
2014-01-03 07:12:41 +08:00
for _, connection := range r.connections {
connection.Finalize()
2013-12-31 12:38:14 +08:00
}
}
func (r *Robot) GetDevices() devices {
return devices(r.devices)
2013-11-24 02:36:08 +08:00
}
func (r *Robot) GetDevice(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
}
func (r *Robot) GetConnections() []*connection {
return r.connections
}
func (r *Robot) GetConnection(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
}