Skip to content

Commit 5ad942c

Browse files
authored
Merge pull request #424 from loopandlearn/not-looping-last-checked
Prevent stale "Not Looping" alarms
2 parents 215c8bc + a868a7b commit 5ad942c

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

LoopFollow/Alarm/AlarmCondition/NotLoopingCondition.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct NotLoopingCondition: AlarmCondition {
88
static let type: AlarmType = .notLooping
99
init() {}
1010

11-
func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
11+
func evaluate(alarm: Alarm, data: AlarmData, now: Date) -> Bool {
1212
// ────────────────────────────────
1313
// 0. sanity checks
1414
// ────────────────────────────────
@@ -19,6 +19,17 @@ struct NotLoopingCondition: AlarmCondition {
1919
guard let lastLoopTime = data.lastLoopTime,
2020
lastLoopTime > 0 else { return false }
2121

22+
guard let lastChecked = Storage.shared.lastLoopingChecked.value else {
23+
// Never checked, so don't alarm.
24+
return false
25+
}
26+
27+
let checkedAgeSeconds = now.timeIntervalSince(lastChecked)
28+
if checkedAgeSeconds > 360 { // 6 minutes
29+
// The check itself is stale, so the data is unreliable. Don't alarm.
30+
return false
31+
}
32+
2233
// ────────────────────────────────
2334
// 1. elapsed-time test
2435
// ────────────────────────────────

LoopFollow/Controllers/Nightscout/DeviceStatus.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import UIKit
88

99
extension MainViewController {
1010
func webLoadNSDeviceStatus() {
11+
Storage.shared.lastLoopingChecked.value = Date()
12+
1113
let parameters = ["count": "1"]
1214
NightscoutUtils.executeDynamicRequest(eventType: .deviceStatus, parameters: parameters) { result in
1315
switch result {

LoopFollow/Storage/Storage.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class Storage {
160160
var persistentNotification = StorageValue<Bool>(key: "persistentNotification", defaultValue: false)
161161
var persistentNotificationLastBGTime = StorageValue<Date>(key: "persistentNotificationLastBGTime", defaultValue: .distantPast)
162162

163+
var lastLoopingChecked = StorageValue<Date?>(key: "lastLoopingChecked", defaultValue: nil)
164+
163165
static let shared = Storage()
164166
private init() {}
165167
}

0 commit comments

Comments
 (0)