-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqrcodetask.go
More file actions
354 lines (323 loc) · 8.96 KB
/
qrcodetask.go
File metadata and controls
354 lines (323 loc) · 8.96 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package xctrack
import (
"encoding/json"
"errors"
"fmt"
"math"
"strings"
polyline "github.com/twpayne/go-polyline"
)
// Constants.
const (
QRCodeScheme = "XCTSK:"
QRCodeTaskVersion = 2
)
// A QRCodeDirection is a QR code direction.
type QRCodeDirection int
// QR code directions.
const (
QRCodeDirectionEnter QRCodeDirection = 1
QRCodeDirectionExit QRCodeDirection = 2
)
// A QRCodeEarthModel is a QR code Earth model.
type QRCodeEarthModel int
// QR code Earth models.
const (
QRCodeEarthModelWGS84 QRCodeEarthModel = 0
QRCodeEarthModelFAISphere QRCodeEarthModel = 1
)
// A QRCodeGoal is a QR code goal.
type QRCodeGoal struct {
Deadline *TimeOfDay `json:"d,omitempty"`
Type QRCodeGoalType `json:"t,omitempty"`
}
// A QRCodeGoalType is a QR code goal type.
type QRCodeGoalType int
// QR code goal types.
const (
QRCodeGoalTypeLine QRCodeGoalType = 1
QRCodeGoalTypeCylinder QRCodeGoalType = 2
)
// A QRCodeSSSType is a QR code start of speed section type.
type QRCodeSSSType int
// QR code start of speed section types.
const (
QRCodeSSSTypeRace QRCodeSSSType = 1
QRCodeSSSTypeElapsedTime QRCodeSSSType = 2
)
// A QRCodeSSS is a QR code start.
type QRCodeSSS struct {
TimeGates []*TimeOfDay `json:"g"`
Direction QRCodeDirection `json:"d"`
Type QRCodeSSSType `json:"t"`
}
// A QRCodeTask is a QR code task.
type QRCodeTask struct {
TaskType TaskType `json:"taskType"`
Version int `json:"version"`
Turnpoints []*QRCodeTurnpoint `json:"t"`
TakeoffOpen *TimeOfDay `json:"to,omitempty"`
TakeoffClose *TimeOfDay `json:"tc,omitempty"`
SSS *QRCodeSSS `json:"s,omitempty"`
Goal *QRCodeGoal `json:"g,omitempty"`
EarthModel QRCodeEarthModel `json:"e"`
}
// A QRCodeTaskType is a QR code task type.
type QRCodeTaskType int
// QR code task types.
const (
QRCodeTaskTypeRace QRCodeTaskType = 1
QRCodeTaskTypeElapsedTime QRCodeTaskType = 2
)
// A QRCodeTurnpoint is a QR code turnpoint.
type QRCodeTurnpoint struct {
Z QRCodeTurnpointZ `json:"z"`
Name string `json:"n"`
Description string `json:"d,omitempty"`
Type QRCodeTurnpointType `json:"t,omitempty"`
}
// A QRCodeTurnpointType is a QR code turnpoint type.
type QRCodeTurnpointType int
// QR code turnpoint types.
const (
QRCodeTurnpointTypeNone QRCodeTurnpointType = 0
QRCodeTurnpointTypeSSS QRCodeTurnpointType = 2
QRCodeTurnpointTypeESS QRCodeTurnpointType = 3
)
// A QRCodeTurnpointZ is a QR code turnpoint Z.
type QRCodeTurnpointZ struct {
Lon float64
Lat float64
Alt int
Radius int
}
var (
errExpectedClosingDoubleQuote = errors.New("expected closing double quote")
errExpectedOpeningDoubleQuote = errors.New("expected opening double quote")
errTrailingBytes = errors.New("trailing bytes")
)
// An invalidQRCodeTurnpointZError is an invalid QR code turnpoint Z error.
type invalidQRCodeTurnpointZError struct {
Value []byte
Err error
}
func (e invalidQRCodeTurnpointZError) Error() string {
return fmt.Sprintf("invalid QR code turnpoint z: %q: %v", string(e.Value), e.Err)
}
var (
qrCodeDirectionValue = map[Direction]QRCodeDirection{
DirectionEnter: QRCodeDirectionEnter,
DirectionExit: QRCodeDirectionExit,
}
qrCodeEarthModelValue = map[EarthModel]QRCodeEarthModel{
EarthModelWGS84: QRCodeEarthModelWGS84,
EarthModelFAISphere: QRCodeEarthModelFAISphere,
}
qrCodeGoalTypeValue = map[GoalType]QRCodeGoalType{
GoalTypeLine: QRCodeGoalTypeLine,
GoalTypeCylinder: QRCodeGoalTypeCylinder,
}
qrCodeSSSTypeValue = map[SSSType]QRCodeSSSType{
SSSTypeRace: QRCodeSSSTypeRace,
SSSTypeElapsedTime: QRCodeSSSTypeElapsedTime,
}
qrCodeTurnpointTypeValue = map[TurnpointType]QRCodeTurnpointType{
TurnpointTypeNone: QRCodeTurnpointTypeNone,
TurnpointTypeSSS: QRCodeTurnpointTypeSSS,
TurnpointTypeESS: QRCodeTurnpointTypeESS,
}
directionValue = map[QRCodeDirection]Direction{
QRCodeDirectionEnter: DirectionEnter,
QRCodeDirectionExit: DirectionExit,
}
earthModelValue = map[QRCodeEarthModel]EarthModel{
QRCodeEarthModelWGS84: EarthModelWGS84,
QRCodeEarthModelFAISphere: EarthModelFAISphere,
}
goalTypeValue = map[QRCodeGoalType]GoalType{
QRCodeGoalTypeLine: GoalTypeLine,
QRCodeGoalTypeCylinder: GoalTypeCylinder,
}
sssTypeValue = map[QRCodeSSSType]SSSType{
QRCodeSSSTypeRace: SSSTypeRace,
QRCodeSSSTypeElapsedTime: SSSTypeElapsedTime,
}
turnpointTypeValue = map[QRCodeTurnpointType]TurnpointType{
QRCodeTurnpointTypeNone: TurnpointTypeNone,
QRCodeTurnpointTypeSSS: TurnpointTypeSSS,
QRCodeTurnpointTypeESS: TurnpointTypeESS,
}
)
// MarshalJSON implements encoding/json.Marshaler.
func (z *QRCodeTurnpointZ) MarshalJSON() ([]byte, error) {
buf := make([]byte, 0, 64)
buf = polyline.EncodeInt(buf, int(math.Round(1e5*z.Lon)))
buf = polyline.EncodeInt(buf, int(math.Round(1e5*z.Lat)))
buf = polyline.EncodeInt(buf, z.Alt)
buf = polyline.EncodeInt(buf, z.Radius)
return quote(buf), nil
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (z *QRCodeTurnpointZ) UnmarshalJSON(value []byte) error {
b := value
if len(b) == 0 || b[0] != '"' {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: errExpectedOpeningDoubleQuote,
}
}
b = b[1:]
lon, b, err := polyline.DecodeInt(b)
if err != nil {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: err,
}
}
lat, b, err := polyline.DecodeInt(b)
if err != nil {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: err,
}
}
alt, b, err := polyline.DecodeInt(b)
if err != nil {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: err,
}
}
radius, b, err := polyline.DecodeInt(b)
if err != nil {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: err,
}
}
if len(b) == 0 || b[0] != '"' {
return invalidQRCodeTurnpointZError{
Value: value,
Err: errExpectedClosingDoubleQuote,
}
}
b = b[1:]
if len(b) != 0 {
return &invalidQRCodeTurnpointZError{
Value: value,
Err: errTrailingBytes,
}
}
z.Lon = float64(lon) / 1e5
z.Lat = float64(lat) / 1e5
z.Alt = alt
z.Radius = radius
return nil
}
// QRCodeTask returns t as a QRCodeTask.
func (t *Task) QRCodeTask() *QRCodeTask {
qrCodeTask := &QRCodeTask{
TaskType: t.TaskType,
Version: QRCodeTaskVersion,
Turnpoints: make([]*QRCodeTurnpoint, 0, len(t.Turnpoints)),
EarthModel: qrCodeEarthModelValue[t.EarthModel],
}
for _, turnpoint := range t.Turnpoints {
qrCodeTurnpoint := &QRCodeTurnpoint{
Z: QRCodeTurnpointZ{
Lon: turnpoint.Waypoint.Lon,
Lat: turnpoint.Waypoint.Lat,
Alt: turnpoint.Waypoint.AltSmoothed,
Radius: turnpoint.Radius,
},
Name: turnpoint.Waypoint.Name,
Description: turnpoint.Waypoint.Description,
Type: qrCodeTurnpointTypeValue[turnpoint.Type],
}
qrCodeTask.Turnpoints = append(qrCodeTask.Turnpoints, qrCodeTurnpoint)
}
if t.Takeoff != nil {
qrCodeTask.TakeoffOpen = t.Takeoff.TimeOpen
qrCodeTask.TakeoffClose = t.Takeoff.TimeClose
}
if t.SSS != nil {
qrCodeTask.SSS = &QRCodeSSS{
TimeGates: t.SSS.TimeGates,
Direction: qrCodeDirectionValue[t.SSS.Direction],
Type: qrCodeSSSTypeValue[t.SSS.Type],
}
}
if t.Goal != nil {
qrCodeTask.Goal = &QRCodeGoal{
Deadline: t.Goal.Deadline,
Type: qrCodeGoalTypeValue[t.Goal.Type],
}
}
return qrCodeTask
}
func (q *QRCodeTask) String() (string, error) {
sb := &strings.Builder{}
sb.Grow(4096)
sb.WriteString(QRCodeScheme)
if err := json.NewEncoder(sb).Encode(q); err != nil {
return "", err
}
return sb.String(), nil
}
// Task returns t as a Task.
func (q *QRCodeTask) Task() *Task {
task := &Task{
TaskType: q.TaskType,
Version: Version,
Turnpoints: make([]*Turnpoint, 0, len(q.Turnpoints)),
EarthModel: earthModelValue[q.EarthModel],
}
for _, qrCodeTurnpoint := range q.Turnpoints {
turnpoint := &Turnpoint{
Type: turnpointTypeValue[qrCodeTurnpoint.Type],
Radius: qrCodeTurnpoint.Z.Radius,
Waypoint: Waypoint{
Name: qrCodeTurnpoint.Name,
Description: qrCodeTurnpoint.Description,
Lat: qrCodeTurnpoint.Z.Lat,
Lon: qrCodeTurnpoint.Z.Lon,
AltSmoothed: qrCodeTurnpoint.Z.Alt,
},
}
task.Turnpoints = append(task.Turnpoints, turnpoint)
}
if q.TakeoffOpen != nil || q.TakeoffClose != nil {
task.Takeoff = &Takeoff{
TimeOpen: q.TakeoffOpen,
TimeClose: q.TakeoffClose,
}
}
if q.SSS != nil {
task.SSS = &SSS{
Type: sssTypeValue[q.SSS.Type],
Direction: directionValue[q.SSS.Direction],
TimeGates: q.SSS.TimeGates,
}
}
if q.Goal != nil {
task.Goal = &Goal{
Type: goalTypeValue[q.Goal.Type],
Deadline: q.Goal.Deadline,
}
}
return task
}
func quote(data []byte) []byte {
result := make([]byte, 0, 2*len(data)+2)
result = append(result, '"')
for _, b := range data {
switch b {
case '"', '\\':
result = append(result, '\\', b)
default:
result = append(result, b)
}
}
result = append(result, '"')
return result
}