mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Create Effect configuration files (.json) with JSON RPC (#277)
* Add getEffectSchemas and loadEffectSchema function * Add getEffectSchemas function * add effect schema files to internal resources * Add loadEffectSchema and getEffectSchemas function * add effect schema resources * add getEffectSchemas function * extend handleSchemaGetCommand to get ... ... all available effect schemas add handleCreateEffectCommand function * add handleCreateEffectCommand function * include schema-create-effect.json file * add create-effect schema * Add schema-create-effect.json file * Add createEffect to hyperion-remote * Add createEffect function * add createEffect function * Create fade.schema.json * Add files via upload * Add files via upload * Update police.schema.json * Update EffectEngine.qrc.in * Update CMakeLists.txt
This commit is contained in:
committed by
redPanther
parent
4faa505fa4
commit
fab0c208fe
@@ -82,6 +82,22 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const QJsonObject & jsonEffectCo
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -138,6 +154,11 @@ 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");
|
||||
@@ -232,17 +253,93 @@ bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effe
|
||||
effectDefinition.name = config["name"].toString();
|
||||
if (scriptName.isEmpty())
|
||||
return false;
|
||||
|
||||
QFile fileInfo(scriptName);
|
||||
|
||||
if (scriptName.mid(0, 1) == ":" )
|
||||
effectDefinition.script = ":/effects/"+scriptName.mid(1);
|
||||
else
|
||||
effectDefinition.script = path + QDir::separator().toLatin1() + scriptName;
|
||||
{
|
||||
(!fileInfo.exists())
|
||||
? effectDefinition.script = ":/effects/"+scriptName.mid(1)
|
||||
: effectDefinition.script = scriptName;
|
||||
} else
|
||||
{
|
||||
(!fileInfo.exists())
|
||||
? effectDefinition.script = path + QDir::separator().toLatin1() + scriptName
|
||||
: effectDefinition.script = scriptName;
|
||||
}
|
||||
|
||||
effectDefinition.args = config["args"].toObject();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSchemaFile, EffectSchema & effectSchema)
|
||||
{
|
||||
Logger * log = Logger::getInstance("EFFECTENGINE");
|
||||
|
||||
QString fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
QJsonParseError error;
|
||||
|
||||
// ---------- Read the effect schema file ----------
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
Error( log, "Effect schema '%s' could not be loaded", fileName.toUtf8().constData());
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray fileContent = file.readAll();
|
||||
QJsonDocument schemaEffect = QJsonDocument::fromJson(fileContent, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
// report to the user the failure and their locations in the document.
|
||||
int errorLine(0), errorColumn(0);
|
||||
|
||||
for( int i=0, count=qMin( error.offset,fileContent.size()); i<count; ++i )
|
||||
{
|
||||
++errorColumn;
|
||||
if(fileContent.at(i) == '\n' )
|
||||
{
|
||||
errorColumn = 0;
|
||||
++errorLine;
|
||||
}
|
||||
}
|
||||
|
||||
Error( log, "Error while reading effect schema: '%s' at Line: '%i' , Column: %i", error.errorString().toUtf8().constData(), errorLine, errorColumn);
|
||||
return false;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
// ---------- setup the definition ----------
|
||||
|
||||
QJsonObject tempSchemaEffect = schemaEffect.object();
|
||||
QString scriptName = tempSchemaEffect["script"].toString();
|
||||
effectSchema.schemaFile = fileName;
|
||||
fileName = path + QDir::separator() + scriptName;
|
||||
QFile pyFile(fileName);
|
||||
|
||||
if (scriptName.isEmpty() || !pyFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
Error( log, "Python script '%s' in effect schema '%s' could not be loaded", scriptName.toUtf8().constData(), fileName.toUtf8().constData());
|
||||
return false;
|
||||
}
|
||||
|
||||
pyFile.close();
|
||||
|
||||
if (scriptName.mid(0, 1) == ":" )
|
||||
effectSchema.pyFile = ":/effects/"+scriptName.mid(1);
|
||||
else
|
||||
effectSchema.pyFile = path + QDir::separator().toLatin1() + scriptName;
|
||||
|
||||
effectSchema.pySchema = tempSchemaEffect;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int EffectEngine::runEffect(const QString &effectName, int priority, int timeout)
|
||||
{
|
||||
return runEffect(effectName, QJsonObject(), priority, timeout);
|
||||
|
@@ -814,6 +814,11 @@ const std::list<ActiveEffectDefinition> & Hyperion::getActiveEffects()
|
||||
return _effectEngine->getActiveEffects();
|
||||
}
|
||||
|
||||
const std::list<EffectSchema> & Hyperion::getEffectSchemas()
|
||||
{
|
||||
return _effectEngine->getEffectSchemas();
|
||||
}
|
||||
|
||||
int Hyperion::setEffect(const QString &effectName, int priority, int timeout)
|
||||
{
|
||||
return _effectEngine->runEffect(effectName, priority, timeout);
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QVariantMap>
|
||||
#include <QDir>
|
||||
|
||||
// hyperion util includes
|
||||
#include <hyperion/ImageProcessorFactory.h>
|
||||
@@ -282,6 +283,8 @@ void JsonClientConnection::handleMessage(const QString& messageString)
|
||||
handleImageCommand(message, command, tan);
|
||||
else if (command == "effect")
|
||||
handleEffectCommand(message, command, tan);
|
||||
else if (command == "create-effect")
|
||||
handleCreateEffectCommand(message, command, tan);
|
||||
else if (command == "serverinfo")
|
||||
handleServerInfoCommand(message, command, tan);
|
||||
else if (command == "clear")
|
||||
@@ -434,6 +437,65 @@ void JsonClientConnection::handleEffectCommand(const QJsonObject& message, const
|
||||
sendSuccessReply(command, tan);
|
||||
}
|
||||
|
||||
void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
|
||||
{
|
||||
struct find_schema: std::unary_function<EffectSchema, bool>
|
||||
{
|
||||
QString pyFile;
|
||||
find_schema(QString pyFile):pyFile(pyFile) { }
|
||||
bool operator()(EffectSchema const& schema) const
|
||||
{
|
||||
return schema.pyFile == pyFile;
|
||||
}
|
||||
};
|
||||
|
||||
if(message.size() > 0)
|
||||
{
|
||||
if (!message["args"].toObject().isEmpty())
|
||||
{
|
||||
QString scriptName;
|
||||
(message["script"].toString().mid(0, 1) == ":" )
|
||||
? scriptName = ":/effects//" + message["script"].toString().mid(1)
|
||||
: scriptName = message["script"].toString();
|
||||
|
||||
std::list<EffectSchema> effectsSchemas = _hyperion->getEffectSchemas();
|
||||
std::list<EffectSchema>::iterator it = std::find_if(effectsSchemas.begin(), effectsSchemas.end(), find_schema(scriptName));
|
||||
|
||||
if (it != effectsSchemas.end())
|
||||
{
|
||||
QString errors;
|
||||
|
||||
if (!checkJson(message["args"].toObject(), it->schemaFile, errors))
|
||||
{
|
||||
sendErrorReply("Error while validating json: " + errors, command, tan);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject effectJson;
|
||||
QJsonArray effectArray;
|
||||
effectArray = _hyperion->getQJsonConfig()["effects"].toObject()["paths"].toArray();
|
||||
|
||||
if (effectArray.size() > 0)
|
||||
{
|
||||
effectJson["name"] = message["name"].toString();
|
||||
effectJson["script"] = message["script"].toString();
|
||||
effectJson["args"] = message["args"].toObject();
|
||||
QJsonFactory::writeJson(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"), effectJson);
|
||||
} 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
|
||||
sendErrorReply("Missing or empty Object 'args'", command, tan);
|
||||
} else
|
||||
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
||||
}
|
||||
|
||||
void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
||||
{
|
||||
// create result
|
||||
@@ -939,7 +1001,7 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co
|
||||
Q_INIT_RESOURCE(resource);
|
||||
QJsonParseError error;
|
||||
|
||||
// read the json schema from the resource
|
||||
// read the hyperion json schema from the resource
|
||||
QFile schemaData(":/hyperion-schema");
|
||||
|
||||
if (!schemaData.open(QIODevice::ReadOnly))
|
||||
@@ -976,10 +1038,43 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co
|
||||
|
||||
schemaJson = doc.object();
|
||||
|
||||
//QJsonObject.insert can not merge. See details: http://doc.qt.io/qt-5/qjsonobject.html#insert
|
||||
// collect all LED Devices
|
||||
properties = schemaJson["properties"].toObject();
|
||||
alldevices = LedDevice::getLedDeviceSchemas();
|
||||
properties.insert("alldevices", alldevices);
|
||||
|
||||
// collect all available effect schemas
|
||||
QJsonObject pyEffectSchemas, pyEffectSchema;
|
||||
QJsonArray in, ex;
|
||||
const std::list<EffectSchema> & effectsSchemas = _hyperion->getEffectSchemas();
|
||||
for (const EffectSchema & effectSchema : effectsSchemas)
|
||||
{
|
||||
if (effectSchema.pyFile.mid(0, 1) == ":")
|
||||
{
|
||||
QJsonObject internal;
|
||||
internal.insert("script", effectSchema.pyFile);
|
||||
internal.insert("schema-location", effectSchema.schemaFile);
|
||||
internal.insert("schema-content", effectSchema.pySchema);
|
||||
in.append(internal);
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonObject external;
|
||||
external.insert("script", effectSchema.pyFile);
|
||||
external.insert("schema-location", effectSchema.schemaFile);
|
||||
external.insert("schema-content", effectSchema.pySchema);
|
||||
ex.append(external);
|
||||
}
|
||||
}
|
||||
|
||||
if (!in.empty())
|
||||
pyEffectSchema.insert("internal", in);
|
||||
if (!ex.empty())
|
||||
pyEffectSchema.insert("external", ex);
|
||||
|
||||
pyEffectSchemas = pyEffectSchema;
|
||||
properties.insert("effectSchemas", pyEffectSchemas);
|
||||
|
||||
schemaJson.insert("properties", properties);
|
||||
|
||||
result["result"] = schemaJson;
|
||||
|
@@ -89,6 +89,13 @@ private:
|
||||
///
|
||||
void handleEffectCommand(const QJsonObject & message, const QString &command, const int tan);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON Effect message (Write JSON Effect)
|
||||
///
|
||||
/// @param message the incoming message
|
||||
///
|
||||
void handleCreateEffectCommand(const QJsonObject & message, const QString &command, const int tan);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON Server info message
|
||||
///
|
||||
|
@@ -11,6 +11,7 @@
|
||||
<file alias="schema-temperature">schema/schema-temperature.json</file>
|
||||
<file alias="schema-adjustment">schema/schema-adjustment.json</file>
|
||||
<file alias="schema-effect">schema/schema-effect.json</file>
|
||||
<file alias="schema-create-effect">schema/schema-create-effect.json</file>
|
||||
<file alias="schema-sourceselect">schema/schema-sourceselect.json</file>
|
||||
<file alias="schema-config">schema/schema-config.json</file>
|
||||
<file alias="schema-componentstate">schema/schema-componentstate.json</file>
|
||||
|
30
libsrc/jsonserver/schema/schema-create-effect.json
Normal file
30
libsrc/jsonserver/schema/schema-create-effect.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"type":"object",
|
||||
"required":true,
|
||||
"properties":{
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["create-effect"]
|
||||
},
|
||||
"tan" : {
|
||||
"type" : "integer"
|
||||
},
|
||||
"name" :
|
||||
{
|
||||
"type" : "string",
|
||||
"required" : true
|
||||
},
|
||||
"script" :
|
||||
{
|
||||
"type" : "string",
|
||||
"required" : true
|
||||
},
|
||||
"args" :
|
||||
{
|
||||
"type" : "object",
|
||||
"required" : true
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["color", "image", "effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"]
|
||||
"enum" : ["color", "image", "effect", "create-effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user