Update api robeaux api compatibility

This commit is contained in:
Adrian Zankich 2014-04-18 22:44:50 -07:00
parent c394fa731b
commit 345b60d2ab
4 changed files with 44 additions and 7 deletions

40
api.go
View File

@ -119,6 +119,14 @@ func Api(bot *Master) *api {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})
m.Get("/robots/:robotname/connections", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connections(params["robotname"], res, req)
})
m.Get("/robots/:robotname/connections/:connectionname", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connection(params["robotname"], params["connectionname"], res, req)
})
return a
}
@ -167,6 +175,23 @@ func (me *api) robot_device_commands(robot string, device string, res http.Respo
res.Write(data)
}
func (me *api) robot_connections(name string, res http.ResponseWriter, req *http.Request) {
connections := me.master.FindRobot(name).GetConnections()
jsonConnections := make([]*jsonConnection, 0)
for _, connection := range connections {
jsonConnections = append(jsonConnections, me.formatJsonConnection(connection))
}
data, _ := json.Marshal(jsonConnections)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (me *api) robot_connection(robot string, connection string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(me.formatJsonConnection(me.master.FindRobotConnection(robot, connection)))
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (a *api) formatJsonRobot(robot *Robot) *jsonRobot {
jsonRobot := new(jsonRobot)
jsonRobot.Name = robot.Name
@ -180,14 +205,19 @@ func (a *api) formatJsonRobot(robot *Robot) *jsonRobot {
return jsonRobot
}
func (a *api) formatJsonConnection(connection *connection) *jsonConnection {
jsonConnection := new(jsonConnection)
jsonConnection.Name = connection.Name
jsonConnection.Port = connection.Port
jsonConnection.Adaptor = connection.Type
return jsonConnection
}
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.Driver = device.Type
jsonDevice.Connection = a.formatJsonConnection(a.master.FindRobotConnection(device.Robot.Name, FieldByNamePtr(FieldByNamePtr(device.Driver, "Adaptor").Interface().(AdaptorInterface), "Name").Interface().(string)))
jsonDevice.Commands = FieldByNamePtr(device.Driver, "Commands").Interface().([]string)
return jsonDevice
}

View File

@ -2,11 +2,13 @@ package gobot
import (
"log"
"reflect"
)
type connection struct {
Name string `json:"name"`
Adaptor AdaptorInterface `json:"adaptor"`
Type string `json:"adaptor"`
Adaptor AdaptorInterface `json:"-"`
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
@ -19,6 +21,7 @@ type Connection interface {
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection)
c.Type = reflect.ValueOf(adaptor).Type().String()
c.Name = FieldByNamePtr(adaptor, "Name").String()
c.Port = FieldByNamePtr(adaptor, "Port").String()
c.Params = make(map[string]interface{})

View File

@ -2,13 +2,15 @@ package gobot
import (
"log"
"reflect"
)
type device struct {
Name string `json:"name"`
Type string `json:"driver"`
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver DriverInterface `json:"driver"`
Driver DriverInterface `json:"-"`
}
type Device interface {
@ -19,6 +21,7 @@ type Device interface {
func NewDevice(driver DriverInterface, r *Robot) *device {
d := new(device)
d.Type = reflect.ValueOf(driver).Type().String()
d.Name = FieldByNamePtr(driver, "Name").String()
d.Robot = r
if FieldByNamePtr(driver, "Interval").String() == "" {

View File

@ -51,6 +51,7 @@ func (r *Robot) initName() {
}
func (r *Robot) initCommands() {
r.RobotCommands = make([]string, 0)
for k, _ := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}