2014-04-30 04:20:32 +08:00
|
|
|
package api
|
2013-11-24 02:36:08 +08:00
|
|
|
|
2013-11-24 08:19:11 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-12-08 00:54:50 +08:00
|
|
|
"errors"
|
2014-08-02 12:37:16 +08:00
|
|
|
"fmt"
|
2014-04-23 11:48:08 +08:00
|
|
|
"log"
|
2013-11-24 08:19:11 +08:00
|
|
|
"net/http"
|
2014-10-30 04:18:54 +08:00
|
|
|
"net/http/httptest"
|
2014-07-02 14:10:12 +08:00
|
|
|
"strings"
|
2014-07-11 02:35:00 +08:00
|
|
|
|
|
|
|
"github.com/bmizerany/pat"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api/robeaux"
|
2013-11-24 08:19:11 +08:00
|
|
|
)
|
2013-11-24 02:36:08 +08:00
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
// API represents an API server
|
|
|
|
type API struct {
|
2016-10-16 02:02:54 +08:00
|
|
|
master *gobot.Master
|
2014-07-24 04:37:05 +08:00
|
|
|
router *pat.PatternServeMux
|
2014-04-30 04:20:32 +08:00
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Cert string
|
|
|
|
Key string
|
2014-07-22 13:19:04 +08:00
|
|
|
handlers []func(http.ResponseWriter, *http.Request)
|
2014-12-31 22:21:58 +08:00
|
|
|
start func(*API)
|
2014-01-28 05:15:52 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
// NewAPI returns a new api instance
|
2016-10-16 02:02:54 +08:00
|
|
|
func NewAPI(m *gobot.Master) *API {
|
2014-12-31 22:21:58 +08:00
|
|
|
return &API{
|
2016-10-16 02:02:54 +08:00
|
|
|
master: m,
|
2014-07-24 04:50:46 +08:00
|
|
|
router: pat.New(),
|
|
|
|
Port: "3000",
|
2014-12-31 22:21:58 +08:00
|
|
|
start: func(a *API) {
|
2014-07-22 12:14:00 +08:00
|
|
|
log.Println("Initializing API on " + a.Host + ":" + a.Port + "...")
|
2014-07-24 04:37:05 +08:00
|
|
|
http.Handle("/", a)
|
2014-07-02 14:10:12 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
go func() {
|
2014-07-22 12:14:00 +08:00
|
|
|
if a.Cert != "" && a.Key != "" {
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
} else {
|
2014-07-22 12:14:00 +08:00
|
|
|
log.Println("WARNING: API using insecure connection. " +
|
|
|
|
"We recommend using an SSL certificate with Gobot.")
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := http.ListenAndServe(a.Host+":"+a.Port, nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
},
|
2014-04-19 06:19:09 +08:00
|
|
|
}
|
2014-04-27 00:13:33 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// ServeHTTP calls api handlers and then serves request using api router
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
2014-07-24 04:37:05 +08:00
|
|
|
for _, handler := range a.handlers {
|
2014-10-30 04:18:54 +08:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
handler(rec, req)
|
|
|
|
for k, v := range rec.Header() {
|
|
|
|
res.Header()[k] = v
|
|
|
|
}
|
|
|
|
if rec.Code == http.StatusUnauthorized {
|
|
|
|
http.Error(res, "Not Authorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2014-07-24 04:37:05 +08:00
|
|
|
}
|
|
|
|
a.router.ServeHTTP(res, req)
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Post wraps api router Post call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Post(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Post(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Put wraps api router Put call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Put(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Put(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Delete wraps api router Delete call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Delete(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Del(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Options wraps api router Options call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Options(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Options(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Get wraps api router Get call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Get(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Get(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Head wraps api router Head call
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Head(path string, f func(http.ResponseWriter, *http.Request)) {
|
2014-07-24 04:37:05 +08:00
|
|
|
a.router.Head(path, http.HandlerFunc(f))
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// AddHandler appends handler to api handlers
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request)) {
|
2014-07-22 13:19:04 +08:00
|
|
|
a.handlers = append(a.handlers, f)
|
|
|
|
}
|
|
|
|
|
2018-08-16 17:22:22 +08:00
|
|
|
// Start initializes the api by setting up Robeaux web interface.
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Start() {
|
2018-08-16 05:17:57 +08:00
|
|
|
a.AddRobeauxRoutes()
|
|
|
|
|
|
|
|
a.start(a)
|
|
|
|
}
|
|
|
|
|
2018-08-17 16:32:06 +08:00
|
|
|
// StartWithoutDefaults initializes the api without setting up the default routes.
|
|
|
|
// Good for custom web interfaces.
|
|
|
|
func (a *API) StartWithoutDefaults() {
|
2018-08-16 05:17:57 +08:00
|
|
|
a.start(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddC3PIORoutes adds all of the standard C3PIO routes to the API.
|
2018-08-16 17:22:22 +08:00
|
|
|
// For more information, please see:
|
|
|
|
// http://cppp.io/
|
2018-08-16 05:17:57 +08:00
|
|
|
func (a *API) AddC3PIORoutes() {
|
2014-07-25 07:39:27 +08:00
|
|
|
mcpCommandRoute := "/api/commands/:command"
|
2015-03-16 23:53:05 +08:00
|
|
|
robotDeviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
|
2014-07-25 07:39:27 +08:00
|
|
|
robotCommandRoute := "/api/robots/:robot/commands/:command"
|
|
|
|
|
|
|
|
a.Get("/api/commands", a.mcpCommands)
|
2014-07-24 04:37:05 +08:00
|
|
|
a.Get(mcpCommandRoute, a.executeMcpCommand)
|
|
|
|
a.Post(mcpCommandRoute, a.executeMcpCommand)
|
2014-07-25 07:39:27 +08:00
|
|
|
a.Get("/api/robots", a.robots)
|
|
|
|
a.Get("/api/robots/:robot", a.robot)
|
|
|
|
a.Get("/api/robots/:robot/commands", a.robotCommands)
|
2014-07-24 04:37:05 +08:00
|
|
|
a.Get(robotCommandRoute, a.executeRobotCommand)
|
|
|
|
a.Post(robotCommandRoute, a.executeRobotCommand)
|
2014-07-25 07:39:27 +08:00
|
|
|
a.Get("/api/robots/:robot/devices", a.robotDevices)
|
|
|
|
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
|
2014-08-02 12:37:16 +08:00
|
|
|
a.Get("/api/robots/:robot/devices/:device/events/:event", a.robotDeviceEvent)
|
2014-07-25 07:39:27 +08:00
|
|
|
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
|
2015-03-16 23:53:05 +08:00
|
|
|
a.Get(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
|
|
|
|
a.Post(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
|
2014-07-25 07:39:27 +08:00
|
|
|
a.Get("/api/robots/:robot/connections", a.robotConnections)
|
|
|
|
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
|
|
|
|
a.Get("/api/", a.mcp)
|
2018-08-16 05:17:57 +08:00
|
|
|
}
|
2014-07-25 07:39:27 +08:00
|
|
|
|
2018-08-16 05:17:57 +08:00
|
|
|
// AddRobeauxRoutes adds all of the robeaux web interface routes to the API.
|
2018-08-16 17:22:22 +08:00
|
|
|
// The Robeaux web interface requires the C3PIO API, so it is also
|
|
|
|
// activated when you call this method.
|
2018-08-16 05:17:57 +08:00
|
|
|
func (a *API) AddRobeauxRoutes() {
|
2018-08-16 17:22:22 +08:00
|
|
|
a.AddC3PIORoutes()
|
|
|
|
|
2014-07-25 07:39:27 +08:00
|
|
|
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(res, req, "/index.html", http.StatusMovedPermanently)
|
|
|
|
})
|
2014-07-24 04:37:05 +08:00
|
|
|
a.Get("/index.html", a.robeaux)
|
|
|
|
a.Get("/images/:a", a.robeaux)
|
|
|
|
a.Get("/js/:a", a.robeaux)
|
|
|
|
a.Get("/js/:a/", a.robeaux)
|
|
|
|
a.Get("/js/:a/:b", a.robeaux)
|
|
|
|
a.Get("/css/:a", a.robeaux)
|
|
|
|
a.Get("/css/:a/", a.robeaux)
|
|
|
|
a.Get("/css/:a/:b", a.robeaux)
|
|
|
|
a.Get("/partials/:a", a.robeaux)
|
2014-06-12 06:37:49 +08:00
|
|
|
}
|
2014-04-19 13:44:50 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robeaux returns handler for robeaux routes.
|
|
|
|
// Writes asset in response and sets correct header
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robeaux(res http.ResponseWriter, req *http.Request) {
|
2014-07-02 14:10:12 +08:00
|
|
|
path := req.URL.Path
|
2014-07-11 02:35:00 +08:00
|
|
|
buf, err := robeaux.Asset(path[1:])
|
2014-07-02 14:10:12 +08:00
|
|
|
if err != nil {
|
2014-07-22 12:14:00 +08:00
|
|
|
http.Error(res, err.Error(), http.StatusNotFound)
|
2014-07-02 14:10:12 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t := strings.Split(path, ".")
|
|
|
|
if t[len(t)-1] == "js" {
|
|
|
|
res.Header().Set("Content-Type", "text/javascript; charset=utf-8")
|
|
|
|
} else if t[len(t)-1] == "css" {
|
|
|
|
res.Header().Set("Content-Type", "text/css; charset=utf-8")
|
2016-02-18 08:19:11 +08:00
|
|
|
} else if t[len(t)-1] == "html" {
|
|
|
|
res.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2014-07-02 14:10:12 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
if _, err := res.Write(buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-02 14:10:12 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// mcp returns MCP route handler.
|
|
|
|
// Writes JSON with gobot representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) mcp(res http.ResponseWriter, req *http.Request) {
|
2016-10-16 02:02:54 +08:00
|
|
|
a.writeJSON(map[string]interface{}{"MCP": gobot.NewJSONMaster(a.master)}, res)
|
2014-06-13 11:58:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// mcpCommands returns commands route handler.
|
|
|
|
// Writes JSON with global commands representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) mcpCommands(res http.ResponseWriter, req *http.Request) {
|
2016-10-16 02:02:54 +08:00
|
|
|
a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONMaster(a.master).Commands}, res)
|
2014-06-13 11:58:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robots returns route handler.
|
|
|
|
// Writes JSON with robots representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robots(res http.ResponseWriter, req *http.Request) {
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonRobots := []*gobot.JSONRobot{}
|
2016-10-16 02:02:54 +08:00
|
|
|
a.master.Robots().Each(func(r *gobot.Robot) {
|
2014-11-22 03:57:26 +08:00
|
|
|
jsonRobots = append(jsonRobots, gobot.NewJSONRobot(r))
|
2014-07-03 09:08:44 +08:00
|
|
|
})
|
2014-07-25 07:39:27 +08:00
|
|
|
a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res)
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robot returns route handler.
|
|
|
|
// Writes JSON with robot representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robot(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if robot, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"robot": robot}, res)
|
|
|
|
}
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotCommands returns commands route handler
|
|
|
|
// Writes JSON with robot commands representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotCommands(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if robot, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"commands": robot.Commands}, res)
|
|
|
|
}
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotDevices returns devices route handler.
|
|
|
|
// Writes JSON with robot devices representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotDevices(res http.ResponseWriter, req *http.Request) {
|
2016-10-16 02:02:54 +08:00
|
|
|
if robot := a.master.Robot(req.URL.Query().Get(":robot")); robot != nil {
|
2014-12-08 00:54:50 +08:00
|
|
|
jsonDevices := []*gobot.JSONDevice{}
|
|
|
|
robot.Devices().Each(func(d gobot.Device) {
|
|
|
|
jsonDevices = append(jsonDevices, gobot.NewJSONDevice(d))
|
|
|
|
})
|
|
|
|
a.writeJSON(map[string]interface{}{"devices": jsonDevices}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": "No Robot found with the name " + req.URL.Query().Get(":robot")}, res)
|
|
|
|
}
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotDevice returns device route handler.
|
|
|
|
// Writes JSON with robot device representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotDevice(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if device, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":device")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"device": device}, res)
|
|
|
|
}
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
|
2014-08-02 12:37:16 +08:00
|
|
|
f, _ := res.(http.Flusher)
|
|
|
|
|
2015-03-17 10:49:36 +08:00
|
|
|
dataChan := make(chan string)
|
2014-08-02 12:37:16 +08:00
|
|
|
|
|
|
|
res.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
res.Header().Set("Cache-Control", "no-cache")
|
|
|
|
res.Header().Set("Connection", "keep-alive")
|
|
|
|
|
2016-10-16 02:02:54 +08:00
|
|
|
device := a.master.Robot(req.URL.Query().Get(":robot")).
|
2016-08-30 23:10:50 +08:00
|
|
|
Device(req.URL.Query().Get(":device"))
|
|
|
|
|
2016-10-16 02:02:54 +08:00
|
|
|
if event := a.master.Robot(req.URL.Query().Get(":robot")).
|
2014-12-08 00:54:50 +08:00
|
|
|
Device(req.URL.Query().Get(":device")).(gobot.Eventer).
|
2016-08-30 23:10:50 +08:00
|
|
|
Event(req.URL.Query().Get(":event")); len(event) > 0 {
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := device.(gobot.Eventer).On(event, func(data interface{}) {
|
2014-08-02 12:37:16 +08:00
|
|
|
d, _ := json.Marshal(data)
|
2015-03-17 10:49:36 +08:00
|
|
|
dataChan <- string(d)
|
2023-06-13 01:51:25 +08:00
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-02 12:37:16 +08:00
|
|
|
|
2014-12-08 00:54:50 +08:00
|
|
|
for {
|
|
|
|
select {
|
2015-03-17 10:49:36 +08:00
|
|
|
case data := <-dataChan:
|
2014-12-08 00:54:50 +08:00
|
|
|
fmt.Fprintf(res, "data: %v\n\n", data)
|
|
|
|
f.Flush()
|
2023-05-19 20:16:22 +08:00
|
|
|
case <-req.Context().Done():
|
2014-12-08 00:54:50 +08:00
|
|
|
log.Println("Closing connection")
|
|
|
|
return
|
|
|
|
}
|
2014-08-02 12:37:16 +08:00
|
|
|
}
|
2014-12-08 00:54:50 +08:00
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{
|
2015-03-17 10:49:36 +08:00
|
|
|
"error": "No Event found with the name " + req.URL.Query().Get(":event"),
|
2014-12-08 00:54:50 +08:00
|
|
|
}, res)
|
2014-08-02 12:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotDeviceCommands returns device commands route handler
|
|
|
|
// writes JSON with robot device commands representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if device, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":device")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"commands": device.Commands}, res)
|
|
|
|
}
|
2013-11-24 02:36:08 +08:00
|
|
|
}
|
2013-11-25 07:47:48 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotConnections returns connections route handler
|
|
|
|
// writes JSON with robot connections representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotConnections(res http.ResponseWriter, req *http.Request) {
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonConnections := []*gobot.JSONConnection{}
|
2016-10-16 02:02:54 +08:00
|
|
|
if robot := a.master.Robot(req.URL.Query().Get(":robot")); robot != nil {
|
2014-12-08 00:54:50 +08:00
|
|
|
robot.Connections().Each(func(c gobot.Connection) {
|
|
|
|
jsonConnections = append(jsonConnections, gobot.NewJSONConnection(c))
|
|
|
|
})
|
|
|
|
a.writeJSON(map[string]interface{}{"connections": jsonConnections}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": "No Robot found with the name " + req.URL.Query().Get(":robot")}, res)
|
|
|
|
}
|
|
|
|
|
2014-04-19 13:44:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// robotConnection returns connection route handler
|
|
|
|
// writes JSON with robot connection representation
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) robotConnection(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if conn, err := a.jsonConnectionFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":connection")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.writeJSON(map[string]interface{}{"connection": conn}, res)
|
|
|
|
}
|
2014-04-19 13:44:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-14 00:44:47 +08:00
|
|
|
// executeMcpCommand calls a global command associated to requested route
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
|
2016-10-16 02:02:54 +08:00
|
|
|
a.executeCommand(a.master.Command(req.URL.Query().Get(":command")),
|
2014-07-24 02:24:41 +08:00
|
|
|
res,
|
|
|
|
req,
|
|
|
|
)
|
2014-06-13 11:58:54 +08:00
|
|
|
}
|
|
|
|
|
2016-07-14 00:44:47 +08:00
|
|
|
// executeRobotDeviceCommand calls a device command associated to requested route
|
2015-03-16 23:53:05 +08:00
|
|
|
func (a *API) executeRobotDeviceCommand(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if _, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"),
|
|
|
|
req.URL.Query().Get(":device")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.executeCommand(
|
2016-10-16 02:02:54 +08:00
|
|
|
a.master.Robot(req.URL.Query().Get(":robot")).
|
2014-12-08 00:54:50 +08:00
|
|
|
Device(req.URL.Query().Get(":device")).(gobot.Commander).
|
|
|
|
Command(req.URL.Query().Get(":command")),
|
|
|
|
res,
|
|
|
|
req,
|
|
|
|
)
|
|
|
|
}
|
2013-11-25 07:47:48 +08:00
|
|
|
}
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2016-07-14 00:44:47 +08:00
|
|
|
// executeRobotCommand calls a robot command associated to requested route
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
|
2014-12-08 00:54:50 +08:00
|
|
|
if _, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
|
|
|
|
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
|
|
|
|
} else {
|
|
|
|
a.executeCommand(
|
2016-10-16 02:02:54 +08:00
|
|
|
a.master.Robot(req.URL.Query().Get(":robot")).
|
2014-12-08 00:54:50 +08:00
|
|
|
Command(req.URL.Query().Get(":command")),
|
|
|
|
res,
|
|
|
|
req,
|
|
|
|
)
|
|
|
|
}
|
2014-07-24 02:24:41 +08:00
|
|
|
}
|
2014-07-22 12:14:00 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// executeCommand writes JSON response with `f` returned value.
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) executeCommand(f func(map[string]interface{}) interface{},
|
2014-07-24 02:24:41 +08:00
|
|
|
res http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
2014-06-12 06:37:49 +08:00
|
|
|
|
2014-07-22 13:19:04 +08:00
|
|
|
body := make(map[string]interface{})
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-12 07:44:23 +08:00
|
|
|
|
|
|
|
if f != nil {
|
2014-07-25 07:39:27 +08:00
|
|
|
a.writeJSON(map[string]interface{}{"result": f(body)}, res)
|
2014-04-16 10:19:14 +08:00
|
|
|
} else {
|
2014-12-08 00:54:50 +08:00
|
|
|
a.writeJSON(map[string]interface{}{"error": "Unknown Command"}, res)
|
2013-11-28 12:05:45 +08:00
|
|
|
}
|
2014-07-22 12:14:00 +08:00
|
|
|
}
|
2014-07-24 02:24:41 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// writeJSON writes `j` as JSON in response
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) writeJSON(j interface{}, res http.ResponseWriter) {
|
2014-07-24 02:24:41 +08:00
|
|
|
data, _ := json.Marshal(j)
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2023-06-13 01:51:25 +08:00
|
|
|
if _, err := res.Write(data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-24 02:24:41 +08:00
|
|
|
}
|
2014-09-19 11:12:49 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Debug add handler to api that prints each request
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) Debug() {
|
2014-09-19 11:12:49 +08:00
|
|
|
a.AddHandler(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Println(req)
|
|
|
|
})
|
|
|
|
}
|
2014-12-08 00:54:50 +08:00
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) jsonRobotFor(name string) (jrobot *gobot.JSONRobot, err error) {
|
2016-10-16 02:02:54 +08:00
|
|
|
if robot := a.master.Robot(name); robot != nil {
|
2014-12-08 00:54:50 +08:00
|
|
|
jrobot = gobot.NewJSONRobot(robot)
|
|
|
|
} else {
|
|
|
|
err = errors.New("No Robot found with the name " + name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) jsonDeviceFor(robot string, name string) (jdevice *gobot.JSONDevice, err error) {
|
2016-10-16 02:02:54 +08:00
|
|
|
if device := a.master.Robot(robot).Device(name); device != nil {
|
2014-12-08 00:54:50 +08:00
|
|
|
jdevice = gobot.NewJSONDevice(device)
|
|
|
|
} else {
|
|
|
|
err = errors.New("No Device found with the name " + name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:21:58 +08:00
|
|
|
func (a *API) jsonConnectionFor(robot string, name string) (jconnection *gobot.JSONConnection, err error) {
|
2016-10-16 02:02:54 +08:00
|
|
|
if connection := a.master.Robot(robot).Connection(name); connection != nil {
|
2014-12-08 00:54:50 +08:00
|
|
|
jconnection = gobot.NewJSONConnection(connection)
|
|
|
|
} else {
|
|
|
|
err = errors.New("No Connection found with the name " + name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|