diff --git a/api.go b/api.go index 73cfe3fd6..2fa487102 100644 --- a/api.go +++ b/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/codegangsta/martini" "net/http" + "reflect" ) type api struct{} @@ -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()) }) @@ -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()}) +} diff --git a/examples/hello.go b/examples/hello.go index 01e3b5ad2..11722d912 100644 --- a/examples/hello.go +++ b/examples/hello.go @@ -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") }) }, } diff --git a/examples/hello_api.go b/examples/hello_api.go new file mode 100644 index 000000000..969a5df82 --- /dev/null +++ b/examples/hello_api.go @@ -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() +} diff --git a/examples/sphero_api.go b/examples/sphero_api.go index e06c66f82..ae111077d 100644 --- a/examples/sphero_api.go +++ b/examples/sphero_api.go @@ -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", @@ -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() } diff --git a/master.go b/master.go index c87fa52ae..d1aaddbe6 100644 --- a/master.go +++ b/master.go @@ -1,7 +1,5 @@ package gobot -import "time" - type Master struct { Robots []Robot } @@ -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 { @@ -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 { @@ -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 { diff --git a/robot.go b/robot.go index 97e166744..313a24d37 100644 --- a/robot.go +++ b/robot.go @@ -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() { @@ -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() {