Add hyperion-remote instance cmd (#596)

* Add hyperion-remote instance cmd

- Adds a new command to hyperion-remote that allows the selection of a instance by name
- Fixed broken umlauts
- Fixed wrong text placement inside icon tags

* Add warning if selected instance is not found
This commit is contained in:
brindosch 2019-08-13 23:22:06 +02:00 committed by GitHub
parent 034f821d46
commit d190e6f294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 13 deletions

View File

@ -65,13 +65,13 @@ $(document).ready( function() {
var startBtnColor = inst[key].running ? "success" : "danger"; var startBtnColor = inst[key].running ? "success" : "danger";
var startBtnIcon = inst[key].running ? "stop" : "play"; var startBtnIcon = inst[key].running ? "stop" : "play";
var startBtnText = inst[key].running ? $.i18n('general_btn_stop') : $.i18n('general_btn_start'); var startBtnText = inst[key].running ? $.i18n('general_btn_stop') : $.i18n('general_btn_start');
var renameBtn = '<button id="instren_'+inst[key].instance+'" type="button" class="btn btn-primary"><i class="fa fa-pencil"> '+$.i18n('general_btn_rename')+'</i></button>'; var renameBtn = '<button id="instren_'+inst[key].instance+'" type="button" class="btn btn-primary"><i class="fa fa-fw fa-pencil"></i>'+$.i18n('general_btn_rename')+'</button>';
var startBtn = "" var startBtn = ""
var delBtn = ""; var delBtn = "";
if(inst[key].instance > 0) if(inst[key].instance > 0)
{ {
delBtn = '<button id="instdel_'+inst[key].instance+'" type="button" class="btn btn-warning"><i class="fa fa-remove"> '+$.i18n('general_btn_delete')+'</i></button>'; delBtn = '<button id="instdel_'+inst[key].instance+'" type="button" class="btn btn-warning"><i class="fa fa-fw fa-remove"></i>'+$.i18n('general_btn_delete')+'</button>';
startBtn = '<button id="inst_'+inst[key].instance+'" type="button" class="btn btn-'+startBtnColor+'"><i class="fa fa-'+startBtnIcon+'"> '+startBtnText+'</i></button>'; startBtn = '<button id="inst_'+inst[key].instance+'" type="button" class="btn btn-'+startBtnColor+'"><i class="fa fa-fw fa-'+startBtnIcon+'"></i>'+startBtnText+'</button>';
} }
$('.itbody').append(createTableRow([inst[key].friendly_name, renameBtn, startBtn, delBtn], false, true)); $('.itbody').append(createTableRow([inst[key].friendly_name, renameBtn, startBtn, delBtn], false, true));
$('#instren_'+inst[key].instance).off().on('click', handleInstanceRename); $('#instren_'+inst[key].instance).off().on('click', handleInstanceRename);

View File

@ -159,7 +159,7 @@ function sendToHyperion(command, subcommand, msg)
else else
msg = ""; msg = "";
window.websocket.send(encode_utf8('{"command":"'+command+'", "tan":'+window.wsTan+subcommand+msg+'}')); window.websocket.send('{"command":"'+command+'", "tan":'+window.wsTan+subcommand+msg+'}');
} }
// ----------------------------------------------------------- // -----------------------------------------------------------

View File

@ -199,7 +199,14 @@ void JsonConnection::deleteEffect(const QString &effectName)
parseReply(reply); parseReply(reply);
} }
QString JsonConnection::getServerInfo() QString JsonConnection::getServerInfoString()
{
QJsonDocument doc(getServerInfo());
QString info(doc.toJson(QJsonDocument::Indented));
return info;
}
QJsonObject JsonConnection::getServerInfo()
{ {
qDebug() << "Get server info"; qDebug() << "Get server info";
@ -218,12 +225,10 @@ QString JsonConnection::getServerInfo()
throw std::runtime_error("No info available in result"); throw std::runtime_error("No info available in result");
} }
QJsonDocument doc(reply["info"].toObject()); return reply["info"].toObject();
QString info(doc.toJson(QJsonDocument::Indented));
return info;
} }
return QString(); return QJsonObject();
} }
QString JsonConnection::getSysInfo() QString JsonConnection::getSysInfo()
@ -562,6 +567,21 @@ void JsonConnection::setToken(const QString &token)
parseReply(reply); parseReply(reply);
} }
void JsonConnection::setInstance(const int &instance)
{
// create command
QJsonObject command;
command["command"] = QString("instance");
command["subcommand"] = QString("switchTo");
command["instance"] = instance;
// send command message
QJsonObject reply = sendMessage(command);
// parse reply message
parseReply(reply);
}
QJsonObject JsonConnection::sendMessage(const QJsonObject & message) QJsonObject JsonConnection::sendMessage(const QJsonObject & message)
{ {
// serialize message // serialize message

View File

@ -73,11 +73,18 @@ public:
void deleteEffect(const QString &effectName); void deleteEffect(const QString &effectName);
/// ///
/// Retrieve a list of all occupied priority channels /// Retrieve entire serverinfo as String
/// ///
/// @return String with the server info /// @return String with the server info
/// ///
QString getServerInfo(); QString getServerInfoString();
///
/// Get the entire serverinfo
///
/// @return QJsonObject with the server info
///
QJsonObject getServerInfo();
/// ///
/// Retrieve system info /// Retrieve system info
@ -174,6 +181,12 @@ public:
// set the specified authorization token // set the specified authorization token
void setToken(const QString &token); void setToken(const QString &token);
///
/// Send a json message with a specific instance id
/// @param instance The instance id
///
void setInstance(const int &instance);
private: private:
/// ///

View File

@ -39,6 +39,20 @@ void showHelp(Option & option){
qWarning() << qPrintable(QString("\t%1\t%2\t%3").arg(shortOption, longOption, option.description())); qWarning() << qPrintable(QString("\t%1\t%2\t%3").arg(shortOption, longOption, option.description()));
} }
int getInstaneIdbyName(const QJsonObject & reply, const QString name){
if(reply.contains("instance")){
QJsonArray list = reply.value("instance").toArray();
for (const auto & entry : list) {
const QJsonObject obj = entry.toObject();
if(obj["friendly_name"] == name && obj["running"].toBool())
return obj["instance"].toInt();
}
}
std::cout << "Can't find a running instance with name '" << name.toStdString()<< "' at this Hyperion server, will use first instance" << std::endl;
return 0;
}
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
setenv("AVAHI_COMPAT_NOWARN", "1", 1); setenv("AVAHI_COMPAT_NOWARN", "1", 1);
@ -64,6 +78,7 @@ int main(int argc, char * argv[])
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Option & argAddress = parser.add<Option> ('a', "address" , "Set the address of the hyperion server [default: %1]", "localhost:19444"); Option & argAddress = parser.add<Option> ('a', "address" , "Set the address of the hyperion server [default: %1]", "localhost:19444");
Option & argToken = parser.add<Option> ('t', "token " , "If authorization tokens are required, this token is used"); Option & argToken = parser.add<Option> ('t', "token " , "If authorization tokens are required, this token is used");
Option & argInstance = parser.add<Option> ('I', "instance" , "Select a specific target instance by name for your command. By befault it uses always the first instance");
IntOption & argPriority = parser.add<IntOption> ('p', "priority" , "Used to the provided priority channel (suggested 2-99) [default: %1]", "50"); IntOption & argPriority = parser.add<IntOption> ('p', "priority" , "Used to the provided priority channel (suggested 2-99) [default: %1]", "50");
IntOption & argDuration = parser.add<IntOption> ('d', "duration" , "Specify how long the leds should be switched on in milliseconds [default: infinity]"); IntOption & argDuration = parser.add<IntOption> ('d', "duration" , "Specify how long the leds should be switched on in milliseconds [default: infinity]");
ColorsOption & argColor = parser.add<ColorsOption> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex getColors or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)"); ColorsOption & argColor = parser.add<ColorsOption> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex getColors or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)");
@ -165,6 +180,14 @@ int main(int argc, char * argv[])
if (parser.isSet(argToken)) if (parser.isSet(argToken))
connection.setToken(argToken.value(parser)); connection.setToken(argToken.value(parser));
// If a specific Hyperion instance is given, set it
if (parser.isSet(argInstance))
{
QJsonObject reply = connection.getServerInfo();
int val = getInstaneIdbyName(reply, argInstance.value(parser));
connection.setInstance(val);
}
// now execute the given command // now execute the given command
if (parser.isSet(argColor)) if (parser.isSet(argColor))
{ {
@ -189,7 +212,7 @@ int main(int argc, char * argv[])
} }
else if (parser.isSet(argServerInfo)) else if (parser.isSet(argServerInfo))
{ {
std::cout << "Server info:\n" << connection.getServerInfo().toStdString() << std::endl; std::cout << "Server info:\n" << connection.getServerInfoString().toStdString() << std::endl;
} }
else if (parser.isSet(argSysInfo)) else if (parser.isSet(argSysInfo))
{ {