Skip to content

Commit

Permalink
Merge pull request #5 from hybridgroup/robot_functions_via_api
Browse files Browse the repository at this point in the history
Robot functions via api
  • Loading branch information
deadprogram committed Dec 2, 2013
2 parents 66cfedb + 77dd6db commit 5f60485
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
28 changes: 28 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/codegangsta/martini"
"net/http"
"reflect"
)

type api struct{}
Expand All @@ -20,6 +21,23 @@ func Api(bot *Master) {
return toJson(bot.FindRobot(params["robotname"]))
})

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

robot_command_route := "/robots/:robotname/commands/:command"

m.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
decoder := json.NewDecoder(req.Body)
var body map[string]interface{}
decoder.Decode(&body)
if len(body) == 0 {
body = map[string]interface{}{}
}
body["robotname"] = params["robotname"]
return a.executeRobotCommand(bot, params, body)
})

m.Get("/robots/:robotname/devices", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]).GetDevices())
})
Expand Down Expand Up @@ -58,3 +76,13 @@ func (a *api) executeCommand(bot *Master, params martini.Params, res http.Respon
}
return toJson(map[string]interface{}{"results": "Unknown Command"})
}

func (a *api) executeRobotCommand(bot *Master, m_params martini.Params, params ...interface{}) string {
robot := bot.FindRobot(m_params["robotname"])
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
ret := reflect.ValueOf(robot.Commands[m_params["command"]]).Call(in)
return toJson(map[string]interface{}{"results": ret[0].Interface()})
}
8 changes: 3 additions & 5 deletions examples/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import (
"fmt"
. "gobot"
"time"
"github.com/hybridgroup/gobot"
)

func main() {

robot := Robot{
robot := gobot.Robot{
Work: func() {
Every(300*time.Millisecond, func() { fmt.Println("Greetings human") })
gobot.Every("0.5s", func() { fmt.Println("Greetings human") })
},
}

Expand Down
25 changes: 25 additions & 0 deletions examples/hello_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"github.com/hybridgroup/gobot"
)

func Hello(params map[string]interface{}) string {
name := params["name"].(string)
return fmt.Sprintf("hi %v", name)
}

func main() {
master := gobot.GobotMaster()
gobot.Api(master)

hello := new(gobot.Robot)
hello.Name = "hello"
hello.Work = func() {}
hello.Commands = map[string]interface{}{"Hello": Hello}

master.Robots = append(master.Robots, *hello)

master.Start()
}
16 changes: 12 additions & 4 deletions examples/sphero_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import (
"github.com/hybridgroup/gobot-sphero"
)

var Master *gobot.Master = gobot.GobotMaster()

func TurnBlue(params map[string]interface{}) bool {
sphero := Master.FindRobotDevice(params["robotname"].(string), "sphero")
gobot.Call(sphero.Driver, "SetRGB", uint8(0), uint8(0), uint8(255))
return true
}

func main() {
master := gobot.GobotMaster()
gobot.Api(master)
gobot.Api(Master)

spheros := map[string]string{
"Sphero-BPO": "127.0.0.1:4560",
Expand All @@ -26,13 +33,14 @@ func main() {
sphero.SetRGB(uint8(255), uint8(0), uint8(0))
}

master.Robots = append(master.Robots, gobot.Robot{
Master.Robots = append(Master.Robots, gobot.Robot{
Name: name,
Connections: []interface{}{spheroAdaptor},
Devices: []interface{}{sphero},
Work: work,
Commands: map[string]interface{}{"TurnBlue": TurnBlue},
})
}

master.Start()
Master.Start()
}
9 changes: 3 additions & 6 deletions master.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gobot

import "time"

type Master struct {
Robots []Robot
}
Expand All @@ -15,10 +13,7 @@ func (m *Master) Start() {
for s := range m.Robots {
go m.Robots[s].Start()
}

for {
time.Sleep(10 * time.Millisecond)
}
select {}
}

func (m *Master) FindRobot(name string) *Robot {
Expand All @@ -29,6 +24,7 @@ func (m *Master) FindRobot(name string) *Robot {
}
return nil
}

func (m *Master) FindRobotDevice(name string, device string) *Device {
for r := range m.Robots {
if m.Robots[r].Name == name {
Expand All @@ -41,6 +37,7 @@ func (m *Master) FindRobotDevice(name string, device string) *Device {
}
return nil
}

func (m *Master) FindRobotConnection(name string, connection string) *Connection {
for r := range m.Robots {
if m.Robots[r].Name == name {
Expand Down
21 changes: 12 additions & 9 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

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

func (r *Robot) Start() {
Expand All @@ -22,14 +24,15 @@ func (r *Robot) Start() {
i := rand.Int()
r.Name = fmt.Sprintf("Robot %v", i)
}
for k, _ := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}
r.initConnections()
r.initDevices()
r.startConnections()
r.startDevices()
r.Work()
for {
time.Sleep(10 * time.Millisecond)
}
select {}
}

func (r *Robot) initConnections() {
Expand Down

0 comments on commit 5f60485

Please sign in to comment.