2013-10-23 07:45:31 +08:00
|
|
|
package gobot
|
|
|
|
|
2013-10-24 13:00:03 +08:00
|
|
|
import (
|
2013-11-14 12:44:54 +08:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2013-10-24 13:00:03 +08:00
|
|
|
)
|
2013-10-23 07:45:31 +08:00
|
|
|
|
|
|
|
type Connection struct {
|
2013-11-14 12:44:54 +08:00
|
|
|
Name string
|
|
|
|
Adaptor interface{}
|
|
|
|
Port string
|
|
|
|
Robot *Robot
|
|
|
|
Params map[string]string
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|
|
|
|
|
2013-10-26 05:15:31 +08:00
|
|
|
func NewConnection(a interface{}, r *Robot) *Connection {
|
2013-11-14 12:44:54 +08:00
|
|
|
c := new(Connection)
|
|
|
|
c.Name = reflect.ValueOf(a).Elem().FieldByName("Name").String()
|
|
|
|
c.Port = reflect.ValueOf(a).Elem().FieldByName("Port").String()
|
|
|
|
c.Robot = r
|
|
|
|
c.Adaptor = a
|
|
|
|
return c
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|
2013-10-23 07:45:31 +08:00
|
|
|
|
2013-10-25 13:04:58 +08:00
|
|
|
func (c *Connection) Connect() {
|
2013-11-14 12:44:54 +08:00
|
|
|
fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
|
|
|
|
reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{})
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:04:58 +08:00
|
|
|
func (c *Connection) Disconnect() {
|
2013-11-14 12:44:54 +08:00
|
|
|
reflect.ValueOf(c.Adaptor).MethodByName("Disconnect").Call([]reflect.Value{})
|
2013-10-23 07:45:31 +08:00
|
|
|
}
|
2013-10-24 13:00:03 +08:00
|
|
|
|
2013-10-25 13:04:58 +08:00
|
|
|
func (c *Connection) IsConnected() bool {
|
2013-11-14 12:44:54 +08:00
|
|
|
return reflect.ValueOf(c.Adaptor).MethodByName("IsConnected").Call([]reflect.Value{})[0].Bool()
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:04:58 +08:00
|
|
|
func (c *Connection) AdaptorName() string {
|
2013-11-14 12:44:54 +08:00
|
|
|
return c.Name
|
2013-10-24 13:00:03 +08:00
|
|
|
}
|