mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Custom Effects - Clean-ups and Enhancements (#1163)
* Cleanup EffectFileHandler * Support Custom Effect Schemas and align EffectFileHandler * Change back to colon prefix for system effects * WebSockets - Fix error in handling fragmented frames * Correct missing colon updates * Update json with image file location for custom gif effects * Image effect deletion - considere full filename is stored in JSON * Correct selection lists indentions
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
#include <QByteArray>
|
||||
|
||||
// createEffect helper
|
||||
struct find_schema: std::unary_function<EffectSchema, bool>
|
||||
struct find_schema : std::unary_function<EffectSchema, bool>
|
||||
{
|
||||
QString pyFile;
|
||||
find_schema(QString pyFile):pyFile(pyFile) { }
|
||||
find_schema(QString pyFile) :pyFile(std::move(pyFile)) { }
|
||||
bool operator()(EffectSchema const& schema) const
|
||||
{
|
||||
return schema.pyFile == pyFile;
|
||||
@@ -22,10 +22,10 @@ struct find_schema: std::unary_function<EffectSchema, bool>
|
||||
};
|
||||
|
||||
// deleteEffect helper
|
||||
struct find_effect: std::unary_function<EffectDefinition, bool>
|
||||
struct find_effect : std::unary_function<EffectDefinition, bool>
|
||||
{
|
||||
QString effectName;
|
||||
find_effect(QString effectName) :effectName(effectName) { }
|
||||
find_effect(QString effectName) :effectName(std::move(effectName)) { }
|
||||
bool operator()(EffectDefinition const& effectDefinition) const
|
||||
{
|
||||
return effectDefinition.name == effectName;
|
||||
@@ -36,7 +36,6 @@ EffectFileHandler* EffectFileHandler::efhInstance;
|
||||
|
||||
EffectFileHandler::EffectFileHandler(const QString& rootPath, const QJsonDocument& effectConfig, QObject* parent)
|
||||
: QObject(parent)
|
||||
, _effectConfig()
|
||||
, _log(Logger::getInstance("EFFECTFILES"))
|
||||
, _rootPath(rootPath)
|
||||
{
|
||||
@@ -50,7 +49,7 @@ EffectFileHandler::EffectFileHandler(const QString& rootPath, const QJsonDocumen
|
||||
|
||||
void EffectFileHandler::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
|
||||
{
|
||||
if(type == settings::EFFECTS)
|
||||
if (type == settings::EFFECTS)
|
||||
{
|
||||
_effectConfig = config.object();
|
||||
// update effects and schemas
|
||||
@@ -67,15 +66,17 @@ QString EffectFileHandler::deleteEffect(const QString& effectName)
|
||||
if (it != effectsDefinition.end())
|
||||
{
|
||||
QFileInfo effectConfigurationFile(it->file);
|
||||
if (effectConfigurationFile.absoluteFilePath().mid(0, 1) != ":" )
|
||||
if (!effectConfigurationFile.absoluteFilePath().startsWith(':'))
|
||||
{
|
||||
if (effectConfigurationFile.exists())
|
||||
{
|
||||
if ( (it->script == ":/effects/gif.py") && !it->args.value("image").toString("").isEmpty())
|
||||
if ((it->script == ":/effects/gif.py") && !it->args.value("image").toString("").isEmpty())
|
||||
{
|
||||
QFileInfo effectImageFile(effectConfigurationFile.absolutePath() + "/" + it->args.value("image").toString());
|
||||
if (effectImageFile.exists())
|
||||
QFile::remove(effectImageFile.absoluteFilePath());
|
||||
QFileInfo effectImageFile(it->args.value("image").toString());
|
||||
if (effectImageFile.exists())
|
||||
{
|
||||
QFile::remove(effectImageFile.absoluteFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
bool result = QFile::remove(effectConfigurationFile.absoluteFilePath());
|
||||
@@ -83,15 +84,27 @@ QString EffectFileHandler::deleteEffect(const QString& effectName)
|
||||
if (result)
|
||||
{
|
||||
updateEffects();
|
||||
return "";
|
||||
} else
|
||||
resultMsg = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions";
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath();
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Can't delete internal effect: " + effectName;
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Effect " + effectName + " not found";
|
||||
}
|
||||
|
||||
return resultMsg;
|
||||
}
|
||||
@@ -101,17 +114,14 @@ QString EffectFileHandler::saveEffect(const QJsonObject& message)
|
||||
QString resultMsg;
|
||||
if (!message["args"].toObject().isEmpty())
|
||||
{
|
||||
QString scriptName;
|
||||
(message["script"].toString().mid(0, 1) == ":" )
|
||||
? scriptName = ":/effects//" + message["script"].toString().mid(1)
|
||||
: scriptName = message["script"].toString();
|
||||
QString scriptName = message["script"].toString();
|
||||
|
||||
std::list<EffectSchema> effectsSchemas = getEffectSchemas();
|
||||
std::list<EffectSchema>::iterator it = std::find_if(effectsSchemas.begin(), effectsSchemas.end(), find_schema(scriptName));
|
||||
|
||||
if (it != effectsSchemas.end())
|
||||
{
|
||||
if(!JsonUtils::validate("EffectFileHandler", message["args"].toObject(), it->schemaFile, _log))
|
||||
if (!JsonUtils::validate("EffectFileHandler", message["args"].toObject(), it->schemaFile, _log))
|
||||
{
|
||||
return "Error during arg validation against schema, please consult the Hyperion Log";
|
||||
}
|
||||
@@ -120,9 +130,9 @@ QString EffectFileHandler::saveEffect(const QJsonObject& message)
|
||||
QJsonArray effectArray;
|
||||
effectArray = _effectConfig["paths"].toArray();
|
||||
|
||||
if (effectArray.size() > 0)
|
||||
if (!effectArray.empty())
|
||||
{
|
||||
if (message["name"].toString().trimmed().isEmpty() || message["name"].toString().trimmed().startsWith("."))
|
||||
if (message["name"].toString().trimmed().isEmpty() || message["name"].toString().trimmed().startsWith(":"))
|
||||
{
|
||||
return "Can't save new effect. Effect name is empty or begins with a dot.";
|
||||
}
|
||||
@@ -138,41 +148,56 @@ QString EffectFileHandler::saveEffect(const QJsonObject& message)
|
||||
if (iter != availableEffects.end())
|
||||
{
|
||||
newFileName.setFile(iter->file);
|
||||
if (newFileName.absoluteFilePath().mid(0, 1) == ":")
|
||||
if (newFileName.absoluteFilePath().startsWith(':'))
|
||||
{
|
||||
return "The effect name '" + message["name"].toString() + "' is assigned to an internal effect. Please rename your effekt.";
|
||||
return "The effect name '" + message["name"].toString() + "' is assigned to an internal effect. Please rename your effect.";
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO global special keyword handling
|
||||
QString f = effectArray[0].toString().replace("$ROOT",_rootPath) + "/" + message["name"].toString().replace(QString(" "), QString("")) + QString(".json");
|
||||
QString f = effectArray[0].toString().replace("$ROOT", _rootPath) + '/' + message["name"].toString().replace(QString(" "), QString("")) + QString(".json");
|
||||
newFileName.setFile(f);
|
||||
}
|
||||
|
||||
//TODO check if filename exist
|
||||
if (!message["imageData"].toString("").isEmpty() && !message["args"].toObject().value("image").toString("").isEmpty())
|
||||
{
|
||||
QFileInfo imageFileName(effectArray[0].toString().replace("$ROOT",_rootPath) + "/" + message["args"].toObject().value("image").toString());
|
||||
if(!FileUtils::writeFile(imageFileName.absoluteFilePath(), QByteArray::fromBase64(message["imageData"].toString("").toUtf8()), _log))
|
||||
QJsonObject args = message["args"].toObject();
|
||||
QString imageFilePath = effectArray[0].toString().replace("$ROOT", _rootPath) + '/' + args.value("image").toString();
|
||||
|
||||
QFileInfo imageFileName(imageFilePath);
|
||||
if (!FileUtils::writeFile(imageFileName.absoluteFilePath(), QByteArray::fromBase64(message["imageData"].toString("").toUtf8()), _log))
|
||||
{
|
||||
return "Error while saving image file '" + message["args"].toObject().value("image").toString() + ", please check the Hyperion Log";
|
||||
}
|
||||
|
||||
//Update json with image file location
|
||||
args["image"] = imageFilePath;
|
||||
effectJson["args"] = args;
|
||||
}
|
||||
|
||||
if(!JsonUtils::write(newFileName.absoluteFilePath(), effectJson, _log))
|
||||
if (!JsonUtils::write(newFileName.absoluteFilePath(), effectJson, _log))
|
||||
{
|
||||
return "Error while saving effect, please check the Hyperion Log";
|
||||
}
|
||||
|
||||
Info(_log, "Reload effect list");
|
||||
updateEffects();
|
||||
return "";
|
||||
} else
|
||||
resultMsg = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Can't save new effect. Effect path empty";
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Missing schema file for Python script " + message["script"].toString();
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resultMsg = "Missing or empty Object 'args'";
|
||||
}
|
||||
|
||||
return resultMsg;
|
||||
}
|
||||
@@ -184,50 +209,56 @@ void EffectFileHandler::updateEffects()
|
||||
_effectSchemas.clear();
|
||||
|
||||
// read all effects
|
||||
const QJsonArray & paths = _effectConfig["paths"].toArray();
|
||||
const QJsonArray & disabledEfx = _effectConfig["disable"].toArray();
|
||||
const QJsonArray& paths = _effectConfig["paths"].toArray();
|
||||
const QJsonArray& disabledEfx = _effectConfig["disable"].toArray();
|
||||
|
||||
QStringList efxPathList;
|
||||
efxPathList << ":/effects/";
|
||||
QStringList disableList;
|
||||
|
||||
for(auto p : paths)
|
||||
for (const auto& p : paths)
|
||||
{
|
||||
efxPathList << p.toString().replace("$ROOT",_rootPath);
|
||||
QString effectPath = p.toString();
|
||||
if (!effectPath.endsWith('/'))
|
||||
{
|
||||
effectPath.append('/');
|
||||
}
|
||||
efxPathList << effectPath.replace("$ROOT", _rootPath);
|
||||
}
|
||||
for(auto efx : disabledEfx)
|
||||
|
||||
for (const auto& efx : disabledEfx)
|
||||
{
|
||||
disableList << efx.toString();
|
||||
}
|
||||
|
||||
QMap<QString, EffectDefinition> availableEffects;
|
||||
for (const QString & path : efxPathList )
|
||||
for (const QString& path : qAsConst(efxPathList))
|
||||
{
|
||||
QDir directory(path);
|
||||
if (!directory.exists())
|
||||
{
|
||||
if(directory.mkpath(path))
|
||||
if (directory.mkpath(path))
|
||||
{
|
||||
Info(_log, "New Effect path \"%s\" created successfully", QSTRING_CSTR(path) );
|
||||
Info(_log, "New Effect path \"%s\" created successfully", QSTRING_CSTR(path));
|
||||
}
|
||||
else
|
||||
{
|
||||
Warning(_log, "Failed to create Effect path \"%s\", please check permissions", QSTRING_CSTR(path) );
|
||||
Warning(_log, "Failed to create Effect path \"%s\", please check permissions", QSTRING_CSTR(path));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int efxCount = 0;
|
||||
QStringList filenames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
||||
for (const QString & filename : filenames)
|
||||
for (const QString& filename : qAsConst(filenames))
|
||||
{
|
||||
EffectDefinition def;
|
||||
if (loadEffectDefinition(path, filename, def))
|
||||
{
|
||||
InfoIf(availableEffects.find(def.name) != availableEffects.end(), _log,
|
||||
"effect overload effect '%s' is now taken from '%s'", QSTRING_CSTR(def.name), QSTRING_CSTR(path) );
|
||||
"effect overload effect '%s' is now taken from '%s'", QSTRING_CSTR(def.name), QSTRING_CSTR(path));
|
||||
|
||||
if ( disableList.contains(def.name) )
|
||||
if (disableList.contains(def.name))
|
||||
{
|
||||
Info(_log, "effect '%s' not loaded, because it is disabled in hyperion config", QSTRING_CSTR(def.name));
|
||||
}
|
||||
@@ -242,64 +273,65 @@ void EffectFileHandler::updateEffects()
|
||||
|
||||
// collect effect schemas
|
||||
efxCount = 0;
|
||||
directory.setPath(path.endsWith("/") ? (path + "schema/") : (path + "/schema/"));
|
||||
QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
||||
for (const QString & pyname : pynames)
|
||||
|
||||
QString schemaPath = path + "schema" + '/';
|
||||
directory.setPath(schemaPath);
|
||||
QStringList schemaFileNames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
||||
for (const QString& schemaFileName : qAsConst(schemaFileNames))
|
||||
{
|
||||
EffectSchema pyEffect;
|
||||
if (loadEffectSchema(path, pyname, pyEffect))
|
||||
if (loadEffectSchema(path, directory.filePath(schemaFileName), pyEffect))
|
||||
{
|
||||
_effectSchemas.push_back(pyEffect);
|
||||
efxCount++;
|
||||
}
|
||||
}
|
||||
InfoIf(efxCount > 0, _log, "%d effect schemas loaded from directory %s", efxCount, QSTRING_CSTR((path + "schema/")));
|
||||
InfoIf(efxCount > 0, _log, "%d effect schemas loaded from directory %s", efxCount, QSTRING_CSTR(schemaPath));
|
||||
}
|
||||
}
|
||||
|
||||
for(auto item : availableEffects)
|
||||
for (const auto& item : qAsConst(availableEffects))
|
||||
{
|
||||
_availableEffects.push_back(item);
|
||||
}
|
||||
|
||||
ErrorIf(_availableEffects.size()==0, _log, "no effects found, check your effect directories");
|
||||
ErrorIf(_availableEffects.empty(), _log, "no effects found, check your effect directories");
|
||||
|
||||
emit effectListChanged();
|
||||
}
|
||||
|
||||
bool EffectFileHandler::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
|
||||
bool EffectFileHandler::loadEffectDefinition(const QString& path, const QString& effectConfigFile, EffectDefinition& effectDefinition)
|
||||
{
|
||||
QString fileName = path + QDir::separator() + effectConfigFile;
|
||||
QString fileName = path + effectConfigFile;
|
||||
|
||||
// Read and parse the effect json config file
|
||||
QJsonObject configEffect;
|
||||
if(!JsonUtils::readFile(fileName, configEffect, _log))
|
||||
if (!JsonUtils::readFile(fileName, configEffect, _log)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// validate effect config with effect schema(path)
|
||||
if(!JsonUtils::validate(fileName, configEffect, ":effect-schema", _log))
|
||||
if (!JsonUtils::validate(fileName, configEffect, ":effect-schema", _log)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// setup the definition
|
||||
effectDefinition.file = fileName;
|
||||
QJsonObject config = configEffect;
|
||||
QString scriptName = config["script"].toString();
|
||||
effectDefinition.name = config["name"].toString();
|
||||
if (scriptName.isEmpty())
|
||||
if (scriptName.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile fileInfo(scriptName);
|
||||
|
||||
if (scriptName.mid(0, 1) == ":" )
|
||||
if (!fileInfo.exists())
|
||||
{
|
||||
(!fileInfo.exists())
|
||||
? effectDefinition.script = ":/effects/"+scriptName.mid(1)
|
||||
: effectDefinition.script = scriptName;
|
||||
} else
|
||||
effectDefinition.script = path + scriptName;
|
||||
}
|
||||
else
|
||||
{
|
||||
(!fileInfo.exists())
|
||||
? effectDefinition.script = path + QDir::separator() + scriptName
|
||||
: effectDefinition.script = scriptName;
|
||||
effectDefinition.script = scriptName;
|
||||
}
|
||||
|
||||
effectDefinition.args = config["args"].toObject();
|
||||
@@ -307,31 +339,31 @@ bool EffectFileHandler::loadEffectDefinition(const QString &path, const QString
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EffectFileHandler::loadEffectSchema(const QString &path, const QString &effectSchemaFile, EffectSchema & effectSchema)
|
||||
bool EffectFileHandler::loadEffectSchema(const QString& path, const QString& schemaFilePath, EffectSchema& effectSchema)
|
||||
{
|
||||
QString fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
|
||||
// Read and parse the effect schema file
|
||||
QJsonObject schemaEffect;
|
||||
if(!JsonUtils::readFile(fileName, schemaEffect, _log))
|
||||
return false;
|
||||
|
||||
// setup the definition
|
||||
QString scriptName = schemaEffect["script"].toString();
|
||||
effectSchema.schemaFile = fileName;
|
||||
fileName = path + QDir::separator() + scriptName;
|
||||
QFile pyFile(fileName);
|
||||
|
||||
if (scriptName.isEmpty() || !pyFile.open(QIODevice::ReadOnly))
|
||||
if (!JsonUtils::readFile(schemaFilePath, schemaEffect, _log))
|
||||
{
|
||||
fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
Error( _log, "Python script '%s' in effect schema '%s' could not be loaded", QSTRING_CSTR(scriptName), QSTRING_CSTR(fileName));
|
||||
return false;
|
||||
}
|
||||
|
||||
pyFile.close();
|
||||
// setup the definition
|
||||
QString scriptName = schemaEffect["script"].toString();
|
||||
effectSchema.schemaFile = schemaFilePath;
|
||||
|
||||
effectSchema.pyFile = (scriptName.mid(0, 1) == ":" ) ? ":/effects/"+scriptName.mid(1) : path + QDir::separator() + scriptName;
|
||||
QString scriptFilePath = path + scriptName;
|
||||
QFile pyScriptFile(scriptFilePath);
|
||||
|
||||
if (scriptName.isEmpty() || !pyScriptFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
Error(_log, "Python script '%s' in effect schema '%s' could not be loaded", QSTRING_CSTR(scriptName), QSTRING_CSTR(schemaFilePath));
|
||||
return false;
|
||||
}
|
||||
|
||||
pyScriptFile.close();
|
||||
|
||||
effectSchema.pyFile = scriptFilePath;
|
||||
effectSchema.pySchema = schemaEffect;
|
||||
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user