forked from jancona/m17
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_logger.go
More file actions
61 lines (58 loc) · 1.67 KB
/
Copy pathdashboard_logger.go
File metadata and controls
61 lines (58 loc) · 1.67 KB
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
package m17
import (
"encoding/json"
"fmt"
"log"
"log/slog"
"time"
)
type DashboardLogger struct {
*slog.Logger
lastLogTime time.Time
}
func NewDashboardLogger(l *slog.Logger) *DashboardLogger {
return &DashboardLogger{l, time.Now()}
}
func (l *DashboardLogger) Log(logType string, logSubtype string, addlArgs ...any) {
if l == nil {
return
}
args := []any{"type", logType, "subtype", logSubtype}
args = append(args, addlArgs...)
l.Info("", args...)
}
func (l *DashboardLogger) LogFrame(lsf *LSF, logType string, logSubtype string, addlArgs ...any) {
args := []any{"src", lsf.Src.Callsign(), "dst", lsf.Dst.Callsign(), "can", lsf.CAN()}
args = append(args, addlArgs...)
l.Log(logType, logSubtype, args...)
}
func (l *DashboardLogger) LogGNSS(lsf *LSF, logType string) {
if lsf.GNSS() != nil && lsf.GNSS().ValidLatLon && time.Since(l.lastLogTime) > 15*time.Second {
log.Printf("[DEBUG] Writing GNSS data to dashboard.log: %s", lsf.GNSS().String())
l.lastLogTime = time.Now()
args := []any{
"dataSource", lsf.GNSS().DataSource,
"stationType", lsf.GNSS().StationType,
"src", lsf.Src.Callsign(),
"latitude", json.Number(fmt.Sprintf("%f", lsf.GNSS().Latitude)),
"longitude", json.Number(fmt.Sprintf("%f", lsf.GNSS().Longitude)),
}
if lsf.GNSS().ValidAltitude {
args = append(args,
"altitude", json.Number(fmt.Sprintf("%.1f", lsf.GNSS().Altitude)),
)
}
if lsf.GNSS().ValidBearingSpeed {
args = append(args,
"speed", json.Number(fmt.Sprintf("%.1f", lsf.GNSS().Speed)),
"bearing", lsf.GNSS().Bearing,
)
}
if lsf.GNSS().ValidRadius {
args = append(args,
"radius", lsf.GNSS().Radius,
)
}
l.Log(logType, "GNSS", args...)
}
}