diff --git a/adaptor.go b/adaptor.go index 9a7192a0..b6ce78d3 100644 --- a/adaptor.go +++ b/adaptor.go @@ -3,54 +3,84 @@ package gobot import "fmt" type Adaptor struct { - Name string - Port string - Connected bool - Params map[string]interface{} - Type string + name string + port string + connected bool + params map[string]interface{} + adaptorType string } type AdaptorInterface interface { Finalize() bool Connect() bool - port() string - name() string - setName(string) - params() map[string]interface{} + Port() string + Name() string + Type() string + Connected() bool + SetConnected(bool) + SetName(string) + Params() map[string]interface{} ToJSON() *JSONConnection } -func (a *Adaptor) port() string { - return a.Port +func NewAdaptor(name string, adaptorType string, v ...interface{}) *Adaptor { + if name == "" { + name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1))) + } + + a := &Adaptor{ + adaptorType: adaptorType, + name: name, + port: "", + params: make(map[string]interface{}), + } + + for i := range v { + switch v[i].(type) { + case string: + a.port = v[i].(string) + case map[string]interface{}: + a.params = v[i].(map[string]interface{}) + default: + fmt.Println("Unknown argument passed to NewAdaptor") + } + } + + return a } -func (a *Adaptor) name() string { - return a.Name +func (a *Adaptor) Port() string { + return a.port } -func (a *Adaptor) setName(s string) { - a.Name = s +func (a *Adaptor) Name() string { + return a.name } -func (a *Adaptor) params() map[string]interface{} { - return a.Params +func (a *Adaptor) SetName(s string) { + a.name = s +} + +func (a *Adaptor) Type() string { + return a.adaptorType +} + +func (a *Adaptor) Connected() bool { + return a.connected +} + +func (a *Adaptor) SetConnected(b bool) { + a.connected = b +} + +func (a *Adaptor) Params() map[string]interface{} { + return a.params } func (a *Adaptor) ToJSON() *JSONConnection { return &JSONConnection{ - Name: a.Name, - Port: a.Port, - Adaptor: a.Type, - } -} - -func NewAdaptor(name, port, t string) *Adaptor { - if name == "" { - name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1))) - } - return &Adaptor{ - Type: t, - Name: name, - Port: port, + Name: a.Name(), + Port: a.Port(), + Adaptor: a.Type(), } } diff --git a/connection.go b/connection.go index 88544cf2..8c4fd797 100644 --- a/connection.go +++ b/connection.go @@ -37,7 +37,11 @@ func (c connections) Start() error { var err error log.Println("Starting connections...") for _, connection := range c.connections { - log.Println("Starting connection " + connection.name() + "...") + info := "Starting connection " + connection.Name() + if connection.Port() != "" { + info = info + " on Port " + connection.Port() + } + log.Println(info + "...") if connection.Connect() == false { err = errors.New("Could not start connection") break diff --git a/driver.go b/driver.go index 6553558e..71d9ff99 100644 --- a/driver.go +++ b/driver.go @@ -36,36 +36,34 @@ type Driver struct { } func NewDriver(name string, driverType string, v ...interface{}) *Driver { - interval := 10 * time.Millisecond - pin := "" - var adaptor AdaptorInterface - if name == "" { name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1))) } + d := &Driver{ + driverType: driverType, + name: name, + interval: 10 * time.Millisecond, + commands: make(map[string]func(map[string]interface{}) interface{}), + events: make(map[string]*Event), + adaptor: nil, + pin: "", + } + for i := range v { switch v[i].(type) { case string: - pin = v[i].(string) + d.pin = v[i].(string) case AdaptorInterface: - adaptor = v[i].(AdaptorInterface) + d.adaptor = v[i].(AdaptorInterface) case time.Duration: - interval = v[i].(time.Duration) + d.interval = v[i].(time.Duration) default: fmt.Println("Unknown argument passed to NewDriver") } } - return &Driver{ - driverType: driverType, - name: name, - interval: interval, - commands: make(map[string]func(map[string]interface{}) interface{}), - events: make(map[string]*Event), - adaptor: adaptor, - pin: pin, - } + return d } func (d *Driver) Adaptor() AdaptorInterface { diff --git a/platforms/firmata/firmata_adaptor.go b/platforms/firmata/firmata_adaptor.go index 68ec6469..a70899b2 100644 --- a/platforms/firmata/firmata_adaptor.go +++ b/platforms/firmata/firmata_adaptor.go @@ -18,11 +18,11 @@ func NewFirmataAdaptor(name, port string) *FirmataAdaptor { return &FirmataAdaptor{ Adaptor: *gobot.NewAdaptor( name, - port, "FirmataAdaptor", + port, ), connect: func(f *FirmataAdaptor) { - sp, err := serial.OpenPort(&serial.Config{Name: f.Port, Baud: 57600}) + sp, err := serial.OpenPort(&serial.Config{Name: f.Port(), Baud: 57600}) if err != nil { panic(err) } @@ -34,7 +34,7 @@ func NewFirmataAdaptor(name, port string) *FirmataAdaptor { func (f *FirmataAdaptor) Connect() bool { f.connect(f) f.Board.connect() - f.Connected = true + f.SetConnected(true) return true } diff --git a/platforms/gpio/test_helper.go b/platforms/gpio/test_helper.go index ac24add3..9e0705f9 100644 --- a/platforms/gpio/test_helper.go +++ b/platforms/gpio/test_helper.go @@ -22,8 +22,9 @@ func (t *gpioTestAdaptor) Finalize() bool { return true } func newGpioTestAdaptor(name string) *gpioTestAdaptor { return &gpioTestAdaptor{ - Adaptor: gobot.Adaptor{ - Name: name, - }, + Adaptor: *gobot.NewAdaptor( + name, + "/dev/null", + ), } } diff --git a/robot.go b/robot.go index 26b0f765..7934fd55 100644 --- a/robot.go +++ b/robot.go @@ -48,40 +48,38 @@ 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{}, + Work: nil, } log.Println("Initializing Robot", r.Name, "...") - if len(v) > 0 { - if v[0] == nil { - v[0] = connections{} - } - log.Println("Initializing connections...") - for _, connection := range v[0].([]Connection) { - c := r.Connections().Add(connection) - log.Println("Initializing connection", c.name(), "...") + + for i := range v { + switch v[i].(type) { + case []Connection: + log.Println("Initializing connections...") + for _, connection := range v[i].([]Connection) { + c := r.Connections().Add(connection) + log.Println("Initializing connection", c.Name(), "...") + } + case []Device: + log.Println("Initializing devices...") + for _, device := range v[i].([]Device) { + d := r.Devices().Add(device) + log.Println("Initializing device", d.Name(), "...") + } + case func(): + r.Work = v[i].(func()) + default: + fmt.Println("Unknown argument passed to NewRobot") } } - if len(v) > 1 { - if v[1] == nil { - v[1] = devices{} - } - log.Println("Initializing devices...") - for _, device := range v[1].([]Device) { - d := r.Devices().Add(device) - log.Println("Initializing device", d.Name(), "...") - } - } - if len(v) > 2 { - if v[2] == nil { - v[2] = func() {} - } - r.Work = v[2].(func()) - } + return r } @@ -136,7 +134,7 @@ func (r *Robot) Connection(name string) Connection { return nil } for _, connection := range r.connections.connections { - if connection.name() == name { + if connection.Name() == name { return connection } } diff --git a/test_helper.go b/test_helper.go index ae024056..f4d2c519 100644 --- a/test_helper.go +++ b/test_helper.go @@ -85,8 +85,8 @@ func (t *testAdaptor) Connect() bool { return true } func NewTestAdaptor(name string) *testAdaptor { return &testAdaptor{ Adaptor: Adaptor{ - Name: name, - Params: map[string]interface{}{ + name: name, + params: map[string]interface{}{ "param1": "1", "param2": 2, },