feat: Add custom X-Plane dataref for connection state

Fixes #304
This commit is contained in:
Lars Toenning
2024-11-17 14:44:34 +01:00
parent ae8807e543
commit a71b8e3491
9 changed files with 105 additions and 0 deletions

View File

@@ -215,6 +215,7 @@ namespace swift::simplugin::xplane
void CSimulatorXPlane::setFlightNetworkConnected(bool connected)
{
if (connected && !this->isShuttingDownOrDisconnected()) { m_serviceProxy->resetFrameTotals(); }
m_serviceProxy->setFlightNetworkConnected(connected);
CSimulatorPluginCommon::setFlightNetworkConnected(connected);
}

View File

@@ -358,6 +358,11 @@ namespace swift::simplugin::xplane
void CXSwiftBusServiceProxy::resetFrameTotals() { m_dbusInterface->callDBus(QLatin1String("resetFrameTotals")); }
void CXSwiftBusServiceProxy::setFlightNetworkConnected(bool connected)
{
m_dbusInterface->callDBus(QLatin1String("setFlightNetworkConnected"), connected);
}
double CXSwiftBusServiceProxy::getLatitudeDeg() const
{
return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg"));

View File

@@ -212,6 +212,9 @@ namespace swift::simplugin::xplane
//! \copydoc XSwiftBus::CService::resetFrameTotals
void resetFrameTotals();
//! \copydoc XSwiftBus::CService::setFlightNetworkConnected
void setFlightNetworkConnected(bool connected);
//! @{
//! \copydoc XSwiftBus::CService::getLatitudeDeg
double getLatitudeDeg() const;

View File

@@ -12,6 +12,7 @@ add_library(xswiftbus SHARED
command.h
config.cpp
config.h
custom_datarefs.h
datarefs.h
dbuscallbacks.h
dbusconnection.cpp

View File

@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: Copyright (C) swift Project Community / Contributors
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
#ifndef SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H
#define SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H
namespace XSwiftBus
{
//! Is swift connected to a network?
struct TSwiftNetworkConnected
{
//! Dataref name
static constexpr const char *name() { return "org/swift-project/xswiftbus/connected"; }
//! Can be written to?
static constexpr bool writable = false;
//! Dataref type
using type = int;
//! Not an array dataref
static constexpr bool is_array = false;
};
} // namespace XSwiftBus
#endif // SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H

View File

@@ -223,6 +223,58 @@ namespace XSwiftBus
XPLMDataRef m_ref;
};
/*!
* Class providing a custom variable + dataref
* \tparam DataRefTraits The trait class representing the dataref.
*/
template <class DataRefTraits>
class CustomDataRef
{
public:
//! Constructor
CustomDataRef()
{
if constexpr (std::is_same_v<typename DataRefTraits::type, int>)
{
m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Int, 0, read, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, this, NULL);
}
else { XPLMDebugString("Unsupported custom dataref type\n"); }
if (!m_ref)
{
XPLMDebugString("Missing dataref:");
XPLMDebugString(DataRefTraits::name());
XPLMDebugString("\n");
}
}
CustomDataRef(const CustomDataRef &) = delete;
CustomDataRef &operator=(const CustomDataRef &) = delete;
CustomDataRef(CustomDataRef &&other) = delete;
CustomDataRef &operator=(CustomDataRef &&other) = delete;
~CustomDataRef()
{
if (m_ref) { XPLMUnregisterDataAccessor(m_ref); }
}
static typename DataRefTraits::type read(void *refcon)
{
return reinterpret_cast<CustomDataRef *>(refcon)->get();
}
//! True if the dataref exists
bool isValid() const { return m_ref != nullptr; }
//! Set the value
void set(typename DataRefTraits::type val) { m_datarefVal = val; }
//! Get the value
typename DataRefTraits::type get() const { return m_datarefVal; }
XPLMDataRef m_ref;
typename DataRefTraits::type m_datarefVal;
};
template <>
inline void DataRefImpl::implSet<int>(int d)
{

View File

@@ -113,6 +113,9 @@ R"XML(<node>
</method>
<method name="resetFrameTotals">
</method>
<method name="setFlightNetworkConnected">
<arg type="b" direction="in"/>
</method>
<method name="getLatitudeDeg">
<arg type="d" direction="out"/>
</method>

View File

@@ -93,6 +93,7 @@ namespace XSwiftBus
{
this->updateMessageBoxFromSettings();
m_framePeriodSampler->show();
m_swiftNetworkConnected.set(0);
}
// Explicitly in cpp file to allow use of forward declaration
@@ -136,6 +137,8 @@ namespace XSwiftBus
}
}
void CService::setFlightNetworkConnected(bool connected) { m_swiftNetworkConnected.set(connected); }
void CService::addTextMessage(const std::string &text, double red, double green, double blue)
{
if (text.empty()) { return; }
@@ -509,6 +512,14 @@ namespace XSwiftBus
maybeSendEmptyDBusReply(wantsReply, sender, serial);
queueDBusCall([=]() { resetFrameTotals(); });
}
else if (message.getMethodName() == "setFlightNetworkConnected")
{
maybeSendEmptyDBusReply(wantsReply, sender, serial);
bool connected = false;
message.beginArgumentRead();
message.getArgument(connected);
queueDBusCall([=]() { setFlightNetworkConnected(connected); });
}
else if (message.getMethodName() == "getLatitudeDeg")
{
queueDBusCall([=]() { sendDBusReply(sender, serial, getLatitudeDeg()); });

View File

@@ -15,6 +15,7 @@
#include "datarefs.h"
#include "messages.h"
#include "terrainprobe.h"
#include "custom_datarefs.h"
#include <XPLM/XPLMNavigation.h>
#include <string>
#include <chrono>
@@ -119,6 +120,9 @@ namespace XSwiftBus
//! Reset the monitoring of total miles and minutes lost due to low frame rate.
void resetFrameTotals();
//! Set the current connection state
void setFlightNetworkConnected(bool connected);
//! Get aircraft latitude in degrees
double getLatitudeDeg() const { return m_latitude.get(); }
@@ -349,6 +353,7 @@ namespace XSwiftBus
DataRef<xplane::data::sim::graphics::scenery::async_scenery_load_in_progress> m_sceneryIsLoading;
int m_sceneryWasLoading = 0;
CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected;
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;