forked from mutability/dump978
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfaup978_reporter.cc
345 lines (300 loc) · 15.4 KB
/
faup978_reporter.cc
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
// Copyright (c) 2019, FlightAware LLC.
// All rights reserved.
// Licensed under the 2-clause BSD license; see the LICENSE file
#include "faup978_reporter.h"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace flightaware::uat;
using namespace flightaware::faup978;
void Reporter::Start() {
PeriodicReport();
PurgeOld();
}
void Reporter::Stop() {
report_timer_.cancel();
purge_timer_.cancel();
}
void Reporter::HandleMessages(flightaware::uat::SharedMessageVector messages) {
// Process metadata messages, note fecfix and report versions
for (const auto &i : *messages) {
if (i.Type() != MessageType::METADATA)
continue;
auto &metadata = i.Metadata();
if (metadata.count("fecfix"))
fecfix_ = true;
if (metadata.count("program") || metadata.count("version")) {
const std::uint64_t now = now_millis();
std::cout << "_v" << '\t' << TSVVersion() << '\t' << "clock" << '\t' << (now / 1000);
if (metadata.count("program"))
std::cout << '\t' << "uat_program" << '\t' << metadata.at("program");
if (metadata.count("version"))
std::cout << '\t' << "uat_program_version" << '\t' << metadata.at("version");
std::cout << std::endl;
}
}
tracker_->HandleMessages(messages);
}
void Reporter::PurgeOld() {
tracker_->PurgeOld();
auto &aircraft = tracker_->Aircraft();
for (auto i = reported_.begin(); i != reported_.end();) {
if (aircraft.count(i->first) == 0) {
i = reported_.erase(i);
} else {
++i;
}
}
auto self(shared_from_this());
purge_timer_.expires_from_now(timeout_ / 4);
purge_timer_.async_wait(strand_.wrap([this, self](const boost::system::error_code &ec) {
if (!ec) {
PurgeOld();
}
}));
}
template <typename T> static std::string value_map(T value, const std::map<T, std::string> &mappings, const std::string &default_value) {
auto i = mappings.find(value);
if (i == mappings.end()) {
return default_value;
} else {
return i->second;
}
}
template <typename T> static std::function<void(std::ostream &)> simple_emit(const T &field, int precision = 0) {
return [&field, precision](std::ostream &os) { os << std::fixed << std::setprecision(precision) << field.Value(); };
}
void Reporter::PeriodicReport() {
const std::uint64_t now = now_millis();
for (const auto &entry : tracker_->Aircraft()) {
ReportOneAircraft(entry.first, entry.second, now);
}
auto self(shared_from_this());
report_timer_.expires_from_now(interval_);
report_timer_.async_wait(strand_.wrap([this, self](const boost::system::error_code &ec) {
if (!ec) {
PeriodicReport();
}
}));
}
void Reporter::ReportOneAircraft(const Tracker::AddressKey &key, const AircraftState &aircraft, std::uint64_t now) {
// don't report TIS-B at all
if (aircraft.address_qualifier == AddressQualifier::TISB_ICAO || aircraft.address_qualifier == AddressQualifier::TISB_TRACKFILE) {
return;
}
auto &last = reported_[key];
auto &last_state = last.report_state;
if (aircraft.messages < 2) {
// possibly noise
}
if (aircraft.last_message_time <= last.report_time) {
// no data received since last report
return;
}
bool changed = false;
changed |= (last_state.pressure_altitude && aircraft.pressure_altitude && std::abs(last_state.pressure_altitude.Value() - aircraft.pressure_altitude.Value()) >= 50);
changed |= (last_state.geometric_altitude && aircraft.geometric_altitude && std::abs(last_state.pressure_altitude.Value() - aircraft.pressure_altitude.Value()) >= 50);
changed |= (last_state.vertical_velocity_barometric && aircraft.vertical_velocity_barometric && std::abs(last_state.vertical_velocity_barometric.Value() - aircraft.vertical_velocity_barometric.Value()) >= 500);
changed |= (last_state.vertical_velocity_geometric && aircraft.vertical_velocity_geometric && std::abs(last_state.vertical_velocity_geometric.Value() - aircraft.vertical_velocity_geometric.Value()) >= 500);
changed |= (last_state.true_track && aircraft.true_track && std::abs(last_state.true_track.Value() - aircraft.true_track.Value()) >= 2);
changed |= (last_state.true_heading && aircraft.true_heading && std::abs(last_state.true_heading.Value() - aircraft.true_heading.Value()) >= 2);
changed |= (last_state.magnetic_heading && aircraft.magnetic_heading && std::abs(last_state.magnetic_heading.Value() - aircraft.magnetic_heading.Value()) >= 2);
changed |= (last_state.ground_speed && aircraft.ground_speed && std::abs(last_state.ground_speed.Value() - aircraft.ground_speed.Value()) >= 25);
bool immediate = false;
immediate |= (aircraft.selected_altitude_mcp.Changed() > last.report_time);
immediate |= (aircraft.selected_altitude_fms.Changed() > last.report_time);
immediate |= (aircraft.selected_heading.Changed() > last.report_time);
immediate |= (aircraft.mode_indicators.Changed() > last.report_time);
immediate |= (aircraft.barometric_pressure_setting.Changed() > last.report_time);
immediate |= (aircraft.callsign.Changed() > last.report_time);
immediate |= (aircraft.flightplan_id.Changed() > last.report_time);
immediate |= (aircraft.airground_state.Changed() > last.report_time);
immediate |= (aircraft.emergency.Changed() > last.report_time);
boost::optional<int> altitude = -10000; // keep the compiler happier
if (aircraft.pressure_altitude.UpdateAge(now) < 30000)
altitude = aircraft.pressure_altitude.Value();
else if (aircraft.geometric_altitude.UpdateAge(now) < 30000)
altitude = aircraft.geometric_altitude.Value();
else
altitude = boost::none;
boost::optional<AirGroundState> airground = AirGroundState::RESERVED;
if (aircraft.airground_state.UpdateAge(now) < 30000)
airground = aircraft.airground_state.Value();
else
airground = boost::none;
boost::optional<int> groundspeed = -10000;
if (aircraft.ground_speed.UpdateAge(now) < 30000)
groundspeed = aircraft.ground_speed.Value();
else
groundspeed = boost::none;
std::uint64_t minAge;
if (immediate) {
// a change we want to emit right away
minAge = 0;
} else if (airground && airground.get() == AirGroundState::ON_GROUND) {
// we are probably on the ground, increase the update rate
minAge = 1000;
} else if (altitude && altitude.get() < 500 && (!groundspeed || groundspeed.get() < 200)) {
// we are probably on the ground, increase the update rate
minAge = 1000;
} else if (groundspeed && groundspeed.get() < 100 && (!altitude || altitude.get() < 1000)) {
// we are probably on the ground, increase the update rate
minAge = 1000;
} else if (!altitude || altitude.get() < 10000) {
// Below 10000 feet, emit up to every 5s when changing, 10s otherwise
minAge = changed ? 5000 : 10000;
} else {
// Above 10000 feet, emit up to every 10s when changing, 30s otherwise
minAge = changed ? 10000 : 30000;
}
bool force_slow = (now - last.slow_report_time) > 300000;
if ((now - last.report_time) < minAge) {
// Not this time.
return;
}
std::vector<std::pair<std::string, std::string>> kv;
// clang-format off
static std::map<AddressQualifier, std::string> source_map = {
{AddressQualifier::ADSB_ICAO, "A"},
{AddressQualifier::ADSB_OTHER, "A"},
{AddressQualifier::ADSR_OTHER, "A"},
{AddressQualifier::TISB_ICAO, "T"},
{AddressQualifier::TISB_TRACKFILE, "T"}
};
// clang-format on
std::string source = value_map(aircraft.address_qualifier, source_map, "?");
auto add_slow_field = [&kv, &source, &last, force_slow](const std::string &k, const AgedFieldBase &f, std::function<void(std::ostream &)> stringize) {
if (f.Valid() && (force_slow || f.Changed() > last.report_time)) {
std::ostringstream os;
stringize(os);
kv.emplace_back(k, os.str());
}
};
auto add_slow_aged_field = [&kv, &source, &last, now, force_slow](const std::string &k, const AgedFieldBase &f, std::function<void(std::ostream &)> stringize) {
if (f.Valid() && (force_slow || f.Changed() > last.report_time)) {
std::ostringstream os;
stringize(os);
os << ' ' << (f.UpdateAge(now) / 1000) << ' ' << source;
kv.emplace_back(k, os.str());
}
};
auto add_aged_field = [&kv, &source, &last, now](const std::string &k, const AgedFieldBase &f, std::function<void(std::ostream &)> stringize) {
if (f.Valid() && f.Updated() > last.report_time) {
std::ostringstream os;
stringize(os);
os << ' ' << (f.UpdateAge(now) / 1000) << ' ' << source;
kv.emplace_back(k, os.str());
}
};
add_slow_field("uat_version", aircraft.mops_version, simple_emit(aircraft.mops_version));
add_slow_field("category", aircraft.emitter_category, [&aircraft](std::ostream &os) {
unsigned as_hex = 0xA0 + (aircraft.emitter_category.Value() & 7) + ((aircraft.emitter_category.Value() & 0x18) << 1);
os << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << as_hex;
});
add_slow_aged_field("nac_p", aircraft.nac_p, simple_emit(aircraft.nac_p));
add_slow_aged_field("nac_v", aircraft.nac_v, simple_emit(aircraft.nac_v));
add_slow_aged_field("sil", aircraft.sil, simple_emit(aircraft.sil));
add_slow_aged_field("sil_type", aircraft.sil_supplement, [&aircraft](std::ostream &os) {
// clang-format off
static std::map<SILSupplement, std::string> supplement_map = {
{SILSupplement::PER_HOUR, "perhour"},
{SILSupplement::PER_SAMPLE, "persample"},
};
// clang-format on
os << value_map(aircraft.sil_supplement.Value(), supplement_map, "unknown");
});
add_slow_aged_field("nic_baro", aircraft.nic_baro, simple_emit(aircraft.nic_baro));
add_aged_field("airGround", aircraft.airground_state, [&aircraft](std::ostream &os) {
// clang-format off
static std::map<AirGroundState, std::string> airground_map = {
{AirGroundState::AIRBORNE_SUBSONIC, "A+"},
{AirGroundState::AIRBORNE_SUPERSONIC, "A+"},
{AirGroundState::ON_GROUND, "A+"}
};
// clang-format on
os << value_map(aircraft.airground_state.Value(), airground_map, "?");
});
add_aged_field("squawk", aircraft.flightplan_id, [&aircraft](std::ostream &os) { os << '{' << aircraft.flightplan_id.Value() << '}'; });
add_aged_field("ident", aircraft.callsign, [&aircraft](std::ostream &os) { os << '{' << aircraft.callsign.Value() << '}'; });
add_aged_field("alt", aircraft.pressure_altitude, simple_emit(aircraft.pressure_altitude));
add_aged_field("position", aircraft.position, [&aircraft](std::ostream &os) {
auto &p = aircraft.position.Value();
unsigned nic = aircraft.nic.Valid() ? aircraft.nic.Value() : 0; // should always be valid if the position is
double rc = aircraft.horizontal_containment.Valid() ? aircraft.horizontal_containment.Value() : 0;
os << '{' << std::fixed << std::setprecision(5) << p.first << ' ' << p.second << ' ' << nic << ' ' << std::setprecision(0) << std::ceil(rc) << '}';
});
add_aged_field("alt_gnss", aircraft.geometric_altitude, simple_emit(aircraft.geometric_altitude));
add_aged_field("vrate", aircraft.vertical_velocity_barometric, simple_emit(aircraft.vertical_velocity_barometric));
add_aged_field("vrate_geom", aircraft.vertical_velocity_geometric, simple_emit(aircraft.vertical_velocity_geometric));
add_aged_field("speed", aircraft.ground_speed, simple_emit(aircraft.ground_speed));
add_aged_field("track", aircraft.true_track, simple_emit(aircraft.true_track, 1));
add_aged_field("heading_magnetic", aircraft.magnetic_heading, simple_emit(aircraft.magnetic_heading, 1));
add_aged_field("heading_true", aircraft.true_heading, simple_emit(aircraft.true_heading, 1));
add_aged_field("nav_alt_mcp", aircraft.selected_altitude_mcp, simple_emit(aircraft.selected_altitude_mcp));
add_aged_field("nav_alt_fms", aircraft.selected_altitude_fms, simple_emit(aircraft.selected_altitude_fms));
add_aged_field("nav_heading", aircraft.selected_heading, simple_emit(aircraft.selected_heading));
add_aged_field("nav_modes", aircraft.mode_indicators, [&aircraft](std::ostream &os) {
bool first = true;
auto emit = [&os, &first](bool value, const std::string item) {
if (value) {
if (!first)
os << " ";
os << item;
first = false;
}
};
auto &indicators = aircraft.mode_indicators.Value();
os << "{";
emit(indicators.autopilot, "autopilot");
emit(indicators.vnav, "vnav");
emit(indicators.altitude_hold, "althold");
emit(indicators.approach, "approach");
emit(indicators.lnav, "lnav");
os << "}";
});
add_aged_field("nav_qnh", aircraft.barometric_pressure_setting, simple_emit(aircraft.barometric_pressure_setting, 1));
add_aged_field("emergency", aircraft.emergency, [&aircraft](std::ostream &os) {
// clang-format off
static std::map<EmergencyPriorityStatus, std::string> emergency_map = {
{EmergencyPriorityStatus::NONE, "none"},
{EmergencyPriorityStatus::GENERAL, "general"},
{EmergencyPriorityStatus::MEDICAL, "lifeguard"},
{EmergencyPriorityStatus::MINFUEL, "minfuel"},
{EmergencyPriorityStatus::NORDO, "nordo"},
{EmergencyPriorityStatus::UNLAWFUL, "unlawful"},
{EmergencyPriorityStatus::DOWNED, "downed"}};
// clang-format on
os << value_map(aircraft.emergency.Value(), emergency_map, "unknown");
});
// did we actually generate anything?
if (kv.empty()) {
return;
}
// generate the line
std::cout << "_v" << '\t' << TSVVersion() << '\t' << "clock" << '\t' << (now / 1000) << '\t';
bool icao = (aircraft.address_qualifier == AddressQualifier::ADSB_ICAO || aircraft.address_qualifier == AddressQualifier::TISB_ICAO);
std::cout << (icao ? "hexid" : "otherid") << '\t' << std::hex << std::uppercase << std::setfill('0') << std::setw(6) << aircraft.address << std::dec << std::nouppercase << std::setfill(' ');
if (force_slow || !icao) {
// clang-format off
static std::map<AddressQualifier, std::string> qualifier_map = {
{AddressQualifier::ADSB_ICAO, "adsb_icao"},
{AddressQualifier::ADSB_OTHER, "adsb_other"},
{AddressQualifier::TISB_ICAO, "tisb_icao"},
{AddressQualifier::TISB_TRACKFILE, "tisb_trackfile"},
{AddressQualifier::VEHICLE, "vehicle"},
{AddressQualifier::FIXED_BEACON, "fixed_beacon"},
{AddressQualifier::ADSR_OTHER, "adsr_other"}
};
// clang-format on
std::cout << '\t' << "addrtype" << '\t' << value_map(aircraft.address_qualifier, qualifier_map, "unknown");
}
for (const auto &entry : kv) {
std::cout << '\t' << entry.first << '\t' << entry.second;
}
std::cout << std::endl;
if (force_slow)
last.slow_report_time = now;
last.report_time = now;
last.report_state = aircraft;
}