-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflightradar24.go
158 lines (136 loc) · 3.13 KB
/
flightradar24.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
package flightradar
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const FRadar24BaseURL = "https://data-live.flightradar24.com/zones/fcgi/feed.js?"
type FRadar24 struct {
options RadarOptions
}
func NewFRadar24(options RadarOptions) (*FRadar24, error) {
// todo: validate options
return &FRadar24{
options: options,
}, nil
}
// Scan scans the radar for flights
// https://data-live.flightradar24.com/zones/fcgi/feed.js?
//faa=1&
//bounds=49.072%2C48.918%2C2.431%2C2.695& 49.072,48.918,2.431,2.695
//satellite=1&
//mlat=1&
//flarm=1&
//adsb=1&
//gnd=1&
//air=1&
//vehicles=1&
//estimated=1&
//maxage=14400&
//gliders=1&stats=1
func (r FRadar24) Scan() ([]Aircraft, error) {
//var fullCount, version int
// URL
url := FRadar24BaseURL
// faa
if r.options.Faa {
url += "faa=1&"
}
// bounds
url += fmt.Sprintf("bounds=%f,%f,%f,%f&", r.options.Bounds[0], r.options.Bounds[2], r.options.Bounds[1], r.options.Bounds[3])
//satellite
/*if r.options.satellite {
url += "satellite=1&"
}
*/
//mlat
if r.options.Mlat {
url += "mlat=1&"
}
// flarm
if r.options.Flarm {
url += "flarm=1&"
}
// adsb
if r.options.Adsb {
url += "adsb=1&"
}
// gnd
if r.options.OnGround {
url += "gnd=1&"
}
// air
if r.options.InAir {
url += "air=1&"
}
// vehicles
if r.options.Inactive {
url += "vehicles=1&"
}
// estimated
url += "estimated=1&"
// max age
//url += fmt.Sprintf("maxage=%d&", r.options.maxage)
// gliders
if r.options.Gliders {
url += "gliders=1"
}
fmt.Printf("URL: %s\n", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
//log.Println(string(body))
var response map[string]interface{}
if err = json.Unmarshal(body, &response); err != nil {
return nil, err
}
//rr := RadarResponse{}
aircrafts := make([]Aircraft, 0)
//rr.Aircrafts = make(map[string]interface{})
for k, v := range response {
switch k {
case "full_count":
//fullCount = int(v.(float64))
case "version":
//version = int(v.(float64))
case "stats":
//
default:
vMap := v.([]interface{})
aircraft := Aircraft{}
aircraft.Fr24Id = k
aircraft.IcaoRegistration = vMap[0].(string)
aircraft.Latitude = vMap[1].(float64)
aircraft.Longitude = vMap[2].(float64)
aircraft.Bearing = uint8(vMap[3].(float64))
aircraft.Altitude = getUint(vMap[4])
aircraft.Speed = uint(vMap[5].(float64))
aircraft.SquawkCode = getString(vMap[6])
aircraft.RadarID = getString(vMap[7])
aircraft.Registration = getString(vMap[8])
aircraft.IcaoModel = getString(vMap[9])
aircraft.Timestamp = getInt64(vMap[10])
aircraft.Origin = getString(vMap[11])
aircraft.Destination = getString(vMap[12])
aircraft.FlightNumber = getString(vMap[13])
aircraft.IsOnGround = getBoolean(vMap[14])
aircraft.RateOfClimb = getUint(vMap[15])
aircraft.CallSign = getString(vMap[16])
aircraft.IsGlider = getBoolean(vMap[17])
aircraft.Company = getString(vMap[18])
// append
aircrafts = append(aircrafts, aircraft)
}
}
return aircrafts, err
}