added skeleton for the json connection server

This commit is contained in:
johan
2013-08-17 15:39:29 +02:00
parent 2c97353a11
commit 16c260b3dc
9 changed files with 269 additions and 17 deletions

View File

@@ -6,7 +6,8 @@ add_executable(hyperiond
hyperion-d.cpp)
target_link_libraries(hyperiond
hyperion)
hyperion
jsonserver)
# Find the libPNG
find_package(PNG QUIET)

View File

@@ -9,6 +9,9 @@
#include <hyperion/DispmanxWrapper.h>
#include <hyperion/Hyperion.h>
// JsonServer includes
#include <jsonserver/JsonServer.h>
int main(int argc, char** argv)
{
// Initialising QCoreApplication
@@ -16,9 +19,9 @@ int main(int argc, char** argv)
std::cout << "QCoreApplication initialised" << std::endl;
// Select config and schema file
const std::string homeDir = getenv("RASPILIGHT_HOME");
const std::string schemaFile = homeDir + "/hyperion.schema.json";
const std::string configFile = homeDir + "/hyperion.config.json";
//const std::string homeDir = getenv("RASPILIGHT_HOME");
const std::string schemaFile = "hyperion.schema.json";
const std::string configFile = "hyperion.config.json";
// Load configuration and check against the schema at the same time
Json::Value config;
@@ -36,6 +39,9 @@ int main(int argc, char** argv)
dispmanx.start();
std::cout << "Frame grabber created and started" << std::endl;
JsonServer jsonServer(&hyperion);
std::cout << "Json server created and started on port " << jsonServer.getPort() << std::endl;
app.exec();
std::cout << "Application closed" << std::endl;
}

View File

@@ -210,16 +210,15 @@ void JsonConnection::setTransform(ColorTransformValues *threshold, ColorTransfor
Json::Value JsonConnection::sendMessage(const Json::Value & message)
{
// serialize message (FastWriter already appends a newline)
std::string serializedMessage = Json::FastWriter().write(message);
// print command if requested
if (_printJson)
{
std::cout << "Command: " << message << std::endl;
std::cout << "Command: " << serializedMessage;
}
// serialize message (FastWriter already appends a newline
Json::FastWriter jsonWriter;
std::string serializedMessage = jsonWriter.write(message);
// write message
_socket.write(serializedMessage.c_str());
if (!_socket.waitForBytesWritten())
@@ -241,6 +240,12 @@ Json::Value JsonConnection::sendMessage(const Json::Value & message)
}
int bytes = serializedReply.indexOf('\n') + 1; // Find the end of message
// print reply if requested
if (_printJson)
{
std::cout << "Reply: " << std::string(serializedReply.data(), bytes);
}
// parse reply data
Json::Reader jsonReader;
Json::Value reply;
@@ -249,12 +254,6 @@ Json::Value JsonConnection::sendMessage(const Json::Value & message)
throw std::runtime_error("Error while parsing reply: invalid json");
}
// print reply if requested
if (_printJson)
{
std::cout << "Reply:" << reply << std::endl;
}
return reply;
}
@@ -271,12 +270,12 @@ bool JsonConnection::parseReply(const Json::Value &reply)
}
catch (const std::runtime_error &)
{
// Some json paring error: ignore and set parsing error
// Some json parsing error: ignore and set parsing error
}
if (!success)
{
throw std::runtime_error("Error while paring reply: " + reason);
throw std::runtime_error("Error while executing command: " + reason);
}
return success;