forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
40 lines (32 loc) · 923 Bytes
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gobot
import (
"fmt"
"reflect"
)
type Connection struct {
Name string
Adaptor interface{}
Port Port
Robot *Robot
Params map[string]string
}
func NewConnection(a interface{}, r *Robot) *Connection {
c := new(Connection)
c.Name = reflect.ValueOf(a).Elem().FieldByName("Name").String()
c.Robot = r
c.Adaptor = a
return c
}
func (c *Connection) Connect() {
fmt.Println("Connecting to " + reflect.ValueOf(c).Elem().FieldByName("Name").String() + " on port " + c.Port.ToString() + "...")
reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{})
}
func (c *Connection) Disconnect() {
reflect.ValueOf(c.Adaptor).MethodByName("Disconnect").Call([]reflect.Value{})
}
func (c *Connection) IsConnected() bool {
return reflect.ValueOf(c.Adaptor).MethodByName("IsConnect").Call([]reflect.Value{})[0].Bool()
}
func (c *Connection) AdaptorName() string {
return c.Name
}