Additional placefile view/add enhancements

This commit is contained in:
Dan Paulat 2023-07-23 23:24:45 -05:00
parent 0064733679
commit 18c05b3a63
6 changed files with 119 additions and 22 deletions

View file

@ -5,6 +5,8 @@
#include <shared_mutex>
#include <vector>
#include <QDir>
#include <QUrl>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp>
@ -39,6 +41,7 @@ public:
std::string name_;
std::shared_ptr<gr::Placefile> placefile_;
bool enabled_;
bool thresholded_ {true};
boost::asio::thread_pool threadPool_ {1u};
boost::asio::steady_timer refreshTimer_ {threadPool_};
std::mutex refreshMutex_ {};
@ -54,13 +57,52 @@ public:
PlacefileManager* self_;
std::vector<std::unique_ptr<PlacefileRecord>> placefileRecords_ {};
std::shared_mutex placefileRecordLock_ {};
std::vector<std::shared_ptr<PlacefileRecord>> placefileRecords_ {};
std::unordered_map<std::string, std::shared_ptr<PlacefileRecord>>
placefileRecordMap_ {};
std::shared_mutex placefileRecordLock_ {};
};
PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this)) {}
PlacefileManager::~PlacefileManager() = default;
bool PlacefileManager::PlacefileEnabled(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->enabled_;
}
return false;
}
bool PlacefileManager::PlacefileThresholded(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->thresholded_;
}
return false;
}
std::shared_ptr<const gr::Placefile>
PlacefileManager::Placefile(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->placefile_;
}
return nullptr;
}
std::vector<std::shared_ptr<gr::Placefile>>
PlacefileManager::GetActivePlacefiles()
{
@ -79,6 +121,47 @@ PlacefileManager::GetActivePlacefiles()
return placefiles;
}
void PlacefileManager::AddUrl(const std::string& urlString)
{
std::string normalizedUrl;
// Normalize URL string
QUrl url = QUrl::fromUserInput(QString::fromStdString(urlString));
if (url.isLocalFile())
{
normalizedUrl = QDir::toNativeSeparators(url.toLocalFile()).toStdString();
}
else
{
normalizedUrl = urlString;
}
std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously
auto it = std::find_if(p->placefileRecords_.begin(),
p->placefileRecords_.end(),
[&normalizedUrl](auto& record)
{ return record->name_ == normalizedUrl; });
if (it != p->placefileRecords_.end())
{
logger_->debug("Placefile already added: {}", normalizedUrl);
return;
}
// Placefile is new, proceed with adding
logger_->info("AddUrl: {}", normalizedUrl);
// Add an empty placefile record for the new URL
auto placefileRecord = p->placefileRecords_.emplace_back(
std::make_shared<PlacefileRecord>(normalizedUrl, nullptr, false));
p->placefileRecordMap_.insert_or_assign(normalizedUrl, placefileRecord);
lock.unlock();
Q_EMIT PlacefileUpdated(normalizedUrl);
}
void PlacefileManager::LoadFile(const std::string& filename)
{
logger_->debug("LoadFile: {}", filename);
@ -99,14 +182,13 @@ void PlacefileManager::LoadFile(const std::string& filename)
std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously
auto it = std::find_if(p->placefileRecords_.begin(),
p->placefileRecords_.end(),
[&filename](auto& record)
{ return record->name_ == filename; });
if (it != p->placefileRecords_.end())
auto it = p->placefileRecordMap_.find(filename);
if (it != p->placefileRecordMap_.end())
{
// If the placefile has been loaded previously, update it
(*it)->Update(placefile);
it->second->Update(placefile);
lock.unlock();
Q_EMIT PlacefileUpdated(filename);
}
@ -114,7 +196,10 @@ void PlacefileManager::LoadFile(const std::string& filename)
{
// If this is a new placefile, add it
auto& record = p->placefileRecords_.emplace_back(
std::make_unique<PlacefileRecord>(filename, placefile));
std::make_shared<PlacefileRecord>(filename, placefile));
p->placefileRecordMap_.insert_or_assign(filename, record);
lock.unlock();
Q_EMIT PlacefileEnabled(filename, record->enabled_);
Q_EMIT PlacefileUpdated(filename);