forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
46 lines (40 loc) · 1.14 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
package gobot
import (
"log"
"reflect"
)
type connection struct {
Name string `json:"name"`
Type string `json:"adaptor"`
Adaptor AdaptorInterface `json:"-"`
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
}
type Connection interface {
Connect() bool
Finalize() bool
}
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection)
s := reflect.ValueOf(adaptor).Type().String()
c.Type = s[1:len(s)]
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 {
log.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Connect()
}
func (c *connection) Finalize() bool {
log.Println("Finalizing " + c.Name + "...")
return c.Adaptor.Finalize()
}