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

281 lines
9.0 KiB
Go
Raw Normal View History

2013-11-23 10:36:08 -08:00
package gobot
2013-11-23 16:19:11 -08:00
import (
"encoding/json"
2014-04-15 17:59:44 -07:00
"io/ioutil"
2014-04-22 20:48:08 -07:00
"log"
2013-11-23 16:19:11 -08:00
"net/http"
2013-11-27 20:05:45 -08:00
"reflect"
2014-04-26 10:13:33 -06:00
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/cors"
2013-11-23 16:19:11 -08:00
)
2013-11-23 10:36:08 -08:00
2014-04-26 10:13:33 -06:00
type startFuncAlias func(*api)
2013-11-24 15:47:48 -08:00
2014-04-26 10:13:33 -06:00
// Optional restful API through the master to access
// all the robots.
type api struct {
master *Master
server *martini.ClassicMartini
Host string
Port string
Username string
Password string
Cert string
Key string
startFunc startFuncAlias
}
2014-04-26 10:13:33 -06:00
func NewApi() *api {
return &api{startFunc: defaultStartFunc}
}
2014-04-26 10:13:33 -06:00
var defaultStartFunc = func(a *api) {
if a == nil {
return
}
2014-04-26 10:13:33 -06:00
username := a.Username
2014-04-18 15:45:42 -07:00
if username != "" {
2014-04-26 10:13:33 -06:00
password := a.Password
a.server.Use(auth.Basic(username, password))
2014-04-18 15:45:42 -07:00
}
2014-04-26 10:13:33 -06:00
port := a.Port
if port == "" {
port = "3000"
}
2014-04-26 10:13:33 -06:00
host := a.Host
cert := a.Cert
key := a.Key
2014-04-18 16:04:49 -07:00
2014-04-22 20:48:08 -07:00
log.Println("Initializing API on " + host + ":" + port + "...")
2014-04-26 10:13:33 -06:00
go func() {
if cert != "" && key != "" {
http.ListenAndServeTLS(host+":"+port, cert, key, a.server)
} else {
log.Println("WARNING: API using insecure connection. We recommend using an SSL certificate with Gobot.")
http.ListenAndServe(host+":"+port, a.server)
}
}()
}
// start starts the api using the start function
// sets on the API on initialization.
func (a *api) start() {
if a == nil {
return
2014-04-18 16:04:49 -07:00
}
2014-04-26 10:13:33 -06:00
a.startFunc(a)
2014-04-18 14:45:50 -07:00
}
2014-04-15 17:59:44 -07:00
func Api(bot *Master) *api {
2013-11-24 15:47:48 -08:00
a := new(api)
2014-04-15 17:59:44 -07:00
a.master = bot
2014-04-18 14:39:17 -07:00
bot.Api = a
2013-11-23 10:36:08 -08:00
m := martini.Classic()
2014-04-18 14:39:17 -07:00
a.server = m
2013-11-23 10:36:08 -08:00
2014-01-27 13:26:43 -08:00
m.Use(martini.Static("robeaux"))
2014-04-22 20:48:08 -07:00
m.Use(cors.Allow(&cors.Options{
AllowAllOrigins: true,
}))
2014-01-27 13:26:43 -08:00
2014-04-15 17:59:44 -07:00
m.Get("/robots", func(res http.ResponseWriter, req *http.Request) {
a.robots(res, req)
2013-11-23 10:36:08 -08:00
})
2014-04-15 17:59:44 -07:00
m.Get("/robots/:robotname", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot(params["robotname"], res, req)
2013-11-23 10:36:08 -08:00
})
2014-04-15 17:59:44 -07:00
m.Get("/robots/:robotname/commands", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_commands(params["robotname"], res, req)
2013-11-27 20:05:45 -08:00
})
robot_command_route := "/robots/:robotname/commands/:command"
2014-04-15 17:59:44 -07:00
m.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
2014-04-15 19:19:14 -07:00
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)
2013-11-27 20:05:45 -08:00
})
2014-04-15 17:59:44 -07:00
m.Get("/robots/:robotname/devices", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_devices(params["robotname"], res, req)
2013-11-23 10:36:08 -08:00
})
2014-04-15 17:59:44 -07:00
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)
2013-11-23 10:36:08 -08:00
})
2014-04-15 17:59:44 -07:00
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)
2013-11-24 14:41:36 -08:00
})
2013-11-24 15:47:48 -08:00
command_route := "/robots/:robotname/devices/:devicename/commands/:command"
2014-04-15 17:59:44 -07:00
m.Get(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
2013-11-24 15:47:48 -08:00
})
2014-04-15 17:59:44 -07:00
m.Post(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
2013-11-23 16:19:11 -08:00
})
2014-04-18 22:44:50 -07:00
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)
})
2014-04-15 17:59:44 -07:00
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) {
2014-04-26 10:13:33 -06:00
data, _ := json.Marshal(me.formatJsonDevice(me.master.FindRobot(robot).GetDevice(device)))
2014-04-15 17:59:44 -07:00
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) {
2014-04-26 10:13:33 -06:00
data, _ := json.Marshal(me.master.FindRobot(robot).GetDevice(device).Commands())
2014-04-15 17:59:44 -07:00
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
2013-11-23 10:36:08 -08:00
}
2013-11-24 15:47:48 -08:00
2014-04-18 22:44:50 -07:00
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) {
2014-04-26 10:13:33 -06:00
data, _ := json.Marshal(me.formatJsonConnection(me.master.FindRobot(robot).GetConnection(connection)))
2014-04-18 22:44:50 -07:00
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
}
2014-04-18 22:44:50 -07:00
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
2014-04-18 22:44:50 -07:00
jsonDevice.Driver = device.Type
2014-04-26 10:13:33 -06:00
jsonDevice.Connection = a.formatJsonConnection(
a.master.FindRobot(device.Robot.Name).
GetConnection(FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").
Interface().(AdaptorInterface), "Name").
Interface().(string)))
jsonDevice.Commands = FieldByNamePtr(device.Driver, "Commands").Interface().([]string)
return jsonDevice
}
2014-04-15 17:59:44 -07:00
func (a *api) executeCommand(robotname string, devicename string, commandname string, res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
2013-11-24 15:47:48 -08:00
var body map[string]interface{}
2014-04-15 17:59:44 -07:00
json.Unmarshal(data, &body)
2014-04-26 10:13:33 -06:00
robot := a.master.FindRobot(robotname).GetDevice(devicename)
2013-11-24 15:47:48 -08:00
commands := robot.Commands().([]string)
for command := range commands {
2014-04-15 17:59:44 -07:00
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)
2014-04-15 17:59:44 -07:00
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
return
2013-11-24 15:47:48 -08:00
}
}
data, _ = json.Marshal([]interface{}{"Unknown Command"})
2014-04-15 17:59:44 -07:00
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
2013-11-24 15:47:48 -08:00
}
2013-11-27 20:05:45 -08:00
2014-04-15 19:19:14 -07:00
func (a *api) executeRobotCommand(robotname string, commandname string, res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
2014-04-22 20:48:08 -07:00
body := make(map[string]interface{})
2014-04-15 19:19:14 -07:00
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)
2014-04-15 19:19:14 -07:00
} else {
data, _ = json.Marshal([]interface{}{"Unknown Command"})
2013-11-27 20:05:45 -08:00
}
2014-04-15 19:19:14 -07:00
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
2013-11-27 20:05:45 -08:00
}