Skip to content

Commit

Permalink
Add POST command
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 24, 2013
1 parent e8ee0c9 commit 58df3a8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gobot

import "github.com/codegangsta/martini"
import (
"encoding/json"
"github.com/codegangsta/martini"
"net/http"
)

func Api(bot *Gobot) {
m := martini.Classic()
Expand All @@ -21,5 +25,20 @@ func Api(bot *Gobot) {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]))
})

m.Post("/robots/:robotname/devices/:devicename/commands/:command", func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
decoder := json.NewDecoder(req.Body)
var response_hash map[string]interface{}
decoder.Decode(&response_hash)
robot := bot.FindRobotDevice(params["robotname"], params["devicename"])
p := make([]interface{}, len(response_hash))
i := 0
for _, v := range response_hash {
p[i] = v
i++
}
Call(robot.Driver, params["command"], p...)
return ""
})

go m.Run()
}

0 comments on commit 58df3a8

Please sign in to comment.