2021-08-30 22:17:59 +01:00
|
|
|
#include "InsightWorker.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
#include "src/TargetController/TargetControllerState.hpp"
|
|
|
|
|
|
2021-08-30 22:17:59 +01:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom
|
|
|
|
|
{
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-05-24 20:58:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
using Bloom::Targets::TargetState;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
|
|
|
|
auto taskQueueLock = this->queuedTasks.acquireLock();
|
|
|
|
|
task->moveToThread(this->thread());
|
|
|
|
|
task->setParent(this);
|
2022-04-15 14:32:26 +01:00
|
|
|
this->queuedTasks.getValue().push(task);
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->taskQueued();
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::startup() {
|
|
|
|
|
Logger::debug("Starting InsightWorker thread");
|
2022-03-20 17:37:36 +00:00
|
|
|
EventManager::registerListener(this->eventListener);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetControllerStateChanged>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetControllerStateChangedEvent, this, std::placeholders::_1)
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetExecutionStopped>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetStoppedEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetExecutionResumed>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetResumedEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-04-08 22:14:37 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetReset>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetResetEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-05 17:00:56 +01:00
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventDispatchTimer = new QTimer(this);
|
|
|
|
|
QObject::connect(this->eventDispatchTimer, &QTimer::timeout, this, &InsightWorker::dispatchEvents);
|
|
|
|
|
this->eventDispatchTimer->start(5);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
QObject::connect(this, &InsightWorker::taskQueued, this, &InsightWorker::executeTasks);
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-03-20 17:37:36 +00:00
|
|
|
EventManager::triggerEvent(
|
2022-02-05 15:32:08 +00:00
|
|
|
std::make_shared<Events::InsightThreadStateChanged>(ThreadState::READY)
|
|
|
|
|
);
|
2021-10-18 01:03:22 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->ready();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-04-28 21:06:57 +01:00
|
|
|
void InsightWorker::onInsightWindowActivated() {
|
|
|
|
|
this->lastTargetState = this->targetControllerConsole.getTargetState();
|
|
|
|
|
emit this->targetStateUpdated(this->lastTargetState);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
|
|
|
|
|
auto task = std::optional<InsightWorkerTask*>();
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-04-15 14:32:26 +01:00
|
|
|
auto& queuedTasks = this->queuedTasks.getValue();
|
2022-02-05 15:32:08 +00:00
|
|
|
auto taskQueueLock = this->queuedTasks.acquireLock();
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (!queuedTasks.empty()) {
|
|
|
|
|
task = queuedTasks.front();
|
|
|
|
|
queuedTasks.pop();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return task;
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::onTargetStoppedEvent(const Events::TargetExecutionStopped& event) {
|
2022-04-23 17:30:14 +01:00
|
|
|
if (this->lastTargetState == TargetState::STOPPED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 20:27:27 +01:00
|
|
|
this->lastTargetState = TargetState::STOPPED;
|
|
|
|
|
emit this->targetStateUpdated(TargetState::STOPPED);
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-09-11 20:45:06 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::onTargetResumedEvent(const Events::TargetExecutionResumed& event) {
|
2022-06-04 21:53:13 +01:00
|
|
|
if (this->lastTargetState != TargetState::RUNNING) {
|
|
|
|
|
this->lastTargetState = TargetState::RUNNING;
|
|
|
|
|
emit this->targetStateUpdated(TargetState::RUNNING);
|
|
|
|
|
}
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-04-08 22:14:37 +01:00
|
|
|
void InsightWorker::onTargetResetEvent(const Events::TargetReset& event) {
|
2022-05-01 21:17:55 +01:00
|
|
|
try {
|
2022-05-02 18:56:17 +01:00
|
|
|
if (this->targetControllerConsole.getTargetState() != TargetState::STOPPED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 21:53:13 +01:00
|
|
|
if (this->lastTargetState != TargetState::STOPPED) {
|
|
|
|
|
this->lastTargetState = TargetState::STOPPED;
|
|
|
|
|
emit this->targetStateUpdated(TargetState::STOPPED);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 00:51:48 +01:00
|
|
|
emit this->targetReset();
|
2022-05-01 21:17:55 +01:00
|
|
|
|
|
|
|
|
} catch (const Exceptions::Exception& exception) {
|
|
|
|
|
Logger::debug("Error handling TargetReset event - " + exception.getMessage());
|
|
|
|
|
}
|
2022-04-08 22:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
void InsightWorker::onTargetControllerStateChangedEvent(const Events::TargetControllerStateChanged& event) {
|
2022-04-09 15:57:24 +01:00
|
|
|
using TargetController::TargetControllerState;
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
if (event.state == TargetControllerState::SUSPENDED) {
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->targetControllerSuspended();
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
} else if (event.state == TargetControllerState::ACTIVE) {
|
2022-02-05 15:32:08 +00:00
|
|
|
try {
|
|
|
|
|
emit this->targetControllerResumed(this->targetControllerConsole.getTargetDescriptor());
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Insight resume failed - " + exception.getMessage());
|
|
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-06-05 17:00:56 +01:00
|
|
|
void InsightWorker::onProgrammingModeEnabledEvent(const Events::ProgrammingModeEnabled& event) {
|
|
|
|
|
emit this->programmingModeEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InsightWorker::onProgrammingModeDisabledEvent(const Events::ProgrammingModeDisabled& event) {
|
|
|
|
|
emit this->programmingModeDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::executeTasks() {
|
|
|
|
|
auto task = std::optional<InsightWorkerTask*>();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
while ((task = this->getQueuedTask()).has_value()) {
|
|
|
|
|
task.value()->execute(this->targetControllerConsole);
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|