Skip to content

Commit c2faf07

Browse files
Paulchen Pantherbrindosch
authored andcommitted
Delete custom created effect configurations with JSON RPC (#289)
* Add ability to delete custom created Effects * Add deleteEffect function to Hyperion-Remote * Add deleteEffect function to Hyperion-Remote * Update schema.json * Update JsonSchemas.qrc * Add schema-delete-effect.json * Add deleteEffect function to JSON RPC * Add deleteEffect function to JSON RPC * Add Effect configuration file (.json) to Effect Definition * Update EffectDefinition.h
1 parent 4972bc0 commit c2faf07

File tree

10 files changed

+112
-5
lines changed

10 files changed

+112
-5
lines changed

include/effectengine/EffectDefinition.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
struct EffectDefinition
88
{
9-
QString name;
10-
QString script;
9+
QString name, script, file;
1110
QJsonObject args;
1211
};

libsrc/effectengine/EffectEngine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effe
248248

249249
// ---------- setup the definition ----------
250250

251+
effectDefinition.file = fileName;
251252
QJsonObject config = configEffect.object();
252253
QString scriptName = config["script"].toString();
253254
effectDefinition.name = config["name"].toString();

libsrc/jsonserver/JsonClientConnection.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QHostInfo>
1717
#include <QString>
1818
#include <QFile>
19+
#include <QFileInfo>
1920
#include <QCoreApplication>
2021
#include <QJsonObject>
2122
#include <QJsonDocument>
@@ -285,6 +286,8 @@ void JsonClientConnection::handleMessage(const QString& messageString)
285286
handleEffectCommand(message, command, tan);
286287
else if (command == "create-effect")
287288
handleCreateEffectCommand(message, command, tan);
289+
else if (command == "delete-effect")
290+
handleDeleteEffectCommand(message, command, tan);
288291
else if (command == "serverinfo")
289292
handleServerInfoCommand(message, command, tan);
290293
else if (command == "clear")
@@ -481,7 +484,15 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message,
481484
effectJson["name"] = message["name"].toString();
482485
effectJson["script"] = message["script"].toString();
483486
effectJson["args"] = message["args"].toObject();
484-
QJsonFactory::writeJson(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"), effectJson);
487+
488+
QFileInfo newFileName(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));
489+
490+
while(newFileName.exists())
491+
{
492+
newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json"));
493+
}
494+
495+
QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
485496
} else
486497
{
487498
sendErrorReply("Can't save new effect. Effect path empty", command, tan);
@@ -497,6 +508,43 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message,
497508
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
498509
}
499510

511+
void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
512+
{
513+
struct find_effect: std::unary_function<EffectDefinition, bool>
514+
{
515+
QString effectName;
516+
find_effect(QString effectName) :effectName(effectName) { }
517+
bool operator()(EffectDefinition const& effectDefinition) const
518+
{
519+
return effectDefinition.name == effectName;
520+
}
521+
};
522+
523+
if(message.size() > 0)
524+
{
525+
QString effectName = message["name"].toString();
526+
std::list<EffectDefinition> effectsDefinition = _hyperion->getEffects();
527+
std::list<EffectDefinition>::iterator it = std::find_if(effectsDefinition.begin(), effectsDefinition.end(), find_effect(effectName));
528+
529+
if (it != effectsDefinition.end())
530+
{
531+
QFileInfo effectConfigurationFile(it->file);
532+
if (effectConfigurationFile.absoluteFilePath().mid(0, 1) != ":" )
533+
{
534+
if (effectConfigurationFile.exists())
535+
{
536+
bool result = QFile::remove(effectConfigurationFile.absoluteFilePath());
537+
(result) ? sendSuccessReply(command, tan) : sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan);
538+
} else
539+
sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan);
540+
} else
541+
sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan);
542+
} else
543+
sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan);
544+
} else
545+
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
546+
}
547+
500548
void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QString& command, const int tan)
501549
{
502550
// create result
@@ -649,6 +697,7 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
649697
{
650698
QJsonObject effect;
651699
effect["name"] = effectDefinition.name;
700+
effect["file"] = effectDefinition.file;
652701
effect["script"] = effectDefinition.script;
653702
effect["args"] = effectDefinition.args;
654703
effects.append(effect);

libsrc/jsonserver/JsonClientConnection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ private slots:
9696
///
9797
void handleCreateEffectCommand(const QJsonObject & message, const QString &command, const int tan);
9898

99+
///
100+
/// Handle an incoming JSON Effect message (Delete JSON Effect)
101+
///
102+
/// @param message the incoming message
103+
///
104+
void handleDeleteEffectCommand(const QJsonObject & message, const QString &command, const int tan);
105+
99106
///
100107
/// Handle an incoming JSON Server info message
101108
///

libsrc/jsonserver/JsonSchemas.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<file alias="schema-adjustment">schema/schema-adjustment.json</file>
1313
<file alias="schema-effect">schema/schema-effect.json</file>
1414
<file alias="schema-create-effect">schema/schema-create-effect.json</file>
15+
<file alias="schema-delete-effect">schema/schema-delete-effect.json</file>
1516
<file alias="schema-sourceselect">schema/schema-sourceselect.json</file>
1617
<file alias="schema-config">schema/schema-config.json</file>
1718
<file alias="schema-componentstate">schema/schema-componentstate.json</file>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
{
3+
"type":"object",
4+
"required":true,
5+
"properties":{
6+
"command": {
7+
"type" : "string",
8+
"required" : true,
9+
"enum" : ["delete-effect"]
10+
},
11+
"tan" : {
12+
"type" : "integer"
13+
},
14+
"name" :
15+
{
16+
"type" : "string",
17+
"required" : true
18+
}
19+
},
20+
"additionalProperties": false
21+
}

libsrc/jsonserver/schema/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"command": {
66
"type" : "string",
77
"required" : true,
8-
"enum" : ["color", "image", "effect", "create-effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"]
8+
"enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"]
99
}
1010
}
1111
}

src/hyperion-remote/JsonConnection.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ void JsonConnection::createEffect(const QString &effectName, const QString &effe
209209
parseReply(reply);
210210
}
211211

212+
void JsonConnection::deleteEffect(const QString &effectName)
213+
{
214+
qDebug() << "Delete effect configuration" << effectName;
215+
216+
// create command
217+
QJsonObject effect;
218+
effect["command"] = QString("delete-effect");
219+
effect["name"] = effectName;
220+
221+
// send command message
222+
QJsonObject reply = sendMessage(effect);
223+
224+
// parse reply message
225+
parseReply(reply);
226+
}
227+
212228
QString JsonConnection::getServerInfo()
213229
{
214230
qDebug() << "Get server info";

src/hyperion-remote/JsonConnection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class JsonConnection
6464
/// @param effectArgs The arguments of the effect
6565
///
6666
void createEffect(const QString &effectName, const QString &effectScript, const QString & effectArgs);
67+
68+
///
69+
/// Delete a effect configuration file (.json)
70+
///
71+
/// @param effectName The name of the effect
72+
///
73+
void deleteEffect(const QString &effectName);
6774

6875
///
6976
/// Retrieve a list of all occupied priority channels

src/hyperion-remote/hyperion-remote.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ int main(int argc, char * argv[])
6565
Option & argEffectFile = parser.add<Option> (0x0, "effectFile", "Arguments to use in combination with --createEffect");
6666
Option & argEffectArgs = parser.add<Option> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.", "");
6767
Option & argCreateEffect= parser.add<Option> (0x0, "createEffect","Write a new Json Effect configuration file.\nFirst parameter = Effect name.\nSecond parameter = Effect file (--effectFile).\nLast parameter = Effect arguments (--effectArgs.)", "");
68+
Option & argDeleteEffect= parser.add<Option> (0x0, "deleteEffect","Delete a custom created Json Effect configuration file.");
6869
BooleanOption & argServerInfo = parser.add<BooleanOption>('l', "list" , "List server info and active effects with priority and duration");
6970
BooleanOption & argClear = parser.add<BooleanOption>('x', "clear" , "Clear data for the priority channel provided by the -p option");
7071
BooleanOption & argClearAll = parser.add<BooleanOption>(0x0, "clearall" , "Clear data for all active priority channels");
@@ -108,14 +109,15 @@ int main(int argc, char * argv[])
108109
bool colorModding = colorTransform || colorAdjust;
109110

110111
// check that exactly one command was given
111-
int commandCount = count({parser.isSet(argColor), parser.isSet(argImage), parser.isSet(argEffect), parser.isSet(argCreateEffect), parser.isSet(argServerInfo), parser.isSet(argClear), parser.isSet(argClearAll), parser.isSet(argEnableComponent), parser.isSet(argDisableComponent), colorModding, parser.isSet(argSource), parser.isSet(argSourceAuto), parser.isSet(argSourceOff), parser.isSet(argConfigGet), parser.isSet(argSchemaGet), parser.isSet(argConfigSet)});
112+
int commandCount = count({parser.isSet(argColor), parser.isSet(argImage), parser.isSet(argEffect), parser.isSet(argCreateEffect), parser.isSet(argDeleteEffect), parser.isSet(argServerInfo), parser.isSet(argClear), parser.isSet(argClearAll), parser.isSet(argEnableComponent), parser.isSet(argDisableComponent), colorModding, parser.isSet(argSource), parser.isSet(argSourceAuto), parser.isSet(argSourceOff), parser.isSet(argConfigGet), parser.isSet(argSchemaGet), parser.isSet(argConfigSet)});
112113
if (commandCount != 1)
113114
{
114115
qWarning() << (commandCount == 0 ? "No command found." : "Multiple commands found.") << " Provide exactly one of the following options:";
115116
showHelp(argColor);
116117
showHelp(argImage);
117118
showHelp(argEffect);
118119
showHelp(argCreateEffect);
120+
showHelp(argDeleteEffect);
119121
showHelp(argServerInfo);
120122
showHelp(argClear);
121123
showHelp(argClearAll);
@@ -163,6 +165,10 @@ int main(int argc, char * argv[])
163165
{
164166
connection.createEffect(argCreateEffect.value(parser), argEffectFile.value(parser), argEffectArgs.value(parser));
165167
}
168+
else if (parser.isSet(argDeleteEffect))
169+
{
170+
connection.deleteEffect(argDeleteEffect.value(parser));
171+
}
166172
else if (parser.isSet(argServerInfo))
167173
{
168174
QString info = connection.getServerInfo();

0 commit comments

Comments
 (0)