diff --git a/samples/samples.pro b/samples/samples.pro index a5dfca3d3..d795c0c1d 100644 --- a/samples/samples.pro +++ b/samples/samples.pro @@ -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) diff --git a/samples/weatherdata/main.cpp b/samples/weatherdata/main.cpp new file mode 100644 index 000000000..65f8a0d17 --- /dev/null +++ b/samples/weatherdata/main.cpp @@ -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 +#include +#include + +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: " << endl; + qtout << "Example: 48.5 11.5" << endl; + qtout << "Type x to quit" << endl; + + lineReader.start(); + return a.exec(); +} diff --git a/samples/weatherdata/reader.cpp b/samples/weatherdata/reader.cpp new file mode 100644 index 000000000..938a0fa21 --- /dev/null +++ b/samples/weatherdata/reader.cpp @@ -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 +#include + +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: " << endl; + } + } +} diff --git a/samples/weatherdata/reader.h b/samples/weatherdata/reader.h new file mode 100644 index 000000000..ea7fb20e2 --- /dev/null +++ b/samples/weatherdata/reader.h @@ -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 +#include + +/*! + * 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 diff --git a/samples/weatherdata/sampleweatherdata.pro b/samples/weatherdata/sampleweatherdata.pro new file mode 100644 index 000000000..5f8ddb151 --- /dev/null +++ b/samples/weatherdata/sampleweatherdata.pro @@ -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) diff --git a/samples/weatherdata/weatherdataprinter.cpp b/samples/weatherdata/weatherdataprinter.cpp new file mode 100644 index 000000000..8d619929b --- /dev/null +++ b/samples/weatherdata/weatherdataprinter.cpp @@ -0,0 +1,83 @@ +#include "weatherdataprinter.h" +#include "blackmisc/logmessage.h" +#include + +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(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; + } +} diff --git a/samples/weatherdata/weatherdataprinter.h b/samples/weatherdata/weatherdataprinter.h new file mode 100644 index 000000000..bef23fa04 --- /dev/null +++ b/samples/weatherdata/weatherdataprinter.h @@ -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 + +/*! + * 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 m_weatherDataFactory; + BlackCore::IWeatherData *m_weatherData = nullptr; +}; + +#endif // guard