forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
55 lines (46 loc) · 1.17 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gobot
import (
"fmt"
)
type connection struct {
Name string
Adaptor AdaptorInterface
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
}
type Connection interface {
Connect() bool
Disconnect() bool
Finalize() bool
Reconnect() bool
}
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection)
c.Name = FieldByNamePtr(adaptor, "Name").String()
c.Port = FieldByNamePtr(adaptor, "Port").String()
c.Params = make(map[string]interface{})
keys := FieldByNamePtr(adaptor, "Params").MapKeys()
for k := range keys {
c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k])
}
c.Robot = r
c.Adaptor = adaptor
return c
}
func (c *connection) Connect() bool {
fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Connect()
}
func (c *connection) Disconnect() bool {
return c.Adaptor.Disconnect()
}
func (c *connection) Finalize() bool {
return c.Adaptor.Finalize()
}
func (c *connection) Reconnect() bool {
return c.Adaptor.Reconnect()
}
func (c *connection) AdaptorName() string {
return c.Name
}