add command for getting schema via json api (#179)

* - update coding style
- add command for getting schema via json api
- json api: merge config commands into one single "config" command with subcommands

* make setconfig work
This commit is contained in:
redPanther
2016-08-15 22:32:01 +02:00
committed by GitHub
parent 75fd7ed8ba
commit 8e8c21fa3b
11 changed files with 183 additions and 90 deletions

View File

@@ -14,6 +14,7 @@
#include <QCryptographicHash>
#include <QHostInfo>
#include <QString>
#include <QFile>
// hyperion util includes
#include <hyperion/ImageProcessorFactory.h>
@@ -266,10 +267,8 @@ void JsonClientConnection::handleMessage(const std::string &messageString)
handleAdjustmentCommand(message);
else if (command == "sourceselect")
handleSourceSelectCommand(message);
else if (command == "configget")
handleConfigGetCommand(message);
else if (command == "configset")
handleConfigSetCommand(message);
else if (command == "config")
handleConfigCommand(message);
else if (command == "componentstate")
handleComponentStateCommand(message);
else
@@ -833,7 +832,28 @@ void JsonClientConnection::handleSourceSelectCommand(const Json::Value & message
}
}
void JsonClientConnection::handleConfigGetCommand(const Json::Value &)
void JsonClientConnection::handleConfigCommand(const Json::Value & message)
{
std::string subcommand = message.get("subcommand","").asString();
if (subcommand == "getschema")
{
handleSchemaGetCommand(message);
}
else if (subcommand == "getconfig")
{
handleConfigGetCommand(message);
}
else if (subcommand == "setconfig")
{
handleConfigSetCommand(message);
}
else
{
sendErrorReply("unknown or missing subcommand");
}
}
void JsonClientConnection::handleConfigGetCommand(const Json::Value & message)
{
// create result
Json::Value result;
@@ -845,6 +865,30 @@ void JsonClientConnection::handleConfigGetCommand(const Json::Value &)
sendMessage(result);
}
void JsonClientConnection::handleSchemaGetCommand(const Json::Value & message)
{
// create result
Json::Value result;
result["success"] = true;
Json::Value & schemaJson = result["result"];
// make sure the resources are loaded (they may be left out after static linking)
Q_INIT_RESOURCE(resource);
// read the json schema from the resource
QResource schemaData(":/hyperion-schema");
assert(schemaData.isValid());
Json::Reader jsonReader;
if (!jsonReader.parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schemaJson, false))
{
throw std::runtime_error("ERROR: Json schema wrong: " + jsonReader.getFormattedErrorMessages()) ;
}
// send the result
sendMessage(result);
}
void JsonClientConnection::handleConfigSetCommand(const Json::Value &message)
{
struct nested
@@ -871,10 +915,10 @@ void JsonClientConnection::handleConfigSetCommand(const Json::Value &message)
if(message.size() > 0)
{
if (message.isObject() && message.isMember("configset"))
if (message.isObject() && message.isMember("config"))
{
std::string errors;
if (!checkJson(message["configset"], ":/hyperion-schema", errors, true))
if (!checkJson(message["config"], ":/hyperion-schema", errors, true))
{
sendErrorReply("Error while validating json: " + errors);
return;
@@ -882,7 +926,7 @@ void JsonClientConnection::handleConfigSetCommand(const Json::Value &message)
bool createKey = message.isMember("create");
Json::Value hyperionConfig = _hyperion->getJsonConfig();
nested::configSetCommand(message["configset"], hyperionConfig, createKey);
nested::configSetCommand(message["config"], hyperionConfig, createKey);
JsonFactory::writeJson(_hyperion->getConfigFileName(), hyperionConfig);