Enroute Flight Navigation
A navigation app for VFR pilots
TrafficDataSource_Ogn.h
1/***************************************************************************
2 * Copyright (C) 2021-2026 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 <QElapsedTimer>
24#include <QPointer>
25#include <QTcpSocket>
26#include <string>
27#include <vector>
28
29#include "traffic/TrafficDataSource_AbstractSocket.h"
30#include "OgnFilter.h" // From enrouteOGN library
31#include "OgnParser.h" // From enrouteOGN library
32
33using namespace Qt::Literals::StringLiterals;
34
35namespace Traffic {
36
49
51 Q_OBJECT
52
53public:
64 TrafficDataSource_Ogn(bool isCanonical, QString hostName, quint16 port, QObject *parent = nullptr);
65
66 // Standard destructor
67 ~TrafficDataSource_Ogn() override;
68
69 //
70 // Properties
71 //
72
75 Q_PROPERTY(QString host READ host CONSTANT)
76
77
79 Q_PROPERTY(quint16 port READ port CONSTANT)
80
81 //
82 // Getter Methods
83 //
84
89 [[nodiscard]] QString dataFormat() const override { return u"APRS-IS"_s; }
90
95 [[nodiscard]] QString host() const
96 {
97 return m_hostName;
98 }
99
107 [[nodiscard]] QString icon() const override { return u"/icons/material/ic_wifi.svg"_s; }
108
116 [[nodiscard]] auto sourceName() const -> QString override
117 {
118 return tr("Open Glider Network-Connection");
119 }
120
125 [[nodiscard]] quint16 port() const
126 {
127 return m_port;
128 }
129
130public slots:
137
144
145private slots:
146
147 // Read lines from the socket's text stream and passes the string on to
148 // processAprsisMessage.
149 void onReadyRead();
150
151private:
152 Q_DISABLE_COPY_MOVE(TrafficDataSource_Ogn)
153
154 QTcpSocket m_socket;
155 QTextStream m_textStream;
156 QString m_hostName;
157 quint16 m_port;
158
159 // True while a connection is wanted. The socket's "disconnected" signal only
160 // triggers an automatic reconnect while this is true, so that
161 // disconnectFromTrafficReceiver() can actually stop the connection — abort()
162 // emits "disconnected", which would otherwise reconnect immediately.
163 bool m_connectionDesired = false;
164
165 // Time of the last connection attempt. Automatic reconnects (from the
166 // "disconnected" signal and from the watchdog verifyConnection()) wait at
167 // least reconnectBackoff after an attempt before they try again, so that a
168 // slow host lookup or handshake is not aborted and an unreachable server is
169 // not hammered once per second.
170 QElapsedTimer m_lastConnectionAttempt;
171 static constexpr qint64 reconnectBackoffMs = 10'000;
172
173 QString m_lineBuffer; // Reusable buffer for reading lines
174 Ogn::OgnMessage m_ognMessage; // Reusable message structure
175 Ogn::OgnFilter m_ognFilter; // Deduplication and out-of-order packet filter
176
177 // our own OGN APRS CallSign, like "ENR12345"
178 QString m_callSign;
179
180 // Radius around the approximate position for which traffic data is requested.
181 static constexpr Units::Distance m_receiveRadius = Units::Distance::fromNM(20.0);
182
183 // Current coordinate and GPS usage
184 QGeoCoordinate m_currentPosition;
185 bool m_usingGps = true;
186
187 // Cached ownship filter data (updated when aircraft settings change)
188 std::u16string m_ownAircraftName;
189 std::vector<std::string> m_ownTransponderCodes;
190
191 // Throttling for filter updates
192 QGeoCoordinate m_lastFilterPosition; // Last coordinate actually sent to OGN server
193 qint64 m_lastFilterUpdateTime = 0;
194 static constexpr qint64 MIN_FILTER_UPDATE_INTERVAL_MS = 1000; // 1 second
195 static constexpr double MIN_FILTER_UPDATE_DISTANCE_KM = 5.0; // 5 km
196
197 // Update filter with throttling logic
198 void setFilter(const QGeoCoordinate& coordinate);
199
200 // Update current coordinate based on GPS and map center availability
201 void updateCurrentCoordinate();
202
203 // Update cached ownship filter data from aircraft settings
204 void updateOwnshipFilterData();
205
206 // Periodic update function called once per minute
207 void periodicUpdate();
208
209 // Process OGN message and emit factorWithPosition signal
210 void processOgnMessage(const QString& data);
211
212 // Send a keep-alive message to the APRS-IS server
213 void sendKeepAlive();
214
215 // Verify the connection to the APRS-IS server and reconnect if necessary
216 void verifyConnection();
217};
218
219} // namespace Traffic
TrafficDataSource_AbstractSocket(bool isCanonical, QObject *parent)
Default constructor.
void connectToTrafficReceiver() override
Start attempt to connect to traffic receiver.
QString dataFormat() const override
Getter function for the property with the same name.
void disconnectFromTrafficReceiver() override
Disconnect from traffic receiver.
quint16 port() const
Getter function for the property with the same name.
quint16 port
TCP port of the OGN/APRS-IS server (e.g. 14580).
QString host
Hostname of the OGN/APRS-IS server (e.g. aprs.glidernet.org).
TrafficDataSource_Ogn(bool isCanonical, QString hostName, quint16 port, QObject *parent=nullptr)
Default constructor.
QString host() const
Getter function for the property with the same name.
QString icon() const override
Getter function for the property with the same name.
auto sourceName() const -> QString override
Getter function for the property with the same name.
Convenience class for distance computations.
Definition Distance.h:35
static Q_INVOKABLE constexpr Units::Distance fromNM(double distanceInNM)
Constructs a distance.
Definition Distance.h:82