2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// Hyperion includes
|
|
|
|
#include <hyperion/PriorityMuxer.h>
|
|
|
|
|
2016-07-31 22:21:35 +02:00
|
|
|
PriorityMuxer::PriorityMuxer(int ledCount)
|
|
|
|
: _currentPriority(LOWEST_PRIORITY)
|
|
|
|
, _activeInputs()
|
|
|
|
, _lowestPriorityInfo()
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-03-21 17:55:46 +01:00
|
|
|
_lowestPriorityInfo.priority = LOWEST_PRIORITY;
|
2017-04-03 05:19:05 +02:00
|
|
|
_lowestPriorityInfo.timeoutTime_ms = 0;
|
2017-03-21 17:55:46 +01:00
|
|
|
_lowestPriorityInfo.ledColors = std::vector<ColorRgb>(ledCount, {0, 0, 0});
|
|
|
|
_lowestPriorityInfo.componentId = hyperion::COMP_COLOR;
|
|
|
|
_lowestPriorityInfo.origin = "System";
|
|
|
|
|
2016-08-06 08:28:42 +02:00
|
|
|
_activeInputs[_currentPriority] = _lowestPriorityInfo;
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PriorityMuxer::~PriorityMuxer()
|
|
|
|
{
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
int PriorityMuxer::getCurrentPriority() const
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
return _currentPriority;
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<int> PriorityMuxer::getPriorities() const
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
return _activeInputs.keys();
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PriorityMuxer::hasPriority(const int priority) const
|
|
|
|
{
|
2016-08-06 08:28:42 +02:00
|
|
|
return (priority == LOWEST_PRIORITY) ? true : _activeInputs.contains(priority);
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority) const
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
auto elemIt = _activeInputs.find(priority);
|
|
|
|
if (elemIt == _activeInputs.end())
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2016-08-06 08:28:42 +02:00
|
|
|
throw std::runtime_error("HYPERION (prioritymuxer) ERROR: no such priority");
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
return elemIt.value();
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:53:41 +01:00
|
|
|
void PriorityMuxer::setInput(const int priority, const std::vector<ColorRgb>& ledColors, const int64_t timeoutTime_ms, hyperion::Components component, const QString origin)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-01-10 19:58:41 +01:00
|
|
|
InputInfo& input = _activeInputs[priority];
|
2013-08-13 11:10:45 +02:00
|
|
|
input.priority = priority;
|
|
|
|
input.timeoutTime_ms = timeoutTime_ms;
|
|
|
|
input.ledColors = ledColors;
|
2016-10-10 18:29:54 +02:00
|
|
|
input.componentId = component;
|
2017-02-28 17:53:41 +01:00
|
|
|
input.origin = origin;
|
2017-01-10 19:58:41 +01:00
|
|
|
_currentPriority = std::min(_currentPriority, priority);
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityMuxer::clearInput(const int priority)
|
|
|
|
{
|
2016-08-06 08:28:42 +02:00
|
|
|
if (priority < LOWEST_PRIORITY)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2016-08-06 08:28:42 +02:00
|
|
|
_activeInputs.remove(priority);
|
|
|
|
if (_currentPriority == priority)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
QList<int> keys = _activeInputs.keys();
|
|
|
|
_currentPriority = *std::min_element(keys.begin(), keys.end());
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityMuxer::clearAll()
|
|
|
|
{
|
2017-03-22 11:52:20 +01:00
|
|
|
for(auto key : _activeInputs.keys())
|
|
|
|
{
|
2017-03-30 06:20:20 +02:00
|
|
|
if (key < LOWEST_PRIORITY-1)
|
2017-03-22 11:52:20 +01:00
|
|
|
{
|
|
|
|
_activeInputs.remove(key);
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityMuxer::setCurrentTime(const int64_t& now)
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
_currentPriority = LOWEST_PRIORITY;
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-04-03 05:19:05 +02:00
|
|
|
if (infoIt->timeoutTime_ms > 0 && infoIt->timeoutTime_ms <= now)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
infoIt = _activeInputs.erase(infoIt);
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
_currentPriority = std::min(_currentPriority, infoIt->priority);
|
2013-08-13 11:10:45 +02:00
|
|
|
++infoIt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|