-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
297 lines (248 loc) · 6.26 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
//go:build !wasm
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/minor-industries/rtgraph"
"github.com/minor-industries/rtgraph/broker"
"github.com/minor-industries/rtgraph/database/sqlite"
"github.com/minor-industries/z2/app"
"github.com/minor-industries/z2/app/time_series"
"github.com/minor-industries/z2/cfg"
"github.com/minor-industries/z2/lib/data"
"github.com/minor-industries/z2/lib/source"
"github.com/minor-industries/z2/lib/source/bike"
"github.com/minor-industries/z2/lib/source/heartrate"
"github.com/minor-industries/z2/lib/source/multi"
"github.com/minor-industries/z2/lib/source/replay"
"github.com/minor-industries/z2/lib/source/rower"
"github.com/minor-industries/z2/lib/variables"
"github.com/minor-industries/z2/server"
"github.com/pkg/errors"
webview "github.com/webview/webview_go"
"os"
)
func run() error {
_, err := flags.Parse(&opts)
if err != nil {
return errors.Wrap(err, "parse flags")
}
if opts.ConfigFile == "" {
opts.ConfigFile = cfg.DefaultConfigPath
}
opts, err := cfg.Load(opts.ConfigFile)
if err != nil {
return errors.Wrap(err, "load config file")
}
errCh := make(chan error)
backends, err := getBackends(opts)
if err != nil {
return errors.Wrap(err, "get backends")
}
samples := backends.Samples.(*sqlite.Backend) // TODO
go samples.RunWriter(errCh)
go backends.RawValues.RunWriter(errCh)
if opts.Scan {
return source.Scan()
}
if opts.ReplayDB != "" {
// Don't write back raw values if we're replaying raw values
opts.WriteRawValues = false
}
gin.SetMode(gin.ReleaseMode)
if err := backends.RawValues.GetORM().AutoMigrate(
&data.RawValue{},
); err != nil {
return errors.Wrap(err, "automigrate")
}
if err := samples.GetORM().AutoMigrate(
&data.Variable{},
); err != nil {
return errors.Wrap(err, "automigrate")
}
graph, err := rtgraph.New(
backends.Samples,
errCh,
rtgraph.Opts{},
)
if err != nil {
return errors.Wrap(err, "new graph")
}
vars, err := variables.NewCache(variables.NewSQLiteStorage(samples.GetORM()))
if err != nil {
return errors.Wrap(err, "new cache")
}
time_series.SetupGraphFunctions(graph, vars)
br := broker.NewBroker()
go br.Start()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
allSources := map[string]source.Source{
"bike": &bike.BikeSource{},
"rower": rower.NewRowerSource(),
"hrm": &heartrate.Source{},
}
multiSource := multi.NewSource()
for _, s := range allSources {
err := multiSource.Add(s)
if err != nil {
return errors.Wrap(err, "add source")
}
}
btHandler := app.NewBTHandler(
graph,
br,
backends.RawValues,
multiSource,
opts.WriteRawValues,
cancel,
ctx,
)
sources, err := setupSources(opts.Devices, btHandler)
if err != nil {
return errors.Wrap(err, "setup source")
}
disconnect := make(chan struct{})
router := gin.New()
err = server.SetupRoutes(router, opts, graph, br, backends, vars, disconnect)
if err != nil {
return errors.Wrap(err, "setup routes")
}
go func() {
errCh <- router.Run(fmt.Sprintf("0.0.0.0:%d", opts.Port))
}()
go func() {
if opts.ReplayDB != "" {
fmt.Println("using replay database", opts.ReplayDB)
go btHandler.Monitor(disconnect) // TODO: should monitor each source independently
errCh <- replay.FromDatabase(
ctx,
opts.ReplayDB,
btHandler.Handle,
)
} else if len(sources.connect) > 0 {
source.Connect(ctx, errCh, sources.connect, disconnect)
fmt.Println("all devices found, source.Connect finished")
go btHandler.Monitor(disconnect) // TODO: should monitor each source independently
} else {
fmt.Println("no sources found")
}
}()
if sources.primary != nil {
z2App := app.NewApp(graph, vars, br, sources.primaryKind, opts.Audio)
z2App.Run()
}
if opts.Webview {
if sources.primary == nil {
return errors.New("unknown primary source")
}
w := webview.New(true)
errCh2 := make(chan error)
go func() {
err = <-errCh
w.Terminate()
errCh2 <- err
}()
runWebview(
opts,
w,
errCh,
fmt.Sprintf("http://localhost:%d/z2/pages/%s.html", opts.Port, sources.primaryKind),
)
return <-errCh2
} else {
return <-errCh
}
}
type SourceInfo struct {
primary source.Source
primaryKind string
connect []source.Device
}
func setupSources(devices []cfg.Device, btHandler *app.BTHandler) (*SourceInfo, error) {
var primarySources []source.Source
result := &SourceInfo{}
for _, dev := range devices {
if dev.Disable == true {
continue
}
var src source.Source
switch dev.Kind {
case "bike":
src = &bike.BikeSource{}
result.primaryKind = dev.Kind
primarySources = append(primarySources, src)
case "rower":
src = rower.NewRowerSource()
result.primaryKind = dev.Kind
primarySources = append(primarySources, src)
case "hrm":
src = &heartrate.Source{}
default:
return nil, fmt.Errorf("unknown device kind: %s", dev.Kind)
}
result.connect = append(result.connect, source.Device{
Info: source.DeviceInfo{
Address: dev.Addr,
Kind: dev.Kind,
Name: dev.Name,
},
Source: src,
Callback: btHandler.Handle,
})
}
switch len(primarySources) {
case 0:
// pass
case 1:
result.primary = primarySources[0]
default:
return nil, errors.New("found multiple primary devices")
}
return result, nil
}
func getBackends(opts *cfg.Config) (app.Backends, error) {
backends := app.Backends{}
var err error
backends.Samples, err = getBackend(opts)
if err != nil {
return app.Backends{}, errors.Wrap(err, "get backend")
}
backends.RawValues, err = getBackend(opts)
if err != nil {
return app.Backends{}, errors.Wrap(err, "get backend")
}
return backends, err
}
func getBackend(opts *cfg.Config) (*sqlite.Backend, error) {
if opts.RemoveDB {
_ = os.Remove(opts.DBPath)
}
db, err := sqlite.Get(opts.DBPath)
if err != nil {
return nil, errors.Wrap(err, "get database")
}
return db, nil
}
func runWebview(
opts *cfg.Config,
w webview.WebView,
ch chan error,
url string,
) {
defer w.Destroy()
w.SetTitle("z2")
w.SetSize(opts.XRes, opts.YRes, webview.HintNone)
w.Navigate(url)
w.Run()
ch <- nil
}
var opts struct {
ConfigFile string `long:"config-file" description:"config file path"`
}
func main() {
err := run()
fmt.Println("run exited, error:", err)
}