diff --git a/TODO.md b/TODO.md index 508fd9b..8501880 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,3 @@ - Reverse engineer FSUIPC .NET onRunway Check - Update OSXCross Docker image to SDK 11 - Implement ARM64 arch for Plugin -- Refactor SimDatabase to be a class - - Requires new Airport class \ No newline at end of file diff --git a/file/config.cpp b/file/config.cpp index 686300f..13992e0 100644 --- a/file/config.cpp +++ b/file/config.cpp @@ -20,11 +20,10 @@ namespace file std::ifstream in(XPLANE_PLUGIN_DIRECTORY CONFIG); std::string line; while (std::getline(in, line)) { - std::vector fields = - germanairlinesva::util::split(line, '='); + std::vector fields = util::split(line, '='); if (fields.size() >= 2) { - germanairlinesva::util::trim(fields[0]); - germanairlinesva::util::trim(fields[1]); + util::trim(fields[0]); + util::trim(fields[1]); if (fields[0] == "scenery") { this->scenery = fields[1]; } else if (fields[0] == "user") { diff --git a/file/gate.cpp b/file/gate.cpp index 34fa46a..9f1d6b4 100644 --- a/file/gate.cpp +++ b/file/gate.cpp @@ -7,7 +7,7 @@ namespace file namespace simdata { Gate::Gate(std::string designator, - struct germanairlinesva::geodata::point center, + struct geodata::point center, std::uint8_t radius) { this->designator = designator; @@ -22,10 +22,9 @@ namespace file writeradius)>(out, this->radius); } - bool Gate::contains(germanairlinesva::geodata::point coordinates) const + bool Gate::contains(geodata::point coordinates) const { - return germanairlinesva::geodata::distanceEarthP(this->center, - coordinates); + return geodata::distanceEarthP(this->center, coordinates); } } // namespace simdata } // namespace file diff --git a/file/include/recording/recordingEntry.h b/file/include/recording/recordingEntry.h index 2fb1f5e..8befabb 100644 --- a/file/include/recording/recordingEntry.h +++ b/file/include/recording/recordingEntry.h @@ -26,14 +26,16 @@ namespace file std::uint32_t time; std::uint16_t altitude = 0; std::uint16_t groundSpeed = 0; - struct germanairlinesva::geodata::point coordinates{NAN, NAN}; + struct geodata::point coordinates { + NAN, NAN + }; public: RecordingEntry() = default; RecordingEntry(std::uint32_t time, std::uint16_t altitude, std::uint16_t groundSpeed, - struct germanairlinesva::geodata::point coordinates); + struct geodata::point coordinates); void toFile(std::ofstream &out) const; diff --git a/file/include/simdata/gate.h b/file/include/simdata/gate.h index a839535..65af654 100644 --- a/file/include/simdata/gate.h +++ b/file/include/simdata/gate.h @@ -34,16 +34,16 @@ namespace file { private: std::string designator; - struct germanairlinesva::geodata::point center; + struct geodata::point center; std::uint8_t radius; public: Gate(std::string designator, - struct germanairlinesva::geodata::point center, + struct geodata::point center, std::uint8_t radius); void toFile(std::ofstream &out) const; - bool contains(germanairlinesva::geodata::point coordinates) const; + bool contains(geodata::point coordinates) const; inline const std::string to_string() const { diff --git a/file/include/simdata/runway.h b/file/include/simdata/runway.h index b617d51..5423be7 100644 --- a/file/include/simdata/runway.h +++ b/file/include/simdata/runway.h @@ -34,7 +34,7 @@ namespace file { private: std::string designator; - struct germanairlinesva::geodata::box bounds; + struct geodata::box bounds; std::uint8_t width; std::uint16_t length; std::uint16_t trueHeading; @@ -50,7 +50,7 @@ namespace file double width); // From database Runway(std::string designator, - struct germanairlinesva::geodata::box bounds, + struct geodata::box bounds, std::uint8_t width, std::uint16_t length, std::uint16_t trueHeading); diff --git a/file/include/simdata/simDatabase.h b/file/include/simdata/simDatabase.h index 09a52e7..c471810 100644 --- a/file/include/simdata/simDatabase.h +++ b/file/include/simdata/simDatabase.h @@ -42,10 +42,7 @@ namespace file class SimDatabase { private: - std::map< - std::string, - std::pair, - std::vector>> + std::map, std::vector>> airports; void fromFile(); @@ -64,11 +61,11 @@ namespace file const std::vector> operator[](std::string key) { - try { - return this->airports.at(key); - } catch (std::out_of_range &ex) { - return std::pair, - const std::vector>(); + auto it = this->airports.find(key); + if (it != this->airports.end()) { + return this->airports[key]; + } else { + return std::pair, std::vector>(); } } }; diff --git a/file/include/simdata/simdataXP.h b/file/include/simdata/simdataXP.h index 4690f24..8d7c1d2 100644 --- a/file/include/simdata/simdataXP.h +++ b/file/include/simdata/simdataXP.h @@ -20,15 +20,13 @@ namespace file const std::string defaultFile, const std::string sceneryPack, const std::string logFile, - std::map, std::vector>> + std::map, std::vector>> &airports); void makeAirport( const std::string &kind, std::ifstream &infile, - std::map, std::vector>> + std::map, std::vector>> &airports, std::ofstream &logfile); void makeGate15(std::vector &gates, diff --git a/file/logbook.cpp b/file/logbook.cpp index f9600dd..ed4cd08 100644 --- a/file/logbook.cpp +++ b/file/logbook.cpp @@ -8,7 +8,7 @@ namespace file { Logbook::Logbook() { - if (germanairlinesva::util::fileExists(XPLANE_PLUGIN_DIRECTORY LOGBOOK)) { + if (util::fileExists(XPLANE_PLUGIN_DIRECTORY LOGBOOK)) { this->fromFile(); } } diff --git a/file/recordingEntry.cpp b/file/recordingEntry.cpp index efb4ae4..9c5aefd 100644 --- a/file/recordingEntry.cpp +++ b/file/recordingEntry.cpp @@ -7,11 +7,10 @@ namespace file { namespace recording { - RecordingEntry::RecordingEntry( - std::uint32_t time, - std::uint16_t altitude, - std::uint16_t groundSpeed, - struct germanairlinesva::geodata::point coordinates) + RecordingEntry::RecordingEntry(std::uint32_t time, + std::uint16_t altitude, + std::uint16_t groundSpeed, + struct geodata::point coordinates) { this->time = time; this->altitude = altitude; diff --git a/file/runway.cpp b/file/runway.cpp index 4c82f84..5fcbc7c 100644 --- a/file/runway.cpp +++ b/file/runway.cpp @@ -15,24 +15,23 @@ namespace file { this->designator = designator; this->width = width; - this->length = germanairlinesva::geodata::distanceEarthD(latitudeStart, - longitudeStart, - latitudeEnd, - longitudeEnd); - this->trueHeading = (std::uint16_t)std::round( - germanairlinesva::geodata::bearingDD(latitudeStart, - longitudeStart, - latitudeEnd, - longitudeEnd)); - this->bounds = germanairlinesva::geodata::calculateBoxDD( - {latitudeStart, longitudeStart}, - {latitudeEnd, longitudeEnd}, - this->trueHeading, - this->width); + this->length = geodata::distanceEarthD(latitudeStart, + longitudeStart, + latitudeEnd, + longitudeEnd); + this->trueHeading = + (std::uint16_t)std::round(geodata::bearingDD(latitudeStart, + longitudeStart, + latitudeEnd, + longitudeEnd)); + this->bounds = geodata::calculateBoxDD({latitudeStart, longitudeStart}, + {latitudeEnd, longitudeEnd}, + this->trueHeading, + this->width); } Runway::Runway(std::string designator, - germanairlinesva::geodata::box bounds, + geodata::box bounds, std::uint8_t width, std::uint16_t length, std::uint16_t trueHeading) diff --git a/file/simDatabase.cpp b/file/simDatabase.cpp index e87b781..cf471cc 100644 --- a/file/simDatabase.cpp +++ b/file/simDatabase.cpp @@ -56,8 +56,7 @@ namespace file std::uint16_t numGates = read(in); for (int j = 0; j < numGates; j++) { std::string designator = readString(in); - struct germanairlinesva::geodata::point center = - read(in); + struct geodata::point center = read(in); std::uint8_t radius = read(in); this->airports[icao].first.emplace_back(designator, center, radius); @@ -67,8 +66,7 @@ namespace file for (int j = 0; j < numRunways; j++) { std::string designator = readString(in); // Bounds - struct germanairlinesva::geodata::box bounds = - read(in); + struct geodata::box bounds = read(in); std::uint8_t width = read(in); std::uint16_t length = read(in); std::uint16_t trueHeading = read(in); diff --git a/file/simdataXP.cpp b/file/simdataXP.cpp index dfdd8d4..46454d3 100644 --- a/file/simdataXP.cpp +++ b/file/simdataXP.cpp @@ -39,9 +39,8 @@ namespace file std::vector packs; while (std::getline(custom, line)) { if ((pos = line.find("SCENERY_PACK")) != std::string::npos) { - std::string path = - germanairlinesva::util::rtrim_copy(line.substr(pos + 13)) + - "Earth nav data/apt.dat"; + std::string path = util::rtrim_copy(line.substr(pos + 13)) + + "Earth nav data/apt.dat"; packs.emplace_back(path); } } @@ -84,11 +83,10 @@ namespace file int validCount = 0; while (std::getline(infile, line)) { - std::vector fields = - germanairlinesva::util::split(line, ' '); - fields = germanairlinesva::util::select_T( - fields, - [](const std::string &s) { return s.length() > 0; }); + std::vector fields = util::split(line, ' '); + fields = util::select_T(fields, [](const std::string &s) { + return s.length() > 0; + }); if (fields.empty()) continue; @@ -155,7 +153,7 @@ namespace file } gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); - struct germanairlinesva::geodata::point pt { + struct geodata::point pt { std::stod(fields[1]), std::stod(fields[2]) }; gates.emplace_back(gateName, pt, 40); @@ -188,7 +186,7 @@ namespace file gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); - struct germanairlinesva::geodata::point pt { + struct geodata::point pt { std::stod(fields[1]), std::stod(fields[2]) }; gates.emplace_back(gateName, pt, 40); diff --git a/websocket/include/types.h b/websocket/include/types.h index b8bd8b4..8ce775f 100644 --- a/websocket/include/types.h +++ b/websocket/include/types.h @@ -3,8 +3,9 @@ #include - -namespace germanairlinesva_websocket +namespace germanairlinesva +{ +namespace websocket { #pragma pack(push) /* push current alignment to stack */ #pragma pack(1) /* set alignment to 1 byte boundary */ @@ -44,6 +45,7 @@ namespace germanairlinesva_websocket }; #pragma pack(pop) /* restore original alignment from stack */ -} // namespace germanairlinesva_websocket +} // namespace websocket +} // namespace germanairlinesva #endif diff --git a/websocket/include/websocket.h b/websocket/include/websocket.h index 781afcc..2b26cd2 100644 --- a/websocket/include/websocket.h +++ b/websocket/include/websocket.h @@ -18,7 +18,9 @@ #include #include -namespace germanairlinesva_websocket +namespace germanairlinesva +{ +namespace websocket { class Websocket { @@ -38,6 +40,7 @@ namespace germanairlinesva_websocket void onClientMessageCallback(const ix::WebSocketMessagePtr &msg); void sendData(data &d); }; -} // namespace germanairlinesva_websocket +} // namespace websocket +} // namespace germanairlinesva #endif \ No newline at end of file diff --git a/websocket/websocket.cpp b/websocket/websocket.cpp index c4ad940..f62a4c2 100644 --- a/websocket/websocket.cpp +++ b/websocket/websocket.cpp @@ -1,113 +1,114 @@ #include "include/websocket.h" -namespace germanairlinesva_websocket +namespace germanairlinesva { -Websocket::Websocket(std::string host, - std::string user, - std::function toLog) - : host(host), user(user), toLog(std::move(toLog)) +namespace websocket { + Websocket::Websocket(std::string host, + std::string user, + std::function toLog) + : host(host), user(user), toLog(std::move(toLog)) + { #ifdef IBM - // Required on Windows - ix::initNetSystem(); + // Required on Windows + ix::initNetSystem(); #endif - this->webSocket = new ix::WebSocket(); - this->webSocket->enableAutomaticReconnection(); - this->webSocket->setUrl(host); - this->webSocket->setOnMessageCallback( - [this](const ix::WebSocketMessagePtr &msg) { - this->onClientMessageCallback(msg); - }); - this->webSocket->start(); -} + this->webSocket = new ix::WebSocket(); + this->webSocket->enableAutomaticReconnection(); + this->webSocket->setUrl(host); + this->webSocket->setOnMessageCallback( + [this](const ix::WebSocketMessagePtr &msg) { + this->onClientMessageCallback(msg); + }); + this->webSocket->start(); + } -Websocket::~Websocket() -{ - this->webSocket->stop(); - this->toLog("WebSocket stopped"); + Websocket::~Websocket() + { + this->webSocket->stop(); + this->toLog("WebSocket stopped"); #ifdef IBM - // Required on Windows - ix::uninitNetSystem(); + // Required on Windows + ix::uninitNetSystem(); #endif -} + } -void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg) -{ - if (msg->type == ix::WebSocketMessageType::Open) { - std::stringstream debug_msg; - - debug_msg << std::endl << "New connection" << std::endl; - debug_msg << "Uri: " << msg->openInfo.uri << std::endl; - debug_msg << "Headers:" << std::endl; - for (const auto &it : msg->openInfo.headers) { - debug_msg << it.first << ": " << it.second << std::endl; - } - - this->toLog(debug_msg.str()); - - this->webSocket->send("MASTER:" + user); - this->toLog("Connecting as " + user); - } else if (msg->type == ix::WebSocketMessageType::Close) { - if (msg->closeInfo.reason.compare("DUPLICATE") == 0) { - this->webSocket->disableAutomaticReconnection(); - - this->toLog("Disconnected due to beeing a duplicate simualtor"); - } else { + void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg) + { + if (msg->type == ix::WebSocketMessageType::Open) { std::stringstream debug_msg; - debug_msg << std::endl << "Connection closed" << std::endl; - debug_msg << "Code: " << msg->closeInfo.code << std::endl; - debug_msg << "Reason: " << msg->closeInfo.reason << std::endl; - debug_msg << "Remote: " << msg->closeInfo.remote << std::endl; + debug_msg << std::endl << "New connection" << std::endl; + debug_msg << "Uri: " << msg->openInfo.uri << std::endl; + debug_msg << "Headers:" << std::endl; + for (const auto &it : msg->openInfo.headers) { + debug_msg << it.first << ": " << it.second << std::endl; + } this->toLog(debug_msg.str()); - } - } else if (msg->type == ix::WebSocketMessageType::Error) { - std::stringstream debug_msg; - debug_msg << std::endl << "Connection error" << std::endl; - debug_msg << "Decompression: " << msg->errorInfo.decompressionError - << std::endl; - debug_msg << "HTTP status: " << msg->errorInfo.http_status << std::endl; - debug_msg << "Reason: " << msg->errorInfo.reason << std::endl; - debug_msg << "Retries: " << msg->errorInfo.retries << std::endl; - debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl; + this->webSocket->send("MASTER:" + user); + this->toLog("Connecting as " + user); + } else if (msg->type == ix::WebSocketMessageType::Close) { + if (msg->closeInfo.reason.compare("DUPLICATE") == 0) { + this->webSocket->disableAutomaticReconnection(); - this->toLog(debug_msg.str()); - } else if (msg->type == ix::WebSocketMessageType::Message) { - if (!msg->str.empty()) { - this->toLog(msg->str); - } - } -} + this->toLog("Disconnected due to beeing a duplicate simualtor"); + } else { + std::stringstream debug_msg; -void Websocket::sendData(data &d) -{ - if (strcmp(d.path, this->lastPath) != 0) { - strcpy(this->lastPath, d.path); - if (germanairlinesva::util::generateMD5(d.path, - this->lastHash, - this->toLog)) { - strcpy(this->lastHash, "NOT SET"); + debug_msg << std::endl << "Connection closed" << std::endl; + debug_msg << "Code: " << msg->closeInfo.code << std::endl; + debug_msg << "Reason: " << msg->closeInfo.reason << std::endl; + debug_msg << "Remote: " << msg->closeInfo.remote << std::endl; + + this->toLog(debug_msg.str()); + } + } else if (msg->type == ix::WebSocketMessageType::Error) { + std::stringstream debug_msg; + + debug_msg << std::endl << "Connection error" << std::endl; + debug_msg << "Decompression: " << msg->errorInfo.decompressionError + << std::endl; + debug_msg << "HTTP status: " << msg->errorInfo.http_status << std::endl; + debug_msg << "Reason: " << msg->errorInfo.reason << std::endl; + debug_msg << "Retries: " << msg->errorInfo.retries << std::endl; + debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl; + + this->toLog(debug_msg.str()); + } else if (msg->type == ix::WebSocketMessageType::Message) { + if (!msg->str.empty()) { + this->toLog(msg->str); + } } } - nlohmann::json json = { - {"altitude", d.alt}, - {"vs", d.vs}, - {"ias", d.ias}, - {"magHdg", d.magHeading}, - {"truHdg", d.truHdg}, - {"totFuel", d.totFuelKg}, - {"fuelFlow", d.ff}, - {"hash", this->lastHash}, - }; + void Websocket::sendData(data &d) + { + if (strcmp(d.path, this->lastPath) != 0) { + strcpy(this->lastPath, d.path); + if (util::generateMD5(d.path, this->lastHash, this->toLog)) { + strcpy(this->lastHash, "NOT SET"); + } + } - if (this->webSocket != nullptr) { - std::ostringstream msg; - msg << "SEND:" << user << ":" << json.dump(); - this->webSocket->send(msg.str(), false); + nlohmann::json json = { + {"altitude", d.alt}, + {"vs", d.vs}, + {"ias", d.ias}, + {"magHdg", d.magHeading}, + {"truHdg", d.truHdg}, + {"totFuel", d.totFuelKg}, + {"fuelFlow", d.ff}, + {"hash", this->lastHash}, + }; + + if (this->webSocket != nullptr) { + std::ostringstream msg; + msg << "SEND:" << user << ":" << json.dump(); + this->webSocket->send(msg.str(), false); + } } -} -} // namespace germanairlinesva_websocket \ No newline at end of file +} // namespace websocket +} // namespace germanairlinesva \ No newline at end of file diff --git a/xplugin/main.cpp b/xplugin/main.cpp index 0a4e58c..aa0454a 100644 --- a/xplugin/main.cpp +++ b/xplugin/main.cpp @@ -12,7 +12,7 @@ std::atomic wantsExit; std::unique_ptr configuration; std::unique_ptr database; -std::unique_ptr connector; +std::unique_ptr connector; int xplaneVersion; /* Datarefs */ @@ -43,7 +43,7 @@ XPLMDataRef pitch; XPLMDataRef roll; XPLMDataRef quaternion; -struct germanairlinesva_websocket::data toSend; +struct germanairlinesva::websocket::data toSend; germanairlinesva::file::recording::Recording p; /* @@ -114,7 +114,7 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) configuration = std::make_unique(); toLog("Config loaded"); - connector = std::make_unique( + connector = std::make_unique( "wss://ws.hofmannnet.myhome-server.de:8000", configuration->getUser(), toLog); @@ -128,18 +128,6 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) hash, configuration, toLog); - - toLog("Readback test of sim database using EDDF"); - auto ap = (*database)["EDDF"]; - for (const auto &it : ap.first) { - toLog(" " + it.to_string()); - } - for (const auto &it : ap.second) { - toLog(" " + it.to_string()); - } - toLog("Readback test of sim database using XXXX"); - auto ap2 = (*database)["XXXX"]; - ap2.first.size() == 0 ? toLog(" SUCCESS") : toLog(" ERROR"); } // Thread for sending data to websocket @@ -219,7 +207,7 @@ float flightLoop(float elapsedMe, float elapsedSim, int counter, void *refcon) { const std::lock_guard lock(mutex); - std::memset(&toSend, 0, sizeof(germanairlinesva_websocket::data)); + std::memset(&toSend, 0, sizeof(germanairlinesva::websocket::data)); toSend.pause = XPLMGetDatai(pauseIndicator); toSend.pBrake = XPLMGetDataf(parkingBrake); @@ -260,11 +248,11 @@ void serverWorker() germanairlinesva::util::setThreadName("GAServerWorker"); while (!wantsExit) { - struct germanairlinesva_websocket::data copy; + struct germanairlinesva::websocket::data copy; { const std::lock_guard lock(mutex); - std::memcpy(©, &toSend, sizeof(germanairlinesva_websocket::data)); + std::memcpy(©, &toSend, sizeof(germanairlinesva::websocket::data)); } connector->sendData(copy); @@ -283,11 +271,11 @@ void recordingWorker() std::uint32_t segment = 0; while (!wantsExit) { - germanairlinesva_websocket::data copy; + germanairlinesva::websocket::data copy; { const std::lock_guard lock(mutex); - std::memcpy(©, &toSend, sizeof(germanairlinesva_websocket::data)); + std::memcpy(©, &toSend, sizeof(germanairlinesva::websocket::data)); } germanairlinesva::file::recording::RecordingEntry currentPath(