Enroute Flight Navigation
A navigation app for VFR pilots
TrafficDataProvider.h
1/***************************************************************************
2 * Copyright (C) 2021-2024 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QNetworkDatagram>
24#include <QStandardPaths>
25#include <QUdpSocket>
26
27#include "GlobalObject.h"
28#include "positioning/PositionInfoSource_Abstract.h"
29#include "traffic/ConnectionInfo.h"
30#include "traffic/TrafficDataSource_Abstract.h"
31
32namespace Traffic {
33
54 Q_OBJECT
55 QML_ELEMENT
56 QML_SINGLETON
57
58public:
63 explicit TrafficDataProvider(QObject *parent = nullptr);
64
65 // No default constructor, important for QML singleton
66 explicit TrafficDataProvider() = delete;
67
68 // factory function for QML singleton
69 static Traffic::TrafficDataProvider* create(QQmlEngine* /*unused*/, QJSEngine* /*unused*/)
70 {
72 }
73
74
75
76 //
77 // Properties
78 //
79
85 Q_PROPERTY(QList<Traffic::TrafficDataSource_Abstract*> dataSources READ dataSources NOTIFY dataSourcesChanged)
86
87
96
105
114 Q_PROPERTY(QList<Traffic::TrafficFactor_WithPosition*> trafficObjects READ trafficObjects CONSTANT)
115
123
132
141
148 Q_PROPERTY(Traffic::Warning warning READ warning NOTIFY warningChanged)
149
150
151
152 //
153 // Getter Methods
154 //
155
160 [[nodiscard]] QList<Traffic::TrafficDataSource_Abstract*> dataSources() const;
161
166 [[nodiscard]] Units::Distance pressureAltitude() const {return m_pressureAltitude.value();}
167
172 [[nodiscard]] QBindable<Units::Distance> bindablePressureAltitude() const {return &m_pressureAltitude;}
173
178 [[nodiscard]] bool receivingHeartbeat() const
179 {
180 return m_receivingHeartbeat.value();
181 }
182
187 [[nodiscard]] QBindable<bool> bindableReceivingHeartbeat() const
188 {
189 return &m_receivingHeartbeat;
190 }
191
196 [[nodiscard]] QList<Traffic::TrafficFactor_WithPosition*> trafficObjects() const
197 {
198 return m_trafficObjects;
199 }
200
206 {
207 return m_trafficObjectWithoutPosition;
208 }
209
214 [[nodiscard]] QString trafficReceiverRuntimeError() const
215 {
216 return m_trafficReceiverRuntimeError;
217 }
218
223 [[nodiscard]] QString trafficReceiverSelfTestError() const
224 {
225 return m_trafficReceiverSelfTestError;
226 }
227
232 [[nodiscard]] Traffic::Warning warning() const
233 {
234 return m_Warning;
235 }
236
237
238
239 //
240 // Methods
241 //
242
252
262 Q_INVOKABLE QString addDataSource(const Traffic::ConnectionInfo &connectionInfo);
263
272 Q_INVOKABLE QString addDataSource_UDP(quint16 port);
273
282 Q_INVOKABLE QString addDataSource_SerialPort(const QString& portName);
283
294 Q_INVOKABLE QString addDataSource_TCP(const QString& host, quint16 port);
295
304
305signals:
308
316 void passwordRequest(const QString& SSID);
317
324 void passwordStorageRequest(const QString& SSID, const QString& password);
325
328
331
334
337
338public slots:
347
353
364 void setPassword(const QString& SSID, const QString &password);
365
366private slots:
367 // Identical to addDataSource, but handles Bluetooth Classic connections only.
368 QString addDataSource_BluetoothClassic(const Traffic::ConnectionInfo &connectionInfo);
369
370 // Identical to addDataSource, but handles Bluetooth Low Energy connections only.
371 QString addDataSource_BluetoothLowEnergy(const Traffic::ConnectionInfo &connectionInfo);
372
373 // Clear all data sources
374 void clearDataSources();
375
376 // Intializations that are moved out of the constructor, in order to avoid
377 // nested uses of constructors in Global.
378 void deferredInitialization() const;
379
380 // Sends out foreflight broadcast message See
381 // https://www.foreflight.com/connect/spec/
382 void foreFlightBroadcast();
383
384 // Load connection infos from file and create connections
385 void loadConnectionInfos();
386
387 // Called if one of the sources indicates a heartbeat change
388 void onSourceHeartbeatChanged();
389
390 // Called if one of the sources reports traffic (position unknown)
391 void onTrafficFactorWithPosition(const Traffic::TrafficFactor_WithPosition& factor);
392
393 // Called if one of the sources reports traffic (position known)
394 void onTrafficFactorWithoutPosition(const Traffic::TrafficFactor_DistanceOnly& factor);
395
396 // Called if one of the sources reports or clears an error string
397 void onTrafficReceiverSelfTestError();
398
399 // Called if one of the sources reports or clears an error string
400 void onTrafficReceiverRuntimeError();
401
402 // Resetter method
403 void resetWarning();
404
405 // Save connection infos to file
406 void saveConnectionInfos();
407
408 // Setter method
409 void setWarning(const Traffic::Warning& warning);
410
411private:
412 //
413 // Compute Methods
414 //
415
416 QString computeStatusString();
417
418 // UDP Socket for ForeFlight Broadcast messages.
419 // See https://www.foreflight.com/connect/spec/
420 QNetworkDatagram foreFlightBroadcastDatagram {R"({"App":"Enroute Flight Navigation","GDL90":{"port":4000}})", QHostAddress::Broadcast, 63093};
421 QUdpSocket foreFlightBroadcastSocket;
422 QTimer foreFlightBroadcastTimer;
423
424 // Targets
425 QList<Traffic::TrafficFactor_WithPosition *> m_trafficObjects;
426 QPointer<Traffic::TrafficFactor_DistanceOnly> m_trafficObjectWithoutPosition;
427
428 // TrafficData Sources
429 QProperty<QList<QPointer<Traffic::TrafficDataSource_Abstract>>> m_dataSources;
430 QProperty<QPointer<Traffic::TrafficDataSource_Abstract>> m_currentSource;
431
432 // Property cache
433 Traffic::Warning m_Warning;
434 QTimer m_WarningTimer;
435 QString m_trafficReceiverRuntimeError;
436 QString m_trafficReceiverSelfTestError;
437
438 QProperty<Units::Distance> m_pressureAltitude;
439 Units::Distance computePressureAltitude();
440
441 // Reconnect
442 QTimer reconnectionTimer;
443
444 Q_OBJECT_BINDABLE_PROPERTY(Traffic::TrafficDataProvider, bool, m_receivingHeartbeat, &Traffic::TrafficDataProvider::receivingHeartbeatChanged);
445
446 // Standard file name for saveConnectionInfos()
447 QString stdFileName{QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/connectionInfos.data"};
448};
449
450} // namespace Traffic
static Q_INVOKABLE Traffic::TrafficDataProvider * trafficDataProvider()
Pointer to appplication-wide static TrafficDataProvider instance.
Abstract base class for all classes that provide geographic position information.
Connection to a traffic data receiver.
QString trafficReceiverSelfTestError
String describing the traffic data receiver errors found in self-test.
QString trafficReceiverRuntimeError
String describing the current traffic data receiver errors.
void passwordStorageRequest(const QString &SSID, const QString &password)
Password storage request.
void passwordRequest(const QString &SSID)
Password request.
QString trafficReceiverSelfTestError() const
Getter function for the property with the same name.
Traffic::TrafficFactor_DistanceOnly * trafficObjectWithoutPosition() const
Getter method for property with the same name.
QList< Traffic::TrafficDataSource_Abstract * > dataSources
Traffic data sources.
QBindable< bool > bindableReceivingHeartbeat() const
Getter method for property with the same name.
Q_INVOKABLE QString addDataSource_TCP(const QString &host, quint16 port)
Add an additional data source.
Traffic::Warning warning
Current traffic warning.
Q_INVOKABLE QString addDataSource_UDP(quint16 port)
Add an additional data source.
QList< Traffic::TrafficFactor_WithPosition * > trafficObjects() const
Getter method for property with the same name.
QString trafficReceiverRuntimeError() const
Getter function for the property with the same name.
bool receivingHeartbeat() const
Getter method for property with the same name.
bool receivingHeartbeat
Heartbeat indicator.
Units::Distance pressureAltitude
Pressure altitude.
void receivingHeartbeatChanged()
Notifier signal.
void connectToTrafficReceiver()
Start attempt to connect to traffic receiver.
void trafficReceiverRuntimeErrorChanged()
Notifier signal.
Q_INVOKABLE QString addDataSource_SerialPort(const QString &portName)
Add an additional data source.
void trafficReceiverSelfTestErrorChanged()
Notifier signal.
void dataSourcesChanged()
Notifier signal.
void setPassword(const QString &SSID, const QString &password)
Send password to the traffic data sources.
Q_INVOKABLE void removeDataSource(Traffic::TrafficDataSource_Abstract *source)
Remove data sources.
void disconnectFromTrafficReceiver()
Disconnect from traffic receiver.
Traffic::Warning warning() const
Getter method for property with the same name.
void addDataSource(Traffic::TrafficDataSource_Abstract *source)
Add an additional data source.
TrafficDataProvider(QObject *parent=nullptr)
Default constructor.
QBindable< Units::Distance > bindablePressureAltitude() const
Getter method for property with the same name.
QList< Traffic::TrafficFactor_WithPosition * > trafficObjects
Traffic objects whose position is known.
Traffic::TrafficFactor_DistanceOnly * trafficObjectWithoutPosition
Most relevant traffic object whose position is not known.
void warningChanged(const Traffic::Warning &)
Notifier signal.
Q_INVOKABLE QString addDataSource(const Traffic::ConnectionInfo &connectionInfo)
Add an additional data source.
Base class for all traffic receiver data sources.
Traffic factor where only distance is known.
Traffic factor whose precise position is known.
Traffic warning.
Definition Warning.h:40
Convenience class for distance computations.
Definition Distance.h:35
Conversion between units used in aviation.
Definition Angle.h:34