Skip to content

Commit 622785e

Browse files
committed
API - Backward compatibility
1 parent fe7fe8c commit 622785e

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

include/hyperion/HyperionIManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,16 @@ public slots:
7676

7777
///
7878
/// @brief Get all instance indicies of running instances
79+
/// @return All running instance Ids, returns and empty set if no instance is runnning
7980
///
8081
QSet<quint8> getRunningInstanceIdx() const;
8182

83+
///
84+
/// @brief Get the first running instance's ID
85+
/// @return First instance ID, eturns NO_INSTANCE_ID (255) if none is runnning
86+
///
87+
quint8 getFirstRunningInstanceIdx() const;
88+
8289
///
8390
/// @brief Get the first running Hyperion instance
8491
/// @return Hyperion instance, if none is runnning returns a nullptr

libsrc/api/JsonAPI.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

libsrc/hyperion/HyperionIManager.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ QSet<quint8> HyperionIManager::getRunningInstanceIdx() const
7474
return instanceIds;
7575
}
7676

77+
quint8 HyperionIManager::getFirstRunningInstanceIdx() const
78+
{
79+
quint8 instanceId {NO_INSTANCE_ID};
80+
if (!_runningInstances.isEmpty())
81+
{
82+
instanceId= _runningInstances.firstKey();
83+
}
84+
return instanceId;
85+
}
86+
7787
QSharedPointer<Hyperion> HyperionIManager::getFirstRunningInstance()
7888
{
7989
QSharedPointer<Hyperion> hyperion {nullptr};

0 commit comments

Comments
 (0)