Skip to content

Commit ac01afc

Browse files
committed
sch: stall sunrise & sunset calc when continiously failing
- do not completely fail when either sunrise or sunset missing keep what is valid, fall through otherwise - more event:: helpers for internal comparison fix missing to_minutes when comparing update time
1 parent 0b18a33 commit ac01afc

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

code/espurna/scheduler.cpp

+31-22
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ struct Match {
234234
Location location;
235235
Match match;
236236

237+
auto next_update = event::DefaultTimePoint;
238+
237239
void setup();
238240

239241
void reset() {
@@ -825,41 +827,47 @@ bool update_schedule(Schedule& schedule) {
825827
}
826828

827829
bool needs_update(datetime::Clock::time_point time_point) {
828-
return (match.rising.next < time_point)
829-
|| (match.setting.next < time_point);
830+
const auto next = event::is_valid(next_update);
831+
if (next) {
832+
return event::less(next_update, time_point);
833+
}
834+
835+
return event::less(match.rising.next, time_point)
836+
|| event::less(match.setting.next, time_point);
830837
}
831838

832839
template <typename T>
833-
void delta_compare(tm& out, datetime::Clock::time_point, T);
840+
datetime::Clock::time_point delta_compare(tm& out, datetime::Clock::time_point, T);
834841

835-
void update(const datetime::Clock::time_point&, const tm& today) {
842+
void update(datetime::Clock::time_point, const tm& today) {
836843
const auto result = sun::sunrise_sunset(location, today);
837844
update_event_match(match.rising, result.sunrise);
838845
update_event_match(match.setting, result.sunset);
839846
}
840847

841848
template <typename T>
842-
void update(const datetime::Clock::time_point& time_point, const tm& today, T compare) {
849+
void update(datetime::Clock::time_point time_point, const tm& today, T compare) {
843850
auto result = sun::sunrise_sunset(location, today);
844-
if (!event::is_valid(result.sunrise) || !event::is_valid(result.sunset)) {
845-
return;
846-
}
847851

848-
if (compare(time_point, result.sunrise) || compare(time_point, result.sunset)) {
849-
tm tmp;
852+
const auto reset_sunrise =
853+
!event::is_valid(result.sunrise) || compare(time_point, result.sunrise);
854+
855+
const auto reset_sunset =
856+
!event::is_valid(result.sunset) || compare(time_point, result.sunset);
857+
858+
next_update = event::DefaultTimePoint;
859+
860+
tm tmp;
861+
if (reset_sunrise || reset_sunset) {
850862
std::memcpy(&tmp, &today, sizeof(tmp));
851-
delta_compare(tmp, time_point, compare);
863+
next_update = delta_compare(tmp, time_point, compare);
852864

853865
const auto other = sun::sunrise_sunset(location, tmp);
854-
if (!event::is_valid(other.sunrise) || !event::is_valid(other.sunset)) {
855-
return;
856-
}
857-
858-
if (compare(time_point, result.sunrise)) {
866+
if (reset_sunrise && event::is_valid(other.sunrise)) {
859867
result.sunrise = other.sunrise;
860868
}
861869

862-
if (compare(time_point, result.sunset)) {
870+
if (reset_sunset && event::is_valid(other.sunset)) {
863871
result.sunset = other.sunset;
864872
}
865873
}
@@ -881,15 +889,16 @@ String format_match(const EventMatch& match) {
881889
// round to minutes when doing so as well, since std::greater<> would compare seconds
882890
struct CheckCompare {
883891
bool operator()(const event::time_point& lhs, const event::time_point& rhs) {
884-
return to_minutes(lhs) > to_minutes(rhs);
892+
return event::greater(lhs, rhs);
885893
}
886894
};
887895

888896
template <>
889-
void delta_compare(tm& out, datetime::Clock::time_point time_point, CheckCompare) {
890-
datetime::delta_utc(
891-
out, time_point.time_since_epoch(),
892-
datetime::Days{ 1 });
897+
datetime::Clock::time_point delta_compare(tm& out, datetime::Clock::time_point time_point, CheckCompare) {
898+
return datetime::make_time_point(
899+
datetime::delta_utc(
900+
out, time_point.time_since_epoch(),
901+
datetime::Days{ 1 }));
893902
}
894903

895904
void update_after(const datetime::Context& ctx) {

code/espurna/scheduler_common.ipp

+8
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,14 @@ constexpr datetime::Minutes difference(const datetime::Context& ctx, time_point
981981
return difference(ctx, to_minutes(rhs));
982982
}
983983

984+
constexpr bool greater(datetime::Clock::time_point lhs, datetime::Clock::time_point rhs) {
985+
return to_minutes(lhs) > to_minutes(rhs);
986+
}
987+
988+
constexpr bool less(datetime::Clock::time_point lhs, datetime::Clock::time_point rhs) {
989+
return to_minutes(lhs) < to_minutes(rhs);
990+
}
991+
984992
} // namespace event
985993

986994
using event::to_seconds;

0 commit comments

Comments
 (0)