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

195 lines
6.8 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-07 09:22:23 -07:00
"github.com/go-martini/martini"
2014-04-15 17:59:44 -07:00
"io/ioutil"
2013-11-23 16:19:11 -08:00
"net/http"
2013-11-27 20:05:45 -08:00
"reflect"
2013-11-23 16:19:11 -08:00
)
2013-11-23 10:36:08 -08:00
2014-04-15 17:59:44 -07:00
type api struct {
master *Master
}
2013-11-24 15:47:48 -08:00
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"`
}
2014-04-15 17:59:44 -07:00
var startApi = func(m *martini.ClassicMartini) {
go m.Run()
}
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
2013-11-23 10:36:08 -08:00
m := martini.Classic()
2014-01-27 13:26:43 -08:00
m.Use(martini.Static("robeaux"))
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) {
a.executeRobotCommand(bot, params, 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-15 17:59:44 -07:00
startApi(m)
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)
2013-11-23 10:36:08 -08:00
}
2013-11-24 15:47:48 -08:00
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) formatJsonDevice(device *device) *jsonDevice {
jsonDevice := new(jsonDevice)
jsonDevice.Name = device.Name
jsonDevice.Driver = FieldByNamePtr(device.Driver, "Name").Interface().(string)
jsonDevice.Connection = new(jsonConnection)
jsonDevice.Connection.Name = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string)
jsonDevice.Connection.Port = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Port").Interface().(string)
jsonDevice.Connection.Adaptor = FieldByNamePtr(device.Driver, "Adaptor").Type().Name()
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)
robot := a.master.FindRobotDevice(robotname, 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 := Call(robot.Driver, commandname, body)
data, _ = json.Marshal(map[string]interface{}{"result": ret[0].Interface()})
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
return
2013-11-24 15:47:48 -08:00
}
}
2014-04-15 17:59:44 -07:00
data, _ = json.Marshal(map[string]interface{}{"result": "Unknown Command"})
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 17:59:44 -07:00
func (a *api) executeRobotCommand(bot *Master, params martini.Params, res http.ResponseWriter, req *http.Request) string {
decoder := json.NewDecoder(req.Body)
var body map[string]interface{}
decoder.Decode(&body)
if len(body) == 0 {
body = map[string]interface{}{}
2013-11-27 20:05:45 -08:00
}
2014-04-15 17:59:44 -07:00
body["robotname"] = params["robotname"]
robot := bot.FindRobot(params["robotname"])
in := make([]reflect.Value, len(params))
//for k, param := range params {
// in[k] = reflect.ValueOf(param)
//}
ret := reflect.ValueOf(robot.Commands[params["command"]]).Call(in)
return toJson(map[string]interface{}{"result": ret[0].Interface()})
2013-11-27 20:05:45 -08:00
}