Skip to content

Commit

Permalink
refactor generated name for robot, device and connection
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 15, 2014
1 parent daf6e32 commit 300a1e6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
5 changes: 5 additions & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AdaptorInterface interface {
Connect() bool
port() string
name() string
setName(string)
params() map[string]interface{}
}

Expand All @@ -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
}
9 changes: 9 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

import (
"errors"
"fmt"
"log"
"reflect"
)
Expand All @@ -11,6 +12,7 @@ type Connection interface {
Finalize() bool
port() string
name() string
setName(string)
params() map[string]interface{}
}

Expand Down Expand Up @@ -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)],
Expand Down Expand Up @@ -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()
}
17 changes: 10 additions & 7 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

import (
"errors"
"fmt"
"log"
"reflect"
"time"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
43 changes: 14 additions & 29 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package gobot
import (
"fmt"
"log"
"math/rand"
"time"
)

type JSONRobot struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 300a1e6

Please sign in to comment.