160 lines
5.1 KiB
C++
160 lines
5.1 KiB
C++
#include "TrackedAircraft.h"
|
|
#include "LEDMatrix.cpp"
|
|
#include "Network.cpp"
|
|
#include "SBS1Decoder.cpp"
|
|
#include <arpa/inet.h>
|
|
#include <climits>
|
|
#include <cmath>
|
|
#include <ctime>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#include <unordered_map>
|
|
|
|
struct FlightWallOpts {
|
|
std::string server_ip = "127.0.0.1";
|
|
int port = 30003;
|
|
double lat = 0;
|
|
double lon = 0;
|
|
};
|
|
|
|
class FlightWall {
|
|
private:
|
|
FlightWallOpts opts_;
|
|
|
|
// hex ident, all other info
|
|
std::unordered_map<std::string, TrackedAircraft> tracked;
|
|
double min_distance = INT_MAX;
|
|
std::string min_aircraft_icao;
|
|
TrackedAircraft min_distance_aircraft;
|
|
|
|
static constexpr double EARTH_RADIUS_M = 6371000.0;
|
|
static constexpr double DEG_TO_RAD = M_PI / 180.0;
|
|
|
|
static double get_distance(double lat1, double lon1, double lat2,
|
|
double lon2) {
|
|
double lat1Rad = lat1 * DEG_TO_RAD;
|
|
double lat2Rad = lat2 * DEG_TO_RAD;
|
|
double dLat = lat2Rad - lat1Rad;
|
|
double dLon = (lon2 - lon1) * DEG_TO_RAD;
|
|
|
|
double x = dLon * std::cos((lat1Rad + lat2Rad) * 0.5);
|
|
double y = dLat;
|
|
|
|
return EARTH_RADIUS_M * std::hypot(x, y);
|
|
}
|
|
|
|
public:
|
|
FlightWall(FlightWallOpts opts) : opts_(opts) {
|
|
LEDMatrix m(&min_distance_aircraft);
|
|
}
|
|
|
|
int start() {
|
|
Network n(opts_.server_ip, opts_.port);
|
|
|
|
char buffer[1024];
|
|
std::string line;
|
|
|
|
|
|
while (true) {
|
|
ssize_t bytes_rec = recv(n.get_sock(), buffer, sizeof(buffer) - 1, 0);
|
|
if (bytes_rec < 0) {
|
|
std::cerr << "Connection or stream error\n";
|
|
break;
|
|
}
|
|
|
|
buffer[bytes_rec] = '\0';
|
|
line += buffer;
|
|
|
|
size_t pos = 0;
|
|
while ((pos = line.find('\n')) != std::string::npos) {
|
|
std::string msg = line.substr(0, pos);
|
|
|
|
if (!msg.empty() && msg.back() == '\r') {
|
|
msg.pop_back();
|
|
}
|
|
|
|
// std::cout << msg << "\n";
|
|
// if (auto parsed = SBS1Decoder::decode(msg)) {
|
|
// std::cout << "ICAO: " << parsed->hex_ident << " | Type: MSG,"
|
|
// << parsed->transmission_type;
|
|
//
|
|
// if (parsed->callsign)
|
|
// std::cout << " | Callsign: " << *parsed->callsign;
|
|
// if (parsed->altitude)
|
|
// std::cout << " | Alt: " << *parsed->altitude << " ft";
|
|
// if (parsed->latitude && parsed->longitude)
|
|
// std::cout << " | Pos: (" << *parsed->latitude << ", "
|
|
// << *parsed->longitude << ")";
|
|
// if (parsed->ground_speed)
|
|
// std::cout << " | Speed: " << *parsed->ground_speed << " kts";
|
|
//
|
|
// std::cout << "\n";
|
|
// }
|
|
//
|
|
|
|
// if new aircraft, store, check and update min if necessary
|
|
// if seen aircraft, update record, check and update min if necessary
|
|
if (auto parsed = SBS1Decoder::decode(msg)) {
|
|
std::string aircraft_id = parsed->hex_ident;
|
|
auto stored_aircraft = tracked.find(aircraft_id);
|
|
|
|
TrackedAircraft aircraft_record = {
|
|
parsed->callsign, parsed->altitude, parsed->ground_speed,
|
|
parsed->track, parsed->latitude, parsed->longitude,
|
|
parsed->vertical_rate, parsed->emergency, std::time(nullptr)};
|
|
|
|
// aircraft not in database
|
|
if (stored_aircraft == tracked.end()) {
|
|
tracked[aircraft_id] = aircraft_record;
|
|
|
|
// aircraft in database, upsert
|
|
} else {
|
|
TrackedAircraft updated = stored_aircraft->second;
|
|
|
|
if (aircraft_record.callsign)
|
|
updated.callsign = aircraft_record.callsign;
|
|
if (aircraft_record.altitude)
|
|
updated.altitude = aircraft_record.altitude;
|
|
if (aircraft_record.ground_speed)
|
|
updated.ground_speed = aircraft_record.ground_speed;
|
|
if (aircraft_record.track)
|
|
updated.track = aircraft_record.track;
|
|
if (aircraft_record.latitude)
|
|
updated.latitude = aircraft_record.latitude;
|
|
if (aircraft_record.longitude)
|
|
updated.longitude = aircraft_record.longitude;
|
|
if (aircraft_record.vertical_rate)
|
|
updated.vertical_rate = aircraft_record.vertical_rate;
|
|
if (aircraft_record.emergency)
|
|
updated.emergency = aircraft_record.emergency;
|
|
updated.last_seen = std::time(nullptr);
|
|
|
|
// overwrite existing value
|
|
tracked[aircraft_id] = updated;
|
|
}
|
|
|
|
// update min distance and aircraft if changed
|
|
if (parsed->latitude && parsed->longitude) {
|
|
double parsed_dist =
|
|
get_distance(opts_.lat, opts_.lon, *parsed->latitude, *parsed->longitude);
|
|
if (parsed_dist < min_distance) {
|
|
min_distance = parsed_dist;
|
|
min_aircraft_icao = parsed->hex_ident;
|
|
min_distance_aircraft = tracked.find(parsed->hex_ident)->second;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::cout << "Closest aircraft: " << min_aircraft_icao
|
|
<< ", distance: " << min_distance << "m\n";
|
|
|
|
line.erase(0, pos + 1);
|
|
}
|
|
}
|
|
close(n.get_sock());
|
|
return 0;
|
|
};
|
|
};
|