forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptor.go
86 lines (71 loc) · 1.44 KB
/
adaptor.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gobot
import "fmt"
type Adaptor struct {
name string
port string
connected bool
params map[string]interface{}
adaptorType string
}
type AdaptorInterface interface {
Finalize() bool
Connect() bool
Port() string
Name() string
Type() string
Connected() bool
SetConnected(bool)
SetName(string)
Params() map[string]interface{}
ToJSON() *JSONConnection
}
func NewAdaptor(name string, adaptorType string, v ...interface{}) *Adaptor {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
a := &Adaptor{
adaptorType: adaptorType,
name: name,
port: "",
params: make(map[string]interface{}),
}
for i := range v {
switch v[i].(type) {
case string:
a.port = v[i].(string)
case map[string]interface{}:
a.params = v[i].(map[string]interface{})
default:
fmt.Println("Unknown argument passed to NewAdaptor")
}
}
return a
}
func (a *Adaptor) Port() string {
return a.port
}
func (a *Adaptor) Name() string {
return a.name
}
func (a *Adaptor) SetName(s string) {
a.name = s
}
func (a *Adaptor) Type() string {
return a.adaptorType
}
func (a *Adaptor) Connected() bool {
return a.connected
}
func (a *Adaptor) SetConnected(b bool) {
a.connected = b
}
func (a *Adaptor) Params() map[string]interface{} {
return a.params
}
func (a *Adaptor) ToJSON() *JSONConnection {
return &JSONConnection{
Name: a.Name(),
Port: a.Port(),
Adaptor: a.Type(),
}
}