1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

138 lines
2.9 KiB
Go
Raw Normal View History

2013-10-22 16:45:31 -07:00
package gobot
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
)
type Robot struct {
2014-01-26 18:55:27 -08:00
Connections []Connection `json:"connections"`
Devices []Device `json:"devices"`
Name string `json:"name"`
2013-11-27 20:05:45 -08:00
Commands map[string]interface{} `json:"-"`
2014-01-26 18:55:27 -08:00
RobotCommands []string `json:"commands"`
2013-12-02 11:39:10 -08:00
Work func() `json:"-"`
connections []*connection `json:"-"`
devices []*device `json:"-"`
2013-10-22 16:45:31 -07:00
}
func (r *Robot) Start() {
2013-12-18 23:50:42 -08:00
m := GobotMaster()
m.Robots = []Robot{*r}
m.Start()
}
func (r *Robot) startRobot() {
r.initName()
2013-12-02 18:59:28 -08:00
r.initCommands()
2013-11-13 20:44:54 -08:00
r.initConnections()
r.initDevices()
2013-12-30 16:51:21 -08:00
if r.startConnections() != true {
panic("Could not start connections")
}
if r.startDevices() != true {
panic("Could not start devices")
}
2013-12-03 15:51:17 -08:00
if r.Work != nil {
r.Work()
}
2013-10-22 16:45:31 -07:00
}
func (r *Robot) initName() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
r.Name = fmt.Sprintf("Robot %v", i)
}
}
2013-12-02 18:59:28 -08:00
func (r *Robot) initCommands() {
for k, _ := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}
}
2013-10-24 22:04:58 -07:00
func (r *Robot) initConnections() {
r.connections = make([]*connection, len(r.Connections))
2013-12-30 22:04:23 -08:00
log.Println("Initializing connections...")
2014-01-02 15: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-13 20:44:54 -08:00
}
2013-10-22 16:45:31 -07:00
}
2013-10-24 22:04:58 -07:00
func (r *Robot) initDevices() {
r.devices = make([]*device, len(r.Devices))
2013-12-30 22:04:23 -08:00
log.Println("Initializing devices...")
2014-01-02 15:12:41 -08:00
for i, device := range r.Devices {
log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...")
r.devices[i] = NewDevice(device, r)
2013-11-13 20:44:54 -08:00
}
2013-10-22 16:45:31 -07:00
}
2013-12-30 16:51:21 -08:00
func (r *Robot) startConnections() bool {
2013-12-30 22:04:23 -08:00
log.Println("Starting connections...")
2013-12-30 16:51:21 -08:00
success := true
2014-01-02 15:12:41 -08:00
for _, connection := range r.connections {
log.Println("Starting connection " + connection.Name + "...")
if connection.Connect() == false {
2013-12-30 16:51:21 -08:00
success = false
break
}
2013-11-13 20:44:54 -08:00
}
2013-12-30 16:51:21 -08:00
return success
2013-10-22 16:45:31 -07:00
}
2013-12-30 16:51:21 -08:00
func (r *Robot) startDevices() bool {
2013-12-30 22:04:23 -08:00
log.Println("Starting devices...")
2013-12-30 16:51:21 -08:00
success := true
2014-01-02 15:12:41 -08:00
for _, device := range r.devices {
log.Println("Starting device " + device.Name + "...")
if device.Start() == false {
2013-12-30 16:51:21 -08:00
success = false
break
}
2013-11-13 20:44:54 -08:00
}
2013-12-30 16:51:21 -08:00
return success
2013-10-22 16:45:31 -07:00
}
2013-11-23 09:12:57 -08:00
2014-03-31 14:25:20 -07:00
func (r *Robot) haltDevices() {
for _, device := range r.devices {
device.Halt()
}
}
2013-12-30 20:38:14 -08:00
func (r *Robot) finalizeConnections() {
2014-01-02 15:12:41 -08:00
for _, connection := range r.connections {
connection.Finalize()
2013-12-30 20:38:14 -08:00
}
}
func (r *Robot) GetDevices() []*device {
2013-11-23 10:36:08 -08:00
return r.devices
}
func (r *Robot) GetDevice(name string) *device {
2014-01-02 15: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 {
for _, connection := range r.connections {
if connection.Name == name {
return connection
2013-11-23 09:12:57 -08:00
}
}
return nil
}