add decoder to output message
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
struct SBS1Message {
|
||||
std::string message_type; // Field 0 (e.g., "MSG")
|
||||
int transmission_type = 0; // Field 1 (1-8)
|
||||
std::string session_id; // Field 2
|
||||
std::string aircraft_id; // Field 3
|
||||
std::string hex_ident; // Field 4 (ICAO 24-bit hex)
|
||||
std::string flight_id; // Field 5
|
||||
std::string date_generated; // Field 6
|
||||
std::string time_generated; // Field 7
|
||||
std::string date_logged; // Field 8
|
||||
std::string time_logged; // Field 9
|
||||
std::optional<std::string> callsign; // Field 10
|
||||
std::optional<int> altitude; // Field 11 (ft)
|
||||
std::optional<double> ground_speed; // Field 12 (knots)
|
||||
std::optional<double> track; // Field 13 (degrees)
|
||||
std::optional<double> latitude; // Field 14
|
||||
std::optional<double> longitude; // Field 15
|
||||
std::optional<int> vertical_rate; // Field 16 (ft/min)
|
||||
std::optional<std::string> squawk; // Field 17
|
||||
std::optional<bool> alert; // Field 18 (Squawk change)
|
||||
std::optional<bool> emergency; // Field 19
|
||||
std::optional<bool> spi; // Field 20 (Ident active)
|
||||
std::optional<bool> is_on_ground; // Field 21
|
||||
};
|
||||
|
||||
class SBS1Decoder {
|
||||
private:
|
||||
static std::vector<std::string_view> splitCSV(std::string_view line) {
|
||||
std::vector<std::string_view> fields;
|
||||
fields.reserve(22);
|
||||
|
||||
size_t start = 0;
|
||||
while (start <= line.size()) {
|
||||
size_t end = line.find(',', start);
|
||||
if (end == std::string_view::npos) {
|
||||
fields.push_back(line.substr(start));
|
||||
break;
|
||||
}
|
||||
fields.push_back(line.substr(start, end - start));
|
||||
start = end + 1;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
static std::optional<int> parseInt(std::string_view sv) {
|
||||
if (sv.empty()) return std::nullopt;
|
||||
try {
|
||||
return std::stoi(std::string(sv));
|
||||
} catch (...) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<double> parseDouble(std::string_view sv) {
|
||||
if (sv.empty()) return std::nullopt;
|
||||
try {
|
||||
return std::stod(std::string(sv));
|
||||
} catch (...) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<bool> parseBool(std::string_view sv) {
|
||||
if (sv.empty()) return std::nullopt;
|
||||
if (sv == "1" || sv == "-1") return true;
|
||||
if (sv == "0") return false;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
static std::string_view trim(std::string_view sv) {
|
||||
size_t first = sv.find_first_not_of(" \r\n\t");
|
||||
if (first == std::string_view::npos) return "";
|
||||
size_t last = sv.find_last_not_of(" \r\n\t");
|
||||
return sv.substr(first, (last - first + 1));
|
||||
}
|
||||
|
||||
public:
|
||||
static std::optional<SBS1Message> decode(std::string_view raw_line) {
|
||||
std::string_view clean_line = trim(raw_line);
|
||||
if (clean_line.empty()) return std::nullopt;
|
||||
|
||||
auto fields = splitCSV(clean_line);
|
||||
|
||||
if (fields.size() < 10) return std::nullopt;
|
||||
|
||||
SBS1Message msg;
|
||||
msg.message_type = std::string(fields[0]);
|
||||
msg.transmission_type = parseInt(fields[1]).value_or(0);
|
||||
msg.session_id = std::string(fields[2]);
|
||||
msg.aircraft_id = std::string(fields[3]);
|
||||
msg.hex_ident = std::string(fields[4]);
|
||||
msg.flight_id = std::string(fields[5]);
|
||||
msg.date_generated = std::string(fields[6]);
|
||||
msg.time_generated = std::string(fields[7]);
|
||||
msg.date_logged = std::string(fields[8]);
|
||||
msg.time_logged = std::string(fields[9]);
|
||||
|
||||
if (fields.size() > 10 && !fields[10].empty())
|
||||
msg.callsign = std::string(trim(fields[10]));
|
||||
if (fields.size() > 11) msg.altitude = parseInt(fields[11]);
|
||||
if (fields.size() > 12) msg.ground_speed = parseDouble(fields[12]);
|
||||
if (fields.size() > 13) msg.track = parseDouble(fields[13]);
|
||||
if (fields.size() > 14) msg.latitude = parseDouble(fields[14]);
|
||||
if (fields.size() > 15) msg.longitude = parseDouble(fields[15]);
|
||||
if (fields.size() > 16) msg.vertical_rate = parseInt(fields[16]);
|
||||
if (fields.size() > 17 && !fields[17].empty())
|
||||
msg.squawk = std::string(fields[17]);
|
||||
if (fields.size() > 18) msg.alert = parseBool(fields[18]);
|
||||
if (fields.size() > 19) msg.emergency = parseBool(fields[19]);
|
||||
if (fields.size() > 20) msg.spi = parseBool(fields[20]);
|
||||
if (fields.size() > 21) msg.is_on_ground = parseBool(fields[21]);
|
||||
|
||||
return msg;
|
||||
}
|
||||
};
|
||||
|
||||
// int main() {
|
||||
// std::vector<std::string> sample_stream = {
|
||||
// "MSG,1,111,11111,4CA7B6,111111,2026/11/14,13:10:00.123,2026/11/14,13:10:00.125,RYR1234 ,,,,,,,,,,,0",
|
||||
// "MSG,3,111,11111,4CA7B6,111111,2026/11/14,13:10:01.450,2026/11/14,13:10:01.455,,35000,,,33.6407,-84.4277,,,0,0,0,0",
|
||||
// "MSG,4,111,11111,4CA7B6,111111,2026/11/14,13:10:02.100,2026/11/14,13:10:02.105,,,452.5,275.3,,,-1280,,,,,0"
|
||||
// };
|
||||
//
|
||||
// for (const auto& line : sample_stream) {
|
||||
// if (auto parsed = SBS1Decoder::decode(line)) {
|
||||
// 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";
|
||||
// }
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
Reference in New Issue
Block a user