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

89 lines
2.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"
"github.com/codegangsta/martini"
"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
2013-11-24 15:47:48 -08:00
type api struct{}
2013-11-24 16:17:47 -08:00
func Api(bot *Master) {
2013-11-24 15:47:48 -08:00
a := new(api)
2013-11-23 10:36:08 -08:00
m := martini.Classic()
m.Get("/robots", func() string {
return toJson(bot.Robots)
})
m.Get("/robots/:robotname", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]))
})
2013-11-27 20:05:45 -08:00
m.Get("/robots/:robotname/commands", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]).RobotCommands)
})
robot_command_route := "/robots/:robotname/commands/:command"
m.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
decoder := json.NewDecoder(req.Body)
var body map[string]interface{}
decoder.Decode(&body)
2013-11-27 20:35:32 -08:00
if len(body) == 0 {
body = map[string]interface{}{}
}
body["robotname"] = params["robotname"]
2013-11-27 20:05:45 -08:00
return a.executeRobotCommand(bot, params, body)
})
2013-11-23 10:36:08 -08:00
m.Get("/robots/:robotname/devices", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]).GetDevices())
})
m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params) string {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]))
})
2013-11-24 14:41:36 -08:00
m.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params) string {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]).Commands())
})
2013-11-24 15:47:48 -08:00
command_route := "/robots/:robotname/devices/:devicename/commands/:command"
m.Get(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
return a.executeCommand(bot, params, res, req)
})
m.Post(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
return a.executeCommand(bot, params, res, req)
2013-11-23 16:19:11 -08:00
})
2013-11-23 10:36:08 -08:00
go m.Run()
}
2013-11-24 15:47:48 -08:00
2013-11-24 16:17:47 -08:00
func (a *api) executeCommand(bot *Master, params martini.Params, res http.ResponseWriter, req *http.Request) string {
2013-11-24 15:47:48 -08:00
decoder := json.NewDecoder(req.Body)
var body map[string]interface{}
decoder.Decode(&body)
robot := bot.FindRobotDevice(params["robotname"], params["devicename"])
commands := robot.Commands().([]string)
for command := range commands {
if commands[command] == params["command"] {
ret := Call(robot.Driver, params["command"], body)
return toJson(map[string]interface{}{"results": ret})
}
}
return toJson(map[string]interface{}{"results": "Unknown Command"})
}
2013-11-27 20:05:45 -08:00
func (a *api) executeRobotCommand(bot *Master, m_params martini.Params, params ...interface{}) string {
robot := bot.FindRobot(m_params["robotname"])
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
ret := reflect.ValueOf(robot.Commands[m_params["command"]]).Call(in)
return toJson(map[string]interface{}{"results": ret[0].Interface()})
}