Skip to content

Commit

Permalink
Handle idle scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Grey committed Dec 19, 2022
1 parent 32bf32c commit c4df51b
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 38 deletions.
24 changes: 18 additions & 6 deletions include/hyperion/ComponentRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include <map>

#include <QObject>
#include <QVector>

class Hyperion;

typedef QVector<hyperion::Components> ComponentList;

///
/// @brief The component register reflects and manages the current state of all components and Hyperion as a whole
/// It emits also real component state changes (triggert from the specific component), which can be used for listening APIs (Network Clients/Plugins)
/// It emits also real component state changes (triggered from the specific component), which can be used for listening APIs (Network Clients/Plugins)
///
class ComponentRegister : public QObject
{
Expand All @@ -36,23 +39,32 @@ class ComponentRegister : public QObject
///
/// @brief Emits whenever a component changed (really) the state
/// @param comp The component
/// @param state The new state of the component
/// @param isActive The new state of the component
///
void updatedComponentState(hyperion::Components comp, bool state);
void updatedComponentState(hyperion::Components comp, bool isActive);

public slots:
///
/// @brief is called whenever a component change a state, DO NOT CALL FROM API, use signal hyperion->compStateChangeRequest
/// @param comp The component
/// @param state The new state of the component
/// @param isActive The new state of the component
///
void setNewComponentState(hyperion::Components comp, bool activated);
void setNewComponentState(hyperion::Components comp, bool isActive);

private slots:
///
/// @brief Handle COMP_ALL changes from Hyperion->compStateChangeRequest
/// @param comp COMP_ALL
/// @param isActive The new state for all components
///
void handleCompStateChangeRequest(hyperion::Components comps, bool isActive);

///
/// @brief Activate/Deactivate all components, except those provided by the list of excluded components
/// @param isActive The new state for all components
/// @param execludeList of excluded components
///
void handleCompStateChangeRequest(hyperion::Components comps, bool activated);
void handleCompStateChangeRequestAll(bool isActive, const ComponentList& excludeList = ComponentList{});

private:
/// Hyperion instance
Expand Down
26 changes: 26 additions & 0 deletions include/hyperion/Hyperion.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ public slots:

int getLatchTime() const;

///
/// @brief Set hyperion in suspend mode or resume from suspend/idle.
/// All instances and components will be disabled/enabled.
/// @param isSupend True, components will be deactivated, else put into their previous state before suspend
///
void setSuspend(bool isSupend);

///
/// @brief Set hyperion in idle /working mode.
/// In idle, all instances and components will be disabled besides the output processing (LED-Devices, smoothing).
/// @param isIdle True, selected components will be deactivated, else put into their previous state before idle
///
void setIdle(bool isIdle);

signals:
/// Signal which is emitted when a priority channel is actively cleared
/// This signal will not be emitted when a priority channel time out
Expand All @@ -406,6 +420,18 @@ public slots:
///
void compStateChangeRequest(hyperion::Components component, bool enabled);

///
/// @brief Emits when all (besides excluded) components are subject to state changes
/// @param isActive The new state for all components
/// @param execlude List of excluded components
void compStateChangeRequestAll(bool isActive, const ComponentList& excludeList = {});

/// Signal which is emitted, when system is to be suspended/resumed
void suspendRequest(bool isSuspend);

/// Signal which is emitted, when system should go into idle/working mode
void idleRequest(bool isIdle);

///
/// @brief Emits whenever the imageToLedsMapping has changed
/// @param mappingType The new mapping type
Expand Down
2 changes: 1 addition & 1 deletion libsrc/api/JSONRPC_schema/schema-system.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"subcommand": {
"type" : "string",
"required" : true,
"enum" : ["restart", "resume", "suspend"]
"enum" : ["restart", "resume", "suspend", "idle"]
}
},
"additionalProperties": false
Expand Down
9 changes: 7 additions & 2 deletions libsrc/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,19 +1809,24 @@ void JsonAPI::handleSystemCommand(const QJsonObject &message, const QString &com

if (subc == "suspend")
{
emit _instanceManager->suspend();
_instanceManager->suspend();
sendSuccessReply(command + "-" + subc, tan);
}
else if (subc == "resume")
{
emit _instanceManager->resume();
_instanceManager->resume();
sendSuccessReply(command + "-" + subc, tan);
}
else if (subc == "restart")
{
Process::restartHyperion();
sendSuccessReply(command + "-" + subc, tan);
}
else if (subc == "idle")
{
_instanceManager->toggleIdle(true);
sendSuccessReply(command + "-" + subc, tan);
}
else
{
QString full_command = command + "-" + subc;
Expand Down
82 changes: 60 additions & 22 deletions libsrc/hyperion/ComponentRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ ComponentRegister::ComponentRegister(Hyperion* hyperion)
vect << COMP_FORWARDER;
#endif

for(auto e : vect)
for(auto e : qAsConst(vect))
{
_componentStates.emplace(e, (e == COMP_ALL));
}

connect(_hyperion, &Hyperion::compStateChangeRequest, this, &ComponentRegister::handleCompStateChangeRequest);
connect(_hyperion, &Hyperion::compStateChangeRequestAll, this, &ComponentRegister::handleCompStateChangeRequestAll);
}

ComponentRegister::~ComponentRegister()
Expand All @@ -72,56 +73,93 @@ int ComponentRegister::isComponentEnabled(hyperion::Components comp) const
return (_componentStates.count(comp)) ? _componentStates.at(comp) : -1;
}

void ComponentRegister::setNewComponentState(hyperion::Components comp, bool activated)
void ComponentRegister::setNewComponentState(hyperion::Components comp, bool isActive)
{

if (_componentStates.count(comp) > 0)
{
if (_componentStates[comp] != activated)
if (_componentStates[comp] != isActive)
{
Debug(_log, "%s: %s", componentToString(comp), (activated ? "enabled" : "disabled"));
_componentStates[comp] = activated;
Debug(_log, "%s: %s", componentToString(comp), (isActive ? "enabled" : "disabled"));
_componentStates[comp] = isActive;
// emit component has changed state
emit updatedComponentState(comp, activated);
emit updatedComponentState(comp, isActive);
}
}
}

void ComponentRegister::handleCompStateChangeRequest(hyperion::Components comps, bool activated)
void ComponentRegister::handleCompStateChangeRequest(hyperion::Components comps, bool isActive)
{
if(comps == COMP_ALL && !_inProgress)
if(comps == COMP_ALL )
{
handleCompStateChangeRequestAll(isActive,{});
}
}

void ComponentRegister::handleCompStateChangeRequestAll(bool isActive, const ComponentList& excludeList)
{
if (!_inProgress)
{
_inProgress = true;
if(!activated && _prevComponentStates.empty())
if(!isActive)
{
Debug(_log,"Disable Hyperion instance, store current component states");
if (excludeList.isEmpty())
{
Debug(_log,"Disable Hyperion instance, store current components' state");
}
else
{
Debug(_log,"Disable selected Hyperion components, store their current state");
}

for(const auto &comp : _componentStates)
{
// save state
_prevComponentStates.emplace(comp.first, comp.second);
// disable if enabled
if(comp.second)
if (!excludeList.contains(comp.first) && comp.first != COMP_ALL)
{
emit _hyperion->compStateChangeRequest(comp.first, false);
// save state
_prevComponentStates.emplace(comp.first, comp.second);
// disable if enabled
if(comp.second)
{
emit _hyperion->compStateChangeRequest(comp.first, false);
}
}
}
setNewComponentState(COMP_ALL, false);

if (excludeList.isEmpty())
{
setNewComponentState(COMP_ALL, false);
}
}
else
{
if(activated && !_prevComponentStates.empty())
if(isActive && !_prevComponentStates.empty())
{
Debug(_log,"Enable Hyperion instance, recover previous component states");
if (excludeList.isEmpty())
{
Debug(_log,"Enable Hyperion instance, restore components' previous state");
}
else
{
Debug(_log,"Enable selected Hyperion components, restore their previous state");
}

for(const auto &comp : _prevComponentStates)
{
// if comp was enabled, enable again
if(comp.second)
if (!excludeList.contains(comp.first) && comp.first != COMP_ALL)
{
emit _hyperion->compStateChangeRequest(comp.first, true);
// if comp was enabled, enable again
if(comp.second)
{
emit _hyperion->compStateChangeRequest(comp.first, true);
}
}
}
_prevComponentStates.clear();
setNewComponentState(COMP_ALL, true);
if (excludeList.isEmpty())
{
setNewComponentState(COMP_ALL, true);
}
}
}
_inProgress = false;
Expand Down
21 changes: 19 additions & 2 deletions libsrc/hyperion/Hyperion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Hyperion::Hyperion(quint8 instance, bool readonlyMode)
#endif
, _readOnlyMode(readonlyMode)
{
qRegisterMetaType<ComponentList>("ComponentList");

QString subComponent = "I"+QString::number(instance);
this->setProperty("instance", (QString) subComponent);

Expand Down Expand Up @@ -117,8 +119,9 @@ void Hyperion::start()
connect(_muxer, &PriorityMuxer::visiblePriorityChanged, this, &Hyperion::handleSourceAvailability);
connect(_muxer, &PriorityMuxer::visibleComponentChanged, this, &Hyperion::handleVisibleComponentChanged);

// listens for ComponentRegister changes of COMP_ALL to perform core enable/disable actions
// connect(&_componentRegister, &ComponentRegister::updatedComponentState, this, &Hyperion::updatedComponentState);
// listen for suspend/resume, idle requests to perform core activation/deactivation actions
connect(this, &Hyperion::suspendRequest, this, &Hyperion::setSuspend);
connect(this, &Hyperion::idleRequest, this, &Hyperion::setIdle);

// listen for settings updates of this instance (LEDS & COLOR)
connect(_settingsManager, &SettingsManager::settingsChanged, this, &Hyperion::handleSettingsUpdate);
Expand Down Expand Up @@ -377,6 +380,20 @@ int Hyperion::isComponentEnabled(hyperion::Components comp) const
return _componentRegister->isComponentEnabled(comp);
}

void Hyperion::setSuspend(bool isSuspend)
{
bool enable = !isSuspend;
emit compStateChangeRequestAll(enable);
}

void Hyperion::setIdle(bool isIdle)
{
clear(-1);

bool enable = !isIdle;
emit compStateChangeRequestAll(enable, {hyperion::COMP_LEDDEVICE, hyperion::COMP_SMOOTHING} );
}

void Hyperion::registerInput(int priority, hyperion::Components component, const QString& origin, const QString& owner, unsigned smooth_cfg)
{
_muxer->registerInput(priority, component, origin, owner, smooth_cfg);
Expand Down
19 changes: 16 additions & 3 deletions libsrc/hyperion/HyperionIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,31 @@ void HyperionIManager::stopAll()
void HyperionIManager::suspend()
{
Info(_log,"Suspend all instances and enabled components");
toggleStateAllInstances(false);
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->suspendRequest(true);
}
}

void HyperionIManager::resume()
{
Info(_log,"Resume all instances and enabled components");
toggleStateAllInstances(true);
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->suspendRequest(false);
}
}

void HyperionIManager::toggleIdle(bool isIdle)
{
// TODO: Implement Idle scenario
Info(_log,"Put all instances in %s state", isIdle ? "idle" : "working");
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->idleRequest(isIdle);
}
}

void HyperionIManager::toggleStateAllInstances(bool enable)
Expand Down
4 changes: 2 additions & 2 deletions src/hyperiond/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ void signal_handler(int signum)
{
if (_hyperion != nullptr)
{
_hyperion->toggleStateAllInstances(false);
_hyperion->suspend();
}
}
else if (signum == SIGUSR2)
{
if (_hyperion != nullptr)
{
_hyperion->toggleStateAllInstances(true);
_hyperion->resume();
}
}
}
Expand Down

0 comments on commit c4df51b

Please sign in to comment.