@@ -300,8 +300,30 @@ void JsonAPI::handleInstanceCommand(const JsonApiCommand& cmd, const QJsonObject
300300 }
301301 else
302302 {
303- sendErrorReply (" No instance(s) given nor switched to a valid one yet" , cmd);
304- return ;
303+ // If no instance was given nor one was switched to before, use first running instance (backward compatability)
304+ if (_currInstanceIndex == NO_INSTANCE_ID)
305+ {
306+ quint8 const firstRunningInstanceID = _instanceManager->getFirstRunningInstanceIdx ();
307+ if (firstRunningInstanceID != NO_INSTANCE_ID)
308+ {
309+ instances.append (firstRunningInstanceID);
310+ Debug (_log," No instance ID(s) provided; applying API request to first running instance [%u]" , firstRunningInstanceID);
311+ if (!handleInstanceSwitch (firstRunningInstanceID))
312+ {
313+ QString errorText = QString (" Error switching to first running instance: [%1] to process API request" ).arg (firstRunningInstanceID);
314+ Error (_log, " %s" , QSTRING_CSTR (errorText));
315+ sendErrorReply (errorText, cmd);
316+ return ;
317+ }
318+ }
319+ else
320+ {
321+ QString errorText {" No instance(s) IDs provided and no running instance available applying the API request to" };
322+ Error (_log, " %s" , QSTRING_CSTR (errorText));
323+ sendErrorReply (errorText, cmd);
324+ return ;
325+ }
326+ }
305327 }
306328 }
307329
@@ -1505,31 +1527,15 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
15051527 QString replyMsg;
15061528 QStringList errorDetails;
15071529
1508- QJsonValue const instanceValue = message[" instance" ];
1509-
1510- const quint8 instanceID = static_cast <quint8>(instanceValue.toInt ());
1511- if (cmd.subCommand != SubCommand::CreateInstance)
1512- {
1513- QString errorText;
1514- if (instanceValue.isUndefined ())
1515- {
1516- errorText = " No instance provided, but required" ;
1517-
1518- } else if (!_instanceManager->doesInstanceExist (instanceID))
1519- {
1520- errorText = QString (" Hyperion instance [%1] does not exist." ).arg (instanceID);
1521- }
1522-
1523- if (!errorText.isEmpty ())
1524- {
1525- sendErrorReply ( errorText, cmd);
1526- return ;
1527- }
1528- }
1529-
1530+ const quint8 instanceID = static_cast <quint8>(message[" instance" ].toInt ());
15301531 const QString instanceName = _instanceManager->getInstanceName (instanceID);
15311532 const QString &name = message[" name" ].toString ();
15321533
1534+ if (cmd.subCommand != SubCommand::CreateInstance && !_instanceManager->doesInstanceExist (instanceID))
1535+ {
1536+ sendErrorReply ( QString (" Hyperion instance [%1] does not exist." ).arg (instanceID), cmd);
1537+ return ;
1538+ }
15331539 switch (cmd.subCommand ) {
15341540 case SubCommand::SwitchTo:
15351541 if (handleInstanceSwitch (instanceID))
@@ -1762,6 +1768,10 @@ QJsonObject JsonAPI::getBasicCommandReply(bool success, const QString &command,
17621768 reply[" command" ] = command;
17631769 reply[" tan" ] = tan;
17641770
1771+ if ((_currInstanceIndex != NO_INSTANCE_ID) && instanceCmdType != InstanceCmd::No)
1772+ {
1773+ reply[" instance" ] = _currInstanceIndex;
1774+ }
17651775 return reply;
17661776}
17671777
0 commit comments