forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmslist.go
231 lines (203 loc) · 6.37 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
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
// 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 (
"context"
"encoding/json"
"fmt"
"log"
"math"
"net/url"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/la5nta/pat/internal/cmsapi"
"github.com/la5nta/pat/internal/debug"
"github.com/la5nta/pat/internal/directories"
"github.com/pd0mz/go-maidenhead"
"github.com/spf13/pflag"
)
type JSONURL struct{ url.URL }
func (url JSONURL) MarshalJSON() ([]byte, error) { return json.Marshal(url.String()) }
// JSONFloat64 is a float64 which serializes NaN and Inf(+-) as JSON value null
type JSONFloat64 float64
func (f JSONFloat64) MarshalJSON() ([]byte, error) {
if math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
return json.Marshal(nil)
}
return json.Marshal(float64(f))
}
type RMS struct {
Callsign string `json:"callsign"`
Gridsquare string `json:"gridsquare"`
Distance JSONFloat64 `json:"distance"`
Azimuth JSONFloat64 `json:"azimuth"`
Modes string `json:"modes"`
Freq Frequency `json:"freq"`
Dial Frequency `json:"dial"`
URL *JSONURL `json:"url"`
}
func (r RMS) IsMode(mode string) bool {
if mode == MethodVaraFM {
return strings.HasPrefix(r.Modes, "VARA FM")
}
if mode == MethodVaraHF {
return strings.HasPrefix(r.Modes, "VARA") && !strings.HasPrefix(r.Modes, "VARA FM")
}
return strings.Contains(strings.ToLower(r.Modes), mode)
}
func (r RMS) IsBand(band string) bool {
return bands[band].Contains(r.Freq)
}
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(ctx context.Context, 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)
var query string
if len(set.Args()) > 0 {
query = strings.ToUpper(set.Args()[0])
}
*mode = strings.ToLower(*mode)
rList, err := ReadRMSList(ctx, *forceDownload, func(rms RMS) bool {
switch {
case query != "" && !strings.HasPrefix(rms.Callsign, query):
return false
case mode != nil && !rms.IsMode(*mode):
return false
case band != nil && !rms.IsBand(*band):
return false
default:
return true
}
})
if err != nil {
log.Fatal(err)
}
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")
// Print gateways (separated by blank line)
for i := 0; i < len(rList); i++ {
r := rList[i]
distance := strconv.FormatFloat(float64(r.Distance), 'f', 0, 64)
azimuth := strconv.FormatFloat(float64(r.Azimuth), 'f', 0, 64)
fmt.Printf(fmtStr, r.Callsign, r.Gridsquare, 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 ReadRMSList(ctx context.Context, forceDownload bool, filterFn func(rms RMS) (keep bool)) ([]RMS, error) {
me, err := maidenhead.ParseLocator(config.Locator)
if err != nil {
log.Print("Missing or Invalid Locator, will not compute distance and Azimuth")
}
fileName := "rmslist"
isDefaultServiceCode := len(config.ServiceCodes) == 1 && config.ServiceCodes[0] == "PUBLIC"
if !isDefaultServiceCode {
fileName += "-" + strings.Join(config.ServiceCodes, "-")
}
filePath := filepath.Join(directories.DataDir(), fileName+".json")
debug.Printf("RMS list file is %s", filePath)
f, err := cmsapi.GetGatewayStatusCached(ctx, filePath, forceDownload, config.ServiceCodes...)
if err != nil {
return nil, err
}
defer f.Close()
var status cmsapi.GatewayStatus
if err = json.NewDecoder(f).Decode(&status); err != nil {
return nil, err
}
var slice = []RMS{}
for _, gw := range status.Gateways {
for _, channel := range gw.Channels {
r := RMS{
Callsign: gw.Callsign,
Gridsquare: channel.Gridsquare,
Modes: channel.SupportedModes,
Freq: Frequency(channel.Frequency),
Dial: Frequency(channel.Frequency).Dial(channel.SupportedModes),
}
if chURL := toURL(channel, gw.Callsign); chURL != nil {
r.URL = &JSONURL{*chURL}
}
hasLocator := me != maidenhead.Point{}
if them, err := maidenhead.ParseLocator(channel.Gridsquare); err == nil && hasLocator {
r.Distance = JSONFloat64(me.Distance(them))
r.Azimuth = JSONFloat64(me.Bearing(them))
}
if keep := filterFn(r); !keep {
continue
}
slice = append(slice, r)
}
}
return slice, nil
}
func toURL(gc cmsapi.GatewayChannel, targetCall string) *url.URL {
freq := Frequency(gc.Frequency).Dial(gc.SupportedModes)
chURL, _ := url.Parse(fmt.Sprintf("%s:///%s?freq=%v", toTransport(gc), targetCall, freq.KHz()))
addBandwidth(gc, chURL)
return chURL
}
func addBandwidth(gc cmsapi.GatewayChannel, chURL *url.URL) {
bw := ""
modeF := strings.Fields(gc.SupportedModes)
if modeF[0] == "ARDOP" {
if len(modeF) > 1 {
bw = modeF[1] + "MAX"
}
} else if modeF[0] == "VARA" {
if len(modeF) > 1 && modeF[1] == "FM" {
// VARA FM should not set bandwidth in connect URL or sent over the command port,
// it's set in the VARA Setup dialog
bw = ""
} else {
// VARA HF may be 500, 2750, or none which is implicitly 2300
if len(modeF) > 1 {
if len(modeF) > 1 {
bw = modeF[1]
}
} else {
bw = "2300"
}
}
}
if bw != "" {
v := chURL.Query()
v.Set("bw", bw)
chURL.RawQuery = v.Encode()
}
}
var transports = []string{MethodAX25, MethodPactor, MethodArdop, MethodVaraFM, MethodVaraHF}
func toTransport(gc cmsapi.GatewayChannel) string {
modes := strings.ToLower(gc.SupportedModes)
for _, transport := range transports {
if strings.Contains(modes, "packet") {
// bug(maritnhpedersen): We really don't know which transport to use here. It could be serial-tnc or ax25, but ax25 is most likely.
return MethodAX25
}
if strings.HasPrefix(modes, "vara fm") {
return MethodVaraFM
}
if strings.HasPrefix(modes, "vara") {
return MethodVaraHF
}
if strings.Contains(modes, transport) {
return transport
}
}
return ""
}