-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconval.go
309 lines (242 loc) · 6.49 KB
/
conval.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
The package conval helps to evaluate the log files from amateur radio contests.
*/
package conval
import (
"log"
"strconv"
"strings"
"time"
"github.com/ftl/hamradio/callsign"
"github.com/ftl/hamradio/dxcc"
"github.com/ftl/hamradio/locator"
)
type OperatorMode string
const (
SingleOperator OperatorMode = "single"
MultiOperator OperatorMode = "multi"
)
type TXMode string
const (
OneTX TXMode = "one"
TwoTX TXMode = "two"
MultiTX TXMode = "multi"
DistributedTX TXMode = "distributed"
)
type PowerMode string
const (
HighPower PowerMode = "high"
LowPower PowerMode = "low"
QRPPower PowerMode = "qrp"
)
type ContestBand string
const (
BandAll ContestBand = "all"
Band160m ContestBand = "160m"
Band80m ContestBand = "80m"
Band40m ContestBand = "40m"
Band20m ContestBand = "20m"
Band15m ContestBand = "15m"
Band10m ContestBand = "10m"
)
var AllHFBands = []ContestBand{Band160m, Band80m, Band40m, Band20m, Band15m, Band10m}
type BandCount string
const (
AllBands BandCount = "all"
SingleBand BandCount = "single"
)
func (c BandCount) ToInt() int {
switch c {
case AllBands:
return 0
case SingleBand:
return 1
default:
i, err := strconv.Atoi(string(c))
if err != nil {
return 0
}
return i
}
}
type Mode string
const (
ModeALL Mode = "all"
ModeCW Mode = "cw"
ModeSSB Mode = "ssb"
ModeFM Mode = "fm"
ModeRTTY Mode = "rtty"
ModeDigital Mode = "digital"
)
type PropertyValidator interface {
ValidateProperty(string, PrefixDatabase) error
}
type PropertyValidatorFunc func(string, PrefixDatabase) error
func (f PropertyValidatorFunc) ValidateProperty(exchange string, prefixes PrefixDatabase) error {
return f(exchange, prefixes)
}
type PropertyValidators interface {
PropertyValidator(Property) (PropertyValidator, bool)
}
type PropertyValidatorsFunc func(Property) (PropertyValidator, bool)
func (f PropertyValidatorsFunc) PropertyValidator(property Property) (PropertyValidator, bool) {
return f(property)
}
var commonPropertyValidators = map[Property]PropertyValidator{}
func CommonPropertyValidator(property Property) (PropertyValidator, bool) {
validator, ok := commonPropertyValidators[property]
return validator, ok
}
type Continent string
const (
Africa Continent = "af"
Antarctica Continent = "an"
Asia Continent = "as"
Europa Continent = "eu"
NorthAmerica Continent = "na"
Oceania Continent = "oc"
SouthAmerica Continent = "sa"
SameContinent Continent = "same"
OtherContinent Continent = "other"
NotContinent Continent = "not"
)
type DXCCEntity string
func (e DXCCEntity) String() string {
return string(e)
}
const (
SameCountry DXCCEntity = "same"
OtherCountry DXCCEntity = "other"
NotCountry DXCCEntity = "not"
)
const (
SamePrefix string = "same"
OtherPrefix string = "other"
NotPrefix string = "not"
)
type CQZone int
func (z CQZone) String() string {
return strconv.Itoa(int(z))
}
type ITUZone int
func (z ITUZone) String() string {
return strconv.Itoa(int(z))
}
type PrefixDatabase interface {
Find(s string) (Continent, DXCCEntity, CQZone, ITUZone, bool)
}
type PrefixDatabaseFunc func(s string) (Continent, DXCCEntity, bool)
func (f PrefixDatabaseFunc) Find(s string) (Continent, DXCCEntity, bool) {
return f(s)
}
func NewPrefixDatabase(arrlCompliant bool) (*prefixDatabase, error) {
prefixes, _, err := dxcc.DefaultPrefixes(true)
if err != nil {
return nil, err
}
return &prefixDatabase{prefixes: prefixes, arrlCompliant: arrlCompliant}, nil
}
type prefixDatabase struct {
prefixes *dxcc.Prefixes
arrlCompliant bool
}
func (d prefixDatabase) Find(s string) (Continent, DXCCEntity, CQZone, ITUZone, bool) {
var entities []dxcc.Prefix
var found bool
if d.arrlCompliant {
entities, found = d.prefixes.FindARRLCompliant(s)
} else {
entities, found = d.prefixes.Find(s)
}
if !found || len(entities) == 0 {
return "", "", 0, 0, false
}
return Continent(strings.ToLower(entities[0].Continent)),
DXCCEntity(strings.ToLower(entities[0].PrimaryPrefix)),
CQZone(entities[0].CQZone),
ITUZone(entities[0].ITUZone),
true
}
type BandRule string
const (
Once BandRule = "once"
OncePerBand BandRule = "once_per_band"
OncePerBandAndMode BandRule = "once_per_band_and_mode"
)
type Overlay string
const (
NoOverlay Overlay = ""
ClassicOverlay Overlay = "classic"
ThreeBandAndWiresOverlay Overlay = "tb_wires"
RookieOverlay Overlay = "rookie"
YouthOverlay Overlay = "youth"
)
type Property string
type PropertyGetter interface {
GetProperty(QSO, Setup, PrefixDatabase) string
}
type PropertyGetterFunc func(QSO, Setup, PrefixDatabase) string
func (f PropertyGetterFunc) GetProperty(qso QSO, setup Setup, prefixes PrefixDatabase) string {
return f(qso, setup, prefixes)
}
var myPropertyGetters = map[Property]PropertyGetter{}
var commonPropertyGetters = map[Property]PropertyGetter{}
type QSO struct {
TheirCall callsign.Callsign
TheirContinent Continent
TheirCountry DXCCEntity
Timestamp time.Time
Band ContestBand
Mode Mode
MyExchange QSOExchange
TheirExchange QSOExchange
}
func (q QSO) TheirPrefix() string {
return WPXPrefix(q.TheirCall)
}
type QSOExchange map[Property]string
func ParseExchange(fields []ExchangeField, values []string, prefixes PrefixDatabase, propertyValidators PropertyValidators) QSOExchange {
result := make(QSOExchange)
result.Add(fields, values, prefixes, propertyValidators)
return result
}
func (e QSOExchange) Add(fields []ExchangeField, values []string, prefixes PrefixDatabase, propertyValidators PropertyValidators) {
for i, field := range fields {
if i >= len(values) {
break
}
value := strings.ToUpper(strings.TrimSpace(values[i]))
for _, property := range field {
validator, ok := propertyValidators.PropertyValidator(property)
if !ok {
log.Printf("no validator for property %s", property)
continue
}
err := validator.ValidateProperty(value, prefixes)
if err == nil {
e[property] = value
break
}
}
}
}
type Setup struct {
MyCall callsign.Callsign
MyContinent Continent
MyCountry DXCCEntity
GridLocator locator.Locator
Operators []callsign.Callsign
OperatorMode OperatorMode
Overlay Overlay
Power PowerMode
Bands []ContestBand
Modes []Mode
MyExchange QSOExchange
}
func (s Setup) MyPrefix() string {
return WPXPrefix(s.MyCall)
}
type Constraint struct {
OperatorMode OperatorMode `yaml:"operator_mode"`
Overlay Overlay `yaml:"overlay"`
}