Make date "At"-type check more robust

Previously the "At" check could skip the desired time window if e.g another
macro was performing a long wait wait action or condition was taking
a long time to check.
The date condition will now take into consideration when the last time
was the condition checked a date instead of simply applying a fixed
window of "switcher->interval".
This commit is contained in:
WarmUpTill 2022-04-02 13:54:12 +02:00 committed by WarmUpTill
parent eb8a9df627
commit 1a8b185f7d
4 changed files with 32 additions and 12 deletions

View File

@ -48,8 +48,8 @@ public:
bool _dayOfWeekCheck = false;
private:
bool checkDayOfWeek();
bool checkRegularDate();
bool CheckDayOfWeek(int64_t);
bool CheckRegularDate(int64_t);
static bool _registered;
static const std::string id;

View File

@ -78,6 +78,7 @@ public:
bool PerformActions(bool forceParallel = false,
bool ignorePause = false);
bool Matched() { return _matched; }
int64_t MsSinceLastCheck();
std::string Name() { return _name; }
void SetName(const std::string &name);
void SetRunInParallel(bool parallel) { _runInParallel = parallel; }
@ -139,6 +140,8 @@ private:
bool _wasExecutedRecently = false;
bool _onChangeTriggered = false;
std::chrono::high_resolution_clock::time_point _lastCheckTime{};
bool _die = false;
bool _stop = false;
bool _done = true;

View File

@ -35,7 +35,7 @@ static std::map<DayOfWeekSelection, std::string> dayOfWeekNames = {
{DayOfWeekSelection::SUNDAY, "AdvSceneSwitcher.condition.date.sunday"},
};
bool MacroConditionDate::checkDayOfWeek()
bool MacroConditionDate::CheckDayOfWeek(int64_t msSinceLastCheck)
{
QDateTime cur = QDateTime::currentDateTime();
if (_dayOfWeek != DayOfWeekSelection::ANY &&
@ -43,11 +43,10 @@ bool MacroConditionDate::checkDayOfWeek()
return false;
}
_dateTime.setDate(cur.date());
return _dateTime >= cur &&
_dateTime <= cur.addMSecs(switcher->interval);
return _dateTime <= cur && _dateTime >= cur.addMSecs(-msSinceLastCheck);
}
bool MacroConditionDate::checkRegularDate()
bool MacroConditionDate::CheckRegularDate(int64_t msSinceLastCheck)
{
bool match = false;
QDateTime cur = QDateTime::currentDateTime();
@ -62,8 +61,8 @@ bool MacroConditionDate::checkRegularDate()
switch (_condition) {
case DateCondition::AT:
match = _dateTime >= cur &&
_dateTime <= cur.addMSecs(switcher->interval);
match = _dateTime <= cur &&
_dateTime >= cur.addMSecs(-msSinceLastCheck);
break;
case DateCondition::AFTER:
match = cur >= _dateTime;
@ -92,10 +91,15 @@ bool MacroConditionDate::checkRegularDate()
bool MacroConditionDate::CheckCondition()
{
if (_dayOfWeekCheck) {
return checkDayOfWeek();
auto m = GetMacro();
if (!m) {
return false;
}
return checkRegularDate();
auto msSinceLastCheck = m->MsSinceLastCheck();
if (_dayOfWeekCheck) {
return CheckDayOfWeek(msSinceLastCheck);
}
return CheckRegularDate(msSinceLastCheck);
}
bool MacroConditionDate::Save(obs_data_t *obj)

View File

@ -126,7 +126,7 @@ bool Macro::CeckMatch()
if (_matched && _count != std::numeric_limits<int>::max()) {
_count++;
}
_lastCheckTime = std::chrono::high_resolution_clock::now();
return _matched;
}
@ -152,6 +152,18 @@ bool Macro::PerformActions(bool forceParallel, bool ignorePause)
return ret;
}
int64_t Macro::MsSinceLastCheck()
{
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)
{
_name = name;
@ -163,6 +175,7 @@ void Macro::ResetTimers()
for (auto &c : _conditions) {
c->ResetDuration();
}
_lastCheckTime = {};
}
void Macro::RunActions(bool &retVal, bool ignorePause)