forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hybridgroup#3 from hybridgroup/api
Gobot RESful API
- Loading branch information
Showing
10 changed files
with
252 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package gobot | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/codegangsta/martini" | ||
"net/http" | ||
) | ||
|
||
type api struct{} | ||
|
||
func Api(bot *Master) { | ||
a := new(api) | ||
m := martini.Classic() | ||
|
||
m.Get("/robots", func() string { | ||
return toJson(bot.Robots) | ||
}) | ||
|
||
m.Get("/robots/:robotname", func(params martini.Params) string { | ||
return toJson(bot.FindRobot(params["robotname"])) | ||
}) | ||
|
||
m.Get("/robots/:robotname/devices", func(params martini.Params) string { | ||
return toJson(bot.FindRobot(params["robotname"]).GetDevices()) | ||
}) | ||
|
||
m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params) string { | ||
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()) | ||
}) | ||
|
||
command_route := "/robots/:robotname/devices/:devicename/commands/:command" | ||
|
||
m.Get(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string { | ||
return a.executeCommand(bot, params, res, req) | ||
}) | ||
m.Post(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string { | ||
return a.executeCommand(bot, params, res, req) | ||
}) | ||
|
||
go m.Run() | ||
} | ||
|
||
func (a *api) executeCommand(bot *Master, params martini.Params, res http.ResponseWriter, req *http.Request) string { | ||
decoder := json.NewDecoder(req.Body) | ||
var body map[string]interface{} | ||
decoder.Decode(&body) | ||
robot := bot.FindRobotDevice(params["robotname"], params["devicename"]) | ||
commands := robot.Commands().([]string) | ||
for command := range commands { | ||
if commands[command] == params["command"] { | ||
ret := Call(robot.Driver, params["command"], body) | ||
return toJson(map[string]interface{}{"results": ret}) | ||
} | ||
} | ||
return toJson(map[string]interface{}{"results": "Unknown Command"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hybridgroup/gobot" | ||
"github.com/hybridgroup/gobot-sphero" | ||
) | ||
|
||
func main() { | ||
master := gobot.GobotMaster() | ||
gobot.Api(master) | ||
|
||
spheros := map[string]string{ | ||
"Sphero-BPO": "127.0.0.1:4560", | ||
} | ||
|
||
for name, port := range spheros { | ||
spheroAdaptor := new(gobotSphero.SpheroAdaptor) | ||
spheroAdaptor.Name = "sphero" | ||
spheroAdaptor.Port = port | ||
|
||
sphero := gobotSphero.NewSphero(spheroAdaptor) | ||
sphero.Name = "sphero" | ||
sphero.Interval = "0.5s" | ||
|
||
work := func() { | ||
sphero.SetRGB(uint8(255), uint8(0), uint8(0)) | ||
} | ||
|
||
master.Robots = append(master.Robots, gobot.Robot{ | ||
Name: name, | ||
Connections: []interface{}{spheroAdaptor}, | ||
Devices: []interface{}{sphero}, | ||
Work: work, | ||
}) | ||
} | ||
|
||
master.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hybridgroup/gobot" | ||
"github.com/hybridgroup/gobot-sphero" | ||
) | ||
|
||
func main() { | ||
master := gobot.GobotMaster() | ||
|
||
spheros := map[string]string{ | ||
"Sphero-BPO": "127.0.0.1:4560", | ||
} | ||
|
||
for name, port := range spheros { | ||
spheroAdaptor := new(gobotSphero.SpheroAdaptor) | ||
spheroAdaptor.Name = "sphero" | ||
spheroAdaptor.Port = port | ||
|
||
sphero := gobotSphero.NewSphero(spheroAdaptor) | ||
sphero.Name = "sphero" | ||
sphero.Interval = "0.5s" | ||
|
||
work := func() { | ||
sphero.SetRGB(uint8(255), uint8(0), uint8(0)) | ||
} | ||
|
||
master.Robots = append(master.Robots, gobot.Robot{ | ||
Name: name, | ||
Connections: []interface{}{spheroAdaptor}, | ||
Devices: []interface{}{sphero}, | ||
Work: work, | ||
}) | ||
} | ||
|
||
master.Robots = append(master.Robots, gobot.Robot{ | ||
Work: func() { | ||
sphero := master.FindRobot("Sphero-BPO") | ||
gobot.Every("1s", func() { | ||
gobot.Call(sphero.GetDevice("sphero").Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255))) | ||
}) | ||
}, | ||
}) | ||
|
||
master.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package gobot | ||
|
||
import "time" | ||
|
||
type Master struct { | ||
Robots []Robot | ||
} | ||
|
||
func GobotMaster() *Master { | ||
m := new(Master) | ||
return m | ||
} | ||
|
||
func (m *Master) Start() { | ||
for s := range m.Robots { | ||
go m.Robots[s].Start() | ||
} | ||
|
||
for { | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
} | ||
|
||
func (m *Master) FindRobot(name string) *Robot { | ||
for s := range m.Robots { | ||
if m.Robots[s].Name == name { | ||
return &m.Robots[s] | ||
} | ||
} | ||
return nil | ||
} | ||
func (m *Master) FindRobotDevice(name string, device string) *Device { | ||
for r := range m.Robots { | ||
if m.Robots[r].Name == name { | ||
for d := range m.Robots[r].devices { | ||
if m.Robots[r].devices[d].Name == device { | ||
return m.Robots[r].devices[d] | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
func (m *Master) FindRobotConnection(name string, connection string) *Connection { | ||
for r := range m.Robots { | ||
if m.Robots[r].Name == name { | ||
for c := range m.Robots[r].connections { | ||
if m.Robots[r].connections[c].Name == connection { | ||
return m.Robots[r].connections[c] | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters