diff --git a/src/blackcore/weatherdata.h b/src/blackcore/weatherdata.h index 978d9eaea..6964b13fe 100644 --- a/src/blackcore/weatherdata.h +++ b/src/blackcore/weatherdata.h @@ -34,6 +34,11 @@ namespace BlackCore virtual void fetchWeatherData(const BlackMisc::Weather::CWeatherGrid &grid, const BlackMisc::PhysicalQuantities::CLength &range) = 0; + //! Fetch new weather around grid from file + virtual void fetchWeatherDataFromFile(const QString &filePath, + const BlackMisc::Weather::CWeatherGrid &grid, + const BlackMisc::PhysicalQuantities::CLength &range) = 0; + //! Get fetched weather data virtual BlackMisc::Weather::CWeatherGrid getWeatherData() const = 0; diff --git a/src/blackcore/weathermanager.cpp b/src/blackcore/weathermanager.cpp index 20ff1c750..d20d21fe1 100644 --- a/src/blackcore/weathermanager.cpp +++ b/src/blackcore/weathermanager.cpp @@ -41,7 +41,7 @@ namespace BlackCore void CWeatherManager::requestWeatherGrid(const CWeatherGrid &weatherGrid, const CIdentifier &identifier) { - WeatherRequest request { identifier, weatherGrid, {} }; + WeatherRequest request { {}, identifier, weatherGrid, {} }; m_pendingRequests.append(request); // Serialize the requests, since plugins can handle only one at a time if (m_pendingRequests.size() == 1) { fetchNextWeatherData(); } @@ -56,7 +56,23 @@ namespace BlackCore return; } - WeatherRequest weatherRequest { CIdentifier::null(), weatherGrid, callback }; + WeatherRequest weatherRequest { {}, CIdentifier::null(), weatherGrid, callback }; + m_pendingRequests.append(weatherRequest); + + // Serialize the requests, since plugins can handle only one at a time + if (m_pendingRequests.size() == 1) { fetchNextWeatherData(); } + } + + void CWeatherManager::requestWeatherGridFromFile(const QString &filePath, const CWeatherGrid &weatherGrid, + const CSlot &callback) + { + if (m_isWeatherClear) + { + callback(CWeatherGrid::getClearWeatherGrid()); + return; + } + + WeatherRequest weatherRequest { filePath, CIdentifier::null(), weatherGrid, callback }; m_pendingRequests.append(weatherRequest); // Serialize the requests, since plugins can handle only one at a time @@ -102,7 +118,8 @@ namespace BlackCore for (IWeatherData *plugin : as_const(m_weatherDataPlugins)) { - plugin->fetchWeatherData(weatherRequest.weatherGrid, maxDistance); + if (weatherRequest.filePath.isEmpty()) { plugin->fetchWeatherData(weatherRequest.weatherGrid, maxDistance); } + else { plugin->fetchWeatherDataFromFile(weatherRequest.filePath, weatherRequest.weatherGrid, maxDistance); } } } diff --git a/src/blackcore/weathermanager.h b/src/blackcore/weathermanager.h index ddf5ef8e7..1b39b41e7 100644 --- a/src/blackcore/weathermanager.h +++ b/src/blackcore/weathermanager.h @@ -53,6 +53,11 @@ namespace BlackCore virtual void requestWeatherGrid(const BlackMisc::Weather::CWeatherGrid &weatherGrid, const BlackMisc::CSlot &callback) override; + //! \copydoc BlackMisc::Weather::IWeatherGridProvider::requestWeatherGrid + virtual void requestWeatherGridFromFile(const QString &filePath, + const BlackMisc::Weather::CWeatherGrid &weatherGrid, + const BlackMisc::CSlot &callback) override; + signals: //! The weather grid, requested from identified, is available @@ -61,6 +66,7 @@ namespace BlackCore private: struct WeatherRequest { + QString filePath; BlackMisc::CIdentifier identifier; BlackMisc::Weather::CWeatherGrid weatherGrid; BlackMisc::CSlot callback; diff --git a/src/blackmisc/weather/weathergridprovider.h b/src/blackmisc/weather/weathergridprovider.h index 838959c82..cd16bd7b9 100644 --- a/src/blackmisc/weather/weathergridprovider.h +++ b/src/blackmisc/weather/weathergridprovider.h @@ -31,6 +31,11 @@ namespace BlackMisc //! Request weather grid virtual void requestWeatherGrid(const CWeatherGrid &weatherGrid, const CSlot &callback) = 0; + + //! Request weather grid from file + virtual void requestWeatherGridFromFile(const QString &filePath, + const BlackMisc::Weather::CWeatherGrid &weatherGrid, + const BlackMisc::CSlot &callback) = 0; }; //! Delegating class which can be directly used to access an \sa IWeatherGridProvider instance @@ -40,7 +45,6 @@ namespace BlackMisc //! \copydoc IWeatherGridProvider::requestWeatherGrid virtual void requestWeatherGrid(const CWeatherGrid &weatherGrid, const CSlot &callback); - protected: //! Constructor CWeatherGridAware(IWeatherGridProvider *weatherGridProvider) : IProviderAware(weatherGridProvider) { Q_ASSERT(weatherGridProvider); } diff --git a/src/plugins/weatherdata/gfs/weatherdatagfs.cpp b/src/plugins/weatherdata/gfs/weatherdatagfs.cpp index 893da1ff8..8bdf28fae 100644 --- a/src/plugins/weatherdata/gfs/weatherdatagfs.cpp +++ b/src/plugins/weatherdata/gfs/weatherdatagfs.cpp @@ -90,6 +90,23 @@ namespace BlackWxPlugin } } + void CWeatherDataGfs::fetchWeatherDataFromFile(const QString &filePath, const CWeatherGrid &grid, const CLength &range) + { + m_grid = grid; + m_maxRange = range; + + QFile file(filePath); + if (!file.exists() || !file.open(QIODevice::ReadOnly)) { return; } + m_gribData = file.readAll(); + + Q_ASSERT_X(!m_parseGribFileWorker, Q_FUNC_INFO, "Worker already running"); + m_parseGribFileWorker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]() + { + parseGfsFileImpl(m_gribData); + }); + m_parseGribFileWorker->then(this, &CWeatherDataGfs::fetchingWeatherDataFinished); + } + CWeatherGrid CWeatherDataGfs::getWeatherData() const { QReadLocker l(&m_lockData); diff --git a/src/plugins/weatherdata/gfs/weatherdatagfs.h b/src/plugins/weatherdata/gfs/weatherdatagfs.h index 5829f42bd..2948716ec 100644 --- a/src/plugins/weatherdata/gfs/weatherdatagfs.h +++ b/src/plugins/weatherdata/gfs/weatherdatagfs.h @@ -50,6 +50,11 @@ namespace BlackWxPlugin virtual void fetchWeatherData(const BlackMisc::Weather::CWeatherGrid &grid, const BlackMisc::PhysicalQuantities::CLength &range) override; + //! \copydoc BlackCore::IWeatherData::fetchWeatherDataFromFile + virtual void fetchWeatherDataFromFile(const QString &filePath, + const BlackMisc::Weather::CWeatherGrid &grid, + const BlackMisc::PhysicalQuantities::CLength &range) override; + //! \copydoc BlackCore::IWeatherData::getWeatherData() virtual BlackMisc::Weather::CWeatherGrid getWeatherData() const override;