Replaced InsightWorker signals with InsightSignals singleton
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
#include "InsightWorker.hpp"
|
||||
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
|
||||
#include "src/TargetController/TargetControllerState.hpp"
|
||||
|
||||
#include "src/Insight/InsightSignals.hpp"
|
||||
#include "src/Logger/Logger.hpp"
|
||||
|
||||
namespace Bloom
|
||||
@@ -13,137 +11,52 @@ namespace Bloom
|
||||
|
||||
using Bloom::Targets::TargetState;
|
||||
|
||||
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
||||
auto taskQueueLock = this->queuedTasks.acquireLock();
|
||||
task->moveToThread(this->thread());
|
||||
task->setParent(this);
|
||||
this->queuedTasks.getValue().push(task);
|
||||
emit this->taskQueued();
|
||||
}
|
||||
|
||||
void InsightWorker::startup() {
|
||||
Logger::debug("Starting InsightWorker thread");
|
||||
EventManager::registerListener(this->eventListener);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::TargetControllerStateChanged>(
|
||||
std::bind(&InsightWorker::onTargetControllerStateChangedEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::TargetExecutionStopped>(
|
||||
std::bind(&InsightWorker::onTargetStoppedEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::TargetExecutionResumed>(
|
||||
std::bind(&InsightWorker::onTargetResumedEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::TargetReset>(
|
||||
std::bind(&InsightWorker::onTargetResetEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::ProgrammingModeEnabled>(
|
||||
std::bind(&InsightWorker::onProgrammingModeEnabledEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventListener->registerCallbackForEventType<Events::ProgrammingModeDisabled>(
|
||||
std::bind(&InsightWorker::onProgrammingModeDisabledEvent, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
this->eventDispatchTimer = new QTimer(this);
|
||||
QObject::connect(this->eventDispatchTimer, &QTimer::timeout, this, &InsightWorker::dispatchEvents);
|
||||
this->eventDispatchTimer->start(5);
|
||||
|
||||
QObject::connect(this, &InsightWorker::taskQueued, this, &InsightWorker::executeTasks);
|
||||
|
||||
EventManager::triggerEvent(
|
||||
std::make_shared<Events::InsightThreadStateChanged>(ThreadState::READY)
|
||||
QObject::connect(
|
||||
InsightSignals::instance(),
|
||||
&InsightSignals::taskQueued,
|
||||
this,
|
||||
&InsightWorker::executeTasks
|
||||
);
|
||||
|
||||
emit this->ready();
|
||||
}
|
||||
|
||||
void InsightWorker::onInsightWindowActivated() {
|
||||
this->lastTargetState = this->targetControllerConsole.getTargetState();
|
||||
emit this->targetStateUpdated(this->lastTargetState);
|
||||
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
||||
task->moveToThread(nullptr);
|
||||
|
||||
{
|
||||
const auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
|
||||
InsightWorker::queuedTasks.getValue().push(task);
|
||||
}
|
||||
|
||||
emit InsightSignals::instance()->taskQueued();
|
||||
}
|
||||
|
||||
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
|
||||
auto task = std::optional<InsightWorkerTask*>();
|
||||
|
||||
auto& queuedTasks = this->queuedTasks.getValue();
|
||||
auto taskQueueLock = this->queuedTasks.acquireLock();
|
||||
auto taskQueueLock = InsightWorker::queuedTasks.acquireLock();
|
||||
auto& queuedTasks = InsightWorker::queuedTasks.getValue();
|
||||
|
||||
if (!queuedTasks.empty()) {
|
||||
task = queuedTasks.front();
|
||||
auto* task = queuedTasks.front();
|
||||
queuedTasks.pop();
|
||||
return task;
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
void InsightWorker::onTargetStoppedEvent(const Events::TargetExecutionStopped& event) {
|
||||
if (this->lastTargetState == TargetState::STOPPED) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->lastTargetState = TargetState::STOPPED;
|
||||
emit this->targetStateUpdated(TargetState::STOPPED);
|
||||
}
|
||||
|
||||
void InsightWorker::onTargetResumedEvent(const Events::TargetExecutionResumed& event) {
|
||||
if (this->lastTargetState != TargetState::RUNNING) {
|
||||
this->lastTargetState = TargetState::RUNNING;
|
||||
emit this->targetStateUpdated(TargetState::RUNNING);
|
||||
}
|
||||
}
|
||||
|
||||
void InsightWorker::onTargetResetEvent(const Events::TargetReset& event) {
|
||||
try {
|
||||
if (this->targetControllerConsole.getTargetState() != TargetState::STOPPED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->lastTargetState != TargetState::STOPPED) {
|
||||
this->lastTargetState = TargetState::STOPPED;
|
||||
emit this->targetStateUpdated(TargetState::STOPPED);
|
||||
}
|
||||
|
||||
emit this->targetReset();
|
||||
|
||||
} catch (const Exceptions::Exception& exception) {
|
||||
Logger::debug("Error handling TargetReset event - " + exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
void InsightWorker::onTargetControllerStateChangedEvent(const Events::TargetControllerStateChanged& event) {
|
||||
using TargetController::TargetControllerState;
|
||||
|
||||
if (event.state == TargetControllerState::SUSPENDED) {
|
||||
emit this->targetControllerSuspended();
|
||||
|
||||
} else if (event.state == TargetControllerState::ACTIVE) {
|
||||
try {
|
||||
emit this->targetControllerResumed(this->targetControllerConsole.getTargetDescriptor());
|
||||
|
||||
} catch (const Exception& exception) {
|
||||
Logger::error("Insight resume failed - " + exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InsightWorker::onProgrammingModeEnabledEvent(const Events::ProgrammingModeEnabled& event) {
|
||||
emit this->programmingModeEnabled();
|
||||
}
|
||||
|
||||
void InsightWorker::onProgrammingModeDisabledEvent(const Events::ProgrammingModeDisabled& event) {
|
||||
emit this->programmingModeDisabled();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void InsightWorker::executeTasks() {
|
||||
auto task = std::optional<InsightWorkerTask*>();
|
||||
auto queuedTask = std::optional<InsightWorkerTask*>();
|
||||
|
||||
while ((task = this->getQueuedTask()).has_value()) {
|
||||
task.value()->execute(this->targetControllerConsole);
|
||||
while ((queuedTask = InsightWorker::getQueuedTask()).has_value()) {
|
||||
auto* task = queuedTask.value();
|
||||
task->moveToThread(this->thread());
|
||||
task->setParent(this);
|
||||
task->execute(this->targetControllerConsole);
|
||||
task->deleteLater();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore>
|
||||
#include <QApplication>
|
||||
|
||||
#include "src/Helpers/Thread.hpp"
|
||||
#include "src/Helpers/SyncSafe.hpp"
|
||||
#include "src/ProjectConfig.hpp"
|
||||
|
||||
#include "src/EventManager/EventManager.hpp"
|
||||
#include "src/EventManager/EventListener.hpp"
|
||||
#include "src/EventManager/Events/Events.hpp"
|
||||
|
||||
#include "src/TargetController/TargetControllerConsole.hpp"
|
||||
#include <queue>
|
||||
|
||||
#include "Tasks/InsightWorkerTask.hpp"
|
||||
|
||||
#include "src/Helpers/SyncSafe.hpp"
|
||||
#include "src/TargetController/TargetControllerConsole.hpp"
|
||||
|
||||
namespace Bloom
|
||||
{
|
||||
/**
|
||||
@@ -28,47 +21,18 @@ namespace Bloom
|
||||
public:
|
||||
InsightWorker() = default;
|
||||
|
||||
void queueTask(InsightWorkerTask* task);
|
||||
|
||||
void startup();
|
||||
|
||||
void onInsightWindowActivated();
|
||||
static void queueTask(InsightWorkerTask* task);
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
void taskQueued();
|
||||
void targetStateUpdated(Bloom::Targets::TargetState newState);
|
||||
void targetReset();
|
||||
void targetControllerSuspended();
|
||||
void targetControllerResumed(const Bloom::Targets::TargetDescriptor& targetDescriptor);
|
||||
void targetRegistersWritten(const Bloom::Targets::TargetRegisters& targetRegisters, const QDateTime& timestamp);
|
||||
void programmingModeEnabled();
|
||||
void programmingModeDisabled();
|
||||
|
||||
private:
|
||||
EventListenerPointer eventListener = std::make_shared<EventListener>("InsightWorkerEventListener");
|
||||
|
||||
TargetController::TargetControllerConsole targetControllerConsole = TargetController::TargetControllerConsole();
|
||||
|
||||
Targets::TargetState lastTargetState = Targets::TargetState::UNKNOWN;
|
||||
|
||||
QTimer* eventDispatchTimer = nullptr;
|
||||
|
||||
SyncSafe<std::queue<InsightWorkerTask*>> queuedTasks;
|
||||
|
||||
void dispatchEvents() {
|
||||
this->eventListener->dispatchCurrentEvents();
|
||||
}
|
||||
|
||||
std::optional<InsightWorkerTask*> getQueuedTask();
|
||||
|
||||
void onTargetStoppedEvent(const Events::TargetExecutionStopped& event);
|
||||
void onTargetResumedEvent(const Events::TargetExecutionResumed& event);
|
||||
void onTargetResetEvent(const Events::TargetReset& event);
|
||||
void onTargetControllerStateChangedEvent(const Events::TargetControllerStateChanged& event);
|
||||
void onProgrammingModeEnabledEvent(const Events::ProgrammingModeEnabled& event);
|
||||
void onProgrammingModeDisabledEvent(const Events::ProgrammingModeDisabled& event);
|
||||
static inline SyncSafe<std::queue<InsightWorkerTask*>> queuedTasks = {};
|
||||
|
||||
static std::optional<InsightWorkerTask*> getQueuedTask();
|
||||
void executeTasks();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user