mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-08-07 18:15:51 +08:00
Weather data sample
New sample demonstrating how to use the weatherplugins to get and print weatherdata around a geo position refs #556
This commit is contained in:
@@ -11,6 +11,7 @@ SUBDIRS += sampleblackmiscdbus
|
||||
SUBDIRS += sampleblackmisc
|
||||
SUBDIRS += sampleblackmiscsim
|
||||
SUBDIRS += samplehotkey
|
||||
SUBDIRS += sampleweatherdata
|
||||
|
||||
samplecliclient.file = cliclient/samplecliclient.pro
|
||||
sampleblackmiscquantities.file = blackmiscquantities/sampleblackmiscquantities.pro
|
||||
@@ -18,5 +19,6 @@ sampleblackmiscdbus.file = blackmiscdbus/sampleblackmiscdbus.pro
|
||||
sampleblackmisc.file = blackmisc/sampleblackmisc.pro
|
||||
sampleblackmiscsim.file = blackmiscsim/sampleblackmiscsim.pro
|
||||
samplehotkey.file = hotkey/samplehotkey.pro
|
||||
sampleweatherdata.file = weatherdata/sampleweatherdata.pro
|
||||
|
||||
load(common_post)
|
||||
|
||||
42
samples/weatherdata/main.cpp
Normal file
42
samples/weatherdata/main.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "reader.h"
|
||||
#include "weatherdataprinter.h"
|
||||
#include "blackmisc/registermetadata.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QTextStream>
|
||||
#include <QFile>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
BlackMisc::registerMetadata();
|
||||
CLogHandler::instance()->install(true);
|
||||
// CLogHandler::instance()->enableConsoleOutput(false); // default disable
|
||||
BlackMisc::registerMetadata();
|
||||
|
||||
CLineReader lineReader(&a);
|
||||
CWeatherDataPrinter printer(&a);
|
||||
QObject::connect(&lineReader, &CLineReader::weatherDataRequest, &printer, &CWeatherDataPrinter::fetchAndPrintWetherData);
|
||||
QObject::connect(&lineReader, &CLineReader::quit, &lineReader, &CLineReader::terminate);
|
||||
QObject::connect(&lineReader, &CLineReader::finished, &a, &QCoreApplication::quit);
|
||||
|
||||
QTextStream qtout(stdout);
|
||||
qtout << "Usage: <lat> <lon>" << endl;
|
||||
qtout << "Example: 48.5 11.5" << endl;
|
||||
qtout << "Type x to quit" << endl;
|
||||
|
||||
lineReader.start();
|
||||
return a.exec();
|
||||
}
|
||||
50
samples/weatherdata/reader.cpp
Normal file
50
samples/weatherdata/reader.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "reader.h"
|
||||
#include <QFile>
|
||||
#include <QRegularExpression>
|
||||
|
||||
using namespace BlackMisc::Geo;
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
|
||||
void CLineReader::run()
|
||||
{
|
||||
QFile file;
|
||||
file.open(stdin, QIODevice::ReadOnly | QIODevice::Text);
|
||||
QRegularExpression re("^(\\d+).([0,5])\\s(\\d+).([0,5])$");
|
||||
forever
|
||||
{
|
||||
QString line = file.readLine().trimmed();
|
||||
|
||||
if (line == "x")
|
||||
{
|
||||
emit quit();
|
||||
continue;
|
||||
}
|
||||
|
||||
QRegularExpressionMatch match = re.match(line);
|
||||
if (match.hasMatch())
|
||||
{
|
||||
double latitudeValue = match.captured(1).toDouble();
|
||||
latitudeValue += match.captured(2).toDouble() / 10;
|
||||
double longitudeValue = match.captured(3).toDouble();
|
||||
longitudeValue += match.captured(4).toDouble() / 10;
|
||||
const CLatitude latitude(latitudeValue, CAngleUnit::deg());
|
||||
const CLongitude longitude(longitudeValue, CAngleUnit::deg());
|
||||
emit weatherDataRequest(latitude, longitude);
|
||||
}
|
||||
else
|
||||
{
|
||||
QTextStream qtout(stdout);
|
||||
qtout << "Invalid command." << endl;
|
||||
qtout << "Usage: <lat> <lon>" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
samples/weatherdata/reader.h
Normal file
43
samples/weatherdata/reader.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKSAMPLE_WEATERDATA_READER_H
|
||||
#define BLACKSAMPLE_WEATERDATA_READER_H
|
||||
|
||||
#include "blackmisc/geo/latitude.h"
|
||||
#include "blackmisc/geo/longitude.h"
|
||||
#include <QThread>
|
||||
#include <QString>
|
||||
|
||||
/*!
|
||||
* Class reading line input and triggering commands
|
||||
*/
|
||||
class CLineReader : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CLineReader(QObject *parent = nullptr) : QThread(parent) {}
|
||||
|
||||
protected:
|
||||
//! \copydoc QThread::run
|
||||
void run();
|
||||
|
||||
signals:
|
||||
//! User is asking for weather data
|
||||
void weatherDataRequest(const BlackMisc::Geo::CLatitude &lat, const BlackMisc::Geo::CLongitude &lon);
|
||||
|
||||
//! User is asking to quit
|
||||
void quit();
|
||||
};
|
||||
|
||||
#endif
|
||||
23
samples/weatherdata/sampleweatherdata.pro
Normal file
23
samples/weatherdata/sampleweatherdata.pro
Normal file
@@ -0,0 +1,23 @@
|
||||
load(common_pre)
|
||||
|
||||
QT += core dbus
|
||||
|
||||
TARGET = sampleweatherdata
|
||||
TEMPLATE = app
|
||||
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
CONFIG += blackmisc blackcore
|
||||
|
||||
DEPENDPATH += . $$SourceRoot/src
|
||||
INCLUDEPATH += . $$SourceRoot/src
|
||||
|
||||
HEADERS += *.h
|
||||
SOURCES += *.cpp
|
||||
|
||||
DESTDIR = $$DestRoot/bin
|
||||
|
||||
target.path = $$PREFIX/bin
|
||||
INSTALLS += target
|
||||
|
||||
load(common_post)
|
||||
83
samples/weatherdata/weatherdataprinter.cpp
Normal file
83
samples/weatherdata/weatherdataprinter.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "weatherdataprinter.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include <QTextStream>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Weather;
|
||||
using namespace BlackMisc::Geo;
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
using namespace BlackCore;
|
||||
|
||||
|
||||
CWeatherDataPrinter::CWeatherDataPrinter(QObject *parent) : QObject(parent)
|
||||
{
|
||||
m_plugins.collectPlugins();
|
||||
|
||||
if (m_plugins.getAvailableWeatherDataPlugins().isEmpty())
|
||||
{
|
||||
CLogMessage(this).warning("No weather data plugin found!");
|
||||
}
|
||||
|
||||
CWeatherDataPluginInfo info = m_plugins.getAvailableWeatherDataPlugins().front();
|
||||
m_weatherDataFactory.reset(m_plugins.getPluginById<IWeatherDataFactory>(info.getIdentifier()));
|
||||
if (!m_weatherDataFactory)
|
||||
{
|
||||
CLogMessage(this).error("Failed to create IWeatherDataFactory.");
|
||||
}
|
||||
|
||||
m_weatherData = m_weatherDataFactory->create(this);
|
||||
if (!m_weatherData)
|
||||
{
|
||||
CLogMessage(this).error("Failed to create IWeatherData instance.");
|
||||
}
|
||||
|
||||
connect(m_weatherData, &IWeatherData::fetchingFinished, this, &CWeatherDataPrinter::ps_printWeatherData);
|
||||
}
|
||||
|
||||
void CWeatherDataPrinter::fetchAndPrintWetherData(const CLatitude &lat, const CLongitude &lon)
|
||||
{
|
||||
QTextStream qtout(stdout);
|
||||
qtout << "Fetching weather data. This may take a while..." << endl;
|
||||
m_weatherData->fetchWeatherData(lat, lon, 0.001);
|
||||
}
|
||||
|
||||
void CWeatherDataPrinter::ps_printWeatherData()
|
||||
{
|
||||
QTextStream qtout(stdout);
|
||||
qtout << "... finished." << endl;
|
||||
CWeatherGrid weatherGrid = m_weatherData->getWeatherData();
|
||||
for (const CGridPoint &gridPoint : weatherGrid)
|
||||
{
|
||||
qtout << "Latitude:" << gridPoint.getLatitude().toQString() << endl;
|
||||
qtout << "Longitude:" << gridPoint.getLongitude().toQString() << endl;
|
||||
|
||||
CTemperatureLayerList temperatureLayers = gridPoint.getTemperatureLayers();
|
||||
temperatureLayers.sort([](const CTemperatureLayer &a, const CTemperatureLayer &b) { return a.getLevel() < b.getLevel(); });
|
||||
CWindLayerList windLayers = gridPoint.getWindLayers();
|
||||
windLayers.sort([](const CWindLayer &a, const CWindLayer &b) { return a.getLevel() < b.getLevel(); });
|
||||
|
||||
if (temperatureLayers.size() != windLayers.size()) { continue; }
|
||||
for (int i = 0; i < temperatureLayers.size(); i++)
|
||||
{
|
||||
const CTemperatureLayer temperatureLayer = temperatureLayers[i];
|
||||
const CWindLayer windLayer = windLayers[i];
|
||||
qtout << " Level: " << temperatureLayer.getLevel().toQString() << endl;
|
||||
qtout << " Temperature: " << temperatureLayer.getTemperature().toQString() << endl;
|
||||
qtout << " Relative Humidity: " << temperatureLayer.getRelativeHumidity() << " %" << endl;
|
||||
qtout << " Wind: " << windLayer.getDirection().toQString() << " at " << windLayer.getSpeed().toQString() << endl;
|
||||
}
|
||||
qtout << endl;
|
||||
|
||||
qtout << " Clouds: " << endl;
|
||||
CCloudLayerList cloudLayers = gridPoint.getCloudLayers();
|
||||
cloudLayers.sort([](const CCloudLayer &a, const CCloudLayer &b) { return a.getBase() < b.getBase(); });
|
||||
for (int i = 0; i < cloudLayers.size(); i++)
|
||||
{
|
||||
const CCloudLayer &cloudLayer = cloudLayers[i];
|
||||
qtout << " Ceiling: " << cloudLayer.getCeiling().toQString() << endl;
|
||||
qtout << " Base: " << cloudLayer.getBase().toQString() << endl;
|
||||
qtout << " Coverage: " << cloudLayer.getCoveragePercent() << " %" << endl;
|
||||
}
|
||||
qtout << endl << endl;
|
||||
}
|
||||
}
|
||||
35
samples/weatherdata/weatherdataprinter.h
Normal file
35
samples/weatherdata/weatherdataprinter.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef BLACKSAMPLE_WEATERDATA_WEATHERDATAPRINTER_H
|
||||
#define BLACKSAMPLE_WEATERDATA_WEATHERDATAPRINTER_H
|
||||
|
||||
#include "blackcore/pluginmanagerweatherdata.h"
|
||||
#include "blackcore/weatherdata.h"
|
||||
#include "blackmisc/geo/latitude.h"
|
||||
#include "blackmisc/geo/longitude.h"
|
||||
#include <QObject>
|
||||
|
||||
/*!
|
||||
* CWeatherDataPrinter fetches and prints weather data
|
||||
*/
|
||||
class CWeatherDataPrinter : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CWeatherDataPrinter(QObject *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
//! Fetch new weather data for given position and print it once received
|
||||
void fetchAndPrintWetherData(const BlackMisc::Geo::CLatitude &lat, const BlackMisc::Geo::CLongitude &lon);
|
||||
|
||||
private slots:
|
||||
//! Print weather data to stdout
|
||||
void ps_printWeatherData();
|
||||
|
||||
private:
|
||||
BlackCore::CPluginManagerWeatherData m_plugins;
|
||||
QScopedPointer<BlackCore::IWeatherDataFactory> m_weatherDataFactory;
|
||||
BlackCore::IWeatherData *m_weatherData = nullptr;
|
||||
};
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user