refactor generated name for robot, device and connection
This commit is contained in:
parent
daf6e328d5
commit
300a1e6812
|
@ -12,6 +12,7 @@ type AdaptorInterface interface {
|
||||||
Connect() bool
|
Connect() bool
|
||||||
port() string
|
port() string
|
||||||
name() string
|
name() string
|
||||||
|
setName(string)
|
||||||
params() map[string]interface{}
|
params() map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +24,10 @@ func (a *Adaptor) name() string {
|
||||||
return a.Name
|
return a.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Adaptor) setName(s string) {
|
||||||
|
a.Name = s
|
||||||
|
}
|
||||||
|
|
||||||
func (a *Adaptor) params() map[string]interface{} {
|
func (a *Adaptor) params() map[string]interface{} {
|
||||||
return a.Params
|
return a.Params
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package gobot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
@ -11,6 +12,7 @@ type Connection interface {
|
||||||
Finalize() bool
|
Finalize() bool
|
||||||
port() string
|
port() string
|
||||||
name() string
|
name() string
|
||||||
|
setName(string)
|
||||||
params() map[string]interface{}
|
params() map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +55,9 @@ func (c connections) Finalize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
|
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()
|
t := reflect.ValueOf(adaptor).Type().String()
|
||||||
return &connection{
|
return &connection{
|
||||||
Type: t[1:len(t)],
|
Type: t[1:len(t)],
|
||||||
|
@ -90,6 +95,10 @@ func (c *connection) name() string {
|
||||||
return c.Name
|
return c.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *connection) setName(s string) {
|
||||||
|
c.Name = s
|
||||||
|
}
|
||||||
|
|
||||||
func (c *connection) params() map[string]interface{} {
|
func (c *connection) params() map[string]interface{} {
|
||||||
return c.Adaptor.params()
|
return c.Adaptor.params()
|
||||||
}
|
}
|
||||||
|
|
17
device.go
17
device.go
|
@ -2,6 +2,7 @@ package gobot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
@ -25,11 +26,10 @@ type JSONDevice struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type device struct {
|
type device struct {
|
||||||
Name string `json:"-"`
|
Name string `json:"-"`
|
||||||
Type string `json:"-"`
|
Type string `json:"-"`
|
||||||
Interval time.Duration `json:"-"`
|
Robot *Robot `json:"-"`
|
||||||
Robot *Robot `json:"-"`
|
Driver DriverInterface `json:"-"`
|
||||||
Driver DriverInterface `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type devices []*device
|
type devices []*device
|
||||||
|
@ -56,6 +56,9 @@ func (d devices) Halt() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevice(driver DriverInterface, r *Robot) *device {
|
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()
|
t := reflect.ValueOf(driver).Type().String()
|
||||||
if driver.interval() == 0 {
|
if driver.interval() == 0 {
|
||||||
driver.setInterval(10 * time.Millisecond)
|
driver.setInterval(10 * time.Millisecond)
|
||||||
|
@ -69,11 +72,11 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *device) setInterval(t time.Duration) {
|
func (d *device) setInterval(t time.Duration) {
|
||||||
d.Interval = t
|
d.Driver.setInterval(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *device) interval() time.Duration {
|
func (d *device) interval() time.Duration {
|
||||||
return d.Interval
|
return d.Driver.interval()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *device) setName(s string) {
|
func (d *device) setName(s string) {
|
||||||
|
|
43
robot.go
43
robot.go
|
@ -3,8 +3,6 @@ package gobot
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSONRobot struct {
|
type JSONRobot struct {
|
||||||
|
@ -37,25 +35,36 @@ func (r Robots) Each(f func(*Robot)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRobot(name string, v ...interface{}) *Robot {
|
func NewRobot(name string, v ...interface{}) *Robot {
|
||||||
|
if name == "" {
|
||||||
|
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
|
||||||
|
}
|
||||||
r := &Robot{
|
r := &Robot{
|
||||||
Name: name,
|
Name: name,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
connections: connections{},
|
connections: connections{},
|
||||||
devices: devices{},
|
devices: devices{},
|
||||||
}
|
}
|
||||||
r.initName()
|
|
||||||
log.Println("Initializing Robot", r.Name, "...")
|
log.Println("Initializing Robot", r.Name, "...")
|
||||||
if len(v) > 0 {
|
if len(v) > 0 {
|
||||||
if v[0] == nil {
|
if v[0] == nil {
|
||||||
v[0] = []Connection{}
|
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 len(v) > 1 {
|
||||||
if v[1] == nil {
|
if v[1] == nil {
|
||||||
v[1] = []Device{}
|
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 len(v) > 2 {
|
||||||
if v[2] == nil {
|
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 {
|
func (r *Robot) Devices() devices {
|
||||||
return devices(r.devices)
|
return devices(r.devices)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue