From a868a7be2917d0b71e2514319774eda32c74b420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Fri, 4 Jul 2025 19:58:01 +0200 Subject: [PATCH] Prevents stale Not Looping alarms --- .../Alarm/AlarmCondition/NotLoopingCondition.swift | 13 ++++++++++++- .../Controllers/Nightscout/DeviceStatus.swift | 2 ++ LoopFollow/Storage/Storage.swift | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/LoopFollow/Alarm/AlarmCondition/NotLoopingCondition.swift b/LoopFollow/Alarm/AlarmCondition/NotLoopingCondition.swift index dcea898df..16690e095 100644 --- a/LoopFollow/Alarm/AlarmCondition/NotLoopingCondition.swift +++ b/LoopFollow/Alarm/AlarmCondition/NotLoopingCondition.swift @@ -8,7 +8,7 @@ struct NotLoopingCondition: AlarmCondition { static let type: AlarmType = .notLooping init() {} - func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool { + func evaluate(alarm: Alarm, data: AlarmData, now: Date) -> Bool { // ──────────────────────────────── // 0. sanity checks // ──────────────────────────────── @@ -19,6 +19,17 @@ struct NotLoopingCondition: AlarmCondition { guard let lastLoopTime = data.lastLoopTime, lastLoopTime > 0 else { return false } + guard let lastChecked = Storage.shared.lastLoopingChecked.value else { + // Never checked, so don't alarm. + return false + } + + let checkedAgeSeconds = now.timeIntervalSince(lastChecked) + if checkedAgeSeconds > 360 { // 6 minutes + // The check itself is stale, so the data is unreliable. Don't alarm. + return false + } + // ──────────────────────────────── // 1. elapsed-time test // ──────────────────────────────── diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatus.swift b/LoopFollow/Controllers/Nightscout/DeviceStatus.swift index 6374bdec8..642d8bcb2 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatus.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatus.swift @@ -8,6 +8,8 @@ import UIKit extension MainViewController { func webLoadNSDeviceStatus() { + Storage.shared.lastLoopingChecked.value = Date() + let parameters = ["count": "1"] NightscoutUtils.executeDynamicRequest(eventType: .deviceStatus, parameters: parameters) { result in switch result { diff --git a/LoopFollow/Storage/Storage.swift b/LoopFollow/Storage/Storage.swift index 3d0a814b5..9c5852aaa 100644 --- a/LoopFollow/Storage/Storage.swift +++ b/LoopFollow/Storage/Storage.swift @@ -160,6 +160,8 @@ class Storage { var persistentNotification = StorageValue(key: "persistentNotification", defaultValue: false) var persistentNotificationLastBGTime = StorageValue(key: "persistentNotificationLastBGTime", defaultValue: .distantPast) + var lastLoopingChecked = StorageValue(key: "lastLoopingChecked", defaultValue: nil) + static let shared = Storage() private init() {} }