forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.go
156 lines (139 loc) · 3.25 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gobot
import (
"fmt"
"log"
)
type JSONRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*JSONConnection `json:"connections"`
Devices []*JSONDevice `json:"devices"`
}
type Robot struct {
Name string
Commands map[string]func(map[string]interface{}) interface{}
Work func()
connections connections
devices devices
}
type Robots []*Robot
func (r Robots) Start() {
for _, robot := range r {
robot.Start()
}
}
func (r Robots) Each(f func(*Robot)) {
for _, robot := range r {
f(robot)
}
}
func NewRobot(name string, v ...interface{}) *Robot {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
r := &Robot{
Name: name,
Commands: make(map[string]func(map[string]interface{}) interface{}),
connections: connections{},
devices: devices{},
}
log.Println("Initializing Robot", r.Name, "...")
if len(v) > 0 {
if v[0] == nil {
v[0] = []Connection{}
}
log.Println("Initializing connections...")
for _, connection := range v[0].([]Connection) {
c := r.AddConnection(connection)
log.Println("Initializing connection", c.name(), "...")
}
}
if len(v) > 1 {
if v[1] == nil {
v[1] = []Device{}
}
log.Println("Initializing devices...")
for _, device := range v[1].([]Device) {
d := r.AddDevice(device)
log.Println("Initializing device", d.name(), "...")
}
}
if len(v) > 2 {
if v[2] == nil {
v[2] = func() {}
}
r.Work = v[2].(func())
}
return r
}
func (r *Robot) AddDevice(d Device) *device {
device := NewDevice(d, r)
r.devices = append(r.devices, device)
return device
}
func (r *Robot) AddConnection(c Connection) *connection {
connection := NewConnection(c, r)
r.connections = append(r.connections, connection)
return connection
}
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
r.Commands[name] = f
}
func (r *Robot) Start() {
log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil {
panic("Could not start connections")
}
if err := r.Devices().Start(); err != nil {
panic("Could not start devices")
}
if r.Work != nil {
log.Println("Starting work...")
r.Work()
}
}
func (r *Robot) Devices() devices {
return devices(r.devices)
}
func (r *Robot) Device(name string) *device {
if r == nil {
return nil
}
for _, device := range r.devices {
if device.Name == name {
return device
}
}
return nil
}
func (r *Robot) Connections() connections {
return connections(r.connections)
}
func (r *Robot) Connection(name string) *connection {
if r == nil {
return nil
}
for _, connection := range r.connections {
if connection.Name == name {
return connection
}
}
return nil
}
func (r *Robot) ToJSON() *JSONRobot {
jsonRobot := &JSONRobot{
Name: r.Name,
Commands: []string{},
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
for command := range r.Commands {
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
for _, device := range r.Devices() {
jsonDevice := device.ToJSON()
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
}
return jsonRobot
}