VariableController.cpp
1114 lines
| 44.5 KiB
| text/x-c
|
CppLexer
r235 | #include <Variable/Variable.h> | |||
r539 | #include <Variable/VariableAcquisitionWorker.h> | |||
#include <Variable/VariableCacheStrategy.h> | ||||
Alexandre Leroux
|
r771 | #include <Variable/VariableCacheStrategyFactory.h> | ||
Alexandre Leroux
|
r111 | #include <Variable/VariableController.h> | ||
Alexandre Leroux
|
r113 | #include <Variable/VariableModel.h> | ||
r539 | #include <Variable/VariableSynchronizationGroup.h> | |||
Alexandre Leroux
|
r111 | |||
Alexandre Leroux
|
r166 | #include <Data/DataProviderParameters.h> | ||
#include <Data/IDataProvider.h> | ||||
#include <Data/IDataSeries.h> | ||||
r626 | #include <Data/VariableRequest.h> | |||
r193 | #include <Time/TimeController.h> | |||
Alexandre Leroux
|
r166 | |||
r849 | #include <QDataStream> | |||
Alexandre Leroux
|
r111 | #include <QMutex> | ||
#include <QThread> | ||||
Alexandre Leroux
|
r376 | #include <QUuid> | ||
r304 | #include <QtCore/QItemSelectionModel> | |||
Alexandre Leroux
|
r111 | |||
r626 | #include <deque> | |||
r539 | #include <set> | |||
r225 | #include <unordered_map> | |||
Alexandre Leroux
|
r111 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
r539 | namespace { | |||
r545 | SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange, | |||
r539 | const SqpRange &oldGraphRange) | |||
{ | ||||
r545 | auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange); | |||
r539 | ||||
auto varRangeRequested = varRange; | ||||
switch (zoomType) { | ||||
case AcquisitionZoomType::ZoomIn: { | ||||
r545 | auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart; | |||
auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd; | ||||
r539 | varRangeRequested.m_TStart += deltaLeft; | |||
varRangeRequested.m_TEnd -= deltaRight; | ||||
break; | ||||
} | ||||
case AcquisitionZoomType::ZoomOut: { | ||||
r545 | auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart; | |||
auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd; | ||||
r539 | varRangeRequested.m_TStart -= deltaLeft; | |||
varRangeRequested.m_TEnd += deltaRight; | ||||
break; | ||||
} | ||||
case AcquisitionZoomType::PanRight: { | ||||
r809 | auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart; | |||
r545 | auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd; | |||
r809 | varRangeRequested.m_TStart += deltaLeft; | |||
r539 | varRangeRequested.m_TEnd += deltaRight; | |||
break; | ||||
} | ||||
case AcquisitionZoomType::PanLeft: { | ||||
r545 | auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart; | |||
r809 | auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd; | |||
r539 | varRangeRequested.m_TStart -= deltaLeft; | |||
r809 | varRangeRequested.m_TEnd -= deltaRight; | |||
r539 | break; | |||
} | ||||
case AcquisitionZoomType::Unknown: { | ||||
qCCritical(LOG_VariableController()) | ||||
<< VariableController::tr("Impossible to synchronize: zoom type unknown"); | ||||
break; | ||||
} | ||||
default: | ||||
qCCritical(LOG_VariableController()) << VariableController::tr( | ||||
"Impossible to synchronize: zoom type not take into account"); | ||||
// No action | ||||
break; | ||||
} | ||||
return varRangeRequested; | ||||
} | ||||
} | ||||
r825 | enum class VariableRequestHandlerState { OFF, RUNNING, PENDING }; | |||
struct VariableRequestHandler { | ||||
VariableRequestHandler() | ||||
{ | ||||
m_CanUpdate = false; | ||||
m_State = VariableRequestHandlerState::OFF; | ||||
} | ||||
QUuid m_VarId; | ||||
VariableRequest m_RunningVarRequest; | ||||
VariableRequest m_PendingVarRequest; | ||||
VariableRequestHandlerState m_State; | ||||
bool m_CanUpdate; | ||||
}; | ||||
Alexandre Leroux
|
r111 | struct VariableController::VariableControllerPrivate { | ||
Alexandre Leroux
|
r159 | explicit VariableControllerPrivate(VariableController *parent) | ||
r225 | : m_WorkingMutex{}, | |||
m_VariableModel{new VariableModel{parent}}, | ||||
r304 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, | |||
Alexandre Leroux
|
r771 | // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()}, | ||
m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy( | ||||
CacheStrategy::SingleThreshold)}, | ||||
r571 | m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()}, | |||
q{parent} | ||||
r539 | { | |||
m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread); | ||||
m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread"); | ||||
} | ||||
virtual ~VariableControllerPrivate() | ||||
Alexandre Leroux
|
r111 | { | ||
r539 | qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction"); | |||
m_VariableAcquisitionWorkerThread.quit(); | ||||
m_VariableAcquisitionWorkerThread.wait(); | ||||
Alexandre Leroux
|
r111 | } | ||
r539 | ||||
r626 | void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested, | |||
QUuid varRequestId); | ||||
r539 | ||||
std::shared_ptr<Variable> findVariable(QUuid vIdentifier); | ||||
std::shared_ptr<IDataSeries> | ||||
retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector); | ||||
void registerProvider(std::shared_ptr<IDataProvider> provider); | ||||
r626 | void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest); | |||
QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries); | ||||
r825 | void updateVariables(QUuid varRequestId); | |||
r626 | void updateVariableRequest(QUuid varRequestId); | |||
void cancelVariableRequest(QUuid varRequestId); | ||||
r825 | void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest); | |||
r1340 | bool hasPendingDownloads(); | |||
Alexandre Leroux
|
r1273 | template <typename VariableIterator> | ||
void desynchronize(VariableIterator variableIt, const QUuid &syncGroupId); | ||||
Alexandre Leroux
|
r111 | QMutex m_WorkingMutex; | ||
Alexandre Leroux
|
r159 | /// Variable model. The VariableController has the ownership | ||
VariableModel *m_VariableModel; | ||||
r304 | QItemSelectionModel *m_VariableSelectionModel; | |||
r193 | ||||
r225 | ||||
r195 | TimeController *m_TimeController{nullptr}; | |||
r539 | std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy; | |||
std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker; | ||||
QThread m_VariableAcquisitionWorkerThread; | ||||
r235 | ||||
std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | ||||
m_VariableToProviderMap; | ||||
Alexandre Leroux
|
r420 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; | ||
r539 | std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> > | |||
m_GroupIdToVariableSynchronizationGroupMap; | ||||
std::map<QUuid, QUuid> m_VariableIdGroupIdMap; | ||||
std::set<std::shared_ptr<IDataProvider> > m_ProviderSet; | ||||
r571 | ||||
r825 | std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds; | |||
std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler; | ||||
r626 | ||||
r571 | VariableController *q; | |||
Alexandre Leroux
|
r111 | }; | ||
r539 | ||||
Alexandre Leroux
|
r111 | VariableController::VariableController(QObject *parent) | ||
Alexandre Leroux
|
r159 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
Alexandre Leroux
|
r111 | { | ||
r365 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
r422 | ||||
connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, | ||||
&VariableController::onAbortProgressRequested); | ||||
r539 | ||||
r761 | connect(impl->m_VariableAcquisitionWorker.get(), | |||
&VariableAcquisitionWorker::variableCanceledRequested, this, | ||||
&VariableController::onAbortAcquisitionRequested); | ||||
r539 | connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this, | |||
&VariableController::onDataProvided); | ||||
connect(impl->m_VariableAcquisitionWorker.get(), | ||||
&VariableAcquisitionWorker::variableRequestInProgress, this, | ||||
&VariableController::onVariableRetrieveDataInProgress); | ||||
r761 | ||||
r539 | connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started, | |||
impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize); | ||||
connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished, | ||||
impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize); | ||||
r933 | connect(impl->m_VariableModel, &VariableModel::requestVariableRangeUpdate, this, | |||
&VariableController::onUpdateDateTime); | ||||
r539 | ||||
impl->m_VariableAcquisitionWorkerThread.start(); | ||||
Alexandre Leroux
|
r111 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r365 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |||
<< QThread::currentThread(); | ||||
Alexandre Leroux
|
r111 | this->waitForFinish(); | ||
} | ||||
Alexandre Leroux
|
r166 | VariableModel *VariableController::variableModel() noexcept | ||
Alexandre Leroux
|
r113 | { | ||
Alexandre Leroux
|
r166 | return impl->m_VariableModel; | ||
Alexandre Leroux
|
r113 | } | ||
r304 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |||
{ | ||||
return impl->m_VariableSelectionModel; | ||||
} | ||||
r193 | void VariableController::setTimeController(TimeController *timeController) noexcept | |||
{ | ||||
impl->m_TimeController = timeController; | ||||
} | ||||
Alexandre Leroux
|
r706 | std::shared_ptr<Variable> | ||
VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept | ||||
{ | ||||
Alexandre Leroux
|
r710 | if (impl->m_VariableModel->containsVariable(variable)) { | ||
// Clones variable | ||||
auto duplicate = variable->clone(); | ||||
Alexandre Leroux
|
r707 | |||
Alexandre Leroux
|
r710 | // Adds clone to model | ||
impl->m_VariableModel->addVariable(duplicate); | ||||
Alexandre Leroux
|
r708 | |||
Alexandre Leroux
|
r711 | // Generates clone identifier | ||
impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid(); | ||||
Alexandre Leroux
|
r712 | // Registers provider | ||
auto variableProvider = impl->m_VariableToProviderMap.at(variable); | ||||
auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr; | ||||
impl->m_VariableToProviderMap[duplicate] = duplicateProvider; | ||||
if (duplicateProvider) { | ||||
impl->registerProvider(duplicateProvider); | ||||
} | ||||
Alexandre Leroux
|
r710 | return duplicate; | ||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Can't create duplicate of variable %1: variable not registered in the model") | ||||
.arg(variable->name()); | ||||
return nullptr; | ||||
} | ||||
Alexandre Leroux
|
r706 | } | ||
Alexandre Leroux
|
r329 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | ||||
return; | ||||
} | ||||
Alexandre Leroux
|
r335 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | ||
// make some treatments before the deletion | ||||
emit variableAboutToBeDeleted(variable); | ||||
Alexandre Leroux
|
r330 | |||
Alexandre Leroux
|
r1273 | auto variableIt = impl->m_VariableToIdentifierMap.find(variable); | ||
Q_ASSERT(variableIt != impl->m_VariableToIdentifierMap.cend()); | ||||
auto variableId = variableIt->second; | ||||
// Removes variable's handler | ||||
impl->m_VarIdToVarRequestHandler.erase(variableId); | ||||
// Desynchronizes variable (if the variable is in a sync group) | ||||
auto syncGroupIt = impl->m_VariableIdGroupIdMap.find(variableId); | ||||
if (syncGroupIt != impl->m_VariableIdGroupIdMap.cend()) { | ||||
impl->desynchronize(variableIt, syncGroupIt->second); | ||||
} | ||||
Alexandre Leroux
|
r420 | // Deletes identifier | ||
Alexandre Leroux
|
r1273 | impl->m_VariableToIdentifierMap.erase(variableIt); | ||
Alexandre Leroux
|
r420 | |||
Alexandre Leroux
|
r330 | // Deletes provider | ||
auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); | ||||
qCDebug(LOG_VariableController()) | ||||
<< tr("Number of providers deleted for variable %1: %2") | ||||
.arg(variable->name(), QString::number(nbProvidersDeleted)); | ||||
Alexandre Leroux
|
r329 | |||
Alexandre Leroux
|
r331 | |||
Alexandre Leroux
|
r332 | // Deletes from model | ||
impl->m_VariableModel->deleteVariable(variable); | ||||
} | ||||
Alexandre Leroux
|
r329 | |||
void VariableController::deleteVariables( | ||||
const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
{ | ||||
for (auto variable : qAsConst(variables)) { | ||||
deleteVariable(variable); | ||||
} | ||||
} | ||||
r849 | QByteArray | |||
VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const | ||||
{ | ||||
auto encodedData = QByteArray{}; | ||||
QVariantList ids; | ||||
for (auto &var : variables) { | ||||
auto itVar = impl->m_VariableToIdentifierMap.find(var); | ||||
if (itVar == impl->m_VariableToIdentifierMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to find the data for an unknown variable."); | ||||
} | ||||
ids << itVar->second.toByteArray(); | ||||
} | ||||
QDataStream stream{&encodedData, QIODevice::WriteOnly}; | ||||
stream << ids; | ||||
return encodedData; | ||||
} | ||||
QList<std::shared_ptr<Variable> > | ||||
VariableController::variablesForMimeData(const QByteArray &mimeData) const | ||||
{ | ||||
auto variables = QList<std::shared_ptr<Variable> >{}; | ||||
QDataStream stream{mimeData}; | ||||
QVariantList ids; | ||||
stream >> ids; | ||||
for (auto id : ids) { | ||||
r868 | auto uuid = QUuid{id.toByteArray()}; | |||
r849 | auto var = impl->findVariable(uuid); | |||
variables << var; | ||||
} | ||||
return variables; | ||||
} | ||||
r590 | std::shared_ptr<Variable> | |||
VariableController::createVariable(const QString &name, const QVariantHash &metadata, | ||||
std::shared_ptr<IDataProvider> provider) noexcept | ||||
Alexandre Leroux
|
r152 | { | ||
r193 | if (!impl->m_TimeController) { | |||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to create variable: The time controller is null"); | ||||
r590 | return nullptr; | |||
r193 | } | |||
r539 | auto range = impl->m_TimeController->dateTime(); | |||
Alexandre Leroux
|
r410 | |||
r756 | if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) { | |||
r825 | auto varId = QUuid::createUuid(); | |||
// Create the handler | ||||
auto varRequestHandler = std::make_unique<VariableRequestHandler>(); | ||||
varRequestHandler->m_VarId = varId; | ||||
impl->m_VarIdToVarRequestHandler.insert( | ||||
std::make_pair(varId, std::move(varRequestHandler))); | ||||
r225 | ||||
r235 | // store the provider | |||
r539 | impl->registerProvider(provider); | |||
// Associate the provider | ||||
r235 | impl->m_VariableToProviderMap[newVariable] = provider; | |||
r825 | impl->m_VariableToIdentifierMap[newVariable] = varId; | |||
r318 | ||||
r825 | this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false); | |||
r318 | ||||
r825 | // auto varRequestId = QUuid::createUuid(); | |||
// qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId; | ||||
// impl->processRequest(newVariable, range, varRequestId); | ||||
// impl->updateVariableRequest(varRequestId); | ||||
r590 | ||||
r1288 | emit variableAdded(newVariable); | |||
r590 | return newVariable; | |||
Alexandre Leroux
|
r166 | } | ||
Alexandre Leroux
|
r961 | |||
qCCritical(LOG_VariableController()) << tr("Impossible to create variable"); | ||||
return nullptr; | ||||
Alexandre Leroux
|
r152 | } | ||
r512 | void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) | |||
r304 | { | |||
r811 | // NOTE: Even if acquisition request is aborting, the graphe range will be changed | |||
r365 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" | |||
<< QThread::currentThread()->objectName(); | ||||
r304 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); | |||
r831 | // NOTE we only permit the time modification for one variable | |||
// DEPRECATED | ||||
// auto variables = QVector<std::shared_ptr<Variable> >{}; | ||||
// for (const auto &selectedRow : qAsConst(selectedRows)) { | ||||
// if (auto selectedVariable = | ||||
// impl->m_VariableModel->variable(selectedRow.row())) { | ||||
// variables << selectedVariable; | ||||
// // notify that rescale operation has to be done | ||||
// emit rangeChanged(selectedVariable, dateTime); | ||||
// } | ||||
// } | ||||
// if (!variables.isEmpty()) { | ||||
// this->onRequestDataLoading(variables, dateTime, synchro); | ||||
// } | ||||
r830 | if (selectedRows.size() == 1) { | |||
r831 | ||||
r830 | if (auto selectedVariable | |||
= impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) { | ||||
r933 | onUpdateDateTime(selectedVariable, dateTime); | |||
r304 | } | |||
} | ||||
r830 | else if (selectedRows.size() > 1) { | |||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to set time for more than 1 variable in the same time"); | ||||
} | ||||
else { | ||||
qCWarning(LOG_VariableController()) | ||||
<< tr("There is no variable selected to set the time one"); | ||||
r811 | } | |||
r304 | } | |||
r933 | void VariableController::onUpdateDateTime(std::shared_ptr<Variable> variable, | |||
const SqpRange &dateTime) | ||||
{ | ||||
auto itVar = impl->m_VariableToIdentifierMap.find(variable); | ||||
if (itVar == impl->m_VariableToIdentifierMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to onDateTimeOnSelection request for unknown variable"); | ||||
return; | ||||
} | ||||
// notify that rescale operation has to be done | ||||
emit rangeChanged(variable, dateTime); | ||||
auto synchro | ||||
= impl->m_VariableIdGroupIdMap.find(itVar->second) != impl->m_VariableIdGroupIdMap.cend(); | ||||
this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{variable}, dateTime, synchro); | ||||
} | ||||
r539 | void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested, | |||
const SqpRange &cacheRangeRequested, | ||||
QVector<AcquisitionDataPacket> dataAcquired) | ||||
r401 | { | |||
r828 | qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread(); | |||
r626 | auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired); | |||
auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries); | ||||
if (!varRequestId.isNull()) { | ||||
r825 | impl->updateVariables(varRequestId); | |||
r539 | } | |||
} | ||||
r401 | ||||
r539 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) | |||
{ | ||||
r751 | qCDebug(LOG_VariableController()) | |||
<< "TORM: variableController::onVariableRetrieveDataInProgress" | ||||
<< QThread::currentThread()->objectName() << progress; | ||||
r546 | if (auto var = impl->findVariable(identifier)) { | |||
r539 | impl->m_VariableModel->setDataProgress(var, progress); | |||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to notify progression of a null variable"); | ||||
r401 | } | |||
} | ||||
r422 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) | |||
{ | ||||
r828 | qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested" | |||
<< QThread::currentThread()->objectName() << variable->name(); | ||||
r827 | ||||
auto itVar = impl->m_VariableToIdentifierMap.find(variable); | ||||
if (itVar == impl->m_VariableToIdentifierMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to onAbortProgressRequested request for unknown variable"); | ||||
return; | ||||
} | ||||
auto varId = itVar->second; | ||||
auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId); | ||||
if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to onAbortProgressRequested for variable with unknown handler"); | ||||
return; | ||||
} | ||||
auto varHandler = itVarHandler->second.get(); | ||||
// case where a variable has a running request | ||||
if (varHandler->m_State != VariableRequestHandlerState::OFF) { | ||||
impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId); | ||||
} | ||||
r422 | } | |||
r761 | void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier) | |||
{ | ||||
qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested" | ||||
<< QThread::currentThread()->objectName() << vIdentifier; | ||||
if (auto var = impl->findVariable(vIdentifier)) { | ||||
this->onAbortProgressRequested(var); | ||||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to abort Acquisition Requestof a null variable"); | ||||
} | ||||
} | ||||
r539 | void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId) | |||
{ | ||||
r540 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId" | |||
<< QThread::currentThread()->objectName() | ||||
<< synchronizationGroupId; | ||||
r539 | auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>(); | |||
impl->m_GroupIdToVariableSynchronizationGroupMap.insert( | ||||
std::make_pair(synchronizationGroupId, vSynchroGroup)); | ||||
} | ||||
void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId) | ||||
{ | ||||
impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId); | ||||
} | ||||
r540 | void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable, | |||
QUuid synchronizationGroupId) | ||||
{ | ||||
qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized" | ||||
<< synchronizationGroupId; | ||||
r546 | auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable); | |||
if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) { | ||||
auto groupIdToVSGIt | ||||
r540 | = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId); | |||
r546 | if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) { | |||
r540 | impl->m_VariableIdGroupIdMap.insert( | |||
r546 | std::make_pair(varToVarIdIt->second, synchronizationGroupId)); | |||
groupIdToVSGIt->second->addVariableId(varToVarIdIt->second); | ||||
r540 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to synchronize a variable with an unknown sycnhronization group") | ||||
<< variable->name(); | ||||
} | ||||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to synchronize a variable with no identifier") << variable->name(); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r737 | void VariableController::desynchronize(std::shared_ptr<Variable> variable, | ||
QUuid synchronizationGroupId) | ||||
{ | ||||
// Gets variable id | ||||
auto variableIt = impl->m_VariableToIdentifierMap.find(variable); | ||||
if (variableIt == impl->m_VariableToIdentifierMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Can't desynchronize variable %1: variable identifier not found") | ||||
.arg(variable->name()); | ||||
return; | ||||
} | ||||
Alexandre Leroux
|
r1273 | impl->desynchronize(variableIt, synchronizationGroupId); | ||
Alexandre Leroux
|
r737 | } | ||
r235 | ||||
r539 | void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, | |||
r811 | const SqpRange &range, bool synchronise) | |||
r235 | { | |||
r825 | // variables is assumed synchronized | |||
// TODO: Asser variables synchronization | ||||
r235 | // we want to load data of the variable for the dateTime. | |||
r825 | if (variables.isEmpty()) { | |||
return; | ||||
} | ||||
r235 | ||||
r626 | auto varRequestId = QUuid::createUuid(); | |||
r828 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" | |||
r831 | << QThread::currentThread()->objectName() << varRequestId | |||
<< range << synchronise; | ||||
r626 | ||||
r825 | if (!synchronise) { | |||
auto varIds = std::list<QUuid>{}; | ||||
for (const auto &var : variables) { | ||||
auto vId = impl->m_VariableToIdentifierMap.at(var); | ||||
varIds.push_back(vId); | ||||
} | ||||
impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds)); | ||||
for (const auto &var : variables) { | ||||
r835 | qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId | |||
<< varIds.size(); | ||||
r825 | impl->processRequest(var, range, varRequestId); | |||
} | ||||
r539 | } | |||
r825 | else { | |||
auto vId = impl->m_VariableToIdentifierMap.at(variables.first()); | ||||
auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId); | ||||
if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) { | ||||
auto groupId = varIdToGroupIdIt->second; | ||||
auto vSynchronizationGroup | ||||
= impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId); | ||||
auto vSyncIds = vSynchronizationGroup->getIds(); | ||||
r235 | ||||
r825 | auto varIds = std::list<QUuid>{}; | |||
for (auto vId : vSyncIds) { | ||||
varIds.push_back(vId); | ||||
r539 | } | |||
r825 | impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds)); | |||
r539 | ||||
for (auto vId : vSyncIds) { | ||||
auto var = impl->findVariable(vId); | ||||
r540 | ||||
// Don't process already processed var | ||||
r825 | if (var != nullptr) { | |||
r828 | qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name() | |||
<< varRequestId; | ||||
r825 | auto vSyncRangeRequested | |||
= variables.contains(var) | ||||
? range | ||||
: computeSynchroRangeRequested(var->range(), range, | ||||
variables.first()->range()); | ||||
qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested; | ||||
impl->processRequest(var, vSyncRangeRequested, varRequestId); | ||||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
r540 | ||||
r825 | << tr("Impossible to synchronize a null variable"); | |||
r539 | } | |||
} | ||||
r298 | } | |||
r235 | } | |||
r626 | ||||
r825 | impl->updateVariables(varRequestId); | |||
r235 | } | |||
Alexandre Leroux
|
r111 | void VariableController::initialize() | ||
{ | ||||
qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); | ||||
impl->m_WorkingMutex.lock(); | ||||
qCDebug(LOG_VariableController()) << tr("VariableController init END"); | ||||
} | ||||
void VariableController::finalize() | ||||
{ | ||||
impl->m_WorkingMutex.unlock(); | ||||
} | ||||
void VariableController::waitForFinish() | ||||
{ | ||||
QMutexLocker locker{&impl->m_WorkingMutex}; | ||||
} | ||||
r529 | ||||
r1340 | bool VariableController::hasPendingDownloads() | |||
{ | ||||
return impl->hasPendingDownloads(); | ||||
} | ||||
r539 | AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange) | |||
{ | ||||
// t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd | ||||
auto zoomType = AcquisitionZoomType::Unknown; | ||||
if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) { | ||||
r815 | qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut"; | |||
r539 | zoomType = AcquisitionZoomType::ZoomOut; | |||
} | ||||
else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) { | ||||
r815 | qCDebug(LOG_VariableController()) << "zoomtype: PanRight"; | |||
r539 | zoomType = AcquisitionZoomType::PanRight; | |||
} | ||||
else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) { | ||||
r815 | qCDebug(LOG_VariableController()) << "zoomtype: PanLeft"; | |||
r539 | zoomType = AcquisitionZoomType::PanLeft; | |||
} | ||||
r1139 | else if (range.m_TStart >= oldRange.m_TStart && oldRange.m_TEnd >= range.m_TEnd) { | |||
r815 | qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn"; | |||
r539 | zoomType = AcquisitionZoomType::ZoomIn; | |||
} | ||||
else { | ||||
r815 | qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected"; | |||
r539 | } | |||
return zoomType; | ||||
} | ||||
r529 | ||||
r539 | void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var, | |||
r626 | const SqpRange &rangeRequested, | |||
QUuid varRequestId) | ||||
r529 | { | |||
r825 | auto itVar = m_VariableToIdentifierMap.find(var); | |||
if (itVar == m_VariableToIdentifierMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to process request for unknown variable"); | ||||
return; | ||||
} | ||||
r626 | ||||
r825 | auto varId = itVar->second; | |||
r807 | ||||
r825 | auto itVarHandler = m_VarIdToVarRequestHandler.find(varId); | |||
if (itVarHandler == m_VarIdToVarRequestHandler.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to process request for variable with unknown handler"); | ||||
return; | ||||
} | ||||
r807 | ||||
r825 | auto oldRange = var->range(); | |||
r807 | ||||
r825 | auto varHandler = itVarHandler->second.get(); | |||
r807 | ||||
r825 | if (varHandler->m_State != VariableRequestHandlerState::OFF) { | |||
oldRange = varHandler->m_RunningVarRequest.m_RangeRequested; | ||||
} | ||||
r626 | ||||
r825 | auto varRequest = VariableRequest{}; | |||
varRequest.m_VariableGroupId = varRequestId; | ||||
auto varStrategyRangesRequested | ||||
= m_VariableCacheStrategy->computeRange(oldRange, rangeRequested); | ||||
varRequest.m_RangeRequested = varStrategyRangesRequested.first; | ||||
varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second; | ||||
switch (varHandler->m_State) { | ||||
case VariableRequestHandlerState::OFF: { | ||||
r828 | qCDebug(LOG_VariableController()) << tr("Process Request OFF") | |||
<< varRequest.m_RangeRequested | ||||
<< varRequest.m_CacheRangeRequested; | ||||
r825 | varHandler->m_RunningVarRequest = varRequest; | |||
varHandler->m_State = VariableRequestHandlerState::RUNNING; | ||||
executeVarRequest(var, varRequest); | ||||
break; | ||||
r529 | } | |||
r825 | case VariableRequestHandlerState::RUNNING: { | |||
r828 | qCDebug(LOG_VariableController()) << tr("Process Request RUNNING") | |||
<< varRequest.m_RangeRequested | ||||
<< varRequest.m_CacheRangeRequested; | ||||
r825 | varHandler->m_State = VariableRequestHandlerState::PENDING; | |||
varHandler->m_PendingVarRequest = varRequest; | ||||
break; | ||||
} | ||||
case VariableRequestHandlerState::PENDING: { | ||||
r828 | qCDebug(LOG_VariableController()) << tr("Process Request PENDING") | |||
<< varRequest.m_RangeRequested | ||||
<< varRequest.m_CacheRangeRequested; | ||||
r825 | auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId; | |||
cancelVariableRequest(variableGroupIdToCancel); | ||||
r832 | // Cancel variable can make state downgrade | |||
varHandler->m_State = VariableRequestHandlerState::PENDING; | ||||
varHandler->m_PendingVarRequest = varRequest; | ||||
r825 | ||||
break; | ||||
r571 | } | |||
r825 | default: | |||
qCCritical(LOG_VariableController()) | ||||
<< QObject::tr("Unknown VariableRequestHandlerState"); | ||||
r529 | } | |||
r539 | } | |||
std::shared_ptr<Variable> | ||||
VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier) | ||||
{ | ||||
std::shared_ptr<Variable> var; | ||||
auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; }; | ||||
auto end = m_VariableToIdentifierMap.cend(); | ||||
auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply); | ||||
if (it != end) { | ||||
var = it->first; | ||||
} | ||||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to find the variable with the identifier: ") << vIdentifier; | ||||
} | ||||
r529 | ||||
r539 | return var; | |||
} | ||||
std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries( | ||||
const QVector<AcquisitionDataPacket> acqDataPacketVector) | ||||
{ | ||||
r542 | qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size") | |||
<< acqDataPacketVector.size(); | ||||
r539 | std::shared_ptr<IDataSeries> dataSeries; | |||
if (!acqDataPacketVector.isEmpty()) { | ||||
dataSeries = acqDataPacketVector[0].m_DateSeries; | ||||
for (int i = 1; i < acqDataPacketVector.size(); ++i) { | ||||
dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get()); | ||||
} | ||||
} | ||||
r542 | qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END") | |||
<< acqDataPacketVector.size(); | ||||
r539 | return dataSeries; | |||
} | ||||
void VariableController::VariableControllerPrivate::registerProvider( | ||||
std::shared_ptr<IDataProvider> provider) | ||||
{ | ||||
if (m_ProviderSet.find(provider) == m_ProviderSet.end()) { | ||||
r542 | qCDebug(LOG_VariableController()) << tr("Registering of a new provider") | |||
<< provider->objectName(); | ||||
r539 | m_ProviderSet.insert(provider); | |||
connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(), | ||||
&VariableAcquisitionWorker::onVariableDataAcquired); | ||||
connect(provider.get(), &IDataProvider::dataProvidedProgress, | ||||
m_VariableAcquisitionWorker.get(), | ||||
&VariableAcquisitionWorker::onVariableRetrieveDataInProgress); | ||||
r761 | connect(provider.get(), &IDataProvider::dataProvidedFailed, | |||
m_VariableAcquisitionWorker.get(), | ||||
&VariableAcquisitionWorker::onVariableAcquisitionFailed); | ||||
r539 | } | |||
else { | ||||
r540 | qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists "); | |||
r539 | } | |||
r529 | } | |||
r626 | ||||
r825 | QUuid VariableController::VariableControllerPrivate::acceptVariableRequest( | |||
QUuid varId, std::shared_ptr<IDataSeries> dataSeries) | ||||
r626 | { | |||
r825 | auto itVarHandler = m_VarIdToVarRequestHandler.find(varId); | |||
if (itVarHandler == m_VarIdToVarRequestHandler.cend()) { | ||||
return QUuid(); | ||||
r626 | } | |||
r825 | auto varHandler = itVarHandler->second.get(); | |||
if (varHandler->m_State == VariableRequestHandlerState::OFF) { | ||||
r832 | qCCritical(LOG_VariableController()) | |||
<< tr("acceptVariableRequest impossible on a variable with OFF state"); | ||||
r626 | } | |||
r825 | varHandler->m_RunningVarRequest.m_DataSeries = dataSeries; | |||
varHandler->m_CanUpdate = true; | ||||
r626 | ||||
r825 | // Element traité, on a déjà toutes les données necessaires | |||
auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId; | ||||
r828 | qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId | |||
<< m_VarGroupIdToVarIds.size(); | ||||
r626 | ||||
r825 | return varHandler->m_RunningVarRequest.m_VariableGroupId; | |||
r626 | } | |||
r825 | void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId) | |||
r626 | { | |||
r828 | qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables" | |||
<< QThread::currentThread()->objectName() << varRequestId; | ||||
r825 | ||||
auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); | ||||
if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { | ||||
r828 | qCWarning(LOG_VariableController()) | |||
<< tr("Impossible to updateVariables of unknown variables") << varRequestId; | ||||
r825 | return; | |||
} | ||||
r626 | ||||
r825 | auto &varIds = varGroupIdToVarIdsIt->second; | |||
auto varIdsEnd = varIds.end(); | ||||
bool processVariableUpdate = true; | ||||
r832 | qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables" | |||
<< varRequestId << varIds.size(); | ||||
r825 | for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate; | |||
++varIdsIt) { | ||||
auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); | ||||
if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { | ||||
processVariableUpdate &= itVarHandler->second->m_CanUpdate; | ||||
r626 | } | |||
r825 | } | |||
r626 | ||||
r825 | if (processVariableUpdate) { | |||
r828 | qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size(); | |||
r825 | for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) { | |||
auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); | ||||
if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { | ||||
if (auto var = findVariable(*varIdsIt)) { | ||||
auto &varRequest = itVarHandler->second->m_RunningVarRequest; | ||||
r626 | var->setRange(varRequest.m_RangeRequested); | |||
var->setCacheRange(varRequest.m_CacheRangeRequested); | ||||
r828 | qCDebug(LOG_VariableController()) << tr("1: onDataProvided") | |||
<< varRequest.m_RangeRequested | ||||
<< varRequest.m_CacheRangeRequested; | ||||
qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before") | ||||
<< var->nbPoints() | ||||
<< varRequest.m_DataSeries->nbPoints(); | ||||
r626 | var->mergeDataSeries(varRequest.m_DataSeries); | |||
r828 | qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after") | |||
<< var->nbPoints(); | ||||
Alexandre Leroux
|
r702 | |||
r754 | emit var->updated(); | |||
r828 | qCDebug(LOG_VariableController()) << tr("Update OK"); | |||
r626 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to update data to a null variable"); | ||||
} | ||||
} | ||||
} | ||||
r825 | updateVariableRequest(varRequestId); | |||
// cleaning varRequestId | ||||
r828 | qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId; | |||
r825 | m_VarGroupIdToVarIds.erase(varRequestId); | |||
Alexandre Leroux
|
r1214 | if (m_VarGroupIdToVarIds.empty()) { | ||
emit q->acquisitionFinished(); | ||||
} | ||||
r626 | } | |||
} | ||||
r825 | ||||
void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId) | ||||
r626 | { | |||
r825 | auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); | |||
if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { | ||||
r832 | qCCritical(LOG_VariableController()) << QObject::tr( | |||
"Impossible to updateVariableRequest since varGroupdId isn't here anymore"); | ||||
r825 | return; | |||
} | ||||
r626 | ||||
r825 | auto &varIds = varGroupIdToVarIdsIt->second; | |||
auto varIdsEnd = varIds.end(); | ||||
for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) { | ||||
auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); | ||||
if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { | ||||
r818 | ||||
r825 | auto varHandler = itVarHandler->second.get(); | |||
varHandler->m_CanUpdate = false; | ||||
switch (varHandler->m_State) { | ||||
case VariableRequestHandlerState::OFF: { | ||||
qCCritical(LOG_VariableController()) | ||||
<< QObject::tr("Impossible to update a variable with handler in OFF state"); | ||||
} break; | ||||
case VariableRequestHandlerState::RUNNING: { | ||||
varHandler->m_State = VariableRequestHandlerState::OFF; | ||||
varHandler->m_RunningVarRequest = VariableRequest{}; | ||||
break; | ||||
} | ||||
case VariableRequestHandlerState::PENDING: { | ||||
varHandler->m_State = VariableRequestHandlerState::RUNNING; | ||||
varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest; | ||||
varHandler->m_PendingVarRequest = VariableRequest{}; | ||||
auto var = findVariable(itVarHandler->first); | ||||
executeVarRequest(var, varHandler->m_RunningVarRequest); | ||||
Alexandre Leroux
|
r1270 | updateVariables(varHandler->m_RunningVarRequest.m_VariableGroupId); | ||
r825 | break; | |||
} | ||||
default: | ||||
qCCritical(LOG_VariableController()) | ||||
<< QObject::tr("Unknown VariableRequestHandlerState"); | ||||
} | ||||
r626 | } | |||
} | ||||
} | ||||
r807 | ||||
r825 | ||||
void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId) | ||||
r822 | { | |||
r828 | qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId; | |||
r825 | auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); | |||
if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { | ||||
r827 | qCCritical(LOG_VariableController()) | |||
<< tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId; | ||||
r825 | return; | |||
r822 | } | |||
r825 | auto &varIds = varGroupIdToVarIdsIt->second; | |||
auto varIdsEnd = varIds.end(); | ||||
for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) { | ||||
auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); | ||||
r827 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { | |||
r822 | ||||
r825 | auto varHandler = itVarHandler->second.get(); | |||
varHandler->m_VarId = QUuid{}; | ||||
switch (varHandler->m_State) { | ||||
case VariableRequestHandlerState::OFF: { | ||||
qCWarning(LOG_VariableController()) | ||||
<< QObject::tr("Impossible to cancel a variable with no running request"); | ||||
break; | ||||
} | ||||
case VariableRequestHandlerState::RUNNING: { | ||||
if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) { | ||||
auto var = findVariable(itVarHandler->first); | ||||
auto varProvider = m_VariableToProviderMap.at(var); | ||||
if (varProvider != nullptr) { | ||||
m_VariableAcquisitionWorker->abortProgressRequested( | ||||
itVarHandler->first); | ||||
} | ||||
r827 | m_VariableModel->setDataProgress(var, 0.0); | |||
r832 | varHandler->m_CanUpdate = false; | |||
r827 | varHandler->m_State = VariableRequestHandlerState::OFF; | |||
r825 | varHandler->m_RunningVarRequest = VariableRequest{}; | |||
r822 | } | |||
else { | ||||
r825 | // TODO: log Impossible to cancel the running variable request beacause its | |||
// varRequestId isn't not the canceled one | ||||
} | ||||
break; | ||||
} | ||||
case VariableRequestHandlerState::PENDING: { | ||||
if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) { | ||||
auto var = findVariable(itVarHandler->first); | ||||
auto varProvider = m_VariableToProviderMap.at(var); | ||||
if (varProvider != nullptr) { | ||||
m_VariableAcquisitionWorker->abortProgressRequested( | ||||
itVarHandler->first); | ||||
r822 | } | |||
r827 | m_VariableModel->setDataProgress(var, 0.0); | |||
r832 | varHandler->m_CanUpdate = false; | |||
r827 | varHandler->m_State = VariableRequestHandlerState::RUNNING; | |||
r825 | varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest; | |||
r832 | varHandler->m_PendingVarRequest = VariableRequest{}; | |||
r825 | executeVarRequest(var, varHandler->m_RunningVarRequest); | |||
} | ||||
else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) { | ||||
r827 | varHandler->m_State = VariableRequestHandlerState::RUNNING; | |||
r825 | varHandler->m_PendingVarRequest = VariableRequest{}; | |||
} | ||||
else { | ||||
// TODO: log Impossible to cancel the variable request beacause its | ||||
// varRequestId isn't not the canceled one | ||||
r822 | } | |||
break; | ||||
} | ||||
r825 | default: | |||
qCCritical(LOG_VariableController()) | ||||
<< QObject::tr("Unknown VariableRequestHandlerState"); | ||||
r822 | } | |||
} | ||||
} | ||||
r828 | qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId; | |||
r827 | m_VarGroupIdToVarIds.erase(varRequestId); | |||
Alexandre Leroux
|
r1214 | if (m_VarGroupIdToVarIds.empty()) { | ||
emit q->acquisitionFinished(); | ||||
} | ||||
r822 | } | |||
r825 | void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var, | |||
VariableRequest &varRequest) | ||||
r807 | { | |||
r828 | qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest"); | |||
r825 | ||||
Alexandre Leroux
|
r1273 | auto varIdIt = m_VariableToIdentifierMap.find(var); | ||
if (varIdIt == m_VariableToIdentifierMap.cend()) { | ||||
qCWarning(LOG_VariableController()) << tr( | ||||
"Can't execute request of a variable that is not registered (may has been deleted)"); | ||||
return; | ||||
} | ||||
auto varId = varIdIt->second; | ||||
r825 | ||||
auto varCacheRange = var->cacheRange(); | ||||
auto varCacheRangeRequested = varRequest.m_CacheRangeRequested; | ||||
auto notInCacheRangeList | ||||
= Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested); | ||||
r831 | auto inCacheRangeList | |||
= Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested); | ||||
r825 | ||||
if (!notInCacheRangeList.empty()) { | ||||
auto varProvider = m_VariableToProviderMap.at(var); | ||||
if (varProvider != nullptr) { | ||||
r828 | qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested | |||
<< varRequest.m_CacheRangeRequested; | ||||
r825 | m_VariableAcquisitionWorker->pushVariableRequest( | |||
varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested, | ||||
varRequest.m_CacheRangeRequested, | ||||
DataProviderParameters{std::move(notInCacheRangeList), var->metadata()}, | ||||
varProvider); | ||||
r807 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) | ||||
r825 | << "Impossible to provide data with a null provider"; | |||
r807 | } | |||
r825 | ||||
r831 | if (!inCacheRangeList.empty()) { | |||
emit q->updateVarDisplaying(var, inCacheRangeList.first()); | ||||
} | ||||
r807 | } | |||
else { | ||||
r825 | acceptVariableRequest(varId, | |||
var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested)); | ||||
r807 | } | |||
} | ||||
Alexandre Leroux
|
r1273 | |||
r1340 | bool VariableController::VariableControllerPrivate::hasPendingDownloads() | |||
{ | ||||
return !m_VarGroupIdToVarIds.empty(); | ||||
} | ||||
Alexandre Leroux
|
r1273 | template <typename VariableIterator> | ||
void VariableController::VariableControllerPrivate::desynchronize(VariableIterator variableIt, | ||||
const QUuid &syncGroupId) | ||||
{ | ||||
const auto &variable = variableIt->first; | ||||
const auto &variableId = variableIt->second; | ||||
// Gets synchronization group | ||||
auto groupIt = m_GroupIdToVariableSynchronizationGroupMap.find(syncGroupId); | ||||
if (groupIt == m_GroupIdToVariableSynchronizationGroupMap.cend()) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Can't desynchronize variable %1: unknown synchronization group") | ||||
.arg(variable->name()); | ||||
return; | ||||
} | ||||
// Removes variable from synchronization group | ||||
auto synchronizationGroup = groupIt->second; | ||||
synchronizationGroup->removeVariableId(variableId); | ||||
// Removes link between variable and synchronization group | ||||
m_VariableIdGroupIdMap.erase(variableId); | ||||
} | ||||