diff --git a/adaptor.go b/adaptor.go index de925b29..4a3f02b6 100644 --- a/adaptor.go +++ b/adaptor.go @@ -12,6 +12,7 @@ type AdaptorInterface interface { Connect() bool port() string name() string + setName(string) params() map[string]interface{} } @@ -23,6 +24,10 @@ func (a *Adaptor) name() string { return a.Name } +func (a *Adaptor) setName(s string) { + a.Name = s +} + func (a *Adaptor) params() map[string]interface{} { return a.Params } diff --git a/connection.go b/connection.go index 5ede3cdf..1880a83c 100644 --- a/connection.go +++ b/connection.go @@ -2,6 +2,7 @@ package gobot import ( "errors" + "fmt" "log" "reflect" ) @@ -11,6 +12,7 @@ type Connection interface { Finalize() bool port() string name() string + setName(string) params() map[string]interface{} } @@ -53,6 +55,9 @@ func (c connections) Finalize() { } func NewConnection(adaptor AdaptorInterface, r *Robot) *connection { + if adaptor.name() == "" { + adaptor.setName(fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))) + } t := reflect.ValueOf(adaptor).Type().String() return &connection{ Type: t[1:len(t)], @@ -90,6 +95,10 @@ func (c *connection) name() string { return c.Name } +func (c *connection) setName(s string) { + c.Name = s +} + func (c *connection) params() map[string]interface{} { return c.Adaptor.params() } diff --git a/device.go b/device.go index fb05d42c..5d3347ca 100644 --- a/device.go +++ b/device.go @@ -2,6 +2,7 @@ package gobot import ( "errors" + "fmt" "log" "reflect" "time" @@ -25,11 +26,10 @@ type JSONDevice struct { } type device struct { - Name string `json:"-"` - Type string `json:"-"` - Interval time.Duration `json:"-"` - Robot *Robot `json:"-"` - Driver DriverInterface `json:"-"` + Name string `json:"-"` + Type string `json:"-"` + Robot *Robot `json:"-"` + Driver DriverInterface `json:"-"` } type devices []*device @@ -56,6 +56,9 @@ func (d devices) Halt() { } func NewDevice(driver DriverInterface, r *Robot) *device { + if driver.name() == "" { + driver.setName(fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))) + } t := reflect.ValueOf(driver).Type().String() if driver.interval() == 0 { driver.setInterval(10 * time.Millisecond) @@ -69,11 +72,11 @@ func NewDevice(driver DriverInterface, r *Robot) *device { } func (d *device) setInterval(t time.Duration) { - d.Interval = t + d.Driver.setInterval(t) } func (d *device) interval() time.Duration { - return d.Interval + return d.Driver.interval() } func (d *device) setName(s string) { diff --git a/robot.go b/robot.go index d6d0ee9f..15651984 100644 --- a/robot.go +++ b/robot.go @@ -3,8 +3,6 @@ package gobot import ( "fmt" "log" - "math/rand" - "time" ) type JSONRobot struct { @@ -37,25 +35,36 @@ func (r Robots) Each(f func(*Robot)) { } func NewRobot(name string, v ...interface{}) *Robot { + if name == "" { + name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1))) + } r := &Robot{ Name: name, Commands: make(map[string]func(map[string]interface{}) interface{}), connections: connections{}, devices: devices{}, } - r.initName() + log.Println("Initializing Robot", r.Name, "...") if len(v) > 0 { if v[0] == nil { v[0] = []Connection{} } - r.initConnections(v[0].([]Connection)) + log.Println("Initializing connections...") + for _, connection := range v[0].([]Connection) { + c := r.AddConnection(connection) + log.Println("Initializing connection", c.name(), "...") + } } if len(v) > 1 { if v[1] == nil { v[1] = []Device{} } - r.initDevices(v[1].([]Device)) + log.Println("Initializing devices...") + for _, device := range v[1].([]Device) { + d := r.AddDevice(device) + log.Println("Initializing device", d.name(), "...") + } } if len(v) > 2 { if v[2] == nil { @@ -96,30 +105,6 @@ func (r *Robot) Start() { } } -func (r *Robot) initName() { - if r.Name == "" { - rand.Seed(time.Now().UTC().UnixNano()) - i := rand.Int() - r.Name = fmt.Sprintf("Robot%v", i) - } -} - -func (r *Robot) initConnections(c []Connection) { - log.Println("Initializing connections...") - for _, connection := range c { - log.Println("Initializing connection", FieldByNamePtr(connection, "Name"), "...") - r.AddConnection(connection) - } -} - -func (r *Robot) initDevices(d []Device) { - log.Println("Initializing devices...") - for _, device := range d { - log.Println("Initializing device", FieldByNamePtr(device, "Name"), "...") - r.AddDevice(device) - } -} - func (r *Robot) Devices() devices { return devices(r.devices) }