Enroute Flight Navigation
A navigation app for VFR pilots
TrafficDataSource_Ogn.h
1/***************************************************************************
2 * Copyright (C) 2021-2025 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 QString m_lineBuffer; // Reusable buffer for reading lines
169 Ogn::OgnMessage m_ognMessage; // Reusable message structure
170
171 // our own OGN APRS CallSign, like "ENR12345"
172 QString m_callSign;
173
174 // our own AircraftType
175 Traffic::AircraftType m_aircraftType = {Traffic::AircraftType::Aircraft};
176
177 // Radius around the approximate position for which traffic data is requested.
178 static constexpr Units::Distance m_receiveRadius = Units::Distance::fromNM(20.0);
179
180 // Current coordinate and GPS usage
181 QGeoCoordinate m_currentPosition;
182 bool m_usingGps = true;
183
184 // Cached ownship filter data (updated when aircraft settings change)
185 std::u16string m_ownAircraftName;
186 std::vector<std::string> m_ownTransponderCodes;
187
188 // Throttling for filter updates
189 QGeoCoordinate m_lastFilterPosition; // Last coordinate actually sent to OGN server
190 qint64 m_lastFilterUpdateTime = 0;
191 static constexpr qint64 MIN_FILTER_UPDATE_INTERVAL_MS = 1000; // 1 second
192 static constexpr double MIN_FILTER_UPDATE_DISTANCE_KM = 5.0; // 5 km
193
194 // Update filter with throttling logic
195 void setFilter(const QGeoCoordinate& coordinate);
196
197 // Update current coordinate based on GPS and map center availability
198 void updateCurrentCoordinate();
199
200 // Update cached ownship filter data from aircraft settings
201 void updateOwnshipFilterData();
202
203 // Periodic update function called once per minute
204 void periodicUpdate();
205
206 // Process OGN message and emit factorWithPosition signal
207 void processOgnMessage(const QString& data);
208
209 // Send a keep-alive message to the APRS-IS server
210 void sendKeepAlive();
211
212 // Verify the connection to the APRS-IS server and reconnect if necessary
213 void verifyConnection();
214};
215
216} // 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.
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