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);
virtual ~EffectEngine();
const std::list<EffectDefinition> & getEffects() const;
void readEffects();
const std::list<EffectDefinition> & getEffects() const
{
return _availableEffects;
};
const std::list<ActiveEffectDefinition> & getActiveEffects();
const std::list<EffectSchema> & getEffectSchemas();
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<EffectSchema> & 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<EffectDefinition> _availableEffects;
std::list<Effect *> _activeEffects;

View File

@ -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<EffectDefinition> &getEffects() const;

View File

@ -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<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");
}
readEffects();
// initialize the python interpreter
Debug(_log,"Initializing Python interpreter");
@ -131,11 +55,6 @@ EffectEngine::~EffectEngine()
Py_Finalize();
}
const std::list<EffectDefinition> &EffectEngine::getEffects() const
{
return _availableEffects;
}
const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
{
_availableActiveEffects.clear();
@ -154,11 +73,6 @@ const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
return _availableActiveEffects;
}
const std::list<EffectSchema> &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<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)

View File

@ -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<EffectDefinition> & Hyperion::getEffects() const
{
return _effectEngine->getEffects();

View File

@ -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