This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
/
unbrake.go
244 lines (200 loc) · 6.09 KB
/
unbrake.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Generates a self contained binary application which reads data from
a serial connection (was developed thinking on arduíno via USB)
and send it through network.
It is also able to send commands in ascii format. We are working
On a application which each ascii character represents a state
where some things are on and others off. In our case acelerator,
braking, etc.
We also provide a simple GUI through systray in which users can
interact with the application.
*/
package main
import (
"log"
"os"
"os/signal"
"strconv"
"sync"
"time"
"github.com/getlantern/systray"
emitter "github.com/icaropires/go/v2"
)
// Channels general for controlling execution
var (
wgQuit sync.WaitGroup
wgGeneral sync.WaitGroup
wgHandleExperimentReceiving sync.WaitGroup
stopCollectingDataCh chan bool
sigsCh chan os.Signal
)
var (
connectStatusCh = make(chan string, 1)
aplicationStatusCh = make(chan string)
mqttKeyStatusCh = make(chan string)
quitExperimentEnableCh = make(chan bool)
quitExperimentCh = make(chan bool)
idRunningExperiment = make(chan int)
changeIcon = make(chan bool)
clientWriting *emitter.Client
clientReading *emitter.Client
)
func main() {
logFile := getLogFile()
defer logFile.Close()
loadConfigFile()
log.Println("--------------------------------------------")
log.Println("Initializing application...")
sigsCh = make(chan os.Signal, 1)
signal.Notify(sigsCh, os.Interrupt)
for i, subChannel := range mqttSubchannelSerialAttrs {
serialAttrs[i].mqttSubchannel = subChannel
serialAttrs[i].publishCh = make(chan string)
serialAttrs[i].handleCh = make(chan float64)
}
clientWriting, _ = emitter.Connect(
getMqttHost(),
func(_ *emitter.Client, msg emitter.Message) {},
emitter.WithConnectTimeout(time.Second*2),
emitter.WithAutoReconnect(true),
emitter.WithPingTimeout(time.Second*2),
)
clientReading, _ = emitter.Connect(
getMqttHost(),
func(_ *emitter.Client, msg emitter.Message) {},
emitter.WithConnectTimeout(time.Second*2),
emitter.WithAutoReconnect(true),
emitter.WithPingTimeout(time.Second*2),
)
clientWriting.OnConnect(func(_ *emitter.Client) {
wgHandleExperimentReceiving.Done()
wgQuit.Done()
connectStatusCh <- "Conectado"
log.Println("Connected with writing broker successfully")
})
clientReading.OnConnect(func(_ *emitter.Client) {
log.Println("Connected with reading broker successfully")
})
clientWriting.OnDisconnect(func(_ *emitter.Client, err error) {
connectStatusCh <- "Desconectado"
log.Println("Disconnected from writing from broker: ", err)
})
clientReading.OnDisconnect(func(_ *emitter.Client, err error) {
connectStatusCh <- "Desconectado"
log.Println("Disconnected from reading from broker: ", err)
})
if clientWriting.IsConnected() {
connectStatusCh <- "Conectado"
} else {
connectStatusCh <- "Desconectado"
}
onExit := func() {
log.Println("Exiting...")
}
systray.Run(onReady, onExit)
log.Println("Application finished!")
log.Println("--------------------------------------------")
}
// Required by systray (GUI)
func onReady() {
systray.SetIcon(IconDisabled)
systray.SetTitle("UnBrake")
systray.SetTooltip("UnBrake")
statusTitle := systray.AddMenuItem("Status", "Seção para visualização do status da aplicação")
statusTitle.Disable()
statusCollecting := systray.AddMenuItem("Status de arquisição", "Não iniciada")
mqttKeyStatus := systray.AddMenuItem("Chave de acesso: Não avaliada", "Status da chave do MQTT")
connectStatus := systray.AddMenuItem("Deconectado", "Status de conecção")
handlePortsSectionGUI()
quitExperiment := systray.AddMenuItem("Encerrar ensaio", "Finaliza o ensaio atual")
quitExperiment.Disable()
mQuitOrig := systray.AddMenuItem("Sair", "Fechar UnBrake")
go func() {
for {
select {
case connectStatusAux := <-connectStatusCh:
connectStatus.SetTitle(connectStatusAux)
case aplicationStatusAux := <-aplicationStatusCh:
statusCollecting.SetTitle(aplicationStatusAux)
case mqttKeyStatusChAux := <-mqttKeyStatusCh:
mqttKeyStatus.SetTitle(mqttKeyStatusChAux)
case quitExperimentAux := <-quitExperimentEnableCh:
if quitExperimentAux {
quitExperiment.Disable()
} else {
quitExperiment.Enable()
}
case <-quitExperiment.ClickedCh:
quitExperimentCh <- true
quitExperiment.Disable()
isAvailable = true
systray.SetIcon(IconDisabled)
log.Println("Experiment finished by user")
}
}
}()
go testKeys()
go func() {
for {
wgQuit.Add(1)
key := getMqttKey()
channel := getMqttChannelPrefix() + "/quitExperiment"
clientReading.Subscribe(key, channel, func(_ *emitter.Client, msg emitter.Message) {
if !isAvailable {
log.Println("Experiment finished by user")
quitExperimentCh <- true
quitExperiment.Disable()
isAvailable = true
systray.SetIcon(IconDisabled)
wgQuit.Done()
wgHandleExperimentReceiving.Done()
}
})
wgQuit.Wait()
}
}()
stopCollectingDataCh = make(chan bool, 1)
// Wait for quitting
go func() {
select {
case <-mQuitOrig.ClickedCh:
log.Println("Quitting request by interface")
port.Write([]byte(cooldown))
log.Println("Application finished by user")
log.Println("Change state: _ ---> cooldown")
case <-sigsCh:
log.Println("Quitting request by signal")
port.Write([]byte(cooldown))
log.Println("Application finished by user")
log.Println("Change state: _ ---> cooldown")
}
stopCollectingDataCh <- true
systray.Quit()
log.Println("Finished systray")
}()
go func() {
for {
select {
case idAux := <-idRunningExperiment:
if !isAvailable {
publishData("false: "+strconv.Itoa(idAux), mqttSubchannelIsAvailable)
}
default:
if isAvailable {
publishData("true", mqttSubchannelIsAvailable)
time.Sleep(time.Millisecond * 500)
}
}
}
}()
wgGeneral.Add(1)
go CollectData()
go HandleExperimentsReceiving()
if getMqttKey() != "" {
go publishSerialAttrs()
} else {
log.Println("MQTT key not set!!! Data will not be published...")
}
wgGeneral.Wait()
}
var ever = true