Merge pull request #19 from hybridgroup/robeaux

Robeaux support
This commit is contained in:
Ron Evans 2014-01-27 13:34:55 -08:00
commit 4d560e56fc
8 changed files with 82 additions and 22 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "robeaux"]
path = robeaux
url = git://github.com/hybridgroup/robeaux.git

View File

@ -1,10 +1,10 @@
package gobot
type Adaptor struct {
Name string
Port string
Connected bool
Params map[string]interface{}
Name string `json:"name"`
Port string `json:"port"`
Connected bool `json:"Connected"`
Params map[string]interface{} `json:"params"`
}
type AdaptorInterface interface {

64
api.go
View File

@ -9,16 +9,42 @@ import (
type api struct{}
type jsonRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*jsonConnection `json:"connections"`
Devices []*jsonDevice `json:"devices"`
}
type jsonDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection *jsonConnection `json:"connection"`
Commands []string `json:"commands"`
}
type jsonConnection struct {
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
}
func Api(bot *Master) {
a := new(api)
m := martini.Classic()
m.Use(martini.Static("robeaux"))
m.Get("/robots", func() string {
return toJson(bot.Robots)
jsonRobots := make([]*jsonRobot, 0)
for _, robot := range bot.Robots {
jsonRobots = append(jsonRobots, a.formatJsonRobot(&robot))
}
return toJson(jsonRobots)
})
m.Get("/robots/:robotname", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]))
return toJson(a.formatJsonRobot(bot.FindRobot(params["robotname"])))
})
m.Get("/robots/:robotname/commands", func(params martini.Params) string {
@ -39,11 +65,16 @@ func Api(bot *Master) {
})
m.Get("/robots/:robotname/devices", func(params martini.Params) string {
return toJson(bot.FindRobot(params["robotname"]).GetDevices())
devices := bot.FindRobot(params["robotname"]).GetDevices()
jsonDevices := make([]*jsonDevice, 0)
for _, device := range devices {
jsonDevices = append(jsonDevices, a.formatJsonDevice(device))
}
return toJson(jsonDevices)
})
m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params) string {
return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"]))
return toJson(a.formatJsonDevice(bot.FindRobotDevice(params["robotname"], params["devicename"])))
})
m.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params) string {
@ -62,6 +93,31 @@ func Api(bot *Master) {
go m.Run()
}
func (a *api) formatJsonRobot(robot *Robot) *jsonRobot {
jsonRobot := new(jsonRobot)
jsonRobot.Name = robot.Name
jsonRobot.Commands = robot.RobotCommands
jsonRobot.Connections = make([]*jsonConnection, 0)
for _, device := range robot.devices {
jsonDevice := a.formatJsonDevice(device)
jsonRobot.Connections = append(jsonRobot.Connections, jsonDevice.Connection)
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
}
return jsonRobot
}
func (a *api) formatJsonDevice(device *device) *jsonDevice {
jsonDevice := new(jsonDevice)
jsonDevice.Name = device.Name
jsonDevice.Driver = FieldByNamePtr(device.Driver, "Name").Interface().(string)
jsonDevice.Connection = new(jsonConnection)
jsonDevice.Connection.Name = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string)
jsonDevice.Connection.Port = FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Port").Interface().(string)
jsonDevice.Connection.Adaptor = FieldByNamePtr(device.Driver, "Adaptor").Type().Name()
jsonDevice.Commands = FieldByNamePtr(device.Driver, "Commands").Interface().([]string)
return jsonDevice
}
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{}

View File

@ -5,8 +5,8 @@ import (
)
type connection struct {
Name string
Adaptor AdaptorInterface
Name string `json:"name"`
Adaptor AdaptorInterface `json:"adaptor"`
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`

View File

@ -5,10 +5,10 @@ import (
)
type device struct {
Name string
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver DriverInterface
Name string `json:"name"`
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver DriverInterface `json:"driver"`
}
type Device interface {

View File

@ -1,10 +1,10 @@
package gobot
type Driver struct {
Interval string
Pin string
Name string
Commands []string
Interval string `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands []string `json:"commands"`
Events map[string]chan interface{} `json:"-"`
}

1
robeaux Submodule

@ -0,0 +1 @@
Subproject commit 054706b48632176eec7e9311060a4c787ab8a746

View File

@ -8,11 +8,11 @@ import (
)
type Robot struct {
Connections []Connection
Devices []Device
Name string
Connections []Connection `json:"connections"`
Devices []Device `json:"devices"`
Name string `json:"name"`
Commands map[string]interface{} `json:"-"`
RobotCommands []string `json:"Commands"`
RobotCommands []string `json:"commands"`
Work func() `json:"-"`
connections []*connection `json:"-"`
devices []*device `json:"-"`