From a809679b34939c31d0792fb26f98b2bd13f90039 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 25 Jul 2019 16:07:08 +0200 Subject: [PATCH] Ref T709, added Qt free base class for settings which can be used on XSwiftBus and swift side This has to be "Qt free" --- .../settings/xswiftbussettingsqtfree.cpp | 91 +++++++++++++++++++ .../settings/xswiftbussettingsqtfree.h | 77 ++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/blackmisc/simulation/settings/xswiftbussettingsqtfree.cpp create mode 100644 src/blackmisc/simulation/settings/xswiftbussettingsqtfree.h diff --git a/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.cpp b/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.cpp new file mode 100644 index 000000000..19279661a --- /dev/null +++ b/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.cpp @@ -0,0 +1,91 @@ +/* Copyright (C) 2019 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +#include "xswiftbussettingsqtfree.h" +#include "rapidjson/document.h" // rapidjson's DOM-style API +#include "rapidjson/prettywriter.h" // for stringify JSON +#include + +using namespace rapidjson; +using namespace BlackMisc::Simulation::Settings; + +//! @cond SWIFT_INTERNAL +constexpr char BlackMisc::Simulation::Settings::CXSwiftBusSettingsQtFree::JsonDBusServerAddress[]; +constexpr char BlackMisc::Simulation::Settings::CXSwiftBusSettingsQtFree::JsonDrawingLabels[]; +constexpr char BlackMisc::Simulation::Settings::CXSwiftBusSettingsQtFree::JsonMaxPlanes[]; +constexpr char BlackMisc::Simulation::Settings::CXSwiftBusSettingsQtFree::JsonMaxDrawDistance[]; +//! @endcond + +namespace BlackMisc +{ + namespace Simulation + { + namespace Settings + { + CXSwiftBusSettingsQtFree::CXSwiftBusSettingsQtFree() + {} + + bool CXSwiftBusSettingsQtFree::parseXSwiftBusString(const std::string &json) + { + if (json.empty()) { return false; } + const char *jsonCStr = json.c_str(); + + Document settingsDoc; + // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream(). + if (settingsDoc.Parse(jsonCStr).HasParseError()) { return false; } + + int c = 0; + if (settingsDoc.HasMember(CXSwiftBusSettingsQtFree::JsonDBusServerAddress) && settingsDoc[CXSwiftBusSettingsQtFree::JsonDBusServerAddress].IsString()) + { + m_dBusServerAddress = settingsDoc[CXSwiftBusSettingsQtFree::JsonDBusServerAddress].GetString(); c++; + } + if (settingsDoc.HasMember(CXSwiftBusSettingsQtFree::JsonDrawingLabels) && settingsDoc[CXSwiftBusSettingsQtFree::JsonDrawingLabels].IsBool()) + { + m_drawingLabels = settingsDoc[CXSwiftBusSettingsQtFree::JsonDrawingLabels].GetBool(); c++; + } + if (settingsDoc.HasMember(CXSwiftBusSettingsQtFree::JsonMaxPlanes) && settingsDoc[CXSwiftBusSettingsQtFree::JsonMaxPlanes].IsInt()) + { + m_maxPlanes = settingsDoc[CXSwiftBusSettingsQtFree::JsonMaxPlanes].GetInt(); c++; + } + if (settingsDoc.HasMember(CXSwiftBusSettingsQtFree::JsonMaxDrawDistance) && settingsDoc[CXSwiftBusSettingsQtFree::JsonMaxDrawDistance].IsDouble()) + { + m_maxDrawDistanceNM = settingsDoc[CXSwiftBusSettingsQtFree::JsonMaxDrawDistance].GetDouble(); c++; + } + return c == 4; + } + + std::string CXSwiftBusSettingsQtFree::toXSwiftBusJsonString() const + { + Document document; + document.SetObject(); + + // 1st version, we could also just concat the JSON string + // Explicit key/value + Document::AllocatorType &a = document.GetAllocator(); + // Value k1(JsonDBusServerAddress, a); + // Value v1(m_dBusServerAddress, a); + document.AddMember(JsonDBusServerAddress, StringRef(m_dBusServerAddress.c_str()), a); + document.AddMember(JsonDrawingLabels, m_drawingLabels, a); + document.AddMember(JsonMaxPlanes, m_maxPlanes, a); + document.AddMember(JsonMaxDrawDistance, m_maxDrawDistanceNM, a); + + // document[CXSwiftBusSettingsQtFree::JsonDBusServerAddress].SetString(StringRef(m_dBusServerAddress.c_str(), m_dBusServerAddress.size())); + // document[CXSwiftBusSettingsQtFree::JsonDrawingLabels].SetBool(m_drawingLabels); + // document[CXSwiftBusSettingsQtFree::JsonMaxPlanes].SetInt(m_maxPlanes); + // document[CXSwiftBusSettingsQtFree::JsonMaxDrawDistance].SetDouble(m_maxDrawDistanceNM); + + StringBuffer sb; + PrettyWriter writer(sb); + document.Accept(writer); // Accept() traverses the DOM and generates Handler events. + const std::string json = sb.GetString(); + return json; + } + } // ns + } // ns +} // ns + diff --git a/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.h b/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.h new file mode 100644 index 000000000..05882e771 --- /dev/null +++ b/src/blackmisc/simulation/settings/xswiftbussettingsqtfree.h @@ -0,0 +1,77 @@ +/* Copyright (C) 2019 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +#ifndef BLACKMISC_SIMULATION_SETTINGS_CXSWIFTBUSSETTINGSQTFREE_H +#define BLACKMISC_SIMULATION_SETTINGS_CXSWIFTBUSSETTINGSQTFREE_H + +#include + +namespace BlackMisc +{ + namespace Simulation + { + namespace Settings + { + /*! + * XSwiftBus/swift side settings class, JSON capable, shared among all services + * \details Used on swift and XSwiftBus side, MUST BE QT free + */ + class CXSwiftBusSettingsQtFree + { + public: + //! Constructor. + CXSwiftBusSettingsQtFree(); + + //! DBus server + const std::string &getDBusServerAddress() const { return m_dBusServerAddress; } + + //! Set DBus server + void setDBusServerAddress(const std::string &dBusServer) { m_dBusServerAddress = dBusServer; } + + //! Set whether the plugin draws type and callsign labels above aircraft + void setDrawingLabels(bool drawing) { m_drawingLabels = drawing; } + + //! Get whether the plugin draws type and callsign labels above aircraft + bool isDrawingLabels() const { return m_drawingLabels; } + + //! Set the maximum number of aircraft. + void setMaxPlanes(int planes) { m_maxPlanes = planes; } + + //! Get the maximum number of aircraft. + int getMaxPlanes() const { return m_maxPlanes; } + + //! Set the maximum distance at which to draw aircraft (nautical miles). + double getMaxDrawDistanceNM() const { return m_maxDrawDistanceNM; } + + //! Set the maximum distance at which to draw aircraft (nautical miles). + void setMaxDrawDistanceNM(double nauticalMiles) { m_maxDrawDistanceNM = nauticalMiles; } + + //! Load and parse config file + bool parseXSwiftBusString(const std::string &json); + + //! As JSON string + std::string toXSwiftBusJsonString() const; + + protected: + //! The JSON members @{ + static constexpr char JsonDBusServerAddress[] = "dbusserveradress"; + static constexpr char JsonDrawingLabels[] = "drawinglabels"; + static constexpr char JsonMaxPlanes[] = "maxplanes"; + static constexpr char JsonMaxDrawDistance[] = "maxDrawDistance"; + //! @} + + std::string m_dBusServerAddress { "tcp:host=127.0.0.1,port=45001" }; //!< DBus server + int m_maxPlanes = 100; //!< max. planes in XPlane + bool m_drawingLabels = true; //!< labels in XPlane + double m_maxDrawDistanceNM = 50.0; //!< distance in XPlane + }; + } // ns + } // ns +} // ns + +#endif