app/scheduler: Enhancement: detect date change & update daily runtimes

accordingly

Signed-off-by: Thomas Klaehn <tkl@blackfinn.de>
This commit is contained in:
2025-08-15 09:03:49 +02:00
parent ce31b61b1b
commit b76d394b16

View File

@@ -31,17 +31,17 @@ func init() {
app_state_cache.Mode = ModeManual app_state_cache.Mode = ModeManual
// override defaults with stored values // override defaults with stored values
store.Path = app_state_storage_path store.SetPath(app_state_storage_path)
res, err := store.Read() res, err := store.Read()
if err != nil { if err != nil {
logger.Print("unable to read app state cache") logger.Print(err)
return return
} }
app_state_mutex.Lock() app_state_mutex.Lock()
err = json.Unmarshal(res, &app_state_cache) err = json.Unmarshal(res, &app_state_cache)
app_state_mutex.Unlock() app_state_mutex.Unlock()
if err != nil { if err != nil {
logger.Print("unable to evaluate config data") logger.Print(err)
} }
} }
@@ -50,10 +50,13 @@ func SetMode(mode AppMode) {
app_state_cache.Mode = mode app_state_cache.Mode = mode
res, err := json.Marshal(app_state_cache) res, err := json.Marshal(app_state_cache)
if err != nil { if err != nil {
logger.Print("unable to store app state cache") logger.Print(err)
return return
} }
store.Write(res) err = store.Write(res)
if err != nil {
logger.Print(err)
}
app_state_mutex.Unlock() app_state_mutex.Unlock()
} }
@@ -77,7 +80,7 @@ func app_state_saver() {
res, err := json.Marshal(app_state_cache) res, err := json.Marshal(app_state_cache)
app_state_mutex.Unlock() app_state_mutex.Unlock()
if err != nil { if err != nil {
logger.Print("unable to marshal object to json") logger.Print(err)
time.Sleep(time.Minute) time.Sleep(time.Minute)
continue continue
} }
@@ -87,6 +90,8 @@ func app_state_saver() {
} }
func poll_auto_off() { func poll_auto_off() {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
for { for {
app_state_mutex.Lock() app_state_mutex.Lock()
for i, od := range app_state_cache.off_devices { for i, od := range app_state_cache.off_devices {
@@ -115,6 +120,18 @@ func poll_auto_off() {
} }
} }
app_state_mutex.Unlock() app_state_mutex.Unlock()
// check date change
now := time.Now()
if now.After(today) {
// reset date
today = time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
for _, od := range app_state_cache.off_devices {
// reset run times
apiservice_state.SetRuntime(od.device.Name, 0)
}
}
time.Sleep(time.Second) time.Sleep(time.Second)
} }
} }
@@ -123,7 +140,6 @@ func register_off_device(dev apiservice_devices.Device) {
for _, od := range app_state_cache.off_devices { for _, od := range app_state_cache.off_devices {
if od.device.Name == dev.Name { if od.device.Name == dev.Name {
// device already in off list // device already in off list
// FIXME: update off time
return return
} }
} }