Skip to content

Commit 94a8209

Browse files
committed
Use switched instance for API calls without instance provided
1 parent 7b4051c commit 94a8209

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

libsrc/api/JsonAPI.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ void JsonAPI::handleInstanceCommand(const JsonApiCommand& cmd, const QJsonObject
280280
QJsonArray instances;
281281
const QJsonValue instanceElement = message.value("instance");
282282

283-
// Extract instance(s) from the message
284-
if (!(instanceElement.isUndefined() && instanceElement.isNull()))
283+
// Extract instanceIds(s) from the message
284+
if (!(instanceElement.isUndefined() || instanceElement.isNull()))
285285
{
286286
if (instanceElement.isDouble())
287287
{
@@ -291,14 +291,27 @@ void JsonAPI::handleInstanceCommand(const JsonApiCommand& cmd, const QJsonObject
291291
instances = instanceElement.toArray();
292292
}
293293
}
294+
else
295+
{
296+
// If no instance element is given use the one that was switched to before
297+
if (instanceElement.isUndefined() && _currInstanceIndex != NO_INSTANCE_ID)
298+
{
299+
instances.append(_currInstanceIndex);
300+
}
301+
else
302+
{
303+
sendErrorReply("No instance(s) given nor switched to a valid one yet", cmd);
304+
return;
305+
}
306+
}
294307

295308
InstanceCmd::MustRun const isRunningInstanceRequired = cmd.getInstanceMustRun();
296309
QSet const runningInstanceIds = _instanceManager->getRunningInstanceIdx();
297310
QSet<quint8> instanceIds;
298311
QStringList errorDetails;
299312

300-
// Determine instance IDs, if not provided or "all" is given
301-
if (instanceElement.isUndefined() || instanceElement.isNull() || instances.contains("all"))
313+
// Determine instance IDs, if empty array provided or "all" is given
314+
if (instances.isEmpty() || instances.contains("all"))
302315
{
303316
instanceIds = (isRunningInstanceRequired == InstanceCmd::MustRun_Yes) ? runningInstanceIds : _instanceManager->getInstanceIds();
304317
}
@@ -350,7 +363,8 @@ void JsonAPI::handleInstanceCommand(const JsonApiCommand& cmd, const QJsonObject
350363
return;
351364
}
352365

353-
// Execute the command for each valid instance; Hyperion is about to quit
366+
// Execute the command for each valid instance
367+
quint8 const currentInstance = _currInstanceIndex;
354368
for (const auto &instanceId : std::as_const(instanceIds))
355369
{
356370
if (isRunningInstanceRequired == InstanceCmd::MustRun_Yes || _currInstanceIndex == NO_INSTANCE_ID)
@@ -365,6 +379,12 @@ void JsonAPI::handleInstanceCommand(const JsonApiCommand& cmd, const QJsonObject
365379
handleCommand(cmd, message);
366380
}
367381
}
382+
383+
//Switch back to current instance, if command was executred against multiple instances
384+
if (currentInstance != _currInstanceIndex && (cmd.getInstanceCmdType() == InstanceCmd::Multi || cmd.getInstanceCmdType() == InstanceCmd::No_or_Multi))
385+
{
386+
handleInstanceSwitch(currentInstance);
387+
}
368388
}
369389

370390
void JsonAPI::handleCommand(const JsonApiCommand& cmd, const QJsonObject &message)
@@ -1482,15 +1502,31 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
14821502
QString replyMsg;
14831503
QStringList errorDetails;
14841504

1485-
const quint8 instanceID = static_cast<quint8>(message["instance"].toInt());
1486-
const QString instanceName = _instanceManager->getInstanceName(instanceID);
1487-
const QString &name = message["name"].toString();
1505+
QJsonValue const instanceValue = message["instance"];
14881506

1489-
if(cmd.subCommand != SubCommand::CreateInstance && !_instanceManager->doesInstanceExist(instanceID))
1507+
const quint8 instanceID = static_cast<quint8>(instanceValue.toInt());
1508+
if(cmd.subCommand != SubCommand::CreateInstance)
14901509
{
1491-
sendErrorReply( QString("Hyperion instance [%1] does not exist.").arg(instanceID), cmd);
1492-
return;
1510+
QString errorText;
1511+
if (instanceValue.isUndefined())
1512+
{
1513+
errorText = "No instance provided, but required";
1514+
1515+
} else if (!_instanceManager->doesInstanceExist(instanceID))
1516+
{
1517+
errorText = QString("Hyperion instance [%1] does not exist.").arg(instanceID);
1518+
}
1519+
1520+
if (!errorText.isEmpty())
1521+
{
1522+
sendErrorReply( errorText, cmd);
1523+
return;
1524+
}
14931525
}
1526+
1527+
const QString instanceName = _instanceManager->getInstanceName(instanceID);
1528+
const QString &name = message["name"].toString();
1529+
14941530
switch (cmd.subCommand) {
14951531
case SubCommand::SwitchTo:
14961532
if (handleInstanceSwitch(instanceID))

0 commit comments

Comments
 (0)