forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmslist.go
176 lines (149 loc) · 4.73 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
// 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"
"sort"
"strconv"
"github.com/la5nta/pat/internal/cmsapi"
"github.com/pd0mz/go-maidenhead"
)
type JSONURL struct{ url.URL }
func (url JSONURL) MarshalJSON() ([]byte, error) { return json.Marshal(url.String()) }
type RMS struct {
Callsign string `json:"callsign"`
Gridsquare string `json:"gridsquare"`
Distance float64 `json:"distance"`
Azimuth float64 `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 {
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(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(*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(r.Distance, 'f', 0, 64)
azimuth := strconv.FormatFloat(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(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")
}
appDir, err := mailbox.DefaultAppDir()
if err != nil {
log.Fatal(err)
}
fileName := "rmslist"
isDefaultServiceCode := len(config.ServiceCodes) == 1 && config.ServiceCodes[0] == "PUBLIC"
if !isDefaultServiceCode {
fileName += "-" + strings.Join(config.ServiceCodes, "-")
}
filePath := path.Join(appDir, fileName+".json") // Should be moved to a tmp-folder, along with logfile.
f, err := cmsapi.GetGatewayStatusCached(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
}
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 url := toURL(channel, gw.Callsign); url != nil {
r.URL = &JSONURL{*url}
}
hasLocator := me != maidenhead.Point{}
if them, err := maidenhead.ParseLocator(channel.Gridsquare); err == nil && hasLocator {
r.Distance = me.Distance(them)
r.Azimuth = 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)
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 ""
}