/
interfacekit.go
88 lines (72 loc) · 1.58 KB
/
interfacekit.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
87
88
package main
import (
"github.com/samuelkadolph/go/phidgets"
"github.com/samuelkadolph/go/phidgets/raw"
"time"
)
type InterfaceKit struct {
AttachmentTimeout *int
Doors []*Door
Host *string
Label *string
LockDelay *int
Password *string
Port *int
Serial *int
*phidgets.InterfaceKit
}
func (i *InterfaceKit) Load() error {
var err error
if i.InterfaceKit, err = phidgets.NewInterfaceKit(); err != nil {
return err
}
if err = i.Open(i.connector()); err != nil {
return err
}
if err = i.WaitForAttachment(i.timeout()); err != nil {
return err
}
for _, d := range i.Doors {
d.ifk = i
}
return nil
}
func (i *InterfaceKit) connector() phidgets.Connector {
var c phidgets.Connector
var host string
var password string
var port int
if i.Password != nil {
password = *i.Password
}
if i.Port != nil {
port = *i.Port
} else {
port = 5001
}
if i.Host != nil {
host = *i.Host
if i.Serial != nil {
c = phidgets.RemoteIPSerial{*i.Serial, host, port, password}
} else if i.Label != nil {
c = phidgets.RemoteIPLabel{*i.Label, host, port, password}
} else {
c = phidgets.RemoteIPSerial{raw.Any, host, port, password}
}
} else {
if i.Serial != nil {
c = phidgets.Serial{*i.Serial}
} else if i.Label != nil {
c = phidgets.Label{*i.Label}
} else {
c = phidgets.Any
}
}
return c
}
func (i *InterfaceKit) timeout() time.Duration {
if i.AttachmentTimeout != nil {
return time.Duration(*i.AttachmentTimeout) * time.Millisecond
}
return 2000 * time.Millisecond
}