mirror of
https://github.com/Xevion/go-ha.git
synced 2026-01-31 08:24:26 -06:00
add Exceptions to schedules
This commit is contained in:
+35
-2
@@ -21,6 +21,9 @@ type Schedule struct {
|
|||||||
isSunrise bool
|
isSunrise bool
|
||||||
isSunset bool
|
isSunset bool
|
||||||
sunOffset DurationString
|
sunOffset DurationString
|
||||||
|
|
||||||
|
exceptionDays []time.Time
|
||||||
|
exceptionRanges []timeRange
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Schedule) Hash() string {
|
func (s Schedule) Hash() string {
|
||||||
@@ -125,10 +128,30 @@ func (sb scheduleBuilderCustom) Offset(s DurationString) scheduleBuilderEnd {
|
|||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sb scheduleBuilderCustom) ExceptionDay(t time.Time) scheduleBuilderCustom {
|
||||||
|
sb.schedule.exceptionDays = append(sb.schedule.exceptionDays, t)
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sb scheduleBuilderCustom) ExceptionRange(start, end time.Time) scheduleBuilderCustom {
|
||||||
|
sb.schedule.exceptionRanges = append(sb.schedule.exceptionRanges, timeRange{start, end})
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderCustom) Build() Schedule {
|
func (sb scheduleBuilderCustom) Build() Schedule {
|
||||||
return sb.schedule
|
return sb.schedule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sb scheduleBuilderEnd) ExceptionDay(t time.Time) scheduleBuilderEnd {
|
||||||
|
sb.schedule.exceptionDays = append(sb.schedule.exceptionDays, t)
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sb scheduleBuilderEnd) ExceptionRange(start, end time.Time) scheduleBuilderEnd {
|
||||||
|
sb.schedule.exceptionRanges = append(sb.schedule.exceptionRanges, timeRange{start, end})
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderEnd) Build() Schedule {
|
func (sb scheduleBuilderEnd) Build() Schedule {
|
||||||
return sb.schedule
|
return sb.schedule
|
||||||
}
|
}
|
||||||
@@ -149,18 +172,28 @@ func runSchedules(a *app) {
|
|||||||
|
|
||||||
// run callback for all schedules before now in case they overlap
|
// run callback for all schedules before now in case they overlap
|
||||||
for sched.realStartTime.Before(time.Now()) {
|
for sched.realStartTime.Before(time.Now()) {
|
||||||
go sched.callback(a.service, a.state)
|
maybeRunCallback(a, sched)
|
||||||
requeueSchedule(a, sched)
|
requeueSchedule(a, sched)
|
||||||
|
|
||||||
sched = popSchedule(a)
|
sched = popSchedule(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Until(sched.realStartTime))
|
time.Sleep(time.Until(sched.realStartTime))
|
||||||
go sched.callback(a.service, a.state)
|
maybeRunCallback(a, sched)
|
||||||
requeueSchedule(a, sched)
|
requeueSchedule(a, sched)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func maybeRunCallback(a *app, s Schedule) {
|
||||||
|
if c := checkExceptionDays(s.exceptionDays); c.fail {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if c := checkExceptionRanges(s.exceptionRanges); c.fail {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go s.callback(a.service, a.state)
|
||||||
|
}
|
||||||
|
|
||||||
func popSchedule(a *app) Schedule {
|
func popSchedule(a *app) Schedule {
|
||||||
_sched, _ := a.schedules.Pop()
|
_sched, _ := a.schedules.Pop()
|
||||||
return _sched.(Schedule)
|
return _sched.(Schedule)
|
||||||
|
|||||||
Reference in New Issue
Block a user