| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | // Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com>                     //
 | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                           //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef INCLUDE_AVIATIONWEATHER_H
 | 
					
						
							|  |  |  | #define INCLUDE_AVIATIONWEATHER_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QtCore>
 | 
					
						
							|  |  |  | #include <QTimer>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "export.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <cmath>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QDateTime>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QNetworkAccessManager; | 
					
						
							|  |  |  | class QNetworkReply; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Aviation Weather API wrapper
 | 
					
						
							|  |  |  | // Allows METAR information to be obtained for a given airport
 | 
					
						
							|  |  |  | // Currently supports checkwxapi.com
 | 
					
						
							|  |  |  | class SDRBASE_API AviationWeather : public QObject | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Q_OBJECT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     struct METAR { | 
					
						
							|  |  |  |         QString m_icao;                 // ICAO of reporting station/airport
 | 
					
						
							|  |  |  |         QString m_text;                 // Raw METAR text
 | 
					
						
							|  |  |  |         QDateTime m_dateTime;           // Date&time of observation
 | 
					
						
							|  |  |  |         float m_windDirection;          // Direction wind is blowing from, in degrees
 | 
					
						
							|  |  |  |         float m_windSpeed;              // Wind speed in knots
 | 
					
						
							|  |  |  |         float m_windGusts;              // Wind gusts in knots
 | 
					
						
							|  |  |  |         QString m_visibility;           // Visibility in metres (may be non-numeric)
 | 
					
						
							|  |  |  |         QStringList m_conditions;       // Weather conditions (Rain, snow)
 | 
					
						
							|  |  |  |         float m_ceiling;                // Ceiling in feet
 | 
					
						
							|  |  |  |         QStringList m_clouds;           // Cloud types and altitudes
 | 
					
						
							| 
									
										
										
										
											2024-07-10 22:37:52 +02:00
										 |  |  |         float m_temperature;            // Air temperature in Celsius
 | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |         float m_dewpoint;               // Dewpoint in Celsuius
 | 
					
						
							|  |  |  |         float m_pressure;               // Air pressure in hPa/mb
 | 
					
						
							|  |  |  |         float m_humidity;               // Humidity in %
 | 
					
						
							|  |  |  |         QString m_flightCateogory;      // VFR/MVFR/IFR/LIFR
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         METAR() : | 
					
						
							|  |  |  |            m_windDirection(NAN), | 
					
						
							|  |  |  |            m_windSpeed(NAN), | 
					
						
							|  |  |  |            m_windGusts(NAN), | 
					
						
							|  |  |  |            m_ceiling(NAN), | 
					
						
							|  |  |  |            m_temperature(NAN), | 
					
						
							|  |  |  |            m_dewpoint(NAN), | 
					
						
							|  |  |  |            m_pressure(NAN), | 
					
						
							|  |  |  |            m_humidity(NAN) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         QString decoded(const QString joinArg="\n") const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             QStringList s; | 
					
						
							|  |  |  |             if (m_dateTime.isValid()) { | 
					
						
							|  |  |  |                 s.append(QString("Observed: %1").arg(m_dateTime.toString())); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_windDirection) && !std::isnan(m_windSpeed)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Wind: %1%2 / %3 knts").arg(m_windDirection).arg(QChar(0xb0)).arg(m_windSpeed)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_windGusts) ) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Gusts: %1 knts").arg(m_windGusts)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_visibility.isEmpty()) { | 
					
						
							|  |  |  |                 s.append(QString("Visibility: %1 metres").arg(m_visibility)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_conditions.isEmpty()) { | 
					
						
							|  |  |  |                 s.append(QString("Conditions: %1").arg(m_conditions.join(", "))); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_ceiling)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Ceiling: %1 ft").arg(m_ceiling)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_clouds.isEmpty()) { | 
					
						
							|  |  |  |                 s.append(QString("Clouds: %1").arg(m_clouds.join(", "))); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_temperature)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Temperature: %1 %2C").arg(m_temperature).arg(QChar(0xb0))); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_dewpoint)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Dewpoint: %1 %2C").arg(m_dewpoint).arg(QChar(0xb0))); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_pressure)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Pressure: %1 hPa").arg(m_pressure)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-06 12:55:52 +01:00
										 |  |  |             if (!std::isnan(m_humidity)) { | 
					
						
							| 
									
										
										
										
											2022-06-06 10:53:10 +01:00
										 |  |  |                 s.append(QString("Humidity: %1 %").arg(m_humidity)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_flightCateogory.isEmpty()) { | 
					
						
							|  |  |  |                 s.append(QString("Category: %1").arg(m_flightCateogory)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return s.join(joinArg); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     AviationWeather(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     static AviationWeather* create(const QString& apiKey, const QString& service="checkwxapi.com"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void getWeather(const QString &icao) = 0; | 
					
						
							|  |  |  |     void getWeatherPeriodically(const QString &, int periodInMins); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public slots: | 
					
						
							|  |  |  |     void update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | signals: | 
					
						
							|  |  |  |     void weatherUpdated(const METAR &metar);  // Called when new data available. If no value is available, parameter will be NAN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     QTimer m_timer;             // Timer for periodic updates
 | 
					
						
							|  |  |  |     QString m_icao;             // Saved airport ICAO for period updates
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SDRBASE_API CheckWXAPI : public AviationWeather { | 
					
						
							|  |  |  |     Q_OBJECT | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CheckWXAPI(const QString& apiKey); | 
					
						
							|  |  |  |     ~CheckWXAPI(); | 
					
						
							|  |  |  |     virtual void getWeather(const QString &icao) override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString m_apiKey; | 
					
						
							|  |  |  |     QNetworkAccessManager *m_networkManager; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public slots: | 
					
						
							|  |  |  |     void handleReply(QNetworkReply* reply); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* INCLUDE_AVIATIONWEATHER_H */
 |