forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
40 lines (33 loc) · 924 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 string
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.Port = reflect.ValueOf(a).Elem().FieldByName("Port").String()
c.Robot = r
c.Adaptor = a
return c
}
func (c *Connection) Connect() {
fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
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("IsConnected").Call([]reflect.Value{})[0].Bool()
}
func (c *Connection) AdaptorName() string {
return c.Name
}