Skip to content

Commit

Permalink
Add api commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 24, 2013
1 parent 58df3a8 commit cfe0d71
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
18 changes: 11 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ func Api(bot *Gobot) {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]))
})

m.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params) string {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]).Commands())
})

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++
commands := robot.Commands().([]string)
for command := range commands {
if commands[command] == params["command"] {
ret := Call(robot.Driver, params["command"], response_hash)
return toJson(map[string]interface{}{"results": ret})
}
}
Call(robot.Driver, params["command"], p...)
return ""
return toJson(map[string]interface{}{"results": "Unknown Command"})
})

go m.Run()
Expand Down
4 changes: 2 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func (d *Device) Start() {
}
}

func (d *Device) Command(method_name string, arguments []string) {
//dt.Driver.Command(method_name, arguments)
func (d *Device) Commands() interface{} {
return reflect.ValueOf(d.Driver).Elem().FieldByName("Commands").Interface()
}
1 change: 1 addition & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Driver struct {
Pin string
Name string
Params map[string]string
Commands []string
Events map[string]chan interface{} `json:"-"`
}

Expand Down
5 changes: 2 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ func ConnectTo(port string) net.Conn {
return tcpPort
}

func Call(thing interface{}, method string, params ...interface{}) (result []reflect.Value, err error) {
func Call(thing interface{}, method string, params ...interface{}) []reflect.Value {
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
result = reflect.ValueOf(thing).MethodByName(method).Call(in)
return
return reflect.ValueOf(thing).MethodByName(method).Call(in)
}

func toJson(obj interface{}) string {
Expand Down

0 comments on commit cfe0d71

Please sign in to comment.