hyperion.ng/libsrc/events/EventScheduler.cpp

156 lines
3.9 KiB
C++
Raw Normal View History

#include "events/EventScheduler.h"
#include <QtGlobal>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDateTime>
#include <events/EventHandler.h>
#include <utils/Logger.h>
EventScheduler::EventScheduler()
: _isEnabled(false)
{
qRegisterMetaType<Event>("Event");
_log = Logger::getInstance("EVENTS-SCHED");
Start SmartPointers (#1679) * Refactor to fix #1671 * Add GUI/NonGUI mode to info page * Do not show lock config, if in non-UI mode * Updae Changelog * Correct includes * Ensure key member initialization - RGB Channels * Ensure key member initialization - WebServer * Update RGBChannels * Fix initialization order * Fix key when inserting new logger in LoggerMap, Prepare logBuffer-JSON snapshot view in LoggerManager, Increase buffered loglines to 500 * Fix Memory leak in GrabberWrapper * Fix Memory leak in BlackBorderProcessor * Fix Memory leak in BlackBorderProcessor * use ninja generator under macos * Fix BGEffectHandler destruction * Fix Mdns code * Clear list after applying qDeleteAll * Fix deletion of CecHandler * Fix memory leak caused by wrong buffer allocation * Remove extra pixel consistently * Change mDNS to Qt SmartPointers * Correct removal * Fix usage of _width/_height (they are the output resolution, not the screen resolution) That avoids unnecessary resizing of the output image with every transferFrame call * Move main non Thread Objects to Smart Pointers * Refactor Hyperion Daemon unsing smartpointers * Correction * Correct typos/ align text * Fix startGrabberDispmanx * Fix startGrabberDispmanx * Address CodeQL finding * Create Screen grabbers via Template * Fix typo * Change way of logging * Revert change * Address deprecation warning * Correct auto screen grabber evaluation --------- Co-authored-by: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2024-02-25 17:35:39 +01:00
QObject::connect(this, &EventScheduler::signalEvent, EventHandler::getInstance().data(), &EventHandler::handleEvent);
Debug(_log, "Hyperion event scheduler created");
}
EventScheduler::~EventScheduler()
{
Start SmartPointers (#1679) * Refactor to fix #1671 * Add GUI/NonGUI mode to info page * Do not show lock config, if in non-UI mode * Updae Changelog * Correct includes * Ensure key member initialization - RGB Channels * Ensure key member initialization - WebServer * Update RGBChannels * Fix initialization order * Fix key when inserting new logger in LoggerMap, Prepare logBuffer-JSON snapshot view in LoggerManager, Increase buffered loglines to 500 * Fix Memory leak in GrabberWrapper * Fix Memory leak in BlackBorderProcessor * Fix Memory leak in BlackBorderProcessor * use ninja generator under macos * Fix BGEffectHandler destruction * Fix Mdns code * Clear list after applying qDeleteAll * Fix deletion of CecHandler * Fix memory leak caused by wrong buffer allocation * Remove extra pixel consistently * Change mDNS to Qt SmartPointers * Correct removal * Fix usage of _width/_height (they are the output resolution, not the screen resolution) That avoids unnecessary resizing of the output image with every transferFrame call * Move main non Thread Objects to Smart Pointers * Refactor Hyperion Daemon unsing smartpointers * Correction * Correct typos/ align text * Fix startGrabberDispmanx * Fix startGrabberDispmanx * Address CodeQL finding * Create Screen grabbers via Template * Fix typo * Change way of logging * Revert change * Address deprecation warning * Correct auto screen grabber evaluation --------- Co-authored-by: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2024-02-25 17:35:39 +01:00
QObject::disconnect(this, &EventScheduler::signalEvent, EventHandler::getInstance().data(), &EventHandler::handleEvent);
clearTimers();
Start SmartPointers (#1679) * Refactor to fix #1671 * Add GUI/NonGUI mode to info page * Do not show lock config, if in non-UI mode * Updae Changelog * Correct includes * Ensure key member initialization - RGB Channels * Ensure key member initialization - WebServer * Update RGBChannels * Fix initialization order * Fix key when inserting new logger in LoggerMap, Prepare logBuffer-JSON snapshot view in LoggerManager, Increase buffered loglines to 500 * Fix Memory leak in GrabberWrapper * Fix Memory leak in BlackBorderProcessor * Fix Memory leak in BlackBorderProcessor * use ninja generator under macos * Fix BGEffectHandler destruction * Fix Mdns code * Clear list after applying qDeleteAll * Fix deletion of CecHandler * Fix memory leak caused by wrong buffer allocation * Remove extra pixel consistently * Change mDNS to Qt SmartPointers * Correct removal * Fix usage of _width/_height (they are the output resolution, not the screen resolution) That avoids unnecessary resizing of the output image with every transferFrame call * Move main non Thread Objects to Smart Pointers * Refactor Hyperion Daemon unsing smartpointers * Correction * Correct typos/ align text * Fix startGrabberDispmanx * Fix startGrabberDispmanx * Address CodeQL finding * Create Screen grabbers via Template * Fix typo * Change way of logging * Revert change * Address deprecation warning * Correct auto screen grabber evaluation --------- Co-authored-by: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2024-02-25 17:35:39 +01:00
Info(_log, "Hyperion event scheduler stopped");
}
void EventScheduler::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if(type == settings::SCHEDEVENTS)
{
const QJsonObject& obj = config.object();
_isEnabled = obj["enable"].toBool(false);
Debug(_log, "Event scheduler is %s", _isEnabled? "enabled" : "disabled");
if (_isEnabled)
{
_scheduledEvents.clear();
const QJsonArray actionItems = obj["actions"].toArray();
if (!actionItems.isEmpty())
{
timeEvent timeEvent;
for (const QJsonValue &item : actionItems)
{
QString action = item.toObject().value("action").toString();
timeEvent.action = stringToEvent(action);
QString event = item.toObject().value("event").toString();
timeEvent.time = QTime::fromString(event,"hh:mm");
if (timeEvent.time.isValid())
{
_scheduledEvents.append(timeEvent);
Debug(_log, "Time-Event : \"%s\" linked to action \"%s\"", QSTRING_CSTR(event), QSTRING_CSTR(action));
}
else
{
Error(_log, "Error in configured time : \"%s\" linked to action \"%s\"", QSTRING_CSTR(event), QSTRING_CSTR(action));
}
}
}
if (!_scheduledEvents.isEmpty())
{
enable();
}
else
{
Warning(_log, "No scheduled events to listen to are configured currently.");
}
}
else
{
disable();
}
}
}
bool EventScheduler::enable()
{
bool enabled {false};
clearTimers();
for (int i = 0; i < _scheduledEvents.size(); ++i)
{
QTimer* timer = new QTimer(this);
timer->setTimerType(Qt::PreciseTimer);
_timers.append(timer);
// Calculate the milliseconds until the next occurrence of the scheduled time
int milliseconds = getMillisecondsToNextScheduledTime(_scheduledEvents.at(i).time);
timer->start(milliseconds);
QObject::connect(timer, &QTimer::timeout, this, [this, i]() { handleEvent(i); });
}
enabled = true;
Info(_log, "Event scheduler started");
return enabled;
}
void EventScheduler::disable()
{
Info(_log, "Disabling event scheduler");
clearTimers();
}
void EventScheduler::clearTimers()
{
for (QTimer *timer : std::as_const(_timers)) {
timer->disconnect();
timer->stop();
delete timer;
}
_timers.clear();
}
void EventScheduler::handleEvent(int timerIndex)
{
QTime time = _scheduledEvents.at(timerIndex).time;
Event action = _scheduledEvents.at(timerIndex).action;
Debug(_log, "Event : \"%s\" triggers action \"%s\"", QSTRING_CSTR(time.toString()), eventToString(action) );
if ( action != Event::Unknown )
{
emit signalEvent(action);
}
// Retrigger the timer for the next occurrence
QTimer* timer = _timers.at(timerIndex);
int milliseconds = getMillisecondsToNextScheduledTime(time);
timer->start(milliseconds);
}
int EventScheduler::getMillisecondsToNextScheduledTime(const QTime& scheduledTime)
{
QDateTime currentDateTime = QDateTime::currentDateTime();
QTime currentTime = currentDateTime.time();
QDateTime nextOccurrence = currentDateTime;
nextOccurrence.setTime(scheduledTime);
// If the scheduled time has already passed for today, schedule it for tomorrow
if (currentTime > scheduledTime)
{
nextOccurrence = nextOccurrence.addDays(1);
}
int milliseconds = currentDateTime.msecsTo(nextOccurrence);
return (milliseconds > 0) ? milliseconds : 0;
}