mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
JSON RPC Writer (configSet) (#175)
* Remove "endOfJson" Value Deprecated value from Hypercon * Remove "endOfJson" Value Deprecated value from Hypercon * Add writeJson function to JsonFactory * ability to ignore required value in schema file * Remove "endOfJson" Value * Add handleConfigSetCommand function * Add handleConfigSetCommand function * Update JsonSchemas.qrc * Update schema.json * Update JsonSchemaChecker.cpp * Add configSet command to Hyperion-remote * Add setConfigFile function * Add setConfigFile function * Add schema-configset.json
This commit is contained in:
committed by
redPanther
parent
97181fa83c
commit
68fd395670
@@ -24,6 +24,7 @@
|
||||
#include <hyperion/ColorAdjustment.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <HyperionConfig.h>
|
||||
#include <utils/jsonschema/JsonFactory.h>
|
||||
|
||||
// project includes
|
||||
#include "JsonClientConnection.h"
|
||||
@@ -267,6 +268,8 @@ void JsonClientConnection::handleMessage(const std::string &messageString)
|
||||
handleSourceSelectCommand(message);
|
||||
else if (command == "configget")
|
||||
handleConfigGetCommand(message);
|
||||
else if (command == "configset")
|
||||
handleConfigSetCommand(message);
|
||||
else if (command == "componentstate")
|
||||
handleComponentStateCommand(message);
|
||||
else
|
||||
@@ -842,6 +845,53 @@ void JsonClientConnection::handleConfigGetCommand(const Json::Value &)
|
||||
sendMessage(result);
|
||||
}
|
||||
|
||||
void JsonClientConnection::handleConfigSetCommand(const Json::Value &message)
|
||||
{
|
||||
struct nested
|
||||
{
|
||||
static void configSetCommand(const Json::Value& message, Json::Value& config, bool& create)
|
||||
{
|
||||
if (!config.isObject() || !message.isObject())
|
||||
return;
|
||||
|
||||
for (const auto& key : message.getMemberNames()) {
|
||||
if ((config.isObject() && config.isMember(key)) || create)
|
||||
{
|
||||
if (config[key].type() == Json::objectValue && message[key].type() == Json::objectValue)
|
||||
{
|
||||
configSetCommand(message[key], config[key], create);
|
||||
}
|
||||
else
|
||||
if ( !config[key].empty() || create)
|
||||
config[key] = message[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if(message.size() > 0)
|
||||
{
|
||||
if (message.isObject() && message.isMember("configset"))
|
||||
{
|
||||
std::string errors;
|
||||
if (!checkJson(message["configset"], ":/hyperion-schema", errors, true))
|
||||
{
|
||||
sendErrorReply("Error while validating json: " + errors);
|
||||
return;
|
||||
}
|
||||
|
||||
bool createKey = message.isMember("create");
|
||||
Json::Value hyperionConfig = _hyperion->getJsonConfig();
|
||||
nested::configSetCommand(message["configset"], hyperionConfig, createKey);
|
||||
|
||||
JsonFactory::writeJson(_hyperion->getConfigFileName(), hyperionConfig);
|
||||
|
||||
sendSuccessReply();
|
||||
}
|
||||
} else
|
||||
sendErrorReply("Error while parsing json: Message size " + message.size());
|
||||
}
|
||||
|
||||
void JsonClientConnection::handleComponentStateCommand(const Json::Value& message)
|
||||
{
|
||||
const Json::Value & componentState = message["componentstate"];
|
||||
@@ -956,7 +1006,7 @@ void JsonClientConnection::sendErrorReply(const std::string &error)
|
||||
sendMessage(reply);
|
||||
}
|
||||
|
||||
bool JsonClientConnection::checkJson(const Json::Value & message, const QString & schemaResource, std::string & errorMessage)
|
||||
bool JsonClientConnection::checkJson(const Json::Value & message, const QString & schemaResource, std::string & errorMessage, bool ignoreRequired)
|
||||
{
|
||||
// read the json schema from the resource
|
||||
QResource schemaData(schemaResource);
|
||||
@@ -974,7 +1024,7 @@ bool JsonClientConnection::checkJson(const Json::Value & message, const QString
|
||||
schema.setSchema(schemaJson);
|
||||
|
||||
// check the message
|
||||
if (!schema.validate(message))
|
||||
if (!schema.validate(message, ignoreRequired))
|
||||
{
|
||||
const std::list<std::string> & errors = schema.getMessages();
|
||||
std::stringstream ss;
|
||||
|
@@ -144,6 +144,11 @@ private:
|
||||
/// @param message the incoming message
|
||||
///
|
||||
void handleConfigGetCommand(const Json::Value & message);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON SetConfig message
|
||||
///
|
||||
void handleConfigSetCommand(const Json::Value & message);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON Component State message
|
||||
@@ -197,12 +202,13 @@ private:
|
||||
/// Check if a JSON messag is valid according to a given JSON schema
|
||||
///
|
||||
/// @param message JSON message which need to be checked
|
||||
/// @param schemaResource Qt esource identifier with the JSON schema
|
||||
/// @param schemaResource Qt Resource identifier with the JSON schema
|
||||
/// @param errors Output error message
|
||||
/// @param ignoreRequired ignore the required value in JSON schema
|
||||
///
|
||||
/// @return true if message conforms the given JSON schema
|
||||
///
|
||||
bool checkJson(const Json::Value & message, const QString &schemaResource, std::string & errors);
|
||||
bool checkJson(const Json::Value & message, const QString &schemaResource, std::string & errors, bool ignoreRequired = false);
|
||||
|
||||
private:
|
||||
/// The TCP-Socket that is connected tot the Json-client
|
||||
|
@@ -13,6 +13,7 @@
|
||||
<file alias="schema-effect">schema/schema-effect.json</file>
|
||||
<file alias="schema-sourceselect">schema/schema-sourceselect.json</file>
|
||||
<file alias="schema-configget">schema/schema-configget.json</file>
|
||||
<file alias="schema-configset">schema/schema-configset.json</file>
|
||||
<file alias="schema-componentstate">schema/schema-componentstate.json</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
20
libsrc/jsonserver/schema/schema-configset.json
Normal file
20
libsrc/jsonserver/schema/schema-configset.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"type" : "object",
|
||||
"required" : true,
|
||||
"properties" : {
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["configset"]
|
||||
},
|
||||
"configset": {
|
||||
"type" : "object",
|
||||
"required" : true
|
||||
},
|
||||
"create": {
|
||||
"type" : "boolean",
|
||||
"required" : false
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["color", "image", "effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "configget", "componentstate"]
|
||||
"enum" : ["color", "image", "effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "configget", "configset", "componentstate"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user