1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

Remove ToJSON methods and create JSON constructors

This commit is contained in:
Adrian Zankich 2014-11-21 11:57:26 -08:00
parent a0a1322fb1
commit 59aee80c91
9 changed files with 83 additions and 65 deletions

View File

@ -6,5 +6,4 @@ type Adaptor interface {
Name() string
Port() string
String() string
ToJSON() *JSONConnection
}

View File

@ -160,13 +160,13 @@ func (a *api) robeaux(res http.ResponseWriter, req *http.Request) {
// mcp returns MCP route handler.
// Writes JSON with gobot representation
func (a *api) mcp(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"MCP": a.gobot.ToJSON()}, res)
a.writeJSON(map[string]interface{}{"MCP": gobot.NewJSONGobot(a.gobot)}, res)
}
// mcpCommands returns commands route handler.
// Writes JSON with global commands representation
func (a *api) mcpCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"commands": a.gobot.ToJSON().Commands}, res)
a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONGobot(a.gobot).Commands}, res)
}
// robots returns route handler.
@ -174,7 +174,7 @@ func (a *api) mcpCommands(res http.ResponseWriter, req *http.Request) {
func (a *api) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := []*gobot.JSONRobot{}
a.gobot.Robots().Each(func(r *gobot.Robot) {
jsonRobots = append(jsonRobots, r.ToJSON())
jsonRobots = append(jsonRobots, gobot.NewJSONRobot(r))
})
a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res)
}
@ -182,13 +182,13 @@ func (a *api) robots(res http.ResponseWriter, req *http.Request) {
// robot returns route handler.
// Writes JSON with robot representation
func (a *api) robot(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"robot": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON()}, res)
a.writeJSON(map[string]interface{}{"robot": gobot.NewJSONRobot(a.gobot.Robot(req.URL.Query().Get(":robot")))}, res)
}
// robotCommands returns commands route handler
// Writes JSON with robot commands representation
func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON().Commands}, res)
a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONRobot(a.gobot.Robot(req.URL.Query().Get(":robot"))).Commands}, res)
}
// robotDevices returns devices route handler.
@ -196,7 +196,7 @@ func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
jsonDevices := []*gobot.JSONDevice{}
a.gobot.Robot(req.URL.Query().Get(":robot")).Devices().Each(func(d gobot.Device) {
jsonDevices = append(jsonDevices, d.ToJSON())
jsonDevices = append(jsonDevices, gobot.NewJSONDevice(d))
})
a.writeJSON(map[string]interface{}{"devices": jsonDevices}, res)
}
@ -205,8 +205,8 @@ func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
// Writes JSON with robot device representation
func (a *api) robotDevice(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
map[string]interface{}{"device": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON()}, res,
map[string]interface{}{"device": gobot.NewJSONDevice(a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")))}, res,
)
}
@ -225,7 +225,7 @@ func (a *api) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Connection", "keep-alive")
gobot.On(a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).Event(req.URL.Query().Get(":event")),
Device(req.URL.Query().Get(":device")).(gobot.Eventer).Event(req.URL.Query().Get(":event")),
func(data interface{}) {
d, _ := json.Marshal(data)
msg <- string(d)
@ -247,8 +247,8 @@ func (a *api) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
// writes JSON with robot device commands representation
func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON().Commands}, res,
map[string]interface{}{"commands": gobot.NewJSONDevice(a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device"))).Commands}, res,
)
}
@ -257,7 +257,7 @@ func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
jsonConnections := []*gobot.JSONConnection{}
a.gobot.Robot(req.URL.Query().Get(":robot")).Connections().Each(func(c gobot.Connection) {
jsonConnections = append(jsonConnections, c.ToJSON())
jsonConnections = append(jsonConnections, gobot.NewJSONConnection(c))
})
a.writeJSON(map[string]interface{}{"connections": jsonConnections}, res)
}
@ -266,8 +266,8 @@ func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
// writes JSON with robot connection representation
func (a *api) robotConnection(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
map[string]interface{}{"connection": a.gobot.Robot(req.URL.Query().Get(":robot")).
Connection(req.URL.Query().Get(":connection")).ToJSON()},
map[string]interface{}{"connection": gobot.NewJSONConnection(a.gobot.Robot(req.URL.Query().Get(":robot")).
Connection(req.URL.Query().Get(":connection")))},
res,
)
}
@ -284,7 +284,7 @@ func (a *api) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
func (a *api) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
a.executeCommand(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).
Device(req.URL.Query().Get(":device")).(gobot.Commander).
Command(req.URL.Query().Get(":command")),
res,
req,

View File

@ -1,13 +1,11 @@
package gobot
import "errors"
type commander struct {
commands map[string]func(map[string]interface{}) interface{}
}
type Commander interface {
Command(string) (command func(map[string]interface{}) interface{}, err error)
Command(string) (command func(map[string]interface{}) interface{})
Commands() (commands map[string]func(map[string]interface{}) interface{})
AddCommand(name string, command func(map[string]interface{}) interface{})
}
@ -19,12 +17,8 @@ func NewCommander() Commander {
}
// Command retrieves a command by name
func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}, err error) {
command, ok := c.commands[name]
if ok {
return
}
err = errors.New("Unknown Command")
func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}) {
command, _ = c.commands[name]
return
}

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
)
// JSONConnection holds a JSON representation of a connection.
@ -12,6 +13,14 @@ type JSONConnection struct {
Adaptor string `json:"adaptor"`
}
// ToJSON returns a json representation of an adaptor
func NewJSONConnection(connection Connection) *JSONConnection {
return &JSONConnection{
Name: connection.Name(),
Adaptor: reflect.TypeOf(connection).String(),
}
}
type Connection Adaptor
type connections []Connection

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
)
// JSONDevice is a JSON representation of a Gobot Device.
@ -14,6 +15,22 @@ type JSONDevice struct {
Commands []string `json:"commands"`
}
func NewJSONDevice(device Device) *JSONDevice {
jsonDevice := &JSONDevice{
Name: device.Name(),
Driver: reflect.TypeOf(device).String(),
Commands: []string{},
Connection: "",
}
if device.Connection() != nil {
jsonDevice.Connection = device.Connection().Name()
}
for command := range device.(Commander).Commands() {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}
return jsonDevice
}
type Device Driver
type devices []Device

View File

@ -7,5 +7,4 @@ type Driver interface {
Pin() string
String() string
Connection() Connection
ToJSON() *JSONDevice
}

View File

@ -12,6 +12,23 @@ type JSONGobot struct {
Commands []string `json:"commands"`
}
// ToJSON returns a JSON representation of this Gobot.
func NewJSONGobot(gobot *Gobot) *JSONGobot {
jsonGobot := &JSONGobot{
Robots: []*JSONRobot{},
Commands: []string{},
}
for command := range gobot.Commands() {
jsonGobot.Commands = append(jsonGobot.Commands, command)
}
gobot.robots.Each(func(r *Robot) {
jsonGobot.Robots = append(jsonGobot.Robots, NewJSONRobot(r))
})
return jsonGobot
}
// Gobot is a container composed of one or more robots
type Gobot struct {
robots *robots
@ -92,20 +109,3 @@ func (g *Gobot) Robot(name string) *Robot {
}
return nil
}
// ToJSON returns a JSON representation of this Gobot.
func (g *Gobot) ToJSON() *JSONGobot {
jsonGobot := &JSONGobot{
Robots: []*JSONRobot{},
Commands: []string{},
}
for command := range g.Commands() {
jsonGobot.Commands = append(jsonGobot.Commands, command)
}
g.robots.Each(func(r *Robot) {
jsonGobot.Robots = append(jsonGobot.Robots, r.ToJSON())
})
return jsonGobot
}

View File

@ -39,7 +39,7 @@ func TestGobotToJSON(t *testing.T) {
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := g.ToJSON()
json := NewJSONGobot(g)
Assert(t, len(json.Robots), g.Robots().Len())
Assert(t, len(json.Commands), len(g.Commands()))
}

View File

@ -14,6 +14,27 @@ type JSONRobot struct {
Devices []*JSONDevice `json:"devices"`
}
// NewJSONRobot returns a JSON representation of the robot.
func NewJSONRobot(robot *Robot) *JSONRobot {
jsonRobot := &JSONRobot{
Name: robot.Name,
Commands: []string{},
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
for command := range robot.Commands() {
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
robot.Devices().Each(func(device Device) {
jsonDevice := NewJSONDevice(device)
jsonRobot.Connections = append(jsonRobot.Connections, NewJSONConnection(robot.Connection(jsonDevice.Connection)))
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
})
return jsonRobot
}
// Robot is a named entitity that manages a collection of connections and devices.
// It containes it's own work routine and a collection of
// custom commands to control a robot remotely via the Gobot api.
@ -168,24 +189,3 @@ func (r *Robot) Connection(name string) Connection {
}
return nil
}
// ToJSON returns a JSON representation of the robot.
func (r *Robot) ToJSON() *JSONRobot {
jsonRobot := &JSONRobot{
Name: r.Name,
Commands: []string{},
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
for command := range r.Commands() {
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
r.Devices().Each(func(device Device) {
jsonDevice := device.ToJSON()
jsonRobot.Connections = append(jsonRobot.Connections, r.Connection(jsonDevice.Connection).ToJSON())
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
})
return jsonRobot
}