2013-08-13 20:10:19 +02:00
// stl includes
2013-08-11 21:49:11 +02:00
# include <initializer_list>
2013-08-13 20:10:19 +02:00
// Qt includes
2013-08-11 11:54:16 +02:00
# include <QCoreApplication>
2013-08-13 20:10:19 +02:00
// getoptPlusPLus includes
2013-08-11 11:54:16 +02:00
# include <getoptPlusPlus/getoptpp.h>
2013-08-13 20:10:19 +02:00
// hyperion-remote include
2013-08-13 19:45:17 +02:00
# include "CustomParameter.h"
2013-08-17 11:54:16 +02:00
# include "JsonConnection.h"
2013-08-11 21:49:11 +02:00
2013-08-11 11:54:16 +02:00
using namespace vlofgren ;
2013-08-13 20:10:19 +02:00
/// Count the number of true values in a list of booleans
2013-08-11 21:49:11 +02:00
int count ( std : : initializer_list < bool > values )
{
2013-08-13 20:10:19 +02:00
int count = 0 ;
for ( bool value : values ) {
if ( value )
count + + ;
}
return count ;
2013-08-11 21:49:11 +02:00
}
2013-08-13 19:31:56 +02:00
int main ( int argc , char * argv [ ] )
2013-08-11 11:54:16 +02:00
{
2013-08-13 20:10:19 +02:00
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 " ) ;
2013-11-24 16:10:48 +01:00
StringParameter & argEffect = parameters . add < StringParameter > ( ' e ' , " effect " , " Enable the effect with the given name " ) ;
2013-08-21 21:50:17 +02:00
SwitchParameter < > & argServerInfo = parameters . add < SwitchParameter < > > ( ' l ' , " list " , " List server info " ) ;
2013-08-13 20:10:19 +02:00
SwitchParameter < > & argClear = parameters . add < SwitchParameter < > > ( ' x ' , " clear " , " Clear data for the priority channel provided by the -p option " ) ;
2013-08-18 13:33:56 +02:00
SwitchParameter < > & argClearAll = parameters . add < SwitchParameter < > > ( 0x0 , " clearall " , " Clear data for all active priority channels " ) ;
2013-08-21 21:50:17 +02:00
DoubleParameter & argSaturation = parameters . add < DoubleParameter > ( ' s ' , " saturation " , " Set the HSV saturation gain of the leds " ) ;
2013-11-10 14:01:46 +01:00
DoubleParameter & argValue = parameters . add < DoubleParameter > ( ' v ' , " value " , " Set the HSV value gain of the leds " ) ;
2013-08-17 11:54:16 +02:00
TransformParameter & argGamma = parameters . add < TransformParameter > ( ' g ' , " gamma " , " Set the gamma of the leds (requires 3 space seperated values) " ) ;
2013-08-13 20:10:19 +02:00
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
2013-08-21 21:50:17 +02:00
bool colorTransform = argSaturation . isSet ( ) | | argValue . isSet ( ) | | argThreshold . isSet ( ) | | argGamma . isSet ( ) | | argBlacklevel . isSet ( ) | | argWhitelevel . isSet ( ) ;
2013-08-13 20:10:19 +02:00
// check that exactly one command was given
2013-11-24 16:10:48 +01:00
int commandCount = count ( { argColor . isSet ( ) , argImage . isSet ( ) , argEffect . isSet ( ) , argServerInfo . isSet ( ) , argClear . isSet ( ) , argClearAll . isSet ( ) , colorTransform } ) ;
2013-08-13 20:10:19 +02:00
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 ;
2013-11-24 16:10:48 +01:00
std : : cerr < < " " < < argEffect . usageLine ( ) < < std : : endl ;
2013-08-17 11:54:16 +02:00
std : : cerr < < " " < < argServerInfo . usageLine ( ) < < std : : endl ;
2013-08-13 20:10:19 +02:00
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 ;
2013-08-21 21:50:17 +02:00
std : : cerr < < " " < < argSaturation . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argValue . usageLine ( ) < < std : : endl ;
2013-08-13 20:10:19 +02:00
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
2013-08-17 11:54:16 +02:00
JsonConnection connection ( argAddress . getValue ( ) , argPrint . isSet ( ) ) ;
2013-08-13 20:10:19 +02:00
// 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 ( ) ) ;
}
2013-11-24 16:10:48 +01:00
else if ( argEffect . isSet ( ) )
{
connection . setEffect ( argEffect . getValue ( ) , argPriority . getValue ( ) , argDuration . getValue ( ) ) ;
}
2013-08-17 11:54:16 +02:00
else if ( argServerInfo . isSet ( ) )
2013-08-13 20:10:19 +02:00
{
2013-08-17 11:54:16 +02:00
QString info = connection . getServerInfo ( ) ;
std : : cout < < " Server info: \n " < < info . toStdString ( ) < < std : : endl ;
2013-08-13 20:10:19 +02:00
}
else if ( argClear . isSet ( ) )
{
connection . clear ( argPriority . getValue ( ) ) ;
}
else if ( argClearAll . isSet ( ) )
{
connection . clearAll ( ) ;
}
else if ( colorTransform )
{
2013-08-21 21:50:17 +02:00
double saturation , value ;
2013-08-17 11:54:16 +02:00
ColorTransformValues threshold , gamma , blacklevel , whitelevel ;
2013-08-21 21:50:17 +02:00
if ( argSaturation . isSet ( ) ) saturation = argSaturation . getValue ( ) ;
if ( argValue . isSet ( ) ) value = argValue . getValue ( ) ;
2013-08-17 11:54:16 +02:00
if ( argThreshold . isSet ( ) ) threshold = argThreshold . getValue ( ) ;
if ( argGamma . isSet ( ) ) gamma = argGamma . getValue ( ) ;
if ( argBlacklevel . isSet ( ) ) blacklevel = argBlacklevel . getValue ( ) ;
if ( argWhitelevel . isSet ( ) ) whitelevel = argWhitelevel . getValue ( ) ;
2013-08-13 20:10:19 +02:00
connection . setTransform (
2013-08-21 21:50:17 +02:00
argSaturation . isSet ( ) ? & saturation : nullptr ,
argValue . isSet ( ) ? & value : nullptr ,
2013-08-13 20:10:19 +02:00
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 ;
2013-08-11 11:54:16 +02:00
}