Rename and refactor macro class functions

LastConditionCheckTime() will be required to support custom condition
check intervals per macro
This commit is contained in:
WarmUpTill 2024-09-02 19:33:29 +02:00 committed by WarmUpTill
parent 94263cc7a0
commit 9eb4e90291
8 changed files with 38 additions and 42 deletions

View File

@ -196,13 +196,13 @@ void MacroActionMacro::RunActions(Macro *actionMacro) const
} }
if (_runOptions.reevaluateConditionState) { if (_runOptions.reevaluateConditionState) {
conditionMacro->CeckMatch(true); conditionMacro->CheckConditions(true);
} }
if ((_runOptions.logic == RunOptions::Logic::CONDITIONS && if ((_runOptions.logic == RunOptions::Logic::CONDITIONS &&
conditionMacro->Matched()) || conditionMacro->ConditionsMatched()) ||
(_runOptions.logic == RunOptions::Logic::INVERT_CONDITIONS && (_runOptions.logic == RunOptions::Logic::INVERT_CONDITIONS &&
!conditionMacro->Matched())) { !conditionMacro->ConditionsMatched())) {
runActionsHelper(actionMacro, _runOptions.runElseActions, runActionsHelper(actionMacro, _runOptions.runElseActions,
_runOptions.setInputs, _runOptions.inputs); _runOptions.setInputs, _runOptions.inputs);
} }

View File

@ -58,7 +58,7 @@ bool MacroConditionMacro::CheckStateCondition()
return false; return false;
} }
return macro->Matched(); return macro->ConditionsMatched();
} }
bool MacroConditionMacro::CheckMultiStateCondition() bool MacroConditionMacro::CheckMultiStateCondition()
@ -72,7 +72,7 @@ bool MacroConditionMacro::CheckMultiStateCondition()
if (!macro) { if (!macro) {
continue; continue;
} }
if (macro->Matched()) { if (macro->ConditionsMatched()) {
matchedCount++; matchedCount++;
} }
} }

View File

@ -138,8 +138,9 @@ void MacroDock::UpdateText()
_pauseToggle->setText(macro->Paused() ? _unpauseButtonText.c_str() _pauseToggle->setText(macro->Paused() ? _unpauseButtonText.c_str()
: _pauseButtonText.c_str()); : _pauseButtonText.c_str());
_statusText->setText(macro->Matched() ? _conditionsTrueText.c_str() _statusText->setText(macro->ConditionsMatched()
: _conditionsFalseText.c_str()); ? _conditionsTrueText.c_str()
: _conditionsFalseText.c_str());
} }
void MacroDock::Highlight() void MacroDock::Highlight()

View File

@ -85,9 +85,11 @@ std::string GetMacroName(Macro *macro)
return macro ? macro->Name() : ""; return macro ? macro->Name() : "";
} }
int64_t MillisecondsSinceMacroConditionCheck(Macro *macro) std::chrono::high_resolution_clock::time_point
LastMacroConditionCheckTime(Macro *macro)
{ {
return macro ? macro->MsSinceLastCheck() : 0; return macro ? macro->LastConditionCheckTime()
: std::chrono::high_resolution_clock::time_point{};
} }
bool MacroIsStopped(Macro *macro) bool MacroIsStopped(Macro *macro)

View File

@ -43,7 +43,8 @@ EXPORT bool MacroSwitchedScene();
EXPORT std::string GetMacroName(Macro *); EXPORT std::string GetMacroName(Macro *);
EXPORT int64_t MillisecondsSinceMacroConditionCheck(Macro *); EXPORT std::chrono::high_resolution_clock::time_point
LastMacroConditionCheckTime(Macro *);
EXPORT bool MacroIsStopped(Macro *); EXPORT bool MacroIsStopped(Macro *);
EXPORT bool MacroIsPaused(Macro *); EXPORT bool MacroIsPaused(Macro *);

View File

@ -238,7 +238,7 @@ bool Macro::CheckConditionHelper(
return result; return result;
} }
bool Macro::CeckMatch(bool ignorePause) bool Macro::CheckConditions(bool ignorePause)
{ {
if (_isGroup) { if (_isGroup) {
return false; return false;
@ -309,8 +309,7 @@ bool Macro::PerformActions(bool match, bool forceParallel, bool ignorePause)
return ret; return ret;
} }
bool Macro::WasExecutedSince( bool Macro::WasExecutedSince(const TimePoint &time) const
const std::chrono::high_resolution_clock::time_point &time) const
{ {
return _lastExecutionTime > time; return _lastExecutionTime > time;
} }
@ -337,18 +336,6 @@ bool Macro::ShouldRunActions() const
return hasActionsToExecute; return hasActionsToExecute;
} }
int64_t Macro::MsSinceLastCheck() const
{
if (_lastCheckTime.time_since_epoch().count() == 0) {
return 0;
}
const auto timePassed =
std::chrono::high_resolution_clock::now() - _lastCheckTime;
return std::chrono::duration_cast<std::chrono::milliseconds>(timePassed)
.count() +
1;
}
void Macro::SetName(const std::string &name) void Macro::SetName(const std::string &name)
{ {
const bool nameChanged = _name == name; const bool nameChanged = _name == name;
@ -414,8 +401,7 @@ bool Macro::RunElseActions(bool ignorePause)
return RunActionsHelper(_elseActions, ignorePause); return RunActionsHelper(_elseActions, ignorePause);
} }
bool Macro::WasPausedSince( bool Macro::WasPausedSince(const TimePoint &time) const
const std::chrono::high_resolution_clock::time_point &time) const
{ {
return _lastUnpauseTime > time; return _lastUnpauseTime > time;
} }
@ -1303,7 +1289,7 @@ bool CheckMacros()
{ {
bool matchFound = false; bool matchFound = false;
for (const auto &m : macros) { for (const auto &m : macros) {
if (m->CeckMatch() || m->ElseActions().size() > 0) { if (m->CheckConditions() || m->ElseActions().size() > 0) {
matchFound = true; matchFound = true;
// This has to be performed here for now as actions are // This has to be performed here for now as actions are
// not performed immediately after checking conditions. // not performed immediately after checking conditions.
@ -1349,7 +1335,7 @@ bool RunMacros()
continue; continue;
} }
vblog(LOG_INFO, "running macro: %s", m->Name().c_str()); vblog(LOG_INFO, "running macro: %s", m->Name().c_str());
if (!m->PerformActions(m->Matched())) { if (!m->PerformActions(m->ConditionsMatched())) {
blog(LOG_WARNING, "abort macro: %s", m->Name().c_str()); blog(LOG_WARNING, "abort macro: %s", m->Name().c_str());
} }
} }

View File

@ -22,6 +22,8 @@ namespace advss {
class MacroDock; class MacroDock;
class Macro { class Macro {
using TimePoint = std::chrono::high_resolution_clock::time_point;
public: public:
Macro(const std::string &name = "", const bool addHotkey = false, Macro(const std::string &name = "", const bool addHotkey = false,
const bool shortCircuitEvaluation = false); const bool shortCircuitEvaluation = false);
@ -30,17 +32,17 @@ public:
std::string Name() const { return _name; } std::string Name() const { return _name; }
void SetName(const std::string &name); void SetName(const std::string &name);
bool CeckMatch(bool ignorePause = false); bool CheckConditions(bool ignorePause = false);
bool Matched() const { return _matched; } bool ConditionsMatched() const { return _matched; }
int64_t MsSinceLastCheck() const; TimePoint LastConditionCheckTime() const { return _lastCheckTime; }
bool ShouldRunActions() const; bool ShouldRunActions() const;
bool PerformActions(bool match, bool forceParallel = false, bool PerformActions(bool match, bool forceParallel = false,
bool ignorePause = false); bool ignorePause = false);
void SetPaused(bool pause = true); void SetPaused(bool pause = true);
bool Paused() const { return _paused; } bool Paused() const { return _paused; }
bool WasPausedSince( bool WasPausedSince(const TimePoint &) const;
const std::chrono::high_resolution_clock::time_point &) const;
void Stop(); void Stop();
bool GetStop() const { return _stop; } bool GetStop() const { return _stop; }
@ -117,8 +119,7 @@ public:
void SetElseActionSplitterPosition(const QList<int>); void SetElseActionSplitterPosition(const QList<int>);
const QList<int> &GetElseActionSplitterPosition() const; const QList<int> &GetElseActionSplitterPosition() const;
bool HasValidSplitterPositions() const; bool HasValidSplitterPositions() const;
bool WasExecutedSince( bool WasExecutedSince(const TimePoint &) const;
const std::chrono::high_resolution_clock::time_point &) const;
bool OnChangePreventedActionsRecently(); bool OnChangePreventedActionsRecently();
void ResetUIHelpers(); void ResetUIHelpers();
@ -171,9 +172,9 @@ private:
bool _die = false; bool _die = false;
bool _stop = false; bool _stop = false;
bool _done = true; bool _done = true;
std::chrono::high_resolution_clock::time_point _lastCheckTime{}; TimePoint _lastCheckTime{};
std::chrono::high_resolution_clock::time_point _lastUnpauseTime{}; TimePoint _lastUnpauseTime{};
std::chrono::high_resolution_clock::time_point _lastExecutionTime{}; TimePoint _lastExecutionTime{};
std::thread _backgroundThread; std::thread _backgroundThread;
std::vector<std::thread> _helperThreads; std::vector<std::thread> _helperThreads;

View File

@ -180,11 +180,16 @@ bool MacroConditionDate::CheckCondition()
if (!m) { if (!m) {
return false; return false;
} }
auto msSinceLastCheck = MillisecondsSinceMacroConditionCheck(m); const auto timePassed = std::chrono::high_resolution_clock::now() -
LastMacroConditionCheckTime(m);
const auto msSinceLastCheck =
std::chrono::duration_cast<std::chrono::milliseconds>(
timePassed);
if (_dayOfWeekCheck) { if (_dayOfWeekCheck) {
return CheckDayOfWeek(msSinceLastCheck); return CheckDayOfWeek(msSinceLastCheck.count());
} }
return CheckRegularDate(msSinceLastCheck); return CheckRegularDate(msSinceLastCheck.count());
} }
bool MacroConditionDate::Save(obs_data_t *obj) const bool MacroConditionDate::Save(obs_data_t *obj) const