hyperion.ng/src/hyperion-remote/hyperion-remote.cpp

138 lines
6.0 KiB
C++
Raw Normal View History

// stl includes
#include <initializer_list>
// Qt includes
#include <QCoreApplication>
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
// hyperion-remote include
#include "CustomParameter.h"
#include "Connection.h"
using namespace vlofgren;
/// Count the number of true values in a list of booleans
int count(std::initializer_list<bool> values)
{
int count = 0;
for (bool value : values) {
if (value)
count++;
}
return count;
}
int main(int argc, char * argv[])
{
QCoreApplication app(argc, argv);
try
{
// some default settings
QString defaultServerAddress = "localhost:19444";
int defaultPriority = 100;
// create the option parser and initialize all parameters
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");
// set the default values
argAddress.setDefault(defaultServerAddress.toStdString());
argPriority.setDefault(defaultPriority);
argDuration.setDefault(-1);
// parse all options
optionParser.parse(argc, const_cast<const char **>(argv));
// check if we need to display the usage. exit if we do.
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// check if at least one of the available color transforms is set
bool colorTransform = argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
// check that exactly one command was given
int commandCount = count({argColor.isSet(), argImage.isSet(), argList.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
if (commandCount != 1)
{
std::cerr << (commandCount == 0 ? "No command found." : "Multiple commands found.") << " Provide exactly 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 available 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 to the hyperion server
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)
{
ColorTransformValues threshold = argThreshold.getValue();
ColorTransformValues gamma = argGamma.getValue();
ColorTransformValues blacklevel = argBlacklevel.getValue();
ColorTransformValues 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)
{
// An error occured. Display error and quit
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}