Skip to content

Commit

Permalink
Refactor driver commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 12, 2014
1 parent ca4d8ce commit addb700
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 166 deletions.
31 changes: 13 additions & 18 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (a *api) Start() {
a.server.Get("/robots/:robot/devices", a.setHeaders(a.robotDevices))
a.server.Get("/robots/:robot/devices/:device", a.setHeaders(a.robotDevice))
a.server.Get("/robots/:robot/devices/:device/commands", a.setHeaders(a.robotDeviceCommands))
a.server.Get(commandRoute, a.setHeaders(a.executeCommand))
a.server.Post(commandRoute, a.setHeaders(a.executeCommand))
a.server.Get(commandRoute, a.setHeaders(a.executeDeviceCommand))
a.server.Post(commandRoute, a.setHeaders(a.executeDeviceCommand))
a.server.Get("/robots/:robot/connections", a.setHeaders(a.robotConnections))
a.server.Get("/robots/:robot/connections/:connection", a.setHeaders(a.robotConnection))

Expand Down Expand Up @@ -168,7 +168,7 @@ func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
robot := req.URL.Query().Get(":robot")
device := req.URL.Query().Get(":device")

data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).Commands())
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).ToJSON().Commands)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
Expand All @@ -195,29 +195,24 @@ func (a *api) robotConnection(res http.ResponseWriter, req *http.Request) {
res.Write(data)
}

func (a *api) executeCommand(res http.ResponseWriter, req *http.Request) {
func (a *api) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
robot := req.URL.Query().Get(":robot")
device := req.URL.Query().Get(":device")
command := req.URL.Query().Get(":command")

data, _ := ioutil.ReadAll(req.Body)
var body map[string]interface{}
body := make(map[string]interface{})
json.Unmarshal(data, &body)
d := a.gobot.Robot(robot).Device(device)
commands := d.Commands().([]string)
for c := range commands {
if commands[c] == command {
ret := []interface{}{}
for _, v := range gobot.Call(d.Driver, command, body) {
ret = append(ret, v.Interface())
}
data, _ = json.Marshal(ret)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
return
}
body["robot"] = robot
f := d.Commands()[command]

if f != nil {
data, _ = json.Marshal(f(body))
} else {
data, _ = json.Marshal("Unknown Command")
}
data, _ = json.Marshal([]interface{}{"Unknown Command"})

res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
Expand Down
18 changes: 9 additions & 9 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("API", func() {
json.Unmarshal(body, &i)
Expect(len(i)).To(Equal(3))
})
PIt("should return robot commands", func() {
It("should return robot commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -71,16 +71,16 @@ var _ = Describe("API", func() {
json.Unmarshal(body, &i)
Expect(i).To(Equal([]string{"robotTestFunction"}))
})
PIt("should execute robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion", bytes.NewBufferString(`{"message":"Beep Boop"}`))
It("should execute robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
var i interface{}
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("hey Robot 1, Beep Boop"))
Expect(i).To(Equal("hey Robot 1, Beep Boop"))
})
It("should not execute unknown robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
Expand Down Expand Up @@ -120,9 +120,9 @@ var _ = Describe("API", func() {
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
var i interface{}
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("hello human"))
Expect(i).To(Equal("hello human"))
})
It("should not execute unknown device command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`))
Expand All @@ -131,9 +131,9 @@ var _ = Describe("API", func() {
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
var i interface{}
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("Unknown Command"))
Expect(i).To(Equal("Unknown Command"))
})
})
})
19 changes: 15 additions & 4 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Device interface {
getInterval() time.Duration
setName(string)
getName() string
getCommands() map[string]func(map[string]interface{}) interface{}
}

type JSONDevice struct {
Expand Down Expand Up @@ -93,17 +94,27 @@ func (d *device) Halt() bool {
return d.Driver.Halt()
}

func (d *device) Commands() interface{} {
return FieldByNamePtr(d.Driver, "Commands").Interface()
func (d *device) getCommands() map[string]func(map[string]interface{}) interface{} {
return d.Driver.getCommands()
}
func (d *device) Commands() map[string]func(map[string]interface{}) interface{} {
return d.getCommands()
}

func (d *device) ToJSON() *JSONDevice {
return &JSONDevice{
jsonDevice := &JSONDevice{
Name: d.Name,
Driver: d.Type,
Connection: d.Robot.Connection(FieldByNamePtr(FieldByNamePtr(d.Driver, "Adaptor").
Interface().(AdaptorInterface), "Name").
Interface().(string)).ToJSON(),
Commands: FieldByNamePtr(d.Driver, "Commands").Interface().([]string),
Commands: []string{},
}

commands := d.getCommands()
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}

return jsonDevice
}
19 changes: 14 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package gobot
import "time"

type Driver struct {
Interval time.Duration `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands []string `json:"commands"`
Events map[string]*Event `json:"-"`
Interval time.Duration `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands map[string]func(map[string]interface{}) interface{} `json:"commands"`
Events map[string]*Event `json:"-"`
}

type DriverInterface interface {
Expand All @@ -17,6 +17,7 @@ type DriverInterface interface {
getInterval() time.Duration
setName(string)
getName() string
getCommands() map[string]func(map[string]interface{}) interface{}
}

func (d *Driver) setInterval(t time.Duration) {
Expand All @@ -34,3 +35,11 @@ func (d *Driver) setName(s string) {
func (d *Driver) getName() string {
return d.Name
}

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

func (d *Driver) getCommands() map[string]func(map[string]interface{}) interface{} {
return d.Commands
}
14 changes: 8 additions & 6 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type AnalogSensorDriver struct {
}

func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
return &AnalogSensorDriver{
d := &AnalogSensorDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: []string{
"ReadC",
},
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
},
Adaptor: a,
}
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
return d.Read()
})
return d
}

func (a *AnalogSensorDriver) Start() bool { return true }
Expand Down
62 changes: 0 additions & 62 deletions platforms/gpio/commands.go

This file was deleted.

45 changes: 34 additions & 11 deletions platforms/gpio/direct_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gpio

import (
"github.com/hybridgroup/gobot"
"strconv"
)

type DirectPinDriver struct {
Expand All @@ -10,21 +11,43 @@ type DirectPinDriver struct {
}

func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
return &DirectPinDriver{
d := &DirectPinDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: []string{
"DigitalReadC",
"DigitalWriteC",
"AnalogReadC",
"AnalogWriteC",
"PwmWriteC",
"ServoWriteC",
},
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
},
Adaptor: a,
}

d.Driver.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
return d.DigitalRead()
})
d.Driver.AddCommand("DigitalWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.DigitalWrite(byte(level))
return nil
})
d.Driver.AddCommand("AnalogRead", func(params map[string]interface{}) interface{} {
return d.AnalogRead()
})
d.Driver.AddCommand("AnalogWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.AnalogWrite(byte(level))
return nil
})
d.Driver.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.PwmWrite(byte(level))
return nil
})
d.Driver.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.ServoWrite(byte(level))
return nil
})

return d
}

func (d *DirectPinDriver) Start() bool { return true }
Expand Down
36 changes: 27 additions & 9 deletions platforms/gpio/led_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,38 @@ type LedDriver struct {
}

func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
return &LedDriver{
l := &LedDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Commands: []string{
"ToggleC",
"OnC",
"OffC",
"BrightnessC",
},
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
},
High: false,
Adaptor: a,
}

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

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

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

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

return l
}

func (l *LedDriver) Start() bool { return true }
Expand Down
Loading

0 comments on commit addb700

Please sign in to comment.