1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

Fix default Interval initialization

This commit is contained in:
Adrian Zankich 2014-06-06 14:44:16 -07:00
parent 6f14570641
commit 57bb8bbce6
4 changed files with 44 additions and 4 deletions

26
device.go Normal file → Executable file
View File

@ -10,6 +10,10 @@ import (
type Device interface {
Start() bool
Halt() bool
setInterval(time.Duration)
getInterval() time.Duration
setName(string)
getName() string
}
type JsonDevice struct {
@ -54,15 +58,31 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
d := new(device)
s := reflect.ValueOf(driver).Type().String()
d.Type = s[1:len(s)]
d.Name = FieldByNamePtr(driver, "Name").String()
d.Name = driver.getName()
d.Robot = r
if FieldByNamePtr(driver, "Interval").String() == "" {
FieldByNamePtr(driver, "Interval").SetString("0.1s")
if driver.getInterval() == 0 {
driver.setInterval(10 * time.Millisecond)
}
d.Driver = driver
return d
}
func (d *device) setInterval(t time.Duration) {
d.Interval = t
}
func (d *device) getInterval() time.Duration {
return d.Interval
}
func (d *device) setName(s string) {
d.Name = s
}
func (d *device) getName() string {
return d.Name
}
func (d *device) Start() bool {
log.Println("Device " + d.Name + " started")
return d.Driver.Start()

20
driver.go Normal file → Executable file
View File

@ -13,4 +13,24 @@ type Driver struct {
type DriverInterface interface {
Start() bool
Halt() bool
setInterval(time.Duration)
getInterval() time.Duration
setName(string)
getName() string
}
func (d *Driver) setInterval(t time.Duration) {
d.Interval = t
}
func (d *Driver) getInterval() time.Duration {
return d.Interval
}
func (d *Driver) setName(s string) {
d.Name = s
}
func (d *Driver) getName() string {
return d.Name
}

0
test_helper.go Normal file → Executable file
View File

View File

@ -1,9 +1,9 @@
package gobot
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
)
var _ = Describe("Utils", func() {