From 8f67294de6e6953d35bd497ffe42a176b8644af6 Mon Sep 17 00:00:00 2001 From: Paulchen Panther Date: Sun, 20 Nov 2016 18:41:10 +0100 Subject: [PATCH] Implement "reloadEffects" function in Hyperion for "write and delete" custom created effect configuration files (#293) * Update EffectEngine.h * add reloadEffects function * add readEffects function * add reloadEffects function * Call reloadEffects after handleCreateEffectCommand and handleDeleteEffectCommand --- include/effectengine/EffectEngine.h | 31 ++-- include/hyperion/Hyperion.h | 3 + libsrc/effectengine/EffectEngine.cpp | 176 ++++++++++----------- libsrc/hyperion/Hyperion.cpp | 5 + libsrc/jsonserver/JsonClientConnection.cpp | 13 +- 5 files changed, 126 insertions(+), 102 deletions(-) diff --git a/include/effectengine/EffectEngine.h b/include/effectengine/EffectEngine.h index 8b3f1ec8..6fcf3808 100644 --- a/include/effectengine/EffectEngine.h +++ b/include/effectengine/EffectEngine.h @@ -29,19 +29,26 @@ public: EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig); virtual ~EffectEngine(); - const std::list & getEffects() const; - - const std::list & getActiveEffects(); - - const std::list & getEffectSchemas(); + void readEffects(); - static bool loadEffectDefinition(const QString & path, const QString & effectConfigFile, EffectDefinition &effectDefinition); - - static bool loadEffectSchema(const QString & path, const QString & effectSchemaFile, EffectSchema &effectSchema); + const std::list & getEffects() const + { + return _availableEffects; + }; + + const std::list & getActiveEffects(); + + const std::list & getEffectSchemas() + { + return _effectSchemas; + }; public slots: /// Run the specified effect on the given priority channel and optionally specify a timeout - int runEffect(const QString &effectName, int priority, int timeout = -1); + int runEffect(const QString &effectName, int priority, int timeout = -1) + { + return runEffect(effectName, QJsonObject(), priority, timeout); + }; /// Run the specified effect on the given priority channel and optionally specify a timeout int runEffect(const QString &effectName, const QJsonObject & args, int priority, int timeout = -1, QString pythonScript = ""); @@ -56,12 +63,18 @@ private slots: void effectFinished(Effect * effect); private: + bool loadEffectDefinition(const QString & path, const QString & effectConfigFile, EffectDefinition &effectDefinition); + + bool loadEffectSchema(const QString & path, const QString & effectSchemaFile, EffectSchema &effectSchema); + /// Run the specified effect on the given priority channel and optionally specify a timeout int runEffectScript(const QString &script, const QString &name, const QJsonObject & args, int priority, int timeout = -1); private: Hyperion * _hyperion; + QJsonObject _effectConfig; + std::list _availableEffects; std::list _activeEffects; diff --git a/include/hyperion/Hyperion.h b/include/hyperion/Hyperion.h index 8ea0b05c..34ff64f5 100644 --- a/include/hyperion/Hyperion.h +++ b/include/hyperion/Hyperion.h @@ -119,6 +119,9 @@ public: /// const InputInfo& getPriorityInfo(const int priority) const; + /// Reload the list of available effects + void reloadEffects(); + /// Get the list of available effects /// @return The list of available effects const std::list &getEffects() const; diff --git a/libsrc/effectengine/EffectEngine.cpp b/libsrc/effectengine/EffectEngine.cpp index fe4cb817..3c392874 100644 --- a/libsrc/effectengine/EffectEngine.cpp +++ b/libsrc/effectengine/EffectEngine.cpp @@ -22,6 +22,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig) : _hyperion(hyperion) + , _effectConfig(jsonEffectConfig) , _availableEffects() , _activeEffects() , _mainThreadState(nullptr) @@ -36,84 +37,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectCo connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared())); // read all effects - const QJsonArray & paths = jsonEffectConfig["paths"].toArray(); - const QJsonArray & disabledEfx = jsonEffectConfig["disable"].toArray(); - - QStringList efxPathList; - efxPathList << ":/effects/"; - QStringList disableList; - - for(auto p : paths) - { - efxPathList << p.toString(); - } - for(auto efx : disabledEfx) - { - disableList << efx.toString(); - } - - std::map availableEffects; - foreach (const QString & path, efxPathList ) - { - QDir directory(path); - if (directory.exists()) - { - int efxCount = 0; - QStringList filenames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase); - foreach (const QString & filename, filenames) - { - EffectDefinition def; - if (loadEffectDefinition(path, filename, def)) - { - if (availableEffects.find(def.name) != availableEffects.end()) - { - Info(_log, "effect overload effect '%s' is now taken from %s'", def.name.toUtf8().constData(), path.toUtf8().constData() ); - } - - if ( disableList.contains(def.name) ) - { - Info(_log, "effect '%s' not loaded, because it is disabled in hyperion config", def.name.toUtf8().constData()); - } - else - { - availableEffects[def.name] = def; - efxCount++; - } - } - } - Info(_log, "%d effects loaded from directory %s", efxCount, path.toUtf8().constData()); - - // collect effect schemas - efxCount = 0; - directory = path + "schema/"; - QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase); - foreach (const QString & pyname, pynames) - { - EffectSchema pyEffect; - if (loadEffectSchema(path, pyname, pyEffect)) - { - _effectSchemas.push_back(pyEffect); - efxCount++; - } - } - if (efxCount > 0) - Info(_log, "%d effect schemas loaded from directory %s", efxCount, (path + "schema/").toUtf8().constData()); - } - else - { - Warning(_log, "Effect path \"%s\" does not exist",path.toUtf8().constData() ); - } - } - - foreach(auto item, availableEffects) - { - _availableEffects.push_back(item.second); - } - - if (_availableEffects.size() == 0) - { - Error(_log, "no effects found, check your effect directories"); - } + readEffects(); // initialize the python interpreter Debug(_log,"Initializing Python interpreter"); @@ -131,11 +55,6 @@ EffectEngine::~EffectEngine() Py_Finalize(); } -const std::list &EffectEngine::getEffects() const -{ - return _availableEffects; -} - const std::list &EffectEngine::getActiveEffects() { _availableActiveEffects.clear(); @@ -154,11 +73,6 @@ const std::list &EffectEngine::getActiveEffects() return _availableActiveEffects; } -const std::list &EffectEngine::getEffectSchemas() -{ - return _effectSchemas; -} - bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition) { Logger * log = Logger::getInstance("EFFECTENGINE"); @@ -341,9 +255,91 @@ bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSc return true; } -int EffectEngine::runEffect(const QString &effectName, int priority, int timeout) +void EffectEngine::readEffects() { - return runEffect(effectName, QJsonObject(), priority, timeout); + // clear all lists + _availableEffects.clear(); + _effectSchemas.clear(); + + // read all effects + const QJsonArray & paths = _effectConfig["paths"].toArray(); + const QJsonArray & disabledEfx = _effectConfig["disable"].toArray(); + + QStringList efxPathList; + efxPathList << ":/effects/"; + QStringList disableList; + + for(auto p : paths) + { + efxPathList << p.toString(); + } + for(auto efx : disabledEfx) + { + disableList << efx.toString(); + } + + std::map availableEffects; + foreach (const QString & path, efxPathList ) + { + QDir directory(path); + if (directory.exists()) + { + int efxCount = 0; + QStringList filenames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase); + foreach (const QString & filename, filenames) + { + EffectDefinition def; + if (loadEffectDefinition(path, filename, def)) + { + if (availableEffects.find(def.name) != availableEffects.end()) + { + Info(_log, "effect overload effect '%s' is now taken from %s'", def.name.toUtf8().constData(), path.toUtf8().constData() ); + } + + if ( disableList.contains(def.name) ) + { + Info(_log, "effect '%s' not loaded, because it is disabled in hyperion config", def.name.toUtf8().constData()); + } + else + { + availableEffects[def.name] = def; + efxCount++; + } + } + } + Info(_log, "%d effects loaded from directory %s", efxCount, path.toUtf8().constData()); + + // collect effect schemas + efxCount = 0; + directory = path + "schema/"; + QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase); + foreach (const QString & pyname, pynames) + { + EffectSchema pyEffect; + if (loadEffectSchema(path, pyname, pyEffect)) + { + _effectSchemas.push_back(pyEffect); + efxCount++; + } + } + if (efxCount > 0) + Info(_log, "%d effect schemas loaded from directory %s", efxCount, (path + "schema/").toUtf8().constData()); + } + else + { + Warning(_log, "Effect path \"%s\" does not exist",path.toUtf8().constData() ); + } + } + + foreach(auto item, availableEffects) + { + _availableEffects.push_back(item.second); + } + + if (_availableEffects.size() == 0) + { + Error(_log, "no effects found, check your effect directories"); + } } int EffectEngine::runEffect(const QString &effectName, const QJsonObject &args, int priority, int timeout, QString pythonScript) diff --git a/libsrc/hyperion/Hyperion.cpp b/libsrc/hyperion/Hyperion.cpp index 2aa4f9f1..98fef676 100644 --- a/libsrc/hyperion/Hyperion.cpp +++ b/libsrc/hyperion/Hyperion.cpp @@ -804,6 +804,11 @@ const Hyperion::InputInfo &Hyperion::getPriorityInfo(const int priority) const return _muxer.getInputInfo(priority); } +void Hyperion::reloadEffects() +{ + _effectEngine->readEffects(); +} + const std::list & Hyperion::getEffects() const { return _effectEngine->getEffects(); diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 70cd97bb..85f42bd1 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -493,13 +493,14 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, } QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson); + Info(_log, "Reload effect list"); + _hyperion->reloadEffects(); + sendSuccessReply(command, tan); } else { sendErrorReply("Can't save new effect. Effect path empty", command, tan); return; } - - sendSuccessReply(command, tan); } else sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan); } else @@ -534,7 +535,13 @@ void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, if (effectConfigurationFile.exists()) { bool result = QFile::remove(effectConfigurationFile.absoluteFilePath()); - (result) ? sendSuccessReply(command, tan) : sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan); + if (result) + { + Info(_log, "Reload effect list"); + _hyperion->reloadEffects(); + sendSuccessReply(command, tan); + } else + sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan); } else sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan); } else