Skip to content

Commit

Permalink
refactor device and driver
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 13, 2014
1 parent 5096d98 commit 97564d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
43 changes: 22 additions & 21 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Device interface {
Start() bool
Halt() bool
setInterval(time.Duration)
getInterval() time.Duration
interval() time.Duration
setName(string)
getName() string
getCommands() map[string]func(map[string]interface{}) interface{}
name() string
commands() map[string]func(map[string]interface{}) interface{}
}

type JSONDevice struct {
Expand Down Expand Up @@ -56,34 +56,42 @@ func (d devices) Halt() {
}

func NewDevice(driver DriverInterface, r *Robot) *device {
d := new(device)
s := reflect.ValueOf(driver).Type().String()
d.Type = s[1:len(s)]
d.Name = driver.getName()
d.Robot = r
if driver.getInterval() == 0 {
t := reflect.ValueOf(driver).Type().String()
if driver.interval() == 0 {
driver.setInterval(10 * time.Millisecond)
}
d.Driver = driver
return d
return &device{
Type: t[1:len(t)],
Name: driver.name(),
Robot: r,
Driver: driver,
}
}

func (d *device) setInterval(t time.Duration) {
d.Interval = t
}

func (d *device) getInterval() time.Duration {
func (d *device) interval() time.Duration {
return d.Interval
}

func (d *device) setName(s string) {
d.Name = s
}

func (d *device) getName() string {
func (d *device) name() string {
return d.Name
}

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

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

func (d *device) Start() bool {
log.Println("Device " + d.Name + " started")
return d.Driver.Start()
Expand All @@ -94,13 +102,6 @@ func (d *device) Halt() bool {
return d.Driver.Halt()
}

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 {
jsonDevice := &JSONDevice{
Name: d.Name,
Expand All @@ -111,7 +112,7 @@ func (d *device) ToJSON() *JSONDevice {
Commands: []string{},
}

commands := d.getCommands()
commands := d.commands()
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}
Expand Down
18 changes: 9 additions & 9 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ type DriverInterface interface {
Start() bool
Halt() bool
setInterval(time.Duration)
getInterval() time.Duration
interval() time.Duration
setName(string)
getName() string
getCommands() map[string]func(map[string]interface{}) interface{}
name() string
commands() map[string]func(map[string]interface{}) interface{}
}

func (d *Driver) setInterval(t time.Duration) {
d.Interval = t
}

func (d *Driver) getInterval() time.Duration {
func (d *Driver) interval() time.Duration {
return d.Interval
}

func (d *Driver) setName(s string) {
d.Name = s
}

func (d *Driver) getName() string {
func (d *Driver) name() string {
return d.Name
}

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

func (d *Driver) getCommands() 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
}

0 comments on commit 97564d9

Please sign in to comment.