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.
Merge pull request hybridgroup#157 from hybridgroup/refactor_interfaces
Refactor Adaptor and Driver interfaces
- Loading branch information
Showing
67 changed files
with
779 additions
and
890 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,93 +1,11 @@ | ||
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) | ||
ToJSON() *JSONConnection | ||
} | ||
|
||
// NewAdaptor returns a new Adaptor given a name, adaptorType and optionally accepts: | ||
// | ||
// string: Port the adaptor connects to | ||
// | ||
// adaptorType is a label used for identification in the api | ||
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 the 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 an adaptor | ||
func (a *Adaptor) ToJSON() *JSONConnection { | ||
return &JSONConnection{ | ||
Name: a.Name(), | ||
Adaptor: a.Type(), | ||
} | ||
type Porter interface { | ||
Port() string | ||
} |
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
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,33 @@ | ||
package gobot | ||
|
||
type commander struct { | ||
commands map[string]func(map[string]interface{}) interface{} | ||
} | ||
|
||
type Commander interface { | ||
Command(string) (command func(map[string]interface{}) interface{}) | ||
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{}) { | ||
command, _ = c.commands[name] | ||
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
Oops, something went wrong.