forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completely overhaul driver and adaptor interfaces
- Loading branch information
Showing
14 changed files
with
225 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,10 @@ | ||
package gobot | ||
|
||
import "fmt" | ||
|
||
type Adaptor struct { | ||
name string | ||
port string | ||
connected bool | ||
adaptorType string | ||
} | ||
|
||
// AdaptorInterface defines behaviour expected for a Gobot Adaptor | ||
type AdaptorInterface interface { | ||
type Adaptor interface { | ||
Finalize() []error | ||
Connect() []error | ||
Port() string | ||
Name() string | ||
Type() string | ||
Connected() bool | ||
SetConnected(bool) | ||
SetName(string) | ||
SetPort(string) | ||
Port() string | ||
String() string | ||
ToJSON() *JSONConnection | ||
} | ||
|
||
// NewAdaptor returns a new Gobot Adaptor | ||
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: "", | ||
} | ||
|
||
for i := range v { | ||
switch v[i].(type) { | ||
case string: | ||
a.port = v[i].(string) | ||
} | ||
} | ||
|
||
return a | ||
} | ||
|
||
// Port returns adaptor port | ||
func (a *Adaptor) Port() string { | ||
return a.port | ||
} | ||
|
||
// SetPort sets adaptor port | ||
func (a *Adaptor) SetPort(s string) { | ||
a.port = s | ||
} | ||
|
||
// Name returns adaptor name | ||
func (a *Adaptor) Name() string { | ||
return a.name | ||
} | ||
|
||
// SetName sets adaptor name | ||
func (a *Adaptor) SetName(s string) { | ||
a.name = s | ||
} | ||
|
||
// Type returns adaptor type | ||
func (a *Adaptor) Type() string { | ||
return a.adaptorType | ||
} | ||
|
||
// Connected returns true if adaptor is connected | ||
func (a *Adaptor) Connected() bool { | ||
return a.connected | ||
} | ||
|
||
// SetConnected sets adaptor as connected/disconnected | ||
func (a *Adaptor) SetConnected(b bool) { | ||
a.connected = b | ||
} | ||
|
||
// ToJSON returns a json representation of adaptor | ||
func (a *Adaptor) ToJSON() *JSONConnection { | ||
return &JSONConnection{ | ||
Name: a.Name(), | ||
Adaptor: a.Type(), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package gobot | ||
|
||
import "errors" | ||
|
||
type commander struct { | ||
commands map[string]func(map[string]interface{}) interface{} | ||
} | ||
|
||
type Commander interface { | ||
Command(string) (command func(map[string]interface{}) interface{}, err error) | ||
Commands() (commands map[string]func(map[string]interface{}) interface{}) | ||
AddCommand(name string, command func(map[string]interface{}) interface{}) | ||
} | ||
|
||
func NewCommander() Commander { | ||
return &commander{ | ||
commands: make(map[string]func(map[string]interface{}) interface{}), | ||
} | ||
} | ||
|
||
// Command retrieves a command by name | ||
func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}, err error) { | ||
command, ok := c.commands[name] | ||
if ok { | ||
return | ||
} | ||
err = errors.New("Unknown Command") | ||
return | ||
} | ||
|
||
// Commands returns a map of driver commands | ||
func (c *commander) Commands() map[string]func(map[string]interface{}) interface{} { | ||
return c.commands | ||
} | ||
|
||
// AddCommand links specified command name to `f` | ||
func (c *commander) AddCommand(name string, command func(map[string]interface{}) interface{}) { | ||
c.commands[name] = command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,11 @@ | ||
package gobot | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// DriverInterface defines Driver expected behaviour | ||
type DriverInterface interface { | ||
type Driver interface { | ||
Start() []error | ||
Halt() []error | ||
Adaptor() AdaptorInterface | ||
SetInterval(time.Duration) | ||
Interval() time.Duration | ||
SetName(string) | ||
Name() string | ||
Pin() string | ||
SetPin(string) | ||
Command(string) func(map[string]interface{}) interface{} | ||
Commands() map[string]func(map[string]interface{}) interface{} | ||
AddCommand(string, func(map[string]interface{}) interface{}) | ||
Events() map[string]*Event | ||
Event(string) *Event | ||
AddEvent(string) | ||
Type() string | ||
String() string | ||
Connection() Connection | ||
ToJSON() *JSONDevice | ||
} | ||
|
||
type Driver struct { | ||
adaptor AdaptorInterface | ||
interval time.Duration | ||
pin string | ||
name string | ||
commands map[string]func(map[string]interface{}) interface{} | ||
events map[string]*Event | ||
driverType string | ||
} | ||
|
||
// NewDriver returns a Driver with specified parameters | ||
// and sets driver pin, adaptor and interval | ||
func NewDriver(name string, driverType string, v ...interface{}) *Driver { | ||
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: | ||
d.pin = v[i].(string) | ||
case AdaptorInterface: | ||
d.adaptor = v[i].(AdaptorInterface) | ||
case time.Duration: | ||
d.interval = v[i].(time.Duration) | ||
} | ||
} | ||
|
||
return d | ||
} | ||
|
||
// Adaptor returns driver adaptor | ||
func (d *Driver) Adaptor() AdaptorInterface { | ||
return d.adaptor | ||
} | ||
|
||
// SetInterval defines driver interval duration. | ||
func (d *Driver) SetInterval(t time.Duration) { | ||
d.interval = t | ||
} | ||
|
||
// Interval current driver interval duration | ||
func (d *Driver) Interval() time.Duration { | ||
return d.interval | ||
} | ||
|
||
// SetName sets driver name. | ||
func (d *Driver) SetName(s string) { | ||
d.name = s | ||
} | ||
|
||
// Name returns driver name. | ||
func (d *Driver) Name() string { | ||
return d.name | ||
} | ||
|
||
// Pin returns driver pin | ||
func (d *Driver) Pin() string { | ||
return d.pin | ||
} | ||
|
||
// SetPin defines driver pin | ||
func (d *Driver) SetPin(pin string) { | ||
d.pin = pin | ||
} | ||
|
||
// Type returns driver type | ||
func (d *Driver) Type() string { | ||
return d.driverType | ||
} | ||
|
||
// Events returns driver events map | ||
func (d *Driver) Events() map[string]*Event { | ||
return d.events | ||
} | ||
|
||
// Event returns an event by name if exists | ||
func (d *Driver) Event(name string) *Event { | ||
e, ok := d.events[name] | ||
if ok { | ||
return e | ||
} else { | ||
panic(fmt.Sprintf("Unknown Driver Event: %v", name)) | ||
} | ||
} | ||
|
||
// AddEvents adds a new event by name | ||
func (d *Driver) AddEvent(name string) { | ||
d.events[name] = NewEvent() | ||
} | ||
|
||
// Command retrieves a command by name | ||
func (d *Driver) Command(name string) func(map[string]interface{}) interface{} { | ||
return d.commands[name] | ||
} | ||
|
||
// Commands returns a map of driver commands | ||
func (d *Driver) Commands() map[string]func(map[string]interface{}) interface{} { | ||
return d.commands | ||
} | ||
|
||
// AddCommand links specified command name to `f` | ||
func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) { | ||
d.commands[name] = f | ||
} | ||
|
||
// ToJSON returns JSON Driver represnentation including adaptor and commands | ||
func (d *Driver) ToJSON() *JSONDevice { | ||
jsonDevice := &JSONDevice{ | ||
Name: d.Name(), | ||
Driver: d.Type(), | ||
Commands: []string{}, | ||
Connection: "", | ||
} | ||
|
||
if d.Adaptor() != nil { | ||
jsonDevice.Connection = d.Adaptor().ToJSON().Name | ||
} | ||
|
||
for command := range d.Commands() { | ||
jsonDevice.Commands = append(jsonDevice.Commands, command) | ||
} | ||
|
||
return jsonDevice | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.