From 97564d9808c6ac399481494ef9c1638b5c3ea67c Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 13 Jun 2014 10:09:03 -0700 Subject: [PATCH] refactor device and driver --- device.go | 43 ++++++++++++++++++++++--------------------- driver.go | 18 +++++++++--------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/device.go b/device.go index 72091f83..fb05d42c 100644 --- a/device.go +++ b/device.go @@ -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 { @@ -56,23 +56,23 @@ 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 } @@ -80,10 +80,18 @@ 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() @@ -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, @@ -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) } diff --git a/driver.go b/driver.go index 7c13dea8..64bc5ae8 100644 --- a/driver.go +++ b/driver.go @@ -14,17 +14,17 @@ 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 } @@ -32,14 +32,14 @@ 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) 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 } - -func (d *Driver) getCommands() map[string]func(map[string]interface{}) interface{} { - return d.Commands -}