forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
275 lines (236 loc) · 9.15 KB
/
api.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package gobot
import (
"encoding/json"
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/cors"
"io/ioutil"
"log"
"net/http"
"reflect"
)
type api struct {
master *Master
server *martini.ClassicMartini
Host string
Port string
Username string
Password string
Cert string
Key string
}
type jsonRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*jsonConnection `json:"connections"`
Devices []*jsonDevice `json:"devices"`
}
type jsonDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection *jsonConnection `json:"connection"`
Commands []string `json:"commands"`
}
type jsonConnection struct {
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
}
var startApi = func(me *api) {
username := me.Username
if username != "" {
password := me.Password
me.server.Use(auth.Basic(username, password))
}
port := me.Port
if port == "" {
port = "3000"
}
host := me.Host
cert := me.Cert
key := me.Key
log.Println("Initializing API on " + host + ":" + port + "...")
if cert != "" && key != "" {
go http.ListenAndServeTLS(host+":"+port, cert, key, me.server)
} else {
log.Println("WARNING: API using insecure connection. We recommend using an SSL certificate with Gobot.")
go http.ListenAndServe(host+":"+port, me.server)
}
}
func (me *api) startApi() {
startApi(me)
}
func Api(bot *Master) *api {
a := new(api)
a.master = bot
bot.Api = a
m := martini.Classic()
a.server = m
m.Use(martini.Static("robeaux"))
m.Use(cors.Allow(&cors.Options{
AllowAllOrigins: true,
}))
m.Get("/robots", func(res http.ResponseWriter, req *http.Request) {
a.robots(res, req)
})
m.Get("/robots/:robotname", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot(params["robotname"], res, req)
})
m.Get("/robots/:robotname/commands", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_commands(params["robotname"], res, req)
})
robot_command_route := "/robots/:robotname/commands/:command"
m.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeRobotCommand(params["robotname"], params["command"], res, req)
})
m.Post(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeRobotCommand(params["robotname"], params["command"], res, req)
})
m.Get("/robots/:robotname/devices", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_devices(params["robotname"], res, req)
})
m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_device(params["robotname"], params["devicename"], res, req)
})
m.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_device_commands(params["robotname"], params["devicename"], res, req)
})
command_route := "/robots/:robotname/devices/:devicename/commands/:command"
m.Get(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})
m.Post(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})
m.Get("/robots/:robotname/connections", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connections(params["robotname"], res, req)
})
m.Get("/robots/:robotname/connections/:connectionname", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connection(params["robotname"], params["connectionname"], res, req)
})
return a
}
func (me *api) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := make([]*jsonRobot, 0)
for _, robot := range me.master.Robots {
jsonRobots = append(jsonRobots, me.formatJsonRobot(robot))
}
data, _ := json.Marshal(jsonRobots)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot(name string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.formatJsonRobot(me.master.FindRobot(name)))
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_commands(name string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.master.FindRobot(name).RobotCommands)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_devices(name string, res http.ResponseWriter, req *http.Request) {
devices := me.master.FindRobot(name).GetDevices()
jsonDevices := make([]*jsonDevice, 0)
for _, device := range devices {
jsonDevices = append(jsonDevices, me.formatJsonDevice(device))
}
data, _ := json.Marshal(jsonDevices)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_device(robot string, device string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.formatJsonDevice(me.master.FindRobotDevice(robot, device)))
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_device_commands(robot string, device string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.master.FindRobotDevice(robot, device).Commands())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_connections(name string, res http.ResponseWriter, req *http.Request) {
connections := me.master.FindRobot(name).GetConnections()
jsonConnections := make([]*jsonConnection, 0)
for _, connection := range connections {
jsonConnections = append(jsonConnections, me.formatJsonConnection(connection))
}
data, _ := json.Marshal(jsonConnections)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_connection(robot string, connection string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.formatJsonConnection(me.master.FindRobotConnection(robot, connection)))
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (a *api) formatJsonRobot(robot *Robot) *jsonRobot {
jsonRobot := new(jsonRobot)
jsonRobot.Name = robot.Name
jsonRobot.Commands = robot.RobotCommands
jsonRobot.Connections = make([]*jsonConnection, 0)
for _, device := range robot.devices {
jsonDevice := a.formatJsonDevice(device)
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
}
return jsonRobot
}
func (a *api) formatJsonConnection(connection *connection) *jsonConnection {
jsonConnection := new(jsonConnection)
jsonConnection.Name = connection.Name
jsonConnection.Port = connection.Port
jsonConnection.Adaptor = connection.Type
return jsonConnection
}
func (a *api) formatJsonDevice(device *device) *jsonDevice {
jsonDevice := new(jsonDevice)
jsonDevice.Name = device.Name
jsonDevice.Driver = device.Type
jsonDevice.Connection = a.formatJsonConnection(a.master.FindRobotConnection(device.Robot.Name, FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string)))
jsonDevice.Commands = FieldByNamePtr(device.Driver, "Commands").Interface().([]string)
return jsonDevice
}
func (a *api) executeCommand(robotname string, devicename string, commandname string, res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
var body map[string]interface{}
json.Unmarshal(data, &body)
robot := a.master.FindRobotDevice(robotname, devicename)
commands := robot.Commands().([]string)
for command := range commands {
if commands[command] == commandname {
ret := make([]interface{}, 0)
for _, v := range Call(robot.Driver, commandname, body) {
ret = append(ret, v.Interface())
}
data, _ = json.Marshal(ret)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
return
}
}
data, _ = json.Marshal([]interface{}{"Unknown Command"})
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (a *api) executeRobotCommand(robotname string, commandname string, res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
body := make(map[string]interface{})
json.Unmarshal(data, &body)
robot := a.master.FindRobot(robotname)
in := make([]reflect.Value, 1)
body["robotname"] = robotname
in[0] = reflect.ValueOf(body)
command := robot.Commands[commandname]
if command != nil {
ret := make([]interface{}, 0)
for _, v := range reflect.ValueOf(robot.Commands[commandname]).Call(in) {
ret = append(ret, v.Interface())
}
data, _ = json.Marshal(ret)
} else {
data, _ = json.Marshal([]interface{}{"Unknown Command"})
}
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}