forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.go
67 lines (60 loc) · 1.54 KB
/
robot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gobot
import (
"fmt"
"math/rand"
"reflect"
"time"
)
type Robot struct {
Connections []interface{}
Devices []interface{}
Name string
Work func()
connections []*Connection
devices []*Device
}
func (r *Robot) Start() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
r.Name = fmt.Sprintf("Robot %v", i)
}
r.initConnections()
r.initDevices()
r.startConnections()
r.startDevices()
r.Work()
for {
time.Sleep(10 * time.Millisecond)
}
}
func (r *Robot) initConnections() {
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)
}
}
func (r *Robot) initDevices() {
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)
}
}
func (r *Robot) startConnections() {
fmt.Println("Starting connections...")
for i := range r.connections {
fmt.Println("Starting connection " + r.connections[i].Name + "...")
r.connections[i].Connect()
}
}
func (r *Robot) startDevices() {
fmt.Println("Starting devices...")
for i := range r.devices {
fmt.Println("Starting device " + r.devices[i].Name + "...")
r.devices[i].Start()
}
}