From 4c33346b81f597e02d5547d33a88eb3d741a59d1 Mon Sep 17 00:00:00 2001 From: Zodiac Date: Sun, 9 Feb 2025 20:45:07 -0500 Subject: [PATCH] chore: wip, send pos data to server --- plugin.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- util.cpp | 4 +++- util.h | 2 ++ websocket.cpp | 5 ++++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/plugin.cpp b/plugin.cpp index ef669ca..100a3f2 100644 --- a/plugin.cpp +++ b/plugin.cpp @@ -9,6 +9,7 @@ #include "XPLMGraphics.h" #include "XPLMMenus.h" #include "XPLMPlugin.h" +#include "XPLMProcessing.h" #include "XPLMUtilities.h" // Include XPMP2 headers @@ -26,6 +27,12 @@ #error This plugin requires version 300 of the SDK #endif +#define POS_LOOP_INTERVAL 0.04f // 1/25 + +static XPLMDataRef gPlaneLat; +static XPLMDataRef gPlaneLon; +static XPLMDataRef gPlaneEl; + /// Initial type / airline / livery to be used to create our 3 planes /// @see https://www.icao.int/publications/DOC8643/Pages/Search.aspx for ICAO /// aircraft types @@ -126,6 +133,30 @@ void CBMenu(void * /*inMenuRef*/, void *inItemRef) { MenuUpdateCheckmarks(); } +// Flightloop, targetting 25 fps +float PlanePosReportLoopCallback(float inElapsedSinceLastCall, + float inElapsedTimeSinceLastFlightLoop, + int inCounter, void *inRefcon) { +// if (inElapsedTimeSinceLastFlightLoop > 0.05f) { +// LogMsg("FPS is too low: %f", inElapsedSinceLastCall); +// } + float lat = XPLMGetDataf(gPlaneLat); + float lon = XPLMGetDataf(gPlaneLon); + float el = XPLMGetDataf(gPlaneEl); + + // Create a comma-separated string from the values. + std::string message = std::to_string(lat) + "," + std::to_string(lon) + + "," + std::to_string(el); + // Send the binary message. + try { + WebSocketClient::getInstance().send(message); + } catch (const websocketpp::lib::error_code &e) { + LogMsg("Send error: %s", e.message().c_str()); + } + + return POS_LOOP_INTERVAL; +} + // // MARK: Standard Plugin Callbacks // @@ -149,10 +180,19 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) { CBMenu, NULL); XPLMAppendMenuItem(hMenu, "Toggle AI control", (void *)MENU_AI, 0); MenuUpdateCheckmarks(); + + /* Find the data refs we want to record. */ + gPlaneLat = XPLMFindDataRef("sim/flightmodel/position/latitude"); + gPlaneLon = XPLMFindDataRef("sim/flightmodel/position/longitude"); + gPlaneEl = XPLMFindDataRef("sim/flightmodel/position/elevation"); + return 1; } -PLUGIN_API void XPluginStop(void) {} +PLUGIN_API void XPluginStop(void) { + // Stop pos reporting flight loop + XPLMUnregisterFlightLoopCallback(PlanePosReportLoopCallback, NULL); +} PLUGIN_API int XPluginEnable(void) { // The path separation character, one out of /\: @@ -224,6 +264,9 @@ PLUGIN_API int XPluginEnable(void) { LogMsg("Failed to get Token: check %s", szPath); } + // Resister Pos report flight loop + XPLMRegisterFlightLoopCallback(PlanePosReportLoopCallback, -1.0f, NULL); + LogMsg("XPMP2-Sample: Enabled"); return 1; } diff --git a/util.cpp b/util.cpp index 40e7bed..9e09bab 100644 --- a/util.cpp +++ b/util.cpp @@ -12,14 +12,16 @@ bool gbFreeze = false; /// Log a message to X-Plane's Log.txt with sprintf-style parameters void LogMsg(const char *szMsg, ...) { char buf[512]; + char finalBuf[512]; va_list args; // Write all the variable parameters va_start(args, szMsg); std::vsnprintf(buf, sizeof(buf) - 2, szMsg, args); va_end(args); std::strcat(buf, "\n"); + std::snprintf(finalBuf, sizeof(finalBuf), "[%s] %s\n", PLUGIN_NAME, buf); // write to log (flushed immediately -> expensive!) - XPLMDebugString(buf); + XPLMDebugString(finalBuf); } /// This is a callback the XPMP2 calls regularly to learn about configuration diff --git a/util.h b/util.h index 1c4d32a..42a26b6 100644 --- a/util.h +++ b/util.h @@ -27,6 +27,8 @@ #include "XPMPAircraft.h" #include "XPMPMultiplayer.h" +#define PLUGIN_NAME "fly-with-me" + /// Freeze all movements at the moment? extern bool gbFreeze; diff --git a/websocket.cpp b/websocket.cpp index 01e95ea..8aa1844 100644 --- a/websocket.cpp +++ b/websocket.cpp @@ -42,6 +42,8 @@ WebSocketClient::WebSocketClient() { std::bind(&WebSocketClient::on_close, this, std::placeholders::_1)); m_client.set_fail_handler( std::bind(&WebSocketClient::on_fail, this, std::placeholders::_1)); + + m_client.clear_access_channels(websocketpp::log::alevel::all); // Start the ASIO io_service loop in a separate thread. // Since start_perpetual() is used, the run() call will continue running @@ -117,7 +119,8 @@ void WebSocketClient::on_open(websocketpp::connection_hdl hdl) { // Event handler called when a message is received. void WebSocketClient::on_message(websocketpp::connection_hdl hdl, ws_client::message_ptr msg) { - LogMsg("Received message: %s", msg->get_payload().c_str()); + return; +// LogMsg("Received message: %s", msg->get_payload().c_str()); } // Event handler called when the connection is closed.