-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks.go
159 lines (125 loc) · 3.85 KB
/
tracks.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
package gopensky
import (
"context"
"fmt"
"net/url"
"time"
)
// 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)
}
defer response.Body.Close()
if err := response.process(&flightTrackResponse); err != nil {
return flightTrack, err
}
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
}