morningalarm/internal/cron.go

40 lines
868 B
Go
Raw Permalink Normal View History

2023-08-25 20:44:44 +00:00
package morningalarm
import (
"time"
"github.com/robfig/cron/v3"
)
2023-10-02 17:54:00 +00:00
// nextAlarm returns the time of the next alarm to fire from the perspective of cron
2023-08-25 20:44:44 +00:00
func (ma *MorningAlarm) nextAlarm() *time.Time {
if len(ma.cr.Entries()) == 0 {
return nil
}
min := ma.cr.Entries()[0].Next
for _, entry := range ma.cr.Entries() {
if entry.Next.Before(min) {
min = entry.Next
}
}
return &min
}
2023-10-02 17:54:00 +00:00
// armAlarm arms an alarm to fire at the specified time
func (ma *MorningAlarm) armAlarm(alarm *alarm) (cron.EntryID, error) {
return ma.cr.AddFunc(alarm.Time, ma.fireAlarm)
2023-08-25 20:44:44 +00:00
}
2023-10-02 17:54:00 +00:00
// disarmAlarm disarms an alarm and prevents it from firing at the specified time
func (ma *MorningAlarm) disarmAlarm(alarm *alarm) {
ma.cr.Remove(alarm.cronID)
2023-08-25 20:44:44 +00:00
}
2023-10-02 17:54:00 +00:00
// fireAlarm function that is called when an alarm is fired
2023-08-25 20:44:44 +00:00
func (ma *MorningAlarm) fireAlarm() {
ma.playWakeUpMusic()
}