diff --git a/scwx-qt/source/scwx/qt/map/alert_layer.cpp b/scwx-qt/source/scwx/qt/map/alert_layer.cpp index 892a27ad..cc8bc29b 100644 --- a/scwx-qt/source/scwx/qt/map/alert_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/alert_layer.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include #include #include #include @@ -129,9 +131,16 @@ public: } ConnectSignals(); + ScheduleRefresh(); } ~Impl() { + std::unique_lock refreshLock(refreshMutex_); + refreshTimer_.cancel(); + refreshLock.unlock(); + + threadPool_.join(); + receiver_ = nullptr; std::unique_lock lock(linesMutex_); @@ -147,6 +156,7 @@ public: QEvent* ev); void HandleGeoLinesHover(std::shared_ptr& di, const QPointF& mouseGlobalPos); + void ScheduleRefresh(); void AddLine(std::shared_ptr& geoLines, std::shared_ptr& di, @@ -167,8 +177,13 @@ public: boost::container::stable_vector< std::shared_ptr>& drawItems); + boost::asio::thread_pool threadPool_ {1u}; + AlertLayer* self_; + boost::asio::system_timer refreshTimer_ {threadPool_}; + std::mutex refreshMutex_; + const awips::Phenomenon phenomenon_; std::unique_ptr receiver_ {std::make_unique()}; @@ -387,6 +402,38 @@ void AlertLayer::Impl::ConnectSignals() { selectedTime_ = dateTime; }); } +void AlertLayer::Impl::ScheduleRefresh() +{ + using namespace std::chrono_literals; + + // Take a unique lock before refreshing + std::unique_lock lock(refreshMutex_); + + // Expires at the top of the next minute + std::chrono::system_clock::time_point now = + std::chrono::floor( + std::chrono::system_clock::now()); + refreshTimer_.expires_at(now + 1min); + + refreshTimer_.async_wait( + [this](const boost::system::error_code& e) + { + if (e == boost::asio::error::operation_aborted) + { + logger_->debug("Refresh timer cancelled"); + } + else if (e != boost::system::errc::success) + { + logger_->warn("Refresh timer error: {}", e.message()); + } + else + { + Q_EMIT self_->NeedsRendering(); + ScheduleRefresh(); + } + }); +} + void AlertLayer::Impl::AddAlert( const std::shared_ptr& segmentRecord) {