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 <QPointer>
24#include <QTcpSocket>
25#include <string>
26#include <vector>
27
28#include "traffic/TrafficDataSource_AbstractSocket.h"
29#include "OgnParser.h" // From enrouteOGN library
30
31using namespace Qt::Literals::StringLiterals;
32
33namespace Traffic {
34
47
49 Q_OBJECT
50
51public:
62 TrafficDataSource_Ogn(bool isCanonical, QString hostName, quint16 port, QObject *parent = nullptr);
63
64 // Standard destructor
65 ~TrafficDataSource_Ogn() override;
66
67 //
68 // Properties
69 //
70
73 Q_PROPERTY(QString host READ host CONSTANT)
74
75
77 Q_PROPERTY(quint16 port READ port CONSTANT)
78
79 //
80 // Getter Methods
81 //
82
87 [[nodiscard]] QString dataFormat() const override { return u"APRS-IS"_s; }
88
93 [[nodiscard]] QString host() const
94 {
95 return m_hostName;
96 }
97
105 [[nodiscard]] QString icon() const override { return u"/icons/material/ic_wifi.svg"_s; }
106
114 [[nodiscard]] auto sourceName() const -> QString override
115 {
116 return tr("Open Glider Network-Connection");
117 }
118
123 [[nodiscard]] quint16 port() const
124 {
125 return m_port;
126 }
127
128public slots:
135
142
152 void sendPosition(const QGeoCoordinate& coordinate, double course, double speed, double altitude);
153
154private slots:
155
156 // Read lines from the socket's text stream and passes the string on to
157 // processAprsisMessage.
158 void onReadyRead();
159
160private:
161 Q_DISABLE_COPY_MOVE(TrafficDataSource_Ogn)
162
163 QTcpSocket m_socket;
164 QTextStream m_textStream;
165 QString m_hostName;
166 quint16 m_port;
167
168 // True while a connection is wanted. The socket's "disconnected" signal only
169 // triggers an automatic reconnect while this is true, so that
170 // disconnectFromTrafficReceiver() can actually stop the connection — abort()
171 // emits "disconnected", which would otherwise reconnect immediately.
172 bool m_connectionDesired = false;
173
174 QString m_lineBuffer; // Reusable buffer for reading lines
175 Ogn::OgnMessage m_ognMessage; // Reusable message structure
176
177 // our own OGN APRS CallSign, like "ENR12345"
178 QString m_callSign;
179
180 // our own AircraftType
182
183 // Radius around the approximate position for which traffic data is requested.
184 static constexpr Units::Distance m_receiveRadius = Units::Distance::fromNM(20.0);
185
186 // Current coordinate and GPS usage
187 QGeoCoordinate m_currentPosition;
188 bool m_usingGps = true;
189
190 // Cached ownship filter data (updated when aircraft settings change)
191 std::u16string m_ownAircraftName;
192 std::vector<std::string> m_ownTransponderCodes;
193
194 // Throttling for filter updates
195 QGeoCoordinate m_lastFilterPosition; // Last coordinate actually sent to OGN server
196 qint64 m_lastFilterUpdateTime = 0;
197 static constexpr qint64 MIN_FILTER_UPDATE_INTERVAL_MS = 1000; // 1 second
198 static constexpr double MIN_FILTER_UPDATE_DISTANCE_KM = 5.0; // 5 km
199
200 // Update filter with throttling logic
201 void setFilter(const QGeoCoordinate& coordinate);
202
203 // Update current coordinate based on GPS and map center availability
204 void updateCurrentCoordinate();
205
206 // Update cached ownship filter data from aircraft settings
207 void updateOwnshipFilterData();
208
209 // Periodic update function called once per minute
210 void periodicUpdate();
211
212 // Process OGN message and emit factorWithPosition signal
213 void processOgnMessage(const QString& data);
214
215 // Send a keep-alive message to the APRS-IS server
216 void sendKeepAlive();
217
218 // Verify the connection to the APRS-IS server and reconnect if necessary
219 void verifyConnection();
220};
221
222} // 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 sendPosition(const QGeoCoordinate &coordinate, double course, double speed, double altitude)
Send position report to APRS-IS server.
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