-
Notifications
You must be signed in to change notification settings - Fork 13
/
controller.go
189 lines (158 loc) · 3.52 KB
/
controller.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gozwave
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/stampzilla/gozwave/commands/reports"
"github.com/stampzilla/gozwave/events"
"github.com/stampzilla/gozwave/nodes"
"github.com/stampzilla/gozwave/serialapi"
)
type Controller struct {
Nodes *nodes.List
Connection *Connection `json:"-"`
eventQue chan interface{}
filename string
triggerFileSave chan struct{}
sync.RWMutex
}
func NewController() *Controller {
c := &Controller{
Nodes: nodes.NewList(),
Connection: NewConnection(),
eventQue: make(chan interface{}, 10),
triggerFileSave: make(chan struct{}),
}
return c
}
func (c *Controller) getNodes() (*nodes.List, error) {
t, err := c.Connection.WriteWithTimeout(serialapi.NewRaw([]byte{0x02}), time.Second*5)
if err != nil {
return nil, err
}
resp := <-t
if resp == nil {
return nil, fmt.Errorf("Timeout or invalid response")
}
switch r := resp.Data.(type) {
case *serialapi.DiscoverdNodes:
for index, active := range r.ActiveNodes {
if !active {
continue
}
//skip first node (controller)
if index == 0 {
continue
}
node := c.Nodes.Get(index + 1)
node = c.initNode(index+1, node)
go node.Identify()
}
default:
logrus.Errorf("Got wrong response type: %t", r)
}
return c.Nodes, nil
}
func (c *Controller) initNode(id int, node *nodes.Node) *nodes.Node {
if node != nil {
return node
}
node = nodes.New(id)
c.Nodes.Add(node)
c.pushEvent(events.NodeDiscoverd{
Address: node.Id,
})
node.Setup(c.Connection, c.pushEvent)
return node
}
func (c *Controller) pushEvent(event interface{}) {
select {
case c.eventQue <- event:
default:
}
go func() {
switch event.(type) {
case events.NodeUpdated:
c.triggerFileSave <- struct{}{}
}
}()
}
func (c *Controller) saveDebouncer() {
<-c.triggerFileSave
for {
select {
case <-c.triggerFileSave:
case <-time.After(time.Second * 10):
if c.filename != "" {
c.SaveConfigurationToFile()
}
<-c.triggerFileSave
}
}
}
func (c *Controller) GetNextEvent() chan interface{} {
c.RLock()
defer c.RUnlock()
return c.eventQue
}
func (c *Controller) DeliverReportToNode(node byte, report reports.Report) {
n := c.Nodes.Get(int(node))
if n == nil {
return
}
n.ProcessEvent(report)
}
func (c *Controller) SaveConfigurationToFile() error {
controller, err := c.loadConfigurationFromFile()
if err == nil {
if reflect.DeepEqual(c, controller) {
return nil
}
}
configFile, err := os.Create(c.filename)
if err != nil {
logrus.Error("creating config file", err.Error())
}
defer configFile.Close()
logrus.Info("Save config: ", c.filename)
var out bytes.Buffer
b, err := json.MarshalIndent(c, "", "\t")
if err != nil {
logrus.Error("error marshal json", err)
}
json.Indent(&out, b, "", "\t")
out.WriteTo(configFile)
return nil
}
func (c *Controller) LoadConfigurationFromFile() error {
controller, err := c.loadConfigurationFromFile()
if err != nil {
return err
}
for _, v := range controller.Nodes.All() {
v.Setup(c.Connection, c.pushEvent)
c.Nodes.Add(v)
c.pushEvent(events.NodeDiscoverd{
Address: v.Id,
})
}
return nil
}
func (c *Controller) loadConfigurationFromFile() (*Controller, error) {
configFile, err := os.Open(c.filename)
if err != nil {
return nil, err
}
defer configFile.Close()
controller := &Controller{}
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&controller); err != nil {
return nil, err
}
return controller, nil
}