swift
simulatorflightgear.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_SIMPLUGIN_SIMULATOR_FLIGHTGEAR_H
7 #define SWIFT_SIMPLUGIN_SIMULATOR_FLIGHTGEAR_H
8 
9 #include <QDBusConnection>
10 #include <QHash>
11 #include <QList>
12 #include <QObject>
13 #include <QPair>
14 #include <QString>
15 #include <QStringList>
16 #include <QTimer>
17 
18 #include "flightgearmpaircraft.h"
19 
23 #include "misc/identifier.h"
24 #include "misc/pixmap.h"
25 #include "misc/pq/time.h"
26 #include "misc/pq/units.h"
27 #include "misc/sequence.h"
28 #include "misc/settingscache.h"
34 #include "misc/statusmessage.h"
37 
39 
40 namespace swift::misc
41 {
42  namespace aviation
43  {
44  class CAircraftParts;
45  class CAircraftSituation;
46  class CCallsign;
47  } // namespace aviation
48  namespace network
49  {
50  class CTextMessage;
51  }
52  namespace simulation
53  {
54  class CSimulatedAircraft;
55  class CSimulatorPluginInfo;
56  class IOwnAircraftProvider;
57  class IRemoteAircraftProvider;
58  } // namespace simulation
59 } // namespace swift::misc
60 
61 namespace swift::simplugin::flightgear
62 {
63  extern int FGSWIFTBUS_API_VERSION;
64  extern QList<int> incompatibleVersions;
65  class CFGSwiftBusServiceProxy;
66  class CFGSwiftBusTrafficProxy;
67 
70  {
73  double latitudeDeg = 0;
74  double longitudeDeg = 0;
75  double altitudeFt = 0;
76  double groundspeedKts = 0;
77  double pitchDeg = 0;
78  double rollDeg = 0;
79  double trueHeadingDeg = 0;
80  double velocityXMs = 0;
81  double velocityYMs = 0;
82  double velocityZMs = 0;
83  double pitchRateRadPerSec = 0;
84  double rollRateRadPerSec = 0;
85  double yawRateRadPerSec = 0;
86  bool onGroundAll = false;
87  int com1ActiveKhz = 122800;
88  int com1StandbyKhz = 122800;
89  int com2ActiveKhz = 122800;
90  int com2StandbyKhz = 122800;
91  int xpdrCode = 2000;
92  int xpdrMode = 0;
93  bool xpdrIdent = false;
94  bool beaconLightsOn = false;
95  bool landingLightsOn = false;
96  bool navLightsOn = false;
97  bool strobeLightsOn = false;
98  bool taxiLightsOn = false;
99  double flapsReployRatio = false;
100  double gearReployRatio = false;
102  double speedBrakeRatio = 0;
103  double pressureAltitudeFt = 0;
104  double groundElevation = 0;
105  double volumeCom1 = 1;
106  double volumeCom2 = 1;
107  };
108 
111  {
112  Q_OBJECT
113 
114  public:
118  swift::misc::simulation::IRemoteAircraftProvider *remoteAircraftProvider,
119  swift::misc::network::IClientProvider *clientProvider, QObject *parent = nullptr);
120 
122  ~CSimulatorFlightgear() override;
123 
126  bool connectTo() override;
127  bool disconnectFrom() override;
129  const swift::misc::CIdentifier &originator) override;
131  const swift::misc::CIdentifier &originator) override;
132  void displayStatusMessage(const swift::misc::CStatusMessage &message) const override;
133  void displayTextMessage(const swift::misc::network::CTextMessage &message) const override;
134  bool isPhysicallyRenderedAircraft(const swift::misc::aviation::CCallsign &callsign) const override;
136  void unload() override;
137  QString getStatisticsSimulatorSpecific() const override;
138  void resetAircraftStatistics() override;
140  getInterpolationMessages(const swift::misc::aviation::CCallsign &callsign) const override;
143  const swift::misc::aviation::CAircraftParts &parts) override;
145  const swift::misc::aviation::CCallsign &callsign) override;
147 
148  protected:
151  bool isConnected() const override;
152  bool physicallyAddRemoteAircraft(const swift::misc::simulation::CSimulatedAircraft &newRemoteAircraft) override;
154  int physicallyRemoveAllRemoteAircraft() override;
155  void clearAllRemoteAircraftData() override;
156  bool isPaused() const override { return false; }
158 
159  private slots:
161  void onDBusServiceUnregistered();
162 
163  private:
164  enum DBusMode
165  {
166  Session,
167  P2P
168  };
169 
170  using QDoubleList = QList<double>;
171 
172  void emitOwnAircraftModelChanged(const QString &path, const QString &filename, const QString &livery,
173  const QString &icao, const QString &modelString, const QString &name,
174  const QString &description);
175  void fastTimerTimeout();
176  void slowTimerTimeout();
177 
180  void updateRemoteAircraft();
181 
184  void requestRemoteAircraftDataFromFlightgear();
185  void requestRemoteAircraftDataFromFlightgear(const swift::misc::aviation::CCallsignSet &callsigns);
186  void triggerRequestRemoteAircraftDataFromFlightgear(const swift::misc::aviation::CCallsignSet &callsigns);
188 
191  void addNextPendingAircraft();
192  void triggerAddNextPendingAircraft();
194 
196  int detectTimeoutAdding();
197 
199  void triggerRemoveAircraft(const swift::misc::aviation::CCallsign &callsign, qint64 deferMs);
200 
202  QPair<qint64, qint64> minMaxTimestampsAddInProgress() const;
203 
205  bool canAddAircraft() const;
206 
209  void onRemoteAircraftAdded(const QString &callsign);
210  void onRemoteAircraftAddingFailed(const QString &callsign);
211  void updateRemoteAircraftFromSimulator(const QStringList &callsigns, const QDoubleList &latitudesDeg,
212  const QDoubleList &longitudesDeg, const QDoubleList &elevationsMeters,
213  const QDoubleList &verticalOffsetsMeters);
215 
217  void disconnectFromDBus();
218 
219  DBusMode m_dbusMode;
221  this
222  };
223  static constexpr qint64 TimeoutAdding = 10000;
224  QDBusConnection m_dBusConnection { "default" };
225  QDBusServiceWatcher *m_watcher { nullptr };
226  CFGSwiftBusServiceProxy *m_serviceProxy { nullptr };
227  CFGSwiftBusTrafficProxy *m_trafficProxy { nullptr };
228  QTimer m_fastTimer;
229  QTimer m_slowTimer;
230  QTimer m_pendingAddedTimer;
232  this
233  };
234  swift::misc::simulation::CSimulatedAircraftList m_pendingToBeAddedAircraft;
235  QHash<swift::misc::aviation::CCallsign, qint64> m_addingInProgressAircraft;
237  CFlightgearMPAircraftObjects m_flightgearAircraftObjects;
238  FlightgearData m_flightgearData;
239 
240  // statistics
241  qint64 m_statsAddMaxTimeMs = -1;
242  qint64 m_statsAddCurrentTimeMs = -1;
243 
244  bool m_simulatorPaused = false;
245 
247  void resetFlightgearData() { m_flightgearData = {}; }
248  };
249 
252  {
253  Q_OBJECT
254 
255  public:
258 
259  protected:
261  void startImpl() override;
262 
264  void stopImpl() override;
265 
267  void checkImpl() override;
268 
269  private:
270  void checkConnection();
271  void checkConnectionViaSessionBus();
272  void checkConnectionViaPeer(const QString &address);
273  void checkConnectionCommon();
274 
275  void serviceRegistered(const QString &serviceName);
276  void fgSwiftBusServerSettingChanged();
277 
278  QTimer m_timer { this };
279  QDBusConnection m_conn { "default" };
281  this, &CSimulatorFlightgearListener::fgSwiftBusServerSettingChanged
282  };
283  };
284 
287  {
288  Q_OBJECT
289  Q_PLUGIN_METADATA(IID "org.swift-project.swift_core.simulatorinterface" FILE "simulatorflightgear.json")
291 
292  public:
296  swift::misc::simulation::IRemoteAircraftProvider *remoteAircraftProvider,
297  swift::misc::network::IClientProvider *clientProvider) override;
298 
302  {
303  return new CSimulatorFlightgearListener(info);
304  }
305  };
306 } // namespace swift::simplugin::flightgear
307 
308 #endif // SWIFT_SIMPLUGIN_SIMULATOR_FLIGHTGEAR_H
Factory pattern class to create instances of ISimulator.
Definition: simulator.h:691
Interface to a simulator.
Definition: simulator.h:59
Interface to a simulator listener.
Definition: simulator.h:630
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition: identifier.h:29
Streamable status message, e.g.
Status messages, e.g. from Core -> GUI.
Value object encapsulating information of aircraft's parts.
Definition: aircraftparts.h:26
Value object encapsulating information of an aircraft's situation.
Value object encapsulating information of a callsign.
Definition: callsign.h:30
Value object for a set of callsigns.
Definition: callsignset.h:26
Value object for SELCAL.
Definition: selcal.h:31
Geodetic coordinate, a position in 3D space relative to the reference geoid.
Value object encapsulating information of a text message.
Definition: textmessage.h:31
Direct in memory access to client (network client) data.
Comprehensive information of an aircraft.
Value object encapsulating a list of aircraft.
Direct threadsafe in memory access to own aircraft.
Direct thread safe in memory access to remote aircraft.
Common base class for simulator plugins.
Factory for creating CSimulatorFlightgear instance.
swift::core::ISimulatorListener * createListener(const swift::misc::simulation::CSimulatorPluginInfo &info)
Simulator listener instance.
swift::core::ISimulator * create(const swift::misc::simulation::CSimulatorPluginInfo &info, swift::misc::simulation::IOwnAircraftProvider *ownAircraftProvider, swift::misc::simulation::IRemoteAircraftProvider *remoteAircraftProvider, swift::misc::network::IClientProvider *clientProvider)
Create a new instance of a driver.
swift::misc::CStatusMessageList getInterpolationMessages(const swift::misc::aviation::CCallsign &callsign) const
Interpolation messages for callsign.
bool updateOwnSimulatorSelcal(const swift::misc::aviation::CSelcal &selcal, const swift::misc::CIdentifier &originator)
Update own aircraft cockpit (usually from context)
void displayTextMessage(const swift::misc::network::CTextMessage &message) const
Display a text message.
bool physicallyAddRemoteAircraft(const swift::misc::simulation::CSimulatedAircraft &newRemoteAircraft)
Add new remote aircraft physically to the simulator.
bool updateOwnSimulatorCockpit(const swift::misc::simulation::CSimulatedAircraft &aircraft, const swift::misc::CIdentifier &originator)
Update own aircraft cockpit (usually from context)
int physicallyRemoveAllRemoteAircraft()
Remove all remote aircraft and their data via ISimulator::clearAllRemoteAircraftData.
bool requestElevation(const swift::misc::geo::ICoordinateGeodetic &reference, const swift::misc::aviation::CCallsign &callsign)
Request elevation, there is no guarantee the requested elevation will be available in the provider.
bool physicallyRemoveRemoteAircraft(const swift::misc::aviation::CCallsign &callsign)
Remove remote aircraft from simulator.
bool isConnected() const
Are we connected to the simulator?
CSimulatorFlightgear(const swift::misc::simulation::CSimulatorPluginInfo &info, swift::misc::simulation::IOwnAircraftProvider *ownAircraftProvider, swift::misc::simulation::IRemoteAircraftProvider *remoteAircraftProvider, swift::misc::network::IClientProvider *clientProvider, QObject *parent=nullptr)
Constructor.
swift::misc::aviation::CCallsignSet physicallyRenderedAircraft() const
Physically rendered (displayed in simulator) This shall only return aircraft handled in the simulator...
void displayStatusMessage(const swift::misc::CStatusMessage &message) const
Display a status message in the simulator.
QString getStatisticsSimulatorSpecific() const
Allows to print out simulator specific statistics.
bool isPhysicallyRenderedAircraft(const swift::misc::aviation::CCallsign &callsign) const
Is the aircraft rendered (displayed in simulator)? This shall only return true if the aircraft is rea...
void clearAllRemoteAircraftData()
Clear all aircraft related data, but do not physically remove the aircraft.
bool testSendSituationAndParts(const swift::misc::aviation::CCallsign &callsign, const swift::misc::aviation::CAircraftSituation &situation, const swift::misc::aviation::CAircraftParts &parts)
Send situation/parts for testing.
Listener waits for fgswiftbus service to show up.
void stopImpl()
Plugin specific implementation to stop listener.
CSimulatorFlightgearListener(const swift::misc::simulation::CSimulatorPluginInfo &info)
Constructor.
void startImpl()
Plugin specific implementation to start listener.
void checkImpl()
Plugin specific implementation to check.
Free functions in swift::misc.
Q_INTERFACES(...)
Q_OBJECTQ_OBJECT
QObject * parent() const const
double pressureAltitudeFt
Pressure altitude [inhg].
QList< double > enginesN1Percentage
N1 per engine [%].
double rollRateRadPerSec
Roll angular velocity [rad/s].
double pitchRateRadPerSec
Pitch angular velocity [rad/s].
double gearReployRatio
Gear deployment ratio [%].
int xpdrMode
Transponder mode (off=0,stdby=1-2, >2 on)
double yawRateRadPerSec
Yaw angular velocity [rad/s].
double flapsReployRatio
Flaps deployment ratio [%].