Working on next alarm retrieval

This commit is contained in:
nemunaire 2022-10-06 14:02:56 +02:00
commit 9a06d04ce0
8 changed files with 261 additions and 40 deletions

View file

@ -2,6 +2,7 @@ package reveil
import (
"fmt"
"sort"
"time"
)
@ -37,13 +38,117 @@ func (h *Hour) UnmarshalJSON(src []byte) error {
return nil
}
func GetNextAlarm(db *LevelDBStorage) (*time.Time, error) {
alarmsRepeated, err := GetAlarmsRepeated(db)
if err != nil {
return nil, err
}
var closestAlarm *time.Time
for _, alarm := range alarmsRepeated {
next := alarm.GetNextOccurence(db)
if next != nil && (closestAlarm == nil || closestAlarm.After(*next)) {
closestAlarm = next
}
}
alarmsSingle, err := GetAlarmsSingle(db)
if err != nil {
return nil, err
}
for _, alarm := range alarmsSingle {
if closestAlarm == nil || closestAlarm.After(alarm.Time) {
closestAlarm = &alarm.Time
}
}
return closestAlarm, nil
}
type Exceptions []time.Time
func (e Exceptions) Len() int {
return len(e)
}
func (e Exceptions) Less(i, j int) bool {
return e[i].Before(e[j])
}
func (e Exceptions) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
type AlarmRepeated struct {
Id Identifier `json:"id"`
Weekday uint8 `json:"weekday"`
Weekday time.Weekday `json:"weekday"`
StartTime *Hour `json:"time"`
FollowingRoutines []Identifier `json:"routines"`
IgnoreExceptions bool `json:"ignore_exceptions"`
Comment string `json:"comment"`
Comment string `json:"comment,omitempty"`
Excepts Exceptions `json:"excepts,omitempty"`
NextTime *time.Time `json:"next_time,omitempty"`
}
func (a *AlarmRepeated) FillExcepts(db *LevelDBStorage) error {
if a.IgnoreExceptions {
return nil
}
exceptions, err := GetAlarmExceptions(db)
if err != nil {
return err
}
now := time.Now()
for _, exception := range exceptions {
if now.After(time.Time(*exception.Start)) {
continue
}
end := time.Time(*exception.End).AddDate(0, 0, 1)
for t := time.Time(*exception.Start); end.After(t); t = t.AddDate(0, 0, 1) {
if t.Weekday() == a.Weekday {
a.Excepts = append(a.Excepts, time.Date(t.Year(), t.Month(), t.Day(), time.Time(*a.StartTime).Hour(), time.Time(*a.StartTime).Minute(), time.Time(*a.StartTime).Second(), 0, now.Location()))
t.AddDate(0, 0, 6)
}
}
}
sort.Sort(a.Excepts)
return nil
}
func (a *AlarmRepeated) GetNextOccurence(db *LevelDBStorage) *time.Time {
if len(a.Excepts) == 0 {
a.FillExcepts(db)
}
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), time.Time(*a.StartTime).Hour(), time.Time(*a.StartTime).Minute(), time.Time(*a.StartTime).Second(), 0, now.Location())
if now.After(today) {
today = today.AddDate(0, 0, 1)
}
end := today.AddDate(0, 0, 7)
var nextOccurence time.Time
for nextOccurence = today; end.After(nextOccurence); nextOccurence = nextOccurence.AddDate(0, 0, 1) {
if nextOccurence.Weekday() == a.Weekday {
break
}
}
for _, except := range a.Excepts {
if except.Equal(nextOccurence) {
nextOccurence = nextOccurence.AddDate(0, 0, 7)
}
}
return &nextOccurence
}
func GetAlarmRepeated(db *LevelDBStorage, id Identifier) (alarm *AlarmRepeated, err error) {
@ -82,6 +187,9 @@ func PutAlarmRepeated(db *LevelDBStorage, alarm *AlarmRepeated) (err error) {
}
alarm.Id = id
// Don't store this, this is autocalculated
alarm.Excepts = nil
alarm.NextTime = nil
return db.put(key, alarm)
}
@ -94,7 +202,7 @@ type AlarmSingle struct {
Id Identifier `json:"id"`
Time time.Time `json:"time"`
FollowingRoutines []Identifier `json:"routines"`
Comment string `json:"comment"`
Comment string `json:"comment,omitempty"`
}
func GetAlarmSingle(db *LevelDBStorage, id Identifier) (alarm *AlarmSingle, err error) {
@ -145,7 +253,7 @@ type AlarmException struct {
Id Identifier `json:"id"`
Start *Date `json:"start"`
End *Date `json:"end"`
Comment string `json:"comment"`
Comment string `json:"comment,omitempty"`
}
func GetAlarmException(db *LevelDBStorage, id Identifier) (alarm *AlarmException, err error) {