second part of PR #578

Signed-off-by: Paulchen-Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
Paulchen-Panther
2019-07-14 22:43:22 +02:00
parent ea796160af
commit 90599e820a
52 changed files with 2354 additions and 536 deletions

View File

@@ -1,7 +1,8 @@
#include <hyperion/AuthManager.h>
// util
// db
#include <db/AuthTable.h>
#include <db/MetaTable.h>
// qt
#include <QJsonObject>
@@ -9,15 +10,19 @@
AuthManager* AuthManager::manager = nullptr;
AuthManager::AuthManager(const QString& rootPath, QObject* parent)
AuthManager::AuthManager(QObject* parent)
: QObject(parent)
, _authTable(new AuthTable(rootPath, this))
, _authTable(new AuthTable(this))
, _metaTable(new MetaTable(this))
, _pendingRequests()
, _authRequired(true)
, _timer(new QTimer(this))
{
AuthManager::manager = this;
// get uuid
_uuid = _metaTable->getUUID();
// setup timer
_timer->setInterval(1000);
connect(_timer, &QTimer::timeout, this, &AuthManager::checkTimeout);
@@ -29,12 +34,12 @@ AuthManager::AuthManager(const QString& rootPath, QObject* parent)
}
}
const bool & AuthManager::isAuthRequired()
bool & AuthManager::isAuthRequired()
{
return _authRequired;
}
const bool & AuthManager::isLocalAuthRequired()
bool & AuthManager::isLocalAuthRequired()
{
return _localAuthRequired;
}
@@ -72,12 +77,12 @@ const QVector<AuthManager::AuthDefinition> AuthManager::getTokenList()
return finalVec;
}
const bool AuthManager::isUserAuthorized(const QString& user, const QString& pw)
bool AuthManager::isUserAuthorized(const QString& user, const QString& pw)
{
return _authTable->isUserAuthorized(user, pw);
}
const bool AuthManager::isTokenAuthorized(const QString& token)
bool AuthManager::isTokenAuthorized(const QString& token)
{
return _authTable->tokenExist(token);
}
@@ -93,7 +98,7 @@ void AuthManager::setNewTokenRequest(QObject* caller, const QString& comment, co
}
}
const bool AuthManager::acceptTokenRequest(const QString& id)
bool AuthManager::acceptTokenRequest(const QString& id)
{
if(_pendingRequests.contains(id))
{
@@ -106,7 +111,7 @@ const bool AuthManager::acceptTokenRequest(const QString& id)
return false;
}
const bool AuthManager::denyTokenRequest(const QString& id)
bool AuthManager::denyTokenRequest(const QString& id)
{
if(_pendingRequests.contains(id))
{
@@ -122,7 +127,7 @@ const QMap<QString, AuthManager::AuthDefinition> AuthManager::getPendingRequests
return _pendingRequests;
}
const bool AuthManager::deleteToken(const QString& id)
bool AuthManager::deleteToken(const QString& id)
{
if(_authTable->deleteToken(id))
{

View File

@@ -25,7 +25,7 @@ GrabberWrapper::GrabberWrapper(QString grabberName, Grabber * ggrabber, unsigned
connect(_timer, &QTimer::timeout, this, &GrabberWrapper::action);
// connect the image forwarding
_grabberName.startsWith("V4L")
(_grabberName.startsWith("V4L"))
? connect(this, &GrabberWrapper::systemImage, GlobalSignals::getInstance(), &GlobalSignals::setV4lImage)
: connect(this, &GrabberWrapper::systemImage, GlobalSignals::getInstance(), &GlobalSignals::setSystemImage);
}

View File

@@ -5,17 +5,9 @@
#include <unistd.h>
// QT includes
#include <QDateTime>
#include <QThread>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QCryptographicHash>
#include <QTimer>
#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QCryptographicHash>
#include <QThread>
// hyperion include
#include <hyperion/Hyperion.h>
@@ -35,9 +27,6 @@
// effect engine includes
#include <effectengine/EffectEngine.h>
// Hyperion Daemon
#include <../src/hyperiond/hyperiond.h>
// settingsManagaer
#include <hyperion/SettingsManager.h>
@@ -50,28 +39,10 @@
// Boblight
#include <boblightserver/BoblightServer.h>
Hyperion* Hyperion::_hyperion = nullptr;
Hyperion* Hyperion::initInstance( HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath)
{
if ( Hyperion::_hyperion != nullptr )
throw std::runtime_error("Hyperion::initInstance can be called only one time");
Hyperion::_hyperion = new Hyperion(daemon, instance, configFile, rootPath);
return Hyperion::_hyperion;
}
Hyperion* Hyperion::getInstance()
{
if ( Hyperion::_hyperion == nullptr )
throw std::runtime_error("Hyperion::getInstance used without call of Hyperion::initInstance before");
return Hyperion::_hyperion;
}
Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath)
: _daemon(daemon)
, _settingsManager(new SettingsManager(instance, configFile, this))
Hyperion::Hyperion(const quint8& instance)
: QObject()
, _instIndex(instance)
, _settingsManager(new SettingsManager(instance, this))
, _componentRegister(this)
, _ledString(hyperion::createLedString(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
, _ledStringClone(hyperion::createLedStringClone(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
@@ -79,21 +50,33 @@ Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString
, _muxer(_ledString.leds().size())
, _raw2ledAdjustment(hyperion::createLedColorsAdjustment(_ledString.leds().size(), getSetting(settings::COLOR).object()))
, _effectEngine(nullptr)
, _messageForwarder(new MessageForwarder(this))
, _configFile(configFile)
, _rootPath(rootPath)
// , _messageForwarder(new MessageForwarder(this))
, _log(Logger::getInstance("HYPERION"))
, _hwLedCount()
, _configHash()
, _ledGridSize(hyperion::getLedLayoutGridSize(getSetting(settings::LEDS).array()))
, _prevCompId(hyperion::COMP_INVALID)
, _ledBuffer(_ledString.leds().size(), ColorRgb::BLACK)
{
}
Hyperion::~Hyperion()
{
freeObjects(false);
}
void Hyperion::start()
{
// forward settings changed to Hyperion
connect(_settingsManager, &SettingsManager::settingsChanged, this, &Hyperion::settingsChanged);
// get newVideoMode from HyperionIManager
connect(this, &Hyperion::newVideoMode, this, &Hyperion::handleNewVideoMode);
if (!_raw2ledAdjustment->verifyAdjustments())
{
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
}
// handle hwLedCount
_hwLedCount = qMax(unsigned(getSetting(settings::DEVICE).object()["hardwareLedCount"].toInt(getLedCount())), getLedCount());
@@ -103,12 +86,14 @@ Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString
{
_ledStringColorOrder.push_back(led.colorOrder);
}
for (Led& led : _ledStringClone.leds())
{
_ledStringColorOrder.insert(_ledStringColorOrder.begin() + led.index, led.colorOrder);
}
// connect Hyperion::update with Muxer visible priority changes as muxer updates independent
connect(&_muxer, &PriorityMuxer::visiblePriorityChanged, this, &Hyperion::update);
// listens for ComponentRegister changes of COMP_ALL to perform core enable/disable actions
connect(&_componentRegister, &ComponentRegister::updatedComponentState, this, &Hyperion::updatedComponentState);
@@ -135,18 +120,6 @@ Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString
_effectEngine = new EffectEngine(this);
connect(_effectEngine, &EffectEngine::effectListUpdated, this, &Hyperion::effectListUpdated);
// setup config state checks and initial shot
checkConfigState();
if(_fsWatcher.addPath(_configFile))
QObject::connect(&_fsWatcher, &QFileSystemWatcher::fileChanged, this, &Hyperion::checkConfigState);
else
{
_cTimer = new QTimer(this);
Warning(_log,"Filesystem Observer failed for file: %s, use fallback timer", _configFile.toStdString().c_str());
connect(_cTimer, SIGNAL(timeout()), this, SLOT(checkConfigState()));
_cTimer->start(2000);
}
// initial startup effect
hyperion::handleInitialEffect(this, getSetting(settings::FGEFFECT).object());
@@ -163,13 +136,15 @@ Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString
_boblightServer = new BoblightServer(this, getSetting(settings::BOBLSERVER));
connect(this, &Hyperion::settingsChanged, _boblightServer, &BoblightServer::handleSettingsUpdate);
// set unique id
_id = QString(QCryptographicHash::hash(getConfigFileName().toLocal8Bit(),QCryptographicHash::Sha1).toHex());
// instance inited
emit started();
// enter thread event loop
}
Hyperion::~Hyperion()
void Hyperion::stop()
{
freeObjects(false);
emit finished();
thread()->quit();
}
void Hyperion::freeObjects(bool emitCloseSignal)
@@ -187,7 +162,7 @@ void Hyperion::freeObjects(bool emitCloseSignal)
delete _captureCont;
delete _effectEngine;
delete _raw2ledAdjustment;
delete _messageForwarder;
// delete _messageForwarder;
delete _settingsManager;
delete _ledDeviceWrapper;
}
@@ -238,9 +213,6 @@ void Hyperion::handleSettingsUpdate(const settings::type& type, const QJsonDocum
// handle hwLedCount update
_hwLedCount = qMax(unsigned(getSetting(settings::DEVICE).object()["hardwareLedCount"].toInt(getLedCount())), getLedCount());
// update led count in device
//_ledDeviceWrapper->setLedCount(_hwLedCount);
// change in leds are also reflected in adjustment
delete _raw2ledAdjustment;
_raw2ledAdjustment = hyperion::createLedColorsAdjustment(_ledString.leds().size(), getSetting(settings::COLOR).object());
@@ -264,17 +236,11 @@ void Hyperion::handleSettingsUpdate(const settings::type& type, const QJsonDocum
_imageProcessor->setLedString(_ledString);
}
/* // reinit led device type on change
if(_device->getActiveDevice() != dev["type"].toString("file").toLower())
{
}
// update led count
_device->setLedCount(_hwLedCount);
*/
// do always reinit until the led devices can handle dynamic changes
dev["currentLedCount"] = int(_hwLedCount); // Inject led count info
_ledDeviceWrapper->createLedDevice(dev);
}
// update once to push single color sets / adjustments/ ledlayout resizes and update ledBuffer color
update();
}
@@ -289,12 +255,6 @@ bool Hyperion::saveSettings(QJsonObject config, const bool& correct)
return _settingsManager->saveSettings(config, correct);
}
QString Hyperion::getConfigFileName() const
{
QFileInfo cF(_configFile);
return cF.fileName();
}
int Hyperion::getLatchTime() const
{
return _ledDeviceWrapper->getLatchTime();
@@ -310,40 +270,6 @@ unsigned Hyperion::getLedCount() const
return _ledString.leds().size();
}
void Hyperion::checkConfigState(QString cfile)
{
// Check config modifications
QFile f(_configFile);
if (f.open(QFile::ReadOnly))
{
QCryptographicHash hash(QCryptographicHash::Sha1);
if (hash.addData(&f))
{
if (_configHash.size() == 0)
{
_configHash = hash.result();
}
_configMod = _configHash != hash.result() ? true : false;
}
}
f.close();
if(_prevConfigMod != _configMod)
{
_prevConfigMod = _configMod;
}
// Check config writeable
QFile file(_configFile);
QFileInfo fileInfo(file);
_configWrite = fileInfo.isWritable() && fileInfo.isReadable() ? true : false;
if(_prevConfigWrite != _configWrite)
{
_prevConfigWrite = _configWrite;
}
}
void Hyperion::setSourceAutoSelectEnabled(bool enabled)
{
if(_muxer.setSourceAutoSelectEnabled(enabled))
@@ -367,6 +293,7 @@ void Hyperion::setNewComponentState(const hyperion::Components& component, const
void Hyperion::setComponentState(const hyperion::Components component, const bool state)
{
// TODO REMOVE THIS STEP
emit componentStateChanged(component, state);
}
@@ -547,7 +474,7 @@ void Hyperion::setVideoMode(const VideoMode& mode)
const VideoMode & Hyperion::getCurrentVideoMode()
{
return _daemon->getVideoMode();
return _currVideoMode;
}
const QString & Hyperion::getActiveDevice()
@@ -567,9 +494,6 @@ void Hyperion::updatedComponentState(const hyperion::Components comp, const bool
_prevCompId = comp;
_raw2ledAdjustment->setBacklightEnabled((_prevCompId != hyperion::COMP_COLOR && _prevCompId != hyperion::COMP_EFFECT));
}
if(comp == hyperion::COMP_ALL)
_muxer.setEnable(state); // first muxer to update all inputs
}
void Hyperion::update()

View File

@@ -0,0 +1,207 @@
#include <hyperion/HyperionIManager.h>
// hyperion
#include <hyperion/Hyperion.h>
#include <db/InstanceTable.h>
// qt
#include <QThread>
HyperionIManager* HyperionIManager::HIMinstance;
HyperionIManager::HyperionIManager(const QString& rootPath, QObject* parent)
: QObject(parent)
, _log(Logger::getInstance("HYPERION"))
, _instanceTable( new InstanceTable(rootPath, this) )
, _rootPath( rootPath )
{
HIMinstance = this;
qRegisterMetaType<instanceState>("instanceState");
}
Hyperion* HyperionIManager::getHyperionInstance(const quint8& instance)
{
if(_runningInstances.contains(instance))
return _runningInstances.value(instance);
Warning(_log,"The requested instance index '%d' with name '%s' isn't running, return main instance", instance, QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
return _runningInstances.value(0);
}
const QVector<QVariantMap> HyperionIManager::getInstanceData()
{
QVector<QVariantMap> instances = _instanceTable->getAllInstances();
for( auto & entry : instances)
{
// add running state
entry["running"] = _runningInstances.contains(entry["instance"].toInt());
}
return instances;
}
void HyperionIManager::startAll()
{
for(const auto entry : _instanceTable->getAllInstances(true))
{
startInstance(entry["instance"].toInt());
}
}
void HyperionIManager::stopAll()
{
// copy the instances due to loop corruption, even with .erase() return next iter
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
instance->stop();
}
}
const bool HyperionIManager::startInstance(const quint8& inst, const bool& block)
{
if(_instanceTable->instanceExist(inst))
{
if(!_runningInstances.contains(inst) && !_startQueue.contains(inst))
{
QThread* hyperionThread = new QThread();
Hyperion* hyperion = new Hyperion(inst);
hyperion->moveToThread(hyperionThread);
// setup thread management
connect(hyperionThread, &QThread::started, hyperion, &Hyperion::start);
connect(hyperionThread, &QThread::finished, hyperionThread, &QObject::deleteLater);
connect(hyperion, &Hyperion::started, HyperionIManager::getInstance(), &HyperionIManager::handleStarted);
connect(hyperion, &Hyperion::finished, HyperionIManager::getInstance(), &HyperionIManager::handleFinished);
// setup further connections
// from Hyperion
connect(hyperion, &Hyperion::settingsChanged, HyperionIManager::getInstance(), &HyperionIManager::settingsChanged);
connect(hyperion, &Hyperion::videoMode, HyperionIManager::getInstance(), &HyperionIManager::requestVideoMode);
connect(hyperion, &Hyperion::componentStateChanged, HyperionIManager::getInstance(), &HyperionIManager::componentStateChanged);
// to Hyperion
connect(HyperionIManager::getInstance(), &HyperionIManager::newVideoMode, hyperion, &Hyperion::newVideoMode);
// add to queue and start
_startQueue << inst;
hyperionThread->start();
// update db
_instanceTable->setLastUse(inst);
_instanceTable->setEnable(inst, true);
if(block)
{
while(!hyperionThread->isRunning()){};
}
return true;
}
Debug(_log,"Can't start Hyperion instance index '%d' with name '%s' it's already running or queued for start", inst, QSTRING_CSTR(_instanceTable->getNamebyIndex(inst)));
return false;
}
Debug(_log,"Can't start Hyperion instance index '%d' it doesn't exist in DB", inst);
return false;
}
const bool HyperionIManager::stopInstance(const quint8& inst, const bool& block)
{
// inst 0 can't be stopped
if(!isInstAllowed(inst))
return false;
if(_instanceTable->instanceExist(inst))
{
if(_runningInstances.contains(inst))
{
// notify a ON_STOP rather sooner than later, queued signal listener should have some time to drop the pointer before it's deleted
emit instanceStateChanged(H_ON_STOP, inst);
Hyperion* hyperion = _runningInstances.value(inst);
hyperion->stop();
// update db
_instanceTable->setEnable(inst, false);
if(block)
{
hyperion->thread()->wait(10000);
}
return true;
}
Debug(_log,"Can't stop Hyperion instance index '%d' with name '%s' it's not running'", inst, QSTRING_CSTR(_instanceTable->getNamebyIndex(inst)));
return false;
}
Debug(_log,"Can't stop Hyperion instance index '%d' it doesn't exist in DB", inst);
return false;
}
const bool HyperionIManager::createInstance(const QString& name, const bool& start)
{
quint8 inst;
if(_instanceTable->createInstance(name, inst))
{
Info(_log,"New Hyperion instance created with name '%s'",QSTRING_CSTR(name));
emit instanceStateChanged(H_CREATED, inst, name);
emit change();
if(start)
startInstance(inst);
return true;
}
return false;
}
const bool HyperionIManager::deleteInstance(const quint8& inst)
{
// inst 0 can't be deleted
if(!isInstAllowed(inst))
return false;
// stop it if required as blocking
stopInstance(inst, true);
if(_instanceTable->deleteInstance(inst))
{
Info(_log,"Hyperion instance with index '%d' has been deleted", inst);
emit instanceStateChanged(H_DELETED, inst);
emit change();
return true;
}
return false;
}
const bool HyperionIManager::saveName(const quint8& inst, const QString& name)
{
if(_instanceTable->saveName(inst, name))
{
emit change();
return true;
}
return false;
}
void HyperionIManager::handleFinished()
{
Hyperion* hyperion = qobject_cast<Hyperion*>(sender());
const quint8 & instance = hyperion->getInstanceIndex();
Info(_log,"Hyperion instance '%s' has been stopped", QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
_runningInstances.remove(instance);
hyperion->deleteLater();
emit instanceStateChanged(H_STOPPED, instance);
emit change();
}
void HyperionIManager::handleStarted()
{
Hyperion* hyperion = qobject_cast<Hyperion*>(sender());
const quint8 & instance = hyperion->getInstanceIndex();
Info(_log,"Hyperion instance '%s' has been started", QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
_startQueue.removeAll(instance);
_runningInstances.insert(instance, hyperion);
emit instanceStateChanged(H_STARTED, instance);
emit change();
}

View File

@@ -22,9 +22,9 @@ PriorityMuxer::PriorityMuxer(int ledCount)
, _activeInputs()
, _lowestPriorityInfo()
, _sourceAutoSelectEnabled(true)
, _updateTimer(new QTimer(this))
, _timer(new QTimer(this))
, _blockTimer(new QTimer(this))
, _updateTimer(new QTimer())
, _timer(new QTimer())
, _blockTimer(new QTimer())
{
// init lowest priority info
_lowestPriorityInfo.priority = PriorityMuxer::LOWEST_PRIORITY;

View File

@@ -12,13 +12,10 @@
// write config to filesystem
#include <utils/JsonUtils.h>
// hyperion
#include <hyperion/Hyperion.h>
QJsonObject SettingsManager::schemaJson;
SettingsManager::SettingsManager(const quint8& instance, const QString& configFile, Hyperion* hyperion)
: _hyperion(hyperion)
SettingsManager::SettingsManager(const quint8& instance, QObject* parent)
: QObject(parent)
, _log(Logger::getInstance("SettingsManager"))
, _sTable(new SettingsTable(instance, this))
{
@@ -41,41 +38,6 @@ SettingsManager::SettingsManager(const quint8& instance, const QString& configFi
if(!JsonUtils::readFile(":/hyperion_default.config", defaultConfig, _log))
throw std::runtime_error("Failed to read default config");
// TODO BEGIN - remove when database migration is done
Info(_log, "Selected configuration file: %s", QSTRING_CSTR(configFile));
QJsonSchemaChecker schemaCheckerT;
schemaCheckerT.setSchema(schemaJson);
if(!JsonUtils::readFile(configFile, _qconfig, _log))
throw std::runtime_error("Failed to load config!");
// validate config with schema and correct it if required
QPair<bool, bool> validate = schemaCheckerT.validate(_qconfig);
// errors in schema syntax, abort
if (!validate.second)
{
foreach (auto & schemaError, schemaCheckerT.getMessages())
Error(_log, "Schema Syntax Error: %s", QSTRING_CSTR(schemaError));
throw std::runtime_error("ERROR: Hyperion schema has syntax errors!");
}
// errors in configuration, correct it!
if (!validate.first)
{
Warning(_log,"Errors have been found in the configuration file. Automatic correction has been applied");
_qconfig = schemaCheckerT.getAutoCorrectedConfig(_qconfig);
foreach (auto & schemaError, schemaCheckerT.getMessages())
Warning(_log, "Config Fix: %s", QSTRING_CSTR(schemaError));
if (!JsonUtils::write(configFile, _qconfig, _log))
throw std::runtime_error("ERROR: Can't save configuration file, aborting");
}
// TODO END - remove when database migration is done
// transform json to string lists
QStringList keyList = defaultConfig.keys();
QStringList defValueList;
@@ -163,13 +125,6 @@ bool SettingsManager::saveSettings(QJsonObject config, const bool& correct)
Warning(_log, "Config Fix: %s", QSTRING_CSTR(schemaError));
}
// save data to file
if(_hyperion != nullptr)
{
if(!JsonUtils::write(_hyperion->getConfigFilePath(), config, _log))
return false;
}
// store the new config
_qconfig = config;