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
This commit is contained in:
Paulchen Panther 2016-11-20 18:41:10 +01:00 committed by redPanther
parent c05181666c
commit 8f67294de6
5 changed files with 126 additions and 102 deletions

View File

@ -29,19 +29,26 @@ public:
EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig); EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig);
virtual ~EffectEngine(); virtual ~EffectEngine();
const std::list<EffectDefinition> & getEffects() const; void readEffects();
const std::list<ActiveEffectDefinition> & getActiveEffects();
const std::list<EffectSchema> & getEffectSchemas();
static bool loadEffectDefinition(const QString & path, const QString & effectConfigFile, EffectDefinition &effectDefinition); const std::list<EffectDefinition> & getEffects() const
{
static bool loadEffectSchema(const QString & path, const QString & effectSchemaFile, EffectSchema &effectSchema); return _availableEffects;
};
const std::list<ActiveEffectDefinition> & getActiveEffects();
const std::list<EffectSchema> & getEffectSchemas()
{
return _effectSchemas;
};
public slots: public slots:
/// Run the specified effect on the given priority channel and optionally specify a timeout /// 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 /// 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 = ""); 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); void effectFinished(Effect * effect);
private: 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 /// 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); int runEffectScript(const QString &script, const QString &name, const QJsonObject & args, int priority, int timeout = -1);
private: private:
Hyperion * _hyperion; Hyperion * _hyperion;
QJsonObject _effectConfig;
std::list<EffectDefinition> _availableEffects; std::list<EffectDefinition> _availableEffects;
std::list<Effect *> _activeEffects; std::list<Effect *> _activeEffects;

View File

@ -119,6 +119,9 @@ public:
/// ///
const InputInfo& getPriorityInfo(const int priority) const; const InputInfo& getPriorityInfo(const int priority) const;
/// Reload the list of available effects
void reloadEffects();
/// Get the list of available effects /// Get the list of available effects
/// @return The list of available effects /// @return The list of available effects
const std::list<EffectDefinition> &getEffects() const; const std::list<EffectDefinition> &getEffects() const;

View File

@ -22,6 +22,7 @@
EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig) EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectConfig)
: _hyperion(hyperion) : _hyperion(hyperion)
, _effectConfig(jsonEffectConfig)
, _availableEffects() , _availableEffects()
, _activeEffects() , _activeEffects()
, _mainThreadState(nullptr) , _mainThreadState(nullptr)
@ -36,84 +37,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectCo
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared())); connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
// read all effects // read all effects
const QJsonArray & paths = jsonEffectConfig["paths"].toArray(); readEffects();
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<QString, EffectDefinition> 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");
}
// initialize the python interpreter // initialize the python interpreter
Debug(_log,"Initializing Python interpreter"); Debug(_log,"Initializing Python interpreter");
@ -131,11 +55,6 @@ EffectEngine::~EffectEngine()
Py_Finalize(); Py_Finalize();
} }
const std::list<EffectDefinition> &EffectEngine::getEffects() const
{
return _availableEffects;
}
const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects() const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
{ {
_availableActiveEffects.clear(); _availableActiveEffects.clear();
@ -154,11 +73,6 @@ const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
return _availableActiveEffects; return _availableActiveEffects;
} }
const std::list<EffectSchema> &EffectEngine::getEffectSchemas()
{
return _effectSchemas;
}
bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition) bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
{ {
Logger * log = Logger::getInstance("EFFECTENGINE"); Logger * log = Logger::getInstance("EFFECTENGINE");
@ -341,9 +255,91 @@ bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSc
return true; 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<QString, EffectDefinition> 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) int EffectEngine::runEffect(const QString &effectName, const QJsonObject &args, int priority, int timeout, QString pythonScript)

View File

@ -804,6 +804,11 @@ const Hyperion::InputInfo &Hyperion::getPriorityInfo(const int priority) const
return _muxer.getInputInfo(priority); return _muxer.getInputInfo(priority);
} }
void Hyperion::reloadEffects()
{
_effectEngine->readEffects();
}
const std::list<EffectDefinition> & Hyperion::getEffects() const const std::list<EffectDefinition> & Hyperion::getEffects() const
{ {
return _effectEngine->getEffects(); return _effectEngine->getEffects();

View File

@ -493,13 +493,14 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message,
} }
QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson); QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
Info(_log, "Reload effect list");
_hyperion->reloadEffects();
sendSuccessReply(command, tan);
} else } else
{ {
sendErrorReply("Can't save new effect. Effect path empty", command, tan); sendErrorReply("Can't save new effect. Effect path empty", command, tan);
return; return;
} }
sendSuccessReply(command, tan);
} else } else
sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan); sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan);
} else } else
@ -534,7 +535,13 @@ void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message,
if (effectConfigurationFile.exists()) if (effectConfigurationFile.exists())
{ {
bool result = QFile::remove(effectConfigurationFile.absoluteFilePath()); 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 } else
sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan); sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan);
} else } else