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
// override defaults with stored values
store.Path = app_state_storage_path
store.SetPath(app_state_storage_path)
res, err := store.Read()
if err != nil {
logger.Print("unable to read app state cache")
logger.Print(err)
return
}
app_state_mutex.Lock()
err = json.Unmarshal(res, &app_state_cache)
app_state_mutex.Unlock()
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
res, err := json.Marshal(app_state_cache)
if err != nil {
logger.Print("unable to store app state cache")
logger.Print(err)
return
}
store.Write(res)
err = store.Write(res)
if err != nil {
logger.Print(err)
}
app_state_mutex.Unlock()
}
@@ -77,7 +80,7 @@ func app_state_saver() {
res, err := json.Marshal(app_state_cache)
app_state_mutex.Unlock()
if err != nil {
logger.Print("unable to marshal object to json")
logger.Print(err)
time.Sleep(time.Minute)
continue
}
@@ -87,6 +90,8 @@ func app_state_saver() {
}
func poll_auto_off() {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
for {
app_state_mutex.Lock()
for i, od := range app_state_cache.off_devices {
@@ -115,6 +120,18 @@ func poll_auto_off() {
}
}
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)
}
}
@@ -123,7 +140,6 @@ func register_off_device(dev apiservice_devices.Device) {
for _, od := range app_state_cache.off_devices {
if od.device.Name == dev.Name {
// device already in off list
// FIXME: update off time
return
}
}