2013-10-23 07:45:31 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2013-11-14 12:44:54 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
2013-10-23 07:45:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Robot struct {
|
2013-11-28 12:05:45 +08:00
|
|
|
Connections []interface{}
|
|
|
|
Devices []interface{}
|
|
|
|
Name string
|
|
|
|
Commands map[string]interface{} `json:"-"`
|
2013-12-03 03:39:10 +08:00
|
|
|
RobotCommands []string `json:"Commands"`
|
|
|
|
Work func() `json:"-"`
|
|
|
|
connections []*Connection `json:"-"`
|
|
|
|
devices []*Device `json:"-"`
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Robot) Start() {
|
2013-12-02 14:46:52 +08:00
|
|
|
r.initName()
|
2013-11-28 12:05:45 +08:00
|
|
|
for k, _ := range r.Commands {
|
|
|
|
r.RobotCommands = append(r.RobotCommands, k)
|
|
|
|
}
|
2013-11-14 12:44:54 +08:00
|
|
|
r.initConnections()
|
|
|
|
r.initDevices()
|
|
|
|
r.startConnections()
|
|
|
|
r.startDevices()
|
|
|
|
r.Work()
|
2013-11-28 12:05:45 +08:00
|
|
|
select {}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:46:52 +08: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-10-25 13:04:58 +08:00
|
|
|
func (r *Robot) initConnections() {
|
2013-11-14 12:44:54 +08:00
|
|
|
r.connections = make([]*Connection, len(r.Connections))
|
|
|
|
fmt.Println("Initializing connections...")
|
|
|
|
for i := range r.Connections {
|
|
|
|
fmt.Println("Initializing connection " + reflect.ValueOf(r.Connections[i]).Elem().FieldByName("Name").String() + "...")
|
|
|
|
r.connections[i] = NewConnection(r.Connections[i], r)
|
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:04:58 +08:00
|
|
|
func (r *Robot) initDevices() {
|
2013-11-14 12:44:54 +08:00
|
|
|
r.devices = make([]*Device, len(r.Devices))
|
|
|
|
fmt.Println("Initializing devices...")
|
|
|
|
for i := range r.Devices {
|
|
|
|
fmt.Println("Initializing device " + reflect.ValueOf(r.Devices[i]).Elem().FieldByName("Name").String() + "...")
|
|
|
|
r.devices[i] = NewDevice(r.Devices[i], r)
|
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 13:00:03 +08:00
|
|
|
func (r *Robot) startConnections() {
|
2013-11-14 12:44:54 +08:00
|
|
|
fmt.Println("Starting connections...")
|
|
|
|
for i := range r.connections {
|
|
|
|
fmt.Println("Starting connection " + r.connections[i].Name + "...")
|
|
|
|
r.connections[i].Connect()
|
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-10-24 13:00:03 +08:00
|
|
|
func (r *Robot) startDevices() {
|
2013-11-14 12:44:54 +08:00
|
|
|
fmt.Println("Starting devices...")
|
|
|
|
for i := range r.devices {
|
|
|
|
fmt.Println("Starting device " + r.devices[i].Name + "...")
|
|
|
|
r.devices[i].Start()
|
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
2013-11-24 01:12:57 +08:00
|
|
|
|
2013-11-24 02:36:08 +08:00
|
|
|
func (r *Robot) GetDevices() []*Device {
|
|
|
|
return r.devices
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Robot) GetDevice(name string) *Device {
|
2013-11-24 01:12:57 +08:00
|
|
|
for i := range r.devices {
|
|
|
|
if r.devices[i].Name == name {
|
|
|
|
return r.devices[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|