forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmslist.go
163 lines (131 loc) · 3.88 KB
/
rmslist.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
// Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"path"
"strings"
"github.com/spf13/pflag"
"github.com/la5nta/wl2k-go/mailbox"
"github.com/la5nta/pat/internal/cmsapi"
"github.com/pd0mz/go-maidenhead"
"sort"
"strconv"
)
type rms struct {
callsign string
gridsq string
distance float64
azimuth float64
modes string
freq string
dial string
url *url.URL
}
type byDist []rms
func (r byDist) Len() int { return len(r) }
func (r byDist) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r byDist) Less(i, j int) bool { return r[i].distance < r[j].distance }
func rmsListHandle(args []string) {
set := pflag.NewFlagSet("rmslist", pflag.ExitOnError)
mode := set.StringP("mode", "m", "", "")
band := set.StringP("band", "b", "", "")
forceDownload := set.BoolP("force-download", "d", false, "")
byDistance := set.BoolP("sort-distance", "s", false, "")
set.Parse(args)
appDir, err := mailbox.DefaultAppDir()
if err != nil {
log.Fatal(err)
}
filePath := path.Join(appDir, "rmslist.json") // Should be moved to a tmp-folder, along with logfile.
var query string
if len(set.Args()) > 0 {
query = strings.ToUpper(set.Args()[0])
}
file, err := cmsapi.GetGatewayStatusCached(filePath, *forceDownload)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var status cmsapi.GatewayStatus
err = json.NewDecoder(file).Decode(&status)
if err != nil {
log.Fatal(err)
}
noLocator := false
me, err := maidenhead.ParseLocator(config.Locator)
if err != nil {
log.Print("Missing or Invalid Locator, will not compute distance and Azimuth")
noLocator = true
}
*mode = strings.ToLower(*mode)
rList := []rms{}
// Print gateways (separated by blank line)
for _, gw := range status.Gateways {
switch {
case query != "" && !strings.HasPrefix(gw.Callsign, query):
continue
default:
}
for _, channel := range gw.Channels {
r := rms{
callsign: gw.Callsign,
gridsq: channel.Gridsquare,
modes: channel.SupportedModes,
}
f := Frequency(channel.Frequency)
r.dial = f.Dial(channel.SupportedModes).String()
r.freq = f.String()
switch {
case mode != nil && !strings.Contains(strings.ToLower(channel.SupportedModes), *mode):
continue
case !bands[*band].Contains(f):
continue
}
r.distance = float64(0)
r.azimuth = float64(0)
if !noLocator {
if them, err := maidenhead.ParseLocator(channel.Gridsquare); err == nil {
r.distance = me.Distance(them)
r.azimuth = me.Bearing(them)
}
}
r.url = toURL(channel, gw.Callsign)
rList = append(rList, r)
}
}
if *byDistance {
sort.Sort(byDist(rList))
}
fmtStr := "%-9.9s [%-6.6s] %-6.6s %3.3s %-15.15s %14.14s %14.14s %s\n"
// Print header
fmt.Printf(fmtStr, "callsign", "gridsq", "dist", "Az", "mode(s)", "dial freq", "center freq", "url") //TODO: "center frequency" of packet is wrong...
for i := 0; i < len(rList); i++ {
r := rList[i]
distance := strconv.FormatFloat(r.distance, 'f', 0, 64)
azimuth := strconv.FormatFloat(r.azimuth, 'f', 0, 64)
fmt.Printf(fmtStr, r.callsign, r.gridsq, distance, azimuth, r.modes, r.dial, r.freq, r.url)
if i+1 < len(rList) && rList[i].callsign != rList[i+1].callsign {
fmt.Println("")
}
}
}
func toURL(gc cmsapi.GatewayChannel, targetcall string) *url.URL {
freq := Frequency(gc.Frequency).Dial(gc.SupportedModes)
url, _ := url.Parse(fmt.Sprintf("%s:///%s?freq=%v", toTransport(gc), targetcall, freq.KHz()))
return url
}
var transports = []string{"winmor", "packet", "pactor", "ardop"}
func toTransport(gc cmsapi.GatewayChannel) string {
modes := strings.ToLower(gc.SupportedModes)
for _, transport := range transports {
if strings.Contains(modes, transport) {
return transport
}
}
return ""
}