-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
456 lines (379 loc) · 9.35 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"flag"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"strings"
"github.com/atotto/clipboard"
log "github.com/sirupsen/logrus"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/system"
l "gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/scartill/giox"
xmat "github.com/scartill/giox/material"
)
// This holds the "console" visible text, line number and history (so we can dump everything even when console has been cleared).
type outputWriter struct {
Lines []string
}
// Write just appends to text and history, using the counter as line number for the text.
// It also allows outputWriter to implement the Writer interface so it may be passed to the logger.
func (o *outputWriter) Write(p []byte) (n int, err error) {
o.Lines = append(o.Lines, string(p))
return len(p), nil
}
func (o *outputWriter) Text() string {
text := ""
for i := 0; i < len(o.Lines); i++ {
text = fmt.Sprintf("%s%05d %s", text, i, o.Lines[i])
}
return text
}
// The writer instance
var ow = &outputWriter{Lines: []string{}}
// Message sending control and status.
var (
repeat bool
running bool
stop bool
sendOnce bool
interval int32
)
// Menu variables
var (
fileMI bool
fileMIBtn widget.Clickable
fileOpenBtn widget.Clickable
fileSaveBtn widget.Clickable
fileProvisionBtn widget.Clickable
fileCancelBtn widget.Clickable
consoleMI bool
consoleMIBtn widget.Clickable
consoleClearBtn widget.Clickable
consoleCopyBtn widget.Clickable
consoleDumpBtn widget.Clickable
consoleCancelBtn widget.Clickable
logMI bool
logMIBtn widget.Clickable
logLvlDebugBtn widget.Clickable
logLvlInfoBtn widget.Clickable
logLvlWarningBtn widget.Clickable
logLvlErrorBtn widget.Clickable
)
var (
openFileCombo giox.Combo
openFileCancelBtn widget.Clickable
openFileImportBtn widget.Clickable
)
var (
saveFileEditor widget.Editor
saveFileCancelBtn widget.Clickable
saveFileSaveBtn widget.Clickable
)
func buildMenu(th *material.Theme) (l.FlexChild, bool) {
if openFile {
return buildOpenFile(th)
}
if openProvisioner {
return buildProvisioner(th)
}
if saveFile {
return buildSaveFile(th)
}
for fileMIBtn.Clicked() {
fileMI = true
}
for consoleMIBtn.Clicked() {
consoleMI = true
}
for logMIBtn.Clicked() {
logMI = true
}
for fileOpenBtn.Clicked() {
openFile = true
var err error
files, err = ioutil.ReadDir("./confs/")
if err != nil {
log.Errorf("couldn't list files: %s", err)
}
names := []string{}
for _, info := range files {
filename := fmt.Sprintf("confs/%s", info.Name())
if !strings.Contains(filename, ".toml") {
continue
}
names = append(names, filename)
}
openFileCombo = giox.MakeCombo(names, "<filename>")
fileMI = false
}
for fileSaveBtn.Clicked() {
saveFile = true
fileMI = false
}
for fileProvisionBtn.Clicked() {
openProvisioner = true
fileMI = false
}
for fileCancelBtn.Clicked() {
fileMI = false
}
for consoleClearBtn.Clicked() {
ow.Lines = []string{}
consoleMI = false
}
for consoleClearBtn.Clicked() {
err := clipboard.WriteAll(ow.Text())
if err != nil {
log.Errorf("copy error: %s", err)
}
consoleMI = false
}
for consoleDumpBtn.Clicked() {
writeHistory()
consoleMI = false
}
for consoleCancelBtn.Clicked() {
consoleMI = false
}
for logLvlDebugBtn.Clicked() {
setLevel(log.DebugLevel)
logMI = false
}
for logLvlInfoBtn.Clicked() {
setLevel(log.InfoLevel)
logMI = false
}
for logLvlWarningBtn.Clicked() {
setLevel(log.WarnLevel)
logMI = false
}
for logLvlErrorBtn.Clicked() {
setLevel(log.ErrorLevel)
logMI = false
}
if fileMI {
widget := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidButton(th, "Open", &fileOpenBtn),
xmat.RigidButton(th, "Save", &fileSaveBtn),
xmat.RigidButton(th, "Provision", &fileProvisionBtn),
xmat.RigidButton(th, "Cancel", &fileCancelBtn),
)
})
return widget, true
}
if consoleMI {
widget := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidButton(th, "Clear", &consoleClearBtn),
xmat.RigidButton(th, "Copy", &consoleClearBtn),
xmat.RigidButton(th, "Dump history", &consoleDumpBtn),
xmat.RigidButton(th, "Cancel", &consoleCancelBtn),
)
})
return widget, true
}
if logMI {
widget := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidButton(th, "Debug", &logLvlDebugBtn),
xmat.RigidButton(th, "Info", &logLvlInfoBtn),
xmat.RigidButton(th, "Warning", &logLvlWarningBtn),
xmat.RigidButton(th, "Error", &logLvlErrorBtn),
)
})
return widget, true
}
widget := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
xmat.RigidButton(th, "File", &fileMIBtn),
xmat.RigidButton(th, "Console", &consoleMIBtn),
xmat.RigidButton(th, "Log", &logMIBtn),
)
})
return widget, false
}
func buildOpenFile(th *material.Theme) (l.FlexChild, bool) {
*confFile = openFileCombo.SelectedText()
for openFileCancelBtn.Clicked() {
openFile = false
}
for openFileImportBtn.Clicked() {
importConf()
resetGuiValues()
setDevice()
openFile = false
}
widgets := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidSection(th, "Select file"),
labelCombo(th, "Filename", &openFileCombo),
xmat.RigidButton(th, "Cancel", &openFileCancelBtn),
xmat.RigidButton(th, "Import", &openFileImportBtn),
)
})
return widgets, true
}
func buildSaveFile(th *material.Theme) (l.FlexChild, bool) {
saveFilename = saveFileEditor.Text()
for saveFileCancelBtn.Clicked() {
saveFile = false
}
for saveFileSaveBtn.Clicked() {
exportConf(fmt.Sprintf("confs/%s", saveFilename))
saveFile = false
}
widgets := l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidSection(th, "Save file"),
xmat.RigidEditor(th, "Name", "<filename>", &saveFileEditor),
xmat.RigidButton(th, "Cancel", &saveFileCancelBtn),
xmat.RigidButton(th, "Save", &saveFileSaveBtn),
)
})
return widgets, true
}
func resetGuiValues() {
mqttResetGuiValue()
forwarderResetGuiValues()
loraResetGuiValues()
deviceResetGuiValues()
macResetGuiValues()
dataResetGuiValues()
provResetGuiValues()
}
var (
serversButton widget.Clickable
deviceButton widget.Clickable
loraButton widget.Clickable
controlButton widget.Clickable
dataButton widget.Clickable
tabIndex uint
)
func mainWindow(gtx l.Context, th *material.Theme) {
wOutputForm := outputForm(th)
wMenu, isMenuOpen := buildMenu(th)
if isMenuOpen {
l.NW.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
wMenu,
xmat.RigidSeparator(th, &giox.Separator{}),
wOutputForm,
)
})
return
}
for serversButton.Clicked() {
tabIndex = 0
}
for deviceButton.Clicked() {
tabIndex = 1
}
for loraButton.Clicked() {
tabIndex = 2
}
for controlButton.Clicked() {
tabIndex = 3
}
for dataButton.Clicked() {
tabIndex = 4
}
tabsWidget := l.Rigid(func(gtx l.Context) l.Dimensions {
p100 := gtx.Px(unit.Dp(100))
p500 := gtx.Px(unit.Dp(500))
gtx.Constraints = l.Exact(image.Point{X: p100, Y: p500})
return l.Flex{Axis: l.Vertical}.Layout(gtx,
xmat.RigidButton(th, "Connect", &serversButton),
xmat.RigidButton(th, "Device", &deviceButton),
xmat.RigidButton(th, "LoRa", &loraButton),
xmat.RigidButton(th, "Control", &controlButton),
xmat.RigidButton(th, "Data", &dataButton),
)
})
wMqttForm := mqttForm(th)
wForwarderForm := forwarderForm(th)
wDeviceForm := deviceForm(th)
wLoraForm := loRaForm(th)
wControlForm := controlForm(th)
wDataForm := dataForm(th)
var selectedWidget l.FlexChild
switch tabIndex {
case 0:
selectedWidget = l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
wMqttForm,
xmat.RigidSeparator(th, &giox.Separator{}),
wForwarderForm,
)
})
case 1:
selectedWidget = wDeviceForm
case 2:
selectedWidget = wLoraForm
case 3:
selectedWidget = wControlForm
case 4:
selectedWidget = wDataForm
}
l.NW.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx,
wMenu,
xmat.RigidSeparator(th, &giox.Separator{}),
l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
tabsWidget,
selectedWidget,
)
}),
xmat.RigidSeparator(th, &giox.Separator{}),
wOutputForm,
)
})
}
func loop(w *app.Window) error {
th := material.NewTheme(gofont.Collection())
var ops op.Ops
for {
e := <-w.Events()
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent:
gtx := l.NewContext(&ops, e)
mainWindow(gtx, th)
e.Frame(gtx.Ops)
}
}
}
func main() {
/* runtime.LockOSThread() */
mw := io.MultiWriter(ow, os.Stderr)
log.SetOutput(mw)
confFile = flag.String("conf", "conf.toml", "path to toml configuration file")
flag.Parse()
createLoRaForm()
createDeviceForm()
createDataForm()
createOutputForm()
tabIndex = 0
importConf()
resetGuiValues()
setDevice()
go func() {
defer os.Exit(0)
w := app.NewWindow(app.Size(unit.Dp(1024), unit.Dp(768)))
if err := loop(w); err != nil {
log.Fatal(err)
}
}()
app.Main()
}