2013-10-23 07:45:31 +08:00
|
|
|
package gobot
|
2013-10-24 13:00:03 +08:00
|
|
|
|
2014-06-24 11:33:59 +08:00
|
|
|
import "fmt"
|
|
|
|
|
2013-10-24 13:00:03 +08:00
|
|
|
type Adaptor struct {
|
2014-07-04 10:52:31 +08:00
|
|
|
name string
|
|
|
|
port string
|
|
|
|
connected bool
|
|
|
|
adaptorType string
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// AdaptorInterface defines behaviour expected for a Gobot Adaptor
|
2013-12-19 07:25:07 +08:00
|
|
|
type AdaptorInterface interface {
|
|
|
|
Finalize() bool
|
|
|
|
Connect() bool
|
2014-07-04 10:52:31 +08:00
|
|
|
Port() string
|
|
|
|
Name() string
|
|
|
|
Type() string
|
|
|
|
Connected() bool
|
|
|
|
SetConnected(bool)
|
|
|
|
SetName(string)
|
2014-07-18 02:41:47 +08:00
|
|
|
SetPort(string)
|
2014-07-03 09:08:44 +08:00
|
|
|
ToJSON() *JSONConnection
|
2014-06-14 01:46:58 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// NewAdaptor returns a new Gobot Adaptor
|
2014-07-04 10:52:31 +08:00
|
|
|
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
|
2014-06-14 01:46:58 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Port returns adaptor port
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) Port() string {
|
|
|
|
return a.port
|
2014-06-14 01:46:58 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// SetPort sets adaptor port
|
2014-07-18 02:41:47 +08:00
|
|
|
func (a *Adaptor) SetPort(s string) {
|
|
|
|
a.port = s
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Name returns adaptor name
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) Name() string {
|
|
|
|
return a.name
|
2014-06-15 09:49:02 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// SetName sets adaptor name
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) SetName(s string) {
|
|
|
|
a.name = s
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|
2014-06-24 11:33:59 +08:00
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Type returns adaptor type
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) Type() string {
|
|
|
|
return a.adaptorType
|
2014-06-24 11:33:59 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// Connected returns true if adaptor is connected
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) Connected() bool {
|
|
|
|
return a.connected
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// SetConnected sets adaptor as connected/disconnected
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) SetConnected(b bool) {
|
|
|
|
a.connected = b
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:57:07 +08:00
|
|
|
// ToJSON returns a json representation of adaptor
|
2014-07-04 10:52:31 +08:00
|
|
|
func (a *Adaptor) ToJSON() *JSONConnection {
|
|
|
|
return &JSONConnection{
|
|
|
|
Name: a.Name(),
|
|
|
|
Adaptor: a.Type(),
|
2014-06-24 11:33:59 +08:00
|
|
|
}
|
|
|
|
}
|