2013-08-13 20:10:19 +02:00
// stl includes
2014-01-28 00:39:04 +01:00
# include <clocale>
2013-08-11 21:49:11 +02:00
# include <initializer_list>
2016-08-06 08:28:42 +02:00
# include <limits>
2013-08-11 21:49:11 +02:00
2013-08-13 20:10:19 +02:00
// Qt includes
2013-08-11 11:54:16 +02:00
# include <QCoreApplication>
2014-01-28 22:27:02 +01:00
# include <QLocale>
2013-08-11 11:54:16 +02:00
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
2016-03-10 12:01:10 +01:00
# include "HyperionConfig.h"
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
{
2016-03-10 12:01:10 +01:00
std : : cout
< < " hyperion-remote: " < < std : : endl
2016-06-24 23:22:31 +02:00
< < " \t Version : " < < HYPERION_VERSION < < " ( " < < HYPERION_BUILD_ID < < " ) " < < std : : endl
2016-03-10 12:01:10 +01:00
< < " \t build time: " < < __DATE__ < < " " < < __TIME__ < < std : : endl ;
2013-08-13 20:10:19 +02:00
QCoreApplication app ( argc , argv ) ;
2014-01-28 22:27:02 +01:00
// force the locale
setlocale ( LC_ALL , " C " ) ;
QLocale : : setDefault ( QLocale : : c ( ) ) ;
2013-08-13 20:10:19 +02:00
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 ( ) ;
2016-01-06 17:31:23 +01:00
StringParameter & argAddress = parameters . add < StringParameter > ( ' a ' , " address " , QString ( " Set the address of the hyperion server [default: %1] " ) . arg ( defaultServerAddress ) . toLatin1 ( ) . 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 ) . toLatin1 ( ) . constData ( ) ) ;
2013-08-13 20:10:19 +02:00
IntParameter & argDuration = parameters . add < IntParameter > ( ' d ' , " duration " , " Specify how long the leds should be switched on in millseconds [default: infinity] " ) ;
2013-12-13 00:01:48 +01:00
ColorParameter & argColor = parameters . add < ColorParameter > ( ' c ' , " color " , " Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB) " ) ;
2013-08-13 20:10:19 +02:00
ImageParameter & argImage = parameters . add < ImageParameter > ( ' i ' , " image " , " Set the leds to the colors according to the given image file " ) ;
2016-07-31 22:21:35 +02:00
StringParameter & argEffect = parameters . add < StringParameter > ( ' e ' , " effect " , " Enable the effect with the given name " ) ;
2016-03-11 12:20:59 +01:00
StringParameter & argEffectArgs = parameters . add < StringParameter > ( 0x0 , " effectArgs " , " Arguments to use in combination with the specified effect. Should be a Json object string. " ) ;
2016-04-28 14:46:53 +02:00
SwitchParameter < > & argServerInfo = parameters . add < SwitchParameter < > > ( ' l ' , " list " , " List server info and active effects with priority and duration " ) ;
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 " ) ;
2016-08-04 13:10:53 +02:00
StringParameter & argEnableComponent = parameters . add < StringParameter > ( ' E ' , " enable " , " Enable the Component with the given name. Available Components are [SMOOTHING, BLACKBORDER, KODICHECKER, FORWARDER, UDPLISTENER, BOBLIGHT_SERVER, GRABBER] " ) ;
StringParameter & argDisableComponent = parameters . add < StringParameter > ( ' D ' , " disable " , " Disable the Component with the given name. Available Components are [SMOOTHING, BLACKBORDER, KODICHECKER, FORWARDER, UDPLISTENER, BOBLIGHT_SERVER, GRABBER] " ) ;
2013-11-22 11:48:10 +01:00
StringParameter & argId = parameters . add < StringParameter > ( ' q ' , " qualifier " , " Identifier(qualifier) of the transform to set " ) ;
2016-04-02 00:41:01 +02:00
DoubleParameter & argSaturation = parameters . add < DoubleParameter > ( ' s ' , " saturation " , " !DEPRECATED! Will be removed soon! Set the HSV saturation gain of the leds " ) ;
DoubleParameter & argValue = parameters . add < DoubleParameter > ( ' v ' , " value " , " !DEPRECATED! Will be removed soon! Set the HSV value gain of the leds " ) ;
2016-03-13 12:10:54 +01:00
DoubleParameter & argSaturationL = parameters . add < DoubleParameter > ( ' u ' , " saturationL " , " Set the HSL saturation gain of the leds " ) ;
2016-07-31 22:21:35 +02:00
DoubleParameter & argLuminance = parameters . add < DoubleParameter > ( ' m ' , " luminance " , " Set the HSL luminance gain of the leds " ) ;
DoubleParameter & argLuminanceMin = parameters . add < DoubleParameter > ( ' n ' , " luminanceMin " , " Set the HSL luminance minimum of the leds (backlight) " ) ;
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) " ) ;
2016-04-02 00:41:01 +02:00
TransformParameter & argBlacklevel = parameters . add < TransformParameter > ( ' b ' , " blacklevel " , " !DEPRECATED! Will be removed soon! 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 " , " !DEPRECATED! Will be removed soon! Set the whitelevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0) " ) ;
2013-08-13 20:10:19 +02:00
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 " ) ;
2016-04-02 00:41:01 +02:00
StringParameter & argIdC = parameters . add < StringParameter > ( ' y ' , " qualifier " , " !DEPRECATED! Will be removed soon! Identifier(qualifier) of the correction to set " ) ;
2016-07-31 22:21:35 +02:00
CorrectionParameter & argCorrection = parameters . add < CorrectionParameter > ( ' Y ' , " correction " , " !DEPRECATED! Will be removed soon! Set the correction of the leds (requires 3 space seperated values between 0 and 255) " ) ;
2016-03-23 13:35:38 +01:00
StringParameter & argIdT = parameters . add < StringParameter > ( ' z ' , " qualifier " , " Identifier(qualifier) of the temperature correction to set " ) ;
2016-07-31 22:21:35 +02:00
CorrectionParameter & argTemperature = parameters . add < CorrectionParameter > ( ' Z ' , " temperature " , " Set the temperature correction of the leds (requires 3 space seperated values between 0 and 255) " ) ;
StringParameter & argIdA = parameters . add < StringParameter > ( ' j ' , " qualifier " , " Identifier(qualifier) of the adjustment to set " ) ;
2016-04-02 00:04:11 +02:00
AdjustmentParameter & argRAdjust = parameters . add < AdjustmentParameter > ( ' R ' , " redAdjustment " , " Set the adjustment of the red color (requires 3 space seperated values between 0 and 255) " ) ;
AdjustmentParameter & argGAdjust = parameters . add < AdjustmentParameter > ( ' G ' , " greenAdjustment " , " Set the adjustment of the green color (requires 3 space seperated values between 0 and 255) " ) ;
AdjustmentParameter & argBAdjust = parameters . add < AdjustmentParameter > ( ' B ' , " blueAdjustment " , " Set the adjustment of the blue color (requires 3 space seperated values between 0 and 255) " ) ;
2016-07-31 22:21:35 +02:00
IntParameter & argSource = parameters . add < IntParameter > ( 0x0 , " sourceSelect " , " Set current active priority channel and deactivate auto source switching " ) ;
SwitchParameter < > & argSourceAuto = parameters . add < SwitchParameter < > > ( 0x0 , " sourceAutoSelect " , " Enables auto source, if disabled prio by manual selecting input source " ) ;
2016-08-06 08:28:42 +02:00
SwitchParameter < > & argSourceOff = parameters . add < SwitchParameter < > > ( 0x0 , " sourceOff " , " select no source, this results in leds activly set to black (=off) " ) ;
2016-08-03 22:03:19 +02:00
SwitchParameter < > & argConfigGet = parameters . add < SwitchParameter < > > ( 0x0 , " configget " , " Print the current loaded Hyperion configuration file " ) ;
2013-08-13 20:10:19 +02:00
// set the default values
argAddress . setDefault ( defaultServerAddress . toStdString ( ) ) ;
argPriority . setDefault ( defaultPriority ) ;
argDuration . setDefault ( - 1 ) ;
2013-12-01 14:09:01 +01:00
argEffectArgs . setDefault ( " " ) ;
2013-08-13 20:10:19 +02:00
// 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
2016-05-23 00:00:48 +02:00
bool colorTransform = argSaturation . isSet ( ) | | argValue . isSet ( ) | | argSaturationL . isSet ( ) | | argLuminance . isSet ( ) | | argLuminanceMin . isSet ( ) | | argThreshold . isSet ( ) | | argGamma . isSet ( ) | | argBlacklevel . isSet ( ) | | argWhitelevel . isSet ( ) ;
2016-04-02 00:04:11 +02:00
bool colorAdjust = argRAdjust . isSet ( ) | | argGAdjust . isSet ( ) | | argBAdjust . isSet ( ) ;
bool colorModding = colorTransform | | colorAdjust | | argCorrection . isSet ( ) | | argTemperature . isSet ( ) ;
2016-03-23 13:35:38 +01:00
2013-08-13 20:10:19 +02:00
// check that exactly one command was given
2016-08-06 08:28:42 +02:00
int commandCount = count ( { argColor . isSet ( ) , argImage . isSet ( ) , argEffect . isSet ( ) , argServerInfo . isSet ( ) , argClear . isSet ( ) , argClearAll . isSet ( ) , argEnableComponent . isSet ( ) , argDisableComponent . isSet ( ) , colorModding , argSource . isSet ( ) , argSourceAuto . isSet ( ) , argSourceOff . isSet ( ) , argConfigGet . isSet ( ) } ) ;
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 ;
2016-03-21 17:19:06 +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 ;
2016-08-04 13:10:53 +02:00
std : : cerr < < " " < < argEnableComponent . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argDisableComponent . usageLine ( ) < < std : : endl ;
2016-08-03 22:03:19 +02:00
std : : cerr < < " " < < argSource . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argSourceAuto . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argConfigGet . usageLine ( ) < < std : : endl ;
2016-03-23 13:35:38 +01:00
std : : cerr < < " or one or more of the available color modding operations: " < < std : : endl ;
2013-11-22 11:48:10 +01:00
std : : cerr < < " " < < argId . usageLine ( ) < < std : : endl ;
2013-08-21 21:50:17 +02:00
std : : cerr < < " " < < argSaturation . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argValue . usageLine ( ) < < std : : endl ;
2016-03-11 12:20:59 +01:00
std : : cerr < < " " < < argSaturationL . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argLuminance . usageLine ( ) < < std : : endl ;
2016-05-23 00:00:48 +02:00
std : : cerr < < " " < < argLuminanceMin . 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 ;
2016-03-11 12:20:59 +01:00
std : : cerr < < " " < < argIdC . usageLine ( ) < < std : : endl ;
2016-03-21 17:19:06 +01:00
std : : cerr < < " " < < argCorrection . usageLine ( ) < < std : : endl ;
2016-03-11 12:20:59 +01:00
std : : cerr < < " " < < argIdT . usageLine ( ) < < std : : endl ;
2016-03-21 17:19:06 +01:00
std : : cerr < < " " < < argTemperature . usageLine ( ) < < std : : endl ;
2016-04-02 00:04:11 +02:00
std : : cerr < < " " < < argIdA . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argRAdjust . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argGAdjust . usageLine ( ) < < std : : endl ;
std : : cerr < < " " < < argBAdjust . usageLine ( ) < < std : : endl ;
2013-08-13 20:10:19 +02:00
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 ( ) ) ;
}
2016-03-11 12:20:59 +01:00
else if ( argEffect . isSet ( ) )
{
connection . setEffect ( argEffect . getValue ( ) , argEffectArgs . getValue ( ) , argPriority . getValue ( ) , argDuration . getValue ( ) ) ;
}
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 ( ) ;
}
2016-08-04 13:10:53 +02:00
else if ( argEnableComponent . isSet ( ) )
{
connection . setComponentState ( argEnableComponent . getValue ( ) , true ) ;
}
else if ( argDisableComponent . isSet ( ) )
{
connection . setComponentState ( argDisableComponent . getValue ( ) , false ) ;
}
2016-08-06 08:28:42 +02:00
else if ( argSourceOff . isSet ( ) )
{
connection . setSource ( std : : numeric_limits < int > : : max ( ) ) ;
}
2016-07-31 22:21:35 +02:00
else if ( argSource . isSet ( ) )
{
connection . setSource ( argSource . getValue ( ) ) ;
}
else if ( argSourceAuto . isSet ( ) )
{
connection . setSourceAutoSelect ( ) ;
}
2016-08-03 22:03:19 +02:00
else if ( argConfigGet . isSet ( ) )
{
QString info = connection . getConfigFile ( ) ;
std : : cout < < " Configuration File: \n " < < info . toStdString ( ) < < std : : endl ;
}
2016-03-23 13:35:38 +01:00
else if ( colorModding )
{
if ( argCorrection . isSet ( ) )
{
std : : string corrId ;
ColorCorrectionValues correction ;
if ( argIdC . isSet ( ) ) corrId = argIdC . getValue ( ) ;
if ( argCorrection . isSet ( ) ) correction = argCorrection . getValue ( ) ;
2016-07-01 23:20:41 +02:00
connection . setTemperature (
2016-03-23 13:35:38 +01:00
argIdC . isSet ( ) ? & corrId : nullptr ,
argCorrection . isSet ( ) ? & correction : nullptr ) ;
}
if ( argTemperature . isSet ( ) )
{
std : : string tempId ;
ColorCorrectionValues temperature ;
if ( argIdT . isSet ( ) ) tempId = argIdT . getValue ( ) ;
if ( argTemperature . isSet ( ) ) temperature = argTemperature . getValue ( ) ;
2016-03-13 21:14:22 +01:00
2016-03-23 13:35:38 +01:00
connection . setTemperature (
argIdT . isSet ( ) ? & tempId : nullptr ,
argTemperature . isSet ( ) ? & temperature : nullptr ) ;
}
2016-04-02 00:04:11 +02:00
if ( colorAdjust )
{
std : : string adjustId ;
ColorAdjustmentValues redChannel , greenChannel , blueChannel ;
if ( argIdA . isSet ( ) ) adjustId = argIdA . getValue ( ) ;
if ( argRAdjust . isSet ( ) ) redChannel = argRAdjust . getValue ( ) ;
if ( argGAdjust . isSet ( ) ) greenChannel = argGAdjust . getValue ( ) ;
if ( argBAdjust . isSet ( ) ) blueChannel = argBAdjust . getValue ( ) ;
connection . setAdjustment (
argIdA . isSet ( ) ? & adjustId : nullptr ,
argRAdjust . isSet ( ) ? & redChannel : nullptr ,
argGAdjust . isSet ( ) ? & greenChannel : nullptr ,
argBAdjust . isSet ( ) ? & blueChannel : nullptr ) ;
}
2016-03-23 13:35:38 +01:00
if ( colorTransform )
{
std : : string transId ;
2016-05-23 00:00:48 +02:00
double saturation , value , saturationL , luminance , luminanceMin ;
2016-03-23 13:35:38 +01:00
ColorTransformValues threshold , gamma , blacklevel , whitelevel ;
if ( argId . isSet ( ) ) transId = argId . getValue ( ) ;
if ( argSaturation . isSet ( ) ) saturation = argSaturation . getValue ( ) ;
if ( argValue . isSet ( ) ) value = argValue . getValue ( ) ;
if ( argSaturationL . isSet ( ) ) saturationL = argSaturationL . getValue ( ) ;
if ( argLuminance . isSet ( ) ) luminance = argLuminance . getValue ( ) ;
2016-05-23 00:00:48 +02:00
if ( argLuminanceMin . isSet ( ) ) luminanceMin = argLuminanceMin . getValue ( ) ;
2016-03-23 13:35:38 +01: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 ( ) ;
connection . setTransform (
2013-11-22 11:48:10 +01:00
argId . isSet ( ) ? & transId : nullptr ,
2013-08-21 21:50:17 +02:00
argSaturation . isSet ( ) ? & saturation : nullptr ,
argValue . isSet ( ) ? & value : nullptr ,
2016-03-11 12:20:59 +01:00
argSaturationL . isSet ( ) ? & saturationL : nullptr ,
2016-03-21 17:19:06 +01:00
argLuminance . isSet ( ) ? & luminance : nullptr ,
2016-05-23 00:00:48 +02:00
argLuminanceMin . isSet ( ) ? & luminanceMin : 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 ) ;
2016-03-23 13:35:38 +01:00
}
2016-03-11 12:20:59 +01:00
}
2013-08-13 20:10:19 +02:00
}
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
}