-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (104 loc) · 2.54 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
package main
import (
"fmt"
"go1090/mode_s"
"go1090/rtl_adsb"
"log"
"sort"
"time"
"github.com/awesome-gocui/gocui"
. "github.com/logrusorgru/aurora"
)
type Context struct {
decoder *mode_s.Decoder
sky *mode_s.Sky
}
func CreateContext() *Context {
return &Context{
decoder: &mode_s.Decoder{},
sky: mode_s.NewSky(),
}
}
func (ctx *Context) update(g *gocui.Gui) error {
// update time and aircraft count
s, _ := g.View("status")
s.Clear()
fmt.Fprintf(s, " A/C: %02d LAST UPDATE: %s\n",
Green(ctx.sky.AircraftCount()),
Bold(Green(time.Now().Format("2006-01-02 15:04:05"))))
l, _ := g.View("list")
l.Clear()
// display aircraft list
fmt.Fprintln(l, " ICAO ADDR FLIGHT ALT SPD HDG LAT LON SEEN")
fmt.Fprintln(l, " ===================================================================")
aircrafts := ctx.sky.Aircrafts()
addrs := make([]uint32, 0, len(aircrafts))
for addr := range aircrafts {
addrs = append(addrs, addr)
}
sort.Slice(addrs, func(i, j int) bool { return addrs[i] < addrs[j] })
for _, addr := range addrs {
ac := aircrafts[addr]
fmt.Fprintln(l, Sprintf(Yellow(" %6s %9s %-5d %-5d %-3d %6.2f %6.2f %s"),
ac.HexAddr,
ac.Flight,
ac.Altitude,
ac.Speed,
ac.Track,
ac.Latitude,
ac.Longitude,
ac.Seen.Format("15:04:05")))
}
return nil
}
func main() {
// init ui
g, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
// init decoder and sky
ctx := CreateContext()
ctx.decoder.Init()
// start receive
handler := func(rcv rtl_adsb.ADSBMsg) {
msg := mode_s.ModeSMessage{}
ctx.decoder.DecodeModesMessage(&msg, rcv[:])
ctx.sky.UpdateData(&msg)
g.Update(ctx.update)
}
stopFunc, e := rtl_adsb.StartReceive("rtl_adsb.exe", handler)
if e != nil {
log.Panicln("error: ", e)
}
//
go func() {
for ; ; <-time.Tick(time.Second * 1) {
ctx.sky.RemoveStaleAircrafts()
g.Update(ctx.update)
}
}()
if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) {
log.Panicln(err)
}
stopFunc()
}
func layout(g *gocui.Gui) error {
// layout
const maxX = 80
_, maxY := g.Size()
v, _ := g.SetView("status", 0, 0, maxX-2, 2, 0)
v.Title = " STATUS "
fmt.Fprintln(v, " A/C: -- LAST UPDATE: 0000-00-00 00:00:00")
v, _ = g.SetView("list", 0, 3, maxX-2, maxY-1, 0)
v.Title = " A/C "
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}