Ref T515, background validation for models (in simulator context)

This commit is contained in:
Klaus Basan
2019-01-20 19:28:34 +01:00
committed by Mat Sutcliffe
parent 4be8ff9e3d
commit 0bcd47b277
2 changed files with 205 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "backgroundvalidation.h"
#include "blackmisc/simulation/aircraftmodelutils.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/threadutils.h"
#include "blackmisc/eventloop.h"
#include "blackmisc/logmessage.h"
#include <QDateTime>
using namespace BlackMisc::Network;
using namespace BlackMisc::Simulation::Data;
namespace BlackMisc
{
namespace Simulation
{
const CLogCategoryList &CBackgroundValidation::getLogCategories()
{
static const CLogCategoryList cats({ CLogCategory::worker(), CLogCategory::modelSetCache() });
return cats;
}
CBackgroundValidation::CBackgroundValidation(QObject *owner) :
CContinuousWorker(owner, "Background validation")
{
connect(&m_updateTimer, &QTimer::timeout, this, &CBackgroundValidation::doWork);
m_updateTimer.setInterval(60 * 1000);
}
void CBackgroundValidation::setCurrentSimulator(const CSimulatorInfo &simulator)
{
QWriteLocker l(&m_lock);
m_simulator = simulator;
}
bool CBackgroundValidation::wasAlreadyChecked(const CSimulatorInfo &simulator) const
{
QReadLocker l(&m_lock);
return m_checkedSimulatorMsgs.contains(simulator);
}
void CBackgroundValidation::resetAlreadyChecked(const CSimulatorInfo &simulator)
{
QWriteLocker l(&m_lock);
m_checkedSimulatorMsgs.remove(simulator);
}
CSimulatorInfo CBackgroundValidation::getCurrentSimulator() const
{
QReadLocker l(&m_lock);
return m_simulator;
}
bool CBackgroundValidation::triggerValidation(const CSimulatorInfo &simulator)
{
{
QWriteLocker l(&m_lock);
if (m_inWork) { return false; }
m_simulator = simulator;
m_checkedSimulatorMsgs.remove(simulator);
}
QTimer::singleShot(0, this, &CBackgroundValidation::doWork);
return true;
}
void CBackgroundValidation::doWork()
{
if (m_inWork) { return; }
m_inWork = true;
emit this->validating(true);
CAircraftModelList valid;
CAircraftModelList invalid;
CAircraftModelList models;
CStatusMessageList msgs;
bool wasStopped = false;
bool validated = false;
const CSimulatorInfo simulator = this->getCurrentSimulator();
const qint64 started = QDateTime::currentMSecsSinceEpoch();
do
{
if (!simulator.isSingleSimulator()) { break; }
if (this->wasAlreadyChecked(simulator)) { break; }
const CAircraftMatcherSetup setup = m_matchingSettings.get();
if (!setup.doVerificationAtStartup()) { break; }
models = m_modelSets.getCachedModels(simulator);
msgs = CAircraftModelUtilities::validateModelFiles(models, valid, invalid, false, 25, wasStopped);
const qint64 deltaTimeMs = QDateTime::currentMSecsSinceEpoch() - started;
msgs.push_back(CStatusMessage(this, CStatusMessage::SeverityInfo, QStringLiteral("Validated in %1ms").arg(deltaTimeMs)));
validated = true;
QWriteLocker l(&m_lock);
m_checkedSimulatorMsgs.insert(simulator, msgs);
}
while (false);
m_inWork = false;
emit this->validating(false);
if (validated)
{
emit this->validated(simulator, valid, invalid, wasStopped, msgs);
}
}
} // ns
} // ns