Skip to content

Commit

Permalink
chore: wip, send pos data to server
Browse files Browse the repository at this point in the history
  • Loading branch information
zodiac1214 committed Feb 10, 2025
1 parent 678c12e commit 4c33346
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
45 changes: 44 additions & 1 deletion plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "XPLMGraphics.h"
#include "XPLMMenus.h"
#include "XPLMPlugin.h"
#include "XPLMProcessing.h"
#include "XPLMUtilities.h"

// Include XPMP2 headers
Expand All @@ -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
Expand Down Expand Up @@ -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
//
Expand All @@ -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 /\:
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4c33346

Please sign in to comment.