Skip to content

Commit

Permalink
Remove ToJSON methods and create JSON constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 21, 2014
1 parent a0a1322 commit 59aee80
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 65 deletions.
1 change: 0 additions & 1 deletion adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ type Adaptor interface {
Name() string
Port() string
String() string
ToJSON() *JSONConnection
}
30 changes: 15 additions & 15 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,43 +160,43 @@ 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.
// Writes JSON with robots representation
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)
}

// 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.
// Writes JSON with robot devices representation
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)
}
Expand All @@ -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,
)
}

Expand All @@ -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)
Expand All @@ -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,
)
}

Expand All @@ -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)
}
Expand All @@ -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,
)
}
Expand All @@ -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,
Expand Down
12 changes: 3 additions & 9 deletions commander.go
Original file line number Diff line number Diff line change
@@ -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{})
}
Expand All @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
)

// JSONConnection holds a JSON representation of a connection.
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
)

// JSONDevice is a JSON representation of a Gobot Device.
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ type Driver interface {
Pin() string
String() string
Connection() Connection
ToJSON() *JSONDevice
}
34 changes: 17 additions & 17 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion gobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
42 changes: 21 additions & 21 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 59aee80

Please sign in to comment.