-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgozwave.go
78 lines (61 loc) · 1.65 KB
/
gozwave.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
//go:generate go run generators/database/database.go -file database/test.go -package database -databasedir ./database
//go:generate go fmt ./database/test.go
//go:generate go run generators/commandclasses/commandclasses.go -file database/mandatory.go -package database -mandatoryfile ./database/mandatory.txt
//go:generate go fmt ./database/mandatory.go
package gozwave
import (
"fmt"
"io"
"time"
"github.com/sirupsen/logrus"
"github.com/tarm/serial"
)
func ConnectWithCustomPortOpener(port string, filename string, po PortOpener) (*Controller, error) {
if po == nil {
return nil, fmt.Errorf("portopener is nil")
}
c := NewController()
c.filename = filename
c.Connection.portOpener = po
var err error
c.Connection.reportCallback = c.DeliverReportToNode
connected := make(chan error)
//spew.Dump(c)
if filename != "" {
c.LoadConfigurationFromFile()
}
go func() {
for {
err := c.Connection.Connect(connected)
logrus.Error(err)
<-time.After(time.Second)
}
}()
err = <-connected
if err != nil {
return nil, err
}
go c.getNodes()
go c.saveDebouncer()
return c, err
}
func Connect(port string, filename string) (*Controller, error) {
return ConnectWithCustomPortOpener(port, filename, newSerialPortOpener(port, 115200))
}
type PortOpener interface {
/* TODO: add methods */
Open() (io.ReadWriteCloser, error)
}
type serialPortOpener struct {
port string
baud int
}
func (spo *serialPortOpener) Open() (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: spo.port, Baud: spo.baud})
}
func newSerialPortOpener(port string, baud int) *serialPortOpener {
return &serialPortOpener{
port: port,
baud: baud,
}
}