Skip to content

Commit

Permalink
Fix errors in API
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 3, 2014
1 parent bd8d31e commit 958d194
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 70 deletions.
1 change: 1 addition & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AdaptorInterface interface {
name() string
setName(string)
params() map[string]interface{}
ToJSON() *JSONConnection
}

func (a *Adaptor) port() string {
Expand Down
22 changes: 10 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func (a *api) commands(res http.ResponseWriter, req *http.Request) {

func (a *api) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := []*gobot.JSONRobot{}
for _, robot := range a.gobot.Robots {
jsonRobots = append(jsonRobots, robot.ToJSON())
}
a.gobot.Robots().Each(func(r *gobot.Robot) {
jsonRobots = append(jsonRobots, r.ToJSON())
})
data, _ := json.Marshal(jsonRobots)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
Expand All @@ -159,11 +159,10 @@ func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
robot := req.URL.Query().Get(":robot")

devices := a.gobot.Robot(robot).Devices()
jsonDevices := []*gobot.JSONDevice{}
for _, device := range devices {
jsonDevices = append(jsonDevices, device.ToJSON())
}
a.gobot.Robot(robot).Devices().Each(func(d gobot.Device) {
jsonDevices = append(jsonDevices, d.ToJSON())
})
data, _ := json.Marshal(jsonDevices)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
Expand All @@ -190,11 +189,10 @@ func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
robot := req.URL.Query().Get(":robot")

connections := a.gobot.Robot(robot).Connections()
jsonConnections := []*gobot.JSONConnection{}
for _, connection := range connections {
jsonConnections = append(jsonConnections, connection.ToJSON())
}
a.gobot.Robot(robot).Connections().Each(func(c gobot.Connection) {
jsonConnections = append(jsonConnections, c.ToJSON())
})
data, _ := json.Marshal(jsonConnections)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
Expand All @@ -215,7 +213,7 @@ func (a *api) executeCommand(res http.ResponseWriter, req *http.Request) {
data, _ := ioutil.ReadAll(req.Body)
body := make(map[string]interface{})
json.Unmarshal(data, &body)
f := a.gobot.Commands[command]
f := a.gobot.Commands()[command]

if f != nil {
data, _ = json.Marshal(f(body))
Expand Down
8 changes: 3 additions & 5 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func initTestAPI() *api {
a.start = func(m *api) {}
a.Start()

g.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
}
g.Robots().Add(gobot.NewTestRobot("Robot 1"))
g.Robots().Add(gobot.NewTestRobot("Robot 2"))
g.Robots().Add(gobot.NewTestRobot("Robot 3"))

return a
}
Expand Down
17 changes: 0 additions & 17 deletions command.go

This file was deleted.

18 changes: 9 additions & 9 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Driver struct {
Interval time.Duration
Pin string
Name string
Commands map[string]func(map[string]interface{}) interface{}
commands map[string]func(map[string]interface{}) interface{}
Events map[string]*Event
Type string
}
Expand All @@ -23,7 +23,7 @@ type DriverInterface interface {
interval() time.Duration
setName(string)
name() string
commands() map[string]func(map[string]interface{}) interface{}
Commands() map[string]func(map[string]interface{}) interface{}
ToJSON() *JSONDevice
}

Expand All @@ -47,23 +47,23 @@ func (d *Driver) name() string {
return d.Name
}

func (d *Driver) commands() map[string]func(map[string]interface{}) interface{} {
return d.Commands
func (d *Driver) Commands() map[string]func(map[string]interface{}) interface{} {
return d.commands
}

func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) {
d.Commands[name] = f
d.Commands()[name] = f
}

func NewDriver(name string, t string, commands Commands, a AdaptorInterface) *Driver {
func NewDriver(name string, t string, a AdaptorInterface) *Driver {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
return &Driver{
Type: t,
Name: name,
Interval: 10 * time.Millisecond,
Commands: commands,
commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a,
}
}
Expand All @@ -77,10 +77,10 @@ func (d *Driver) ToJSON() *JSONDevice {
}

if d.adaptor() != nil {
//jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
jsonDevice.Connection = d.adaptor().ToJSON()
}

commands := d.commands()
commands := d.Commands()
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}
Expand Down
12 changes: 6 additions & 6 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ type JSONGobot struct {

type Gobot struct {
robots *robots
commands commands
commands map[string]func(map[string]interface{}) interface{}
trap func(chan os.Signal)
}

func NewGobot() *Gobot {
return &Gobot{
robots: &robots{},
commands: make(commands),
commands: make(map[string]func(map[string]interface{}) interface{}),
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
}
}

func (g *Gobot) Commands() commands {
func (g *Gobot) Commands() map[string]func(map[string]interface{}) interface{} {
return g.commands
}

Expand Down Expand Up @@ -65,9 +65,9 @@ func (g *Gobot) ToJSON() *JSONGobot {
Commands: []string{},
}

g.commands.Each(func(c Command) {
jsonGobot.Commands = append(jsonGobot.Commands, c.Name)
})
for command := range g.Commands() {
jsonGobot.Commands = append(jsonGobot.Commands, command)
}

g.robots.Each(func(r *Robot) {
jsonGobot.Robots = append(jsonGobot.Robots, r.ToJSON())
Expand Down
7 changes: 3 additions & 4 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ type AnalogSensorDriver struct {
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
d := &AnalogSensorDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
Name: name,
Pin: pin,
Adaptor: a.(gobot.AdaptorInterface),
},
}
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
Expand Down
7 changes: 3 additions & 4 deletions platforms/gpio/direct_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ type DirectPinDriver struct {
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
d := &DirectPinDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
Name: name,
Pin: pin,
Adaptor: a.(gobot.AdaptorInterface),
},
}

Expand Down
9 changes: 4 additions & 5 deletions platforms/gpio/led_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@ func NewLedDriver(name string, a PwmDigitalWriter, pin string) *LedDriver {
Driver: *gobot.NewDriver(
name,
"LedDriver",
gobot.Commands{},
a.(gobot.AdaptorInterface),
),
Pin: pin,
High: false,
}

l.Driver.Commands().Add("Brightness", func(params map[string]interface{}) interface{} {
l.Driver.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
level := byte(params["level"].(float64))
l.Brightness(level)
return nil
})

l.Driver.Commands().Add("Toggle", func(params map[string]interface{}) interface{} {
l.Driver.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
l.Toggle()
return nil
})

l.Driver.Commands().Add("On", func(params map[string]interface{}) interface{} {
l.Driver.AddCommand("On", func(params map[string]interface{}) interface{} {
l.On()
return nil
})

l.Driver.Commands().Add("Off", func(params map[string]interface{}) interface{} {
l.Driver.AddCommand("Off", func(params map[string]interface{}) interface{} {
l.Off()
return nil
})
Expand Down
7 changes: 3 additions & 4 deletions platforms/gpio/servo_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ type ServoDriver struct {
func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
s := &ServoDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
Name: name,
Pin: pin,
Adaptor: a.(gobot.AdaptorInterface),
},
CurrentAngle: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *Robot) Devices() *devices {
return r.devices
}

func (r *Robot) Device(name string) DriverInterface {
func (r *Robot) Device(name string) Device {
if r == nil {
return nil
}
Expand All @@ -123,7 +123,7 @@ func (r *Robot) Connections() *connections {
return r.connections
}

func (r *Robot) Connection(name string) AdaptorInterface {
func (r *Robot) Connection(name string) Connection {
if r == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (t *testDriver) Halt() bool { return true }
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{
Driver: Driver{
Commands: make(map[string]func(map[string]interface{}) interface{}),
commands: make(map[string]func(map[string]interface{}) interface{}),
Name: name,
Adaptor: adaptor,
},
Expand Down Expand Up @@ -102,7 +102,7 @@ func NewTestRobot(name string) *Robot {
driver2 := NewTestDriver("Device 2", adaptor2)
driver3 := NewTestDriver("Device 3", adaptor3)
work := func() {}
r := NewRobot(name, []AdaptorInterface{adaptor1, adaptor2, adaptor3}, []DriverInterface{driver1, driver2, driver3}, work)
r := NewRobot(name, []Connection{adaptor1, adaptor2, adaptor3}, []Device{driver1, driver2, driver3}, work)
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string)
robot := params["robot"].(string)
Expand Down

0 comments on commit 958d194

Please sign in to comment.