Refactor robot commands

This commit is contained in:
Adrian Zankich 2014-06-11 16:44:23 -07:00
parent f4cbfaf4e7
commit ca4d8ce583
5 changed files with 35 additions and 44 deletions

View File

@ -9,7 +9,6 @@ import (
"io/ioutil"
"log"
"net/http"
"reflect"
)
// Optional restful API through Gobot has access
@ -138,7 +137,7 @@ func (a *api) robot(res http.ResponseWriter, req *http.Request) {
func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
robot := req.URL.Query().Get(":robot")
data, _ := json.Marshal(a.gobot.Robot(robot).RobotCommands)
data, _ := json.Marshal(a.gobot.Robot(robot).ToJSON().Commands)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
@ -231,19 +230,15 @@ func (a *api) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
body := make(map[string]interface{})
json.Unmarshal(data, &body)
r := a.gobot.Robot(robot)
in := make([]reflect.Value, 1)
body["robot"] = robot
in[0] = reflect.ValueOf(body)
c := r.Commands[command]
if c != nil {
ret := []interface{}{}
for _, v := range reflect.ValueOf(c).Call(in) {
ret = append(ret, v.Interface())
}
data, _ = json.Marshal(ret)
f := r.Commands[command]
if f != nil {
data, _ = json.Marshal(f(body))
} else {
data, _ = json.Marshal([]interface{}{"Unknown Command"})
data, _ = json.Marshal("Unknown Command")
}
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

View File

@ -89,9 +89,9 @@ var _ = Describe("API", func() {
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
var i interface{}
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("Unknown Command"))
Expect(i).To(Equal("Unknown Command"))
})
It("should return robot device", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)

View File

@ -6,17 +6,16 @@ import (
"github.com/hybridgroup/gobot/api"
)
func Hello(params map[string]interface{}) string {
name := params["name"].(string)
return fmt.Sprintf("hi %v", name)
}
func main() {
master := gobot.NewGobot()
api.NewAPI(master).Start()
a := api.NewAPI(master)
a.Start()
hello := gobot.NewRobot("hello", nil, nil, nil)
hello.Commands = map[string]interface{}{"Hello": Hello}
hello.AddCommand("HiThere", func(params map[string]interface{}) interface{} {
return []string{fmt.Sprintf("Hey"), fmt.Sprintf("dude!")}
})
master.Robots = append(master.Robots, hello)

View File

@ -6,11 +6,6 @@ import (
"github.com/hybridgroup/gobot/api"
)
func Hello(params map[string]interface{}) string {
name := params["name"].(string)
return fmt.Sprintf("hi %v", name)
}
func main() {
master := gobot.NewGobot()
@ -20,7 +15,10 @@ func main() {
server.Start()
hello := gobot.NewRobot("hello", nil, nil, nil)
hello.Commands = map[string]interface{}{"Hello": Hello}
hello.AddCommand("HiThere", func(params map[string]interface{}) interface{} {
return []string{fmt.Sprintf("Hey"), fmt.Sprintf("dude!")}
})
master.Robots = append(master.Robots, hello)

View File

@ -16,8 +16,7 @@ type JSONRobot struct {
type Robot struct {
Name string `json:"-"`
Commands map[string]interface{} `json:"-"`
RobotCommands []string `json:"-"`
Commands map[string]func(map[string]interface{}) interface{} `json:"-"`
Work func() `json:"-"`
connections connections `json:"-"`
devices devices `json:"-"`
@ -41,6 +40,7 @@ func NewRobot(name string, c []Connection, d []Device, work func()) *Robot {
r := &Robot{
Name: name,
Work: work,
Commands: make(map[string]func(map[string]interface{}) interface{}),
}
r.initName()
log.Println("Initializing Robot", r.Name, "...")
@ -49,9 +49,12 @@ func NewRobot(name string, c []Connection, d []Device, work func()) *Robot {
return r
}
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
r.Commands[name] = f
}
func (r *Robot) Start() {
log.Println("Starting Robot", r.Name, "...")
r.initCommands()
if err := r.Connections().Start(); err != nil {
panic("Could not start connections")
}
@ -72,13 +75,6 @@ func (r *Robot) initName() {
}
}
func (r *Robot) initCommands() {
r.RobotCommands = make([]string, 0)
for k := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}
}
func (r *Robot) initConnections(c []Connection) {
r.connections = make(connections, len(c))
log.Println("Initializing connections...")
@ -132,10 +128,13 @@ func (r *Robot) Connection(name string) *connection {
func (r *Robot) ToJSON() *JSONRobot {
jsonRobot := &JSONRobot{
Name: r.Name,
Commands: r.RobotCommands,
Commands: []string{},
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
for command := range r.Commands {
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
for _, device := range r.Devices() {
jsonDevice := device.ToJSON()
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)