QSocket connection added to hyperion-remote

This commit is contained in:
johan
2013-08-13 19:31:56 +02:00
59 changed files with 2236 additions and 3725 deletions

View File

@@ -12,7 +12,8 @@ include_directories(${QT_INCLUDES})
set(hyperion-remote_HEADERS
specialoptions.h
connection.h)
connection.h
colortransform.h)
set(hyperion-remote_SOURCES
hyperion-remote.cpp

View File

@@ -0,0 +1,8 @@
#pragma once
struct ColorTransform
{
double valueRed;
double valueGreen;
double valueBlue;
};

View File

@@ -1,16 +1,41 @@
#include <stdexcept>
#include "connection.h"
Connection::Connection(const std::string &address, bool printJson)
Connection::Connection(const std::string & a, bool printJson) :
_printJson(printJson),
_socket()
{
QString address(a.c_str());
QStringList parts = address.split(":");
if (parts.size() != 2)
{
throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString());
}
bool ok;
uint16_t port = parts[1].toUShort(&ok);
if (!ok)
{
throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
}
_socket.connectToHost(parts[0], port);
if (!_socket.waitForConnected())
{
throw std::runtime_error("Unable to connect to host");
}
}
Connection::~Connection()
{
_socket.close();
}
bool Connection::setColor(QColor color, int priority, int duration)
{
std::cout << "Set color to " << color.red() << " " << color.green() << " " << color.blue() << std::endl;
sendMessage("Set color message");
return false;
}
@@ -38,32 +63,49 @@ bool Connection::clearAll()
return false;
}
bool Connection::setThreshold(double red, double green, double blue)
bool Connection::setTransform(ColorTransform *threshold, ColorTransform *gamma, ColorTransform *blacklevel, ColorTransform *whitelevel)
{
std::cout << "Set color transforms" << std::endl;
return false;
}
bool Connection::setGamma(double red, double green, double blue)
{
return false;
}
bool Connection::setBlacklevel(double red, double green, double blue)
{
return false;
}
bool Connection::setWhitelevel(double red, double green, double blue)
{
return false;
}
bool Connection::sendMessage(const Json::Value &message)
bool Connection::sendMessage(const Json::Value & message)
{
// rpint if requested
if (_printJson)
{
std::cout << "Command:" << std::endl;
std::cout << message << std::endl;
std::cout << "Command: " << message << std::endl;
}
// serialize message
Json::FastWriter jsonWriter;
std::string serializedMessage = jsonWriter.write(message);
// write message
_socket.write(serializedMessage.c_str());
if (!_socket.waitForBytesWritten())
{
throw std::runtime_error("Error while writing data to host");
}
// receive reply
if (!_socket.waitForReadyRead())
{
throw std::runtime_error("Error while reading data from host");
}
char data[1024 * 100];
uint64_t count = _socket.read(data, sizeof(data));
std::string serializedReply(data, count);
Json::Reader jsonReader;
Json::Value reply;
if (!jsonReader.parse(serializedReply, reply))
{
throw std::runtime_error("Error while parsing reply:" + serializedReply);
}
if (_printJson)
{
std::cout << "Reply:" << reply << std::endl;
}
return true;

View File

@@ -4,9 +4,12 @@
#include <QColor>
#include <QImage>
#include <QTcpSocket>
#include <json/json.h>
#include "colortransform.h"
class Connection
{
public:
@@ -18,14 +21,13 @@ public:
bool listPriorities();
bool clear(int priority);
bool clearAll();
bool setThreshold(double red, double green, double blue);
bool setGamma(double red, double green, double blue);
bool setBlacklevel(double red, double green, double blue);
bool setWhitelevel(double red, double green, double blue);
bool setTransform(ColorTransform * threshold, ColorTransform * gamma, ColorTransform * blacklevel, ColorTransform * whitelevel);
private:
bool sendMessage(const Json::Value & message);
private:
bool _printJson;
QTcpSocket _socket;
};

View File

@@ -20,36 +20,112 @@ int count(std::initializer_list<bool> values)
return count;
}
int main(int argc, const char * argv[])
int main(int argc, char * argv[])
{
// some settings
QString defaultServerAddress = "localhost:19444";
int defaultPriority = 100;
OptionsParser optionParser("Simple application to send a command to hyperion using the Json interface");
ParameterSet & parameters = optionParser.getParameters();
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData());
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData());
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name)");
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
SwitchParameter<> & argList = parameters.add<SwitchParameter<> >('l', "list" , "List all priority channels which are in use");
SwitchParameter<> & argClear = parameters.add<SwitchParameter<> >('x', "clear" , "Clear data for the priority channel provided by the -p option");
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clear-all" , "Clear data for all priority channels");
TransformParameter & argGamma = parameters.add<TransformParameter>('g', "gamma" , "Set the gamma of the leds (requires 3 values)");
TransformParameter & argThreshold = parameters.add<TransformParameter>('t', "threshold" , "Set the threshold of the leds (requires 3 space seperated values between 0.0 and 1.0)");
TransformParameter & argBlacklevel = parameters.add<TransformParameter>('b', "blacklevel", "Set the blacklevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0)");
TransformParameter & argWhitelevel = parameters.add<TransformParameter>('w', "whitelevel", "Set the whitelevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0)");
SwitchParameter<> & argPrint = parameters.add<SwitchParameter<> >(0x0, "print" , "Print the json input and output messages on stdout");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<> >('h', "help" , "Show this help message and exit");
argAddress.setDefault(defaultServerAddress.toStdString());
argPriority.setDefault(defaultPriority);
argDuration.setDefault(-1);
QCoreApplication app(argc, argv);
try
{
optionParser.parse(argc, argv);
// some settings
QString defaultServerAddress = "localhost:19444";
int defaultPriority = 100;
OptionsParser optionParser("Simple application to send a command to hyperion using the Json interface");
ParameterSet & parameters = optionParser.getParameters();
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData());
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData());
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name)");
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
SwitchParameter<> & argList = parameters.add<SwitchParameter<> >('l', "list" , "List all priority channels which are in use");
SwitchParameter<> & argClear = parameters.add<SwitchParameter<> >('x', "clear" , "Clear data for the priority channel provided by the -p option");
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clear-all" , "Clear data for all priority channels");
TransformParameter & argGamma = parameters.add<TransformParameter>('g', "gamma" , "Set the gamma of the leds (requires 3 values)");
TransformParameter & argThreshold = parameters.add<TransformParameter>('t', "threshold" , "Set the threshold of the leds (requires 3 space seperated values between 0.0 and 1.0)");
TransformParameter & argBlacklevel = parameters.add<TransformParameter>('b', "blacklevel", "Set the blacklevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0)");
TransformParameter & argWhitelevel = parameters.add<TransformParameter>('w', "whitelevel", "Set the whitelevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0)");
SwitchParameter<> & argPrint = parameters.add<SwitchParameter<> >(0x0, "print" , "Print the json input and output messages on stdout");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<> >('h', "help" , "Show this help message and exit");
argAddress.setDefault(defaultServerAddress.toStdString());
argPriority.setDefault(defaultPriority);
argDuration.setDefault(-1);
// parse the options
optionParser.parse(argc, const_cast<const char **>(argv));
// check if we need to display the usage
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// check if a color transform is set
bool colorTransform = argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
// check if exactly one command was given
int commandCount = count({argColor.isSet(), argImage.isSet(), argList.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
if (commandCount != 1)
{
if (commandCount == 0)
{
std::cerr << "No command found. Provide one of the following options:" << std::endl;
}
else
{
std::cerr << "Multiple commands found. Provide one of the following options:" << std::endl;
}
std::cerr << " " << argColor.usageLine() << std::endl;
std::cerr << " " << argImage.usageLine() << std::endl;
std::cerr << " " << argList.usageLine() << std::endl;
std::cerr << " " << argClear.usageLine() << std::endl;
std::cerr << " " << argClearAll.usageLine() << std::endl;
std::cerr << "or one or more of the color transformations:" << std::endl;
std::cerr << " " << argThreshold.usageLine() << std::endl;
std::cerr << " " << argGamma.usageLine() << std::endl;
std::cerr << " " << argBlacklevel.usageLine() << std::endl;
std::cerr << " " << argWhitelevel.usageLine() << std::endl;
return 1;
}
// create the connection
Connection connection(argAddress.getValue(), argPrint.isSet());
// now execute the given command
if (argColor.isSet())
{
connection.setColor(argColor.getValue(), argPriority.getValue(), argDuration.getValue());
}
else if (argImage.isSet())
{
connection.setImage(argImage.getValue(), argPriority.getValue(), argDuration.getValue());
}
else if (argList.isSet())
{
connection.listPriorities();
}
else if (argClear.isSet())
{
connection.clear(argPriority.getValue());
}
else if (argClearAll.isSet())
{
connection.clearAll();
}
else if (colorTransform)
{
ColorTransform threshold = argThreshold.getValue();
ColorTransform gamma = argGamma.getValue();
ColorTransform blacklevel = argBlacklevel.getValue();
ColorTransform whitelevel = argWhitelevel.getValue();
connection.setTransform(
argThreshold.isSet() ? &threshold : nullptr,
argGamma.isSet() ? &gamma : nullptr,
argBlacklevel.isSet() ? &blacklevel : nullptr,
argWhitelevel.isSet() ? &whitelevel : nullptr);
}
}
catch (const std::runtime_error & e)
{
@@ -57,67 +133,5 @@ int main(int argc, const char * argv[])
return 1;
}
// check if we need to display the usage
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// check if a color transform is set
bool colorTransform = argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
// check if exactly one command was given
int commandCount = count({argColor.isSet(), argImage.isSet(), argList.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
if (commandCount != 1)
{
if (commandCount == 0)
{
std::cerr << "No command found. Provide one of the following options:" << std::endl;
}
else
{
std::cerr << "Multiple commands found. Provide one of the following options:" << std::endl;
}
std::cerr << " " << argColor.usageLine() << std::endl;
std::cerr << " " << argImage.usageLine() << std::endl;
std::cerr << " " << argList.usageLine() << std::endl;
std::cerr << " " << argClear.usageLine() << std::endl;
std::cerr << " " << argClearAll.usageLine() << std::endl;
std::cerr << "or one or more of the color transformations:" << std::endl;
std::cerr << " " << argThreshold.usageLine() << std::endl;
std::cerr << " " << argGamma.usageLine() << std::endl;
std::cerr << " " << argBlacklevel.usageLine() << std::endl;
std::cerr << " " << argWhitelevel.usageLine() << std::endl;
return 1;
}
Connection connection(argAddress.getValue(), argPrint.isSet());
// now execute the given command
if (argColor.isSet())
{
connection.setColor(argColor.getValue(), argPriority.getValue(), argDuration.getValue());
}
else if (argImage.isSet())
{
connection.setImage(argImage.getValue(), argPriority.getValue(), argDuration.getValue());
}
else if (argList.isSet())
{
connection.listPriorities();
}
else if (argClear.isSet())
{
connection.clear(argPriority.getValue());
}
else if (argClearAll.isSet())
{
connection.clearAll();
}
else if (colorTransform)
{
}
return 0;
}

View File

@@ -5,12 +5,7 @@
#include <getoptPlusPlus/getoptpp.h>
struct ColorTransform
{
double valueRed;
double valueGreen;
double valueBlue;
};
#include "colortransform.h"
typedef vlofgren::PODParameter<QColor> ColorParameter;
typedef vlofgren::PODParameter<QImage> ImageParameter;