-
Notifications
You must be signed in to change notification settings - Fork 1
/
gopolloplus.go
281 lines (243 loc) · 8.69 KB
/
gopolloplus.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
package main
import (
"flag"
"fmt"
homedir "github.com/mitchellh/go-homedir"
"log"
"os"
"time"
fyne "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/cjeanneret/gopolloplus/pkg/apolloMonitor"
"github.com/cjeanneret/gopolloplus/pkg/apolloUtils"
"github.com/cjeanneret/gopolloplus/pkg/apolloUI"
)
func main() {
var cfg *apolloUtils.ApolloConfig
standard_cfg, _ := homedir.Expand("~/.gopolloplus.ini")
_, err := os.Stat(standard_cfg)
if err == nil {
log.Printf("Found default config file: %s", standard_cfg)
cfg = apolloUtils.LoadConfig(standard_cfg)
} else {
log.Printf("File not found, checking parameters")
config_file := flag.String("c", "", "Configuration file")
flag.Parse()
if *config_file == "" {
log.Print("No configuration file, using defaults")
cfg = apolloUtils.DefaultConfig()
} else {
log.Printf("Loading %v", *config_file)
cfg = apolloUtils.LoadConfig(*config_file)
}
}
log_file, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer log_file.Close()
log.Printf("Writing logs to %s", cfg.LogFile)
log.SetOutput(log_file)
log.Print("############ NEW RUN")
monitor := apolloMonitor.NewMonitor(cfg.Socket, 9600)
if !monitor.WaitPort() {
log.Fatal("Port not available - exiting")
}
err = monitor.Connect()
if err != nil {
log.Fatal(err)
}
monitor.ResetSession()
data_flow := make(chan *apolloMonitor.ApolloData, 5) // Make a buffered chan just in case
killWriter := make(chan bool)
hfile := apolloUtils.GetHistoryFile(cfg)
history_file, _ := os.OpenFile(hfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
defer history_file.Close()
apolloUtils.CSVHeader(history_file)
if err != nil {
log.Fatal(err)
}
ui := app.New()
ui.Settings().SetTheme(cfg.Theme)
window := ui.NewWindow("GoPolloPlus - FDF Apollo Plus Rower Stats")
window.SetMaster()
window.CenterOnScreen()
if cfg.FullScreen {
window.SetFullScreen(true)
} else {
window.Resize(fyne.Size{Width:float32(apolloUI.GraphWidth*3 + 10), Height: 600})
window.SetFixedSize(true)
}
// Define time things (clock and elapsed time)
clockLabel := apolloUI.TimeCanvas("Time")
val_elapsed := apolloUI.TimeCanvas("Elapsed")
val_dist := apolloUI.TimeCanvas("Distance")
containerTimes := container.NewGridWithColumns(
3,
container.NewCenter(clockLabel),
container.NewCenter(val_elapsed),
container.NewCenter(val_dist),
)
// Define buttons
button_quit := widget.NewButtonWithIcon("Quit", theme.CancelIcon(), func() {
killWriter <- true
monitor.Disconnect()
history_file.Close()
window.Close()
ui.Quit()
})
// Split canvas
lshift := float32(10)
split_title := &canvas.Text{Color: theme.TextColor(), Text: "Split Time",
TextSize: apolloUI.TitleFontSize,
TextStyle: fyne.TextStyle{Bold: true}}
split_title.Move(fyne.Position{lshift, 5})
split_current, split_curr_txt := apolloUI.CreateCanvas(lshift, 55, apolloUI.CurrColor)
split_avg, split_avg_txt := apolloUI.CreateCanvas(lshift, 110, apolloUI.AVGColor)
split_max, split_max_txt := apolloUI.CreateCanvas(lshift, 165, apolloUI.MaxColor)
// Power canvas
lshift = apolloUI.GraphWidth+10
power_title := &canvas.Text{Color: theme.TextColor(), Text: "Power (Watts)",
TextSize: apolloUI.TitleFontSize,
TextStyle: fyne.TextStyle{Bold: true}}
power_title.Move(fyne.Position{lshift, 5})
power_current, power_curr_txt := apolloUI.CreateCanvas(lshift, 55, apolloUI.CurrColor)
power_avg, power_avg_txt := apolloUI.CreateCanvas(lshift, 110, apolloUI.AVGColor)
power_max, power_max_txt := apolloUI.CreateCanvas(lshift, 165, apolloUI.MaxColor)
// SPM canvas
lshift = (2*apolloUI.GraphWidth)+10
spm_title := &canvas.Text{Color: theme.TextColor(),
Text: "Strokes per minutes",
TextSize: apolloUI.TitleFontSize,
TextStyle: fyne.TextStyle{Bold: true}}
spm_title.Move(fyne.Position{lshift, 5})
spm_current, spm_curr_txt := apolloUI.CreateCanvas(lshift, 55, apolloUI.CurrColor)
spm_avg, spm_avg_txt := apolloUI.CreateCanvas(lshift, 110, apolloUI.AVGColor)
spm_max, spm_max_txt := apolloUI.CreateCanvas(lshift, 165, apolloUI.MaxColor)
// Define graph container
containerGraphs := container.NewWithoutLayout(split_title, split_current, split_curr_txt,
split_avg, split_avg_txt,
split_max, split_max_txt,
power_title, power_current, power_curr_txt,
power_avg, power_avg_txt,
power_max, power_max_txt,
spm_title, spm_current, spm_curr_txt,
spm_avg, spm_avg_txt,
spm_max, spm_max_txt)
button_reset := widget.NewButtonWithIcon("New Session", theme.DeleteIcon(), func() {
log.Print("Resetting remote monitor")
monitor.ResetSession()
history_file.Close()
// Prepare a new history file
hfile = apolloUtils.GetHistoryFile(cfg)
history_file, _ = os.OpenFile(hfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
})
button_c2 := widget.NewButtonWithIcon("Send to log.C2", theme.MailForwardIcon(), func() {
log.Print("Sending data to log.C2")
})
if !cfg.Concept2 {
button_c2.Disable()
}
button_theme := widget.NewCheck("Dark Theme", func(checked bool) {
if checked {
ui.Settings().SetTheme(theme.DarkTheme())
cfg.ThemeVariant = "dark"
} else {
ui.Settings().SetTheme(theme.LightTheme())
cfg.ThemeVariant = "light"
}
cfg.Write()
labels := []*canvas.Text{split_title, power_title, spm_title, clockLabel, val_elapsed, val_dist}
for _, o := range labels {
o.Color = theme.TextColor()
o.Refresh()
}
})
if cfg.ThemeVariant == "dark" {
button_theme.SetChecked(true)
}
containerButtons := container.NewAdaptiveGrid(
4,
button_theme, button_reset, button_c2, button_quit,
)
mainContainer := container.NewVBox(
containerButtons,
containerTimes,
containerGraphs,
)
window.SetContent(mainContainer)
// Update clock
go func(clockLabel *canvas.Text) {
var (
hours, minutes, seconds int
)
for {
hours, minutes, seconds = time.Time.Clock(time.Now())
clockLabel.Text = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
clockLabel.Refresh()
time.Sleep(time.Second)
}
}(clockLabel)
go func() {
var (
err error
)
for {
// Let's take the monitor data!
_, err = monitor.Read(29)
if err != nil {
log.Printf("Reader: %v", err)
}
if monitor.Data != "" && len(monitor.Data) == 29 {
log.Printf("Reader: %s", monitor.Data)
data_flow <- monitor.ParseData()
}
time.Sleep(time.Millisecond * 500) // 0.5 second
}
}()
go func() {
log.Print("Start chan reader")
var (
split_history = []uint64{}
power_history = []uint64{}
spm_history = []uint64{}
duration time.Duration
)
exit := false
for {
select {
case d := <-data_flow:
duration, _ = time.ParseDuration(fmt.Sprintf("%vs", d.TotalTime))
val_dist.Text = fmt.Sprintf("%v meters", d.Distance)
val_dist.Refresh()
val_elapsed.Text = fmt.Sprintf("%v", duration)
val_elapsed.Refresh()
split_history = append(split_history, d.TimeTo500m)
power_history = append(power_history, d.Watt)
spm_history = append(spm_history, d.SPM)
go apolloUI.ResizeCanvas(split_history, split_current, split_avg, split_max,
split_curr_txt, split_avg_txt, split_max_txt, true)
go apolloUI.ResizeCanvas(power_history, power_current, power_avg, power_max,
power_curr_txt, power_avg_txt, power_max_txt, false)
go apolloUI.ResizeCanvas(spm_history, spm_current, spm_avg, spm_max,
spm_curr_txt, spm_avg_txt, spm_max_txt, false)
go func() {
history_file.Write([]byte(d.ToCSV()))
}()
default:
}
select {
case exit = <-killWriter:
log.Print("Killing Writer");
default:
}
if exit { break }
}
}()
// show window - LAST action in main()
window.ShowAndRun()
}