Skip to content

Commit

Permalink
Refactor robot commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 11, 2014
1 parent f4cbfaf commit ca4d8ce
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
19 changes: 7 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"log"
"net/http"
"reflect"
)

// Optional restful API through Gobot has access
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions examples/hello_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions examples/hello_api_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)

Expand Down
33 changes: 16 additions & 17 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ type JSONRobot struct {
}

type Robot struct {
Name string `json:"-"`
Commands map[string]interface{} `json:"-"`
RobotCommands []string `json:"-"`
Work func() `json:"-"`
connections connections `json:"-"`
devices devices `json:"-"`
Name string `json:"-"`
Commands map[string]func(map[string]interface{}) interface{} `json:"-"`
Work func() `json:"-"`
connections connections `json:"-"`
devices devices `json:"-"`
}

type Robots []*Robot
Expand All @@ -39,8 +38,9 @@ func (r Robots) Each(f func(*Robot)) {

func NewRobot(name string, c []Connection, d []Device, work func()) *Robot {
r := &Robot{
Name: name,
Work: work,
Name: name,
Work: work,
Commands: make(map[string]func(map[string]interface{}) interface{}),
}
r.initName()
log.Println("Initializing Robot", r.Name, "...")
Expand All @@ -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")
}
Expand All @@ -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...")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ca4d8ce

Please sign in to comment.