-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracks.go
More file actions
218 lines (170 loc) · 5.53 KB
/
tracks.go
File metadata and controls
218 lines (170 loc) · 5.53 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
package gopensky
import (
"context"
"fmt"
"net/url"
"time"
)
const (
trackTimeIndex = 0 + iota
trackLatitudeIndex
trackLongitudeIndex
trackBaroAltitudeIndex
trackTureTrackIndex
trackOnGroundIndex
)
type FlightTrackResponse struct {
Icao24 string `json:"icao24"`
StartTime float64 `json:"startTime"`
EndTime float64 `json:"endTime"`
Callsign *string `json:"callsign"`
Path [][]interface{} `json:"path"`
}
type FlightTrack struct {
// Unique ICAO 24-bit address of the transponder in hex string representation.
Icao24 string `json:"icao24"`
// Time of the first waypoint in seconds since epoch (Unix time).
StartTime int64 `json:"startTime"`
// Time of the last waypoint in seconds since epoch (Unix time).
EndTime int64 `json:"endTime"`
// Callsign (8 characters) that holds for the whole track. Can be nil.
Callsign *string `json:"callsign"`
// Waypoints of the trajectory (description below).
Path []WayPoint `json:"path"`
}
type WayPoint struct {
// Time which the given waypoint is associated with in seconds since epoch (Unix time).
Time int64 `json:"time"`
// WGS-84 latitude in decimal degrees. Can be nil.
Latitude *float64 `json:"latitude"`
// WGS-84 longitude in decimal degrees. Can be nil.
Longitude *float64 `json:"longitude"`
// Barometric altitude in meters. Can be nil.
BaroAltitude *float64 `json:"baroAltitude"`
// True track in decimal degrees clockwise from north (north=0°). Can be nil.
TrueTrack *float64 `json:"trueTrack"`
// Boolean value which indicates if the position was retrieved from a surface position report.
OnGround bool `json:"onGround"`
}
// GetTrackByAircraft retrieves the trajectory for a certain aircraft at a given time.
func GetTrackByAircraft(ctx context.Context, icao24 string, time int64) (FlightTrack, error) {
var (
flightTrack FlightTrack
flightTrackResponse FlightTrackResponse
)
if icao24 == "" {
return flightTrack, ErrInvalidAircraftName
}
if time < 0 {
return flightTrack, ErrInvalidUnixTime
}
conn, err := getClient(ctx)
if err != nil {
return flightTrack, fmt.Errorf("client: %w", err)
}
requestParams := getTracksRequestParams(time, icao24)
response, err := conn.doGetRequest(ctx, "/tracks/all", requestParams)
if err != nil {
return flightTrack, fmt.Errorf("do request: %w", err)
}
errRespProcess := response.process(&flightTrackResponse)
err = response.Body.Close()
if err != nil {
return flightTrack, fmt.Errorf("response body close %w", err)
}
if errRespProcess != nil {
return flightTrack, errRespProcess
}
flightTrack, err = parseFlightTrackResponse(&flightTrackResponse)
if err != nil {
return flightTrack, fmt.Errorf("parse track: %w", err)
}
return flightTrack, nil
}
func parseFlightTrackResponse(response *FlightTrackResponse) (FlightTrack, error) {
var flightTrack FlightTrack
flightTrack.Icao24 = response.Icao24
flightTrack.Callsign = response.Callsign
flightTrack.EndTime = time.Unix(int64(response.EndTime), 0).Unix()
// the api is not returning proper start time value
// temporary checking if its <= 0 then allocated 1
startTime := time.Unix(int64(response.StartTime), 0).Unix()
if startTime <= 0 {
flightTrack.StartTime = 1
} else {
flightTrack.StartTime = startTime
}
for _, waypointData := range response.Path {
waypoint, err := decodeWaypoint(waypointData)
if err != nil {
return flightTrack, fmt.Errorf("decode waypoint: %w", err)
}
flightTrack.Path = append(flightTrack.Path, *waypoint)
}
return flightTrack, nil
}
func decodeWaypoint(data []interface{}) (*WayPoint, error) { //nolint:funlen,cyclop
if len(data) < trackOnGroundIndex {
return nil, errWaypointsDataCount
}
var waypoint WayPoint
// Time index
if data[trackTimeIndex] != nil {
wtime, assertionOK := data[trackTimeIndex].(int64)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointTime, data[trackTimeIndex])
}
waypoint.Time = wtime
}
// Latitude index
if data[trackLatitudeIndex] != nil {
latitude, assertionOK := data[trackLatitudeIndex].(float64)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointLatitude, data[trackLatitudeIndex])
}
waypoint.Latitude = &latitude
}
// Longitude index
if data[trackLongitudeIndex] != nil {
longitude, assertionOK := data[trackLongitudeIndex].(float64)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointLongitude, data[trackLongitudeIndex])
}
waypoint.Longitude = &longitude
}
// BaroAltitude index
if data[trackBaroAltitudeIndex] != nil {
baroAltitude, assertionOK := data[trackBaroAltitudeIndex].(float64)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointBaroAltitude, data[trackBaroAltitudeIndex])
}
waypoint.BaroAltitude = &baroAltitude
}
// TrueTrack index
if data[trackTureTrackIndex] != nil {
trueTrack, assertionOK := data[trackTureTrackIndex].(float64)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointTrueTrack, data[trackTureTrackIndex])
}
waypoint.TrueTrack = &trueTrack
}
// Onground index
if data[trackOnGroundIndex] != nil {
onGround, assertionOK := data[trackOnGroundIndex].(bool)
if !assertionOK {
return nil, fmt.Errorf("%w: %v", errWaypointOnGround, data[trackOnGroundIndex])
}
waypoint.OnGround = onGround
}
return &waypoint, nil
}
func getTracksRequestParams(time int64, icao24 string) url.Values {
requestParams := make(url.Values)
if time >= 0 {
requestParams.Add("time", fmt.Sprintf("%d", time)) //nolint:perfsprint
}
if icao24 != "" {
requestParams.Add("icao24", icao24)
}
return requestParams
}