Skip to content

Commit 7f7530d

Browse files
authored
Merge pull request #471 from bjorkert/alarm-check-freshness-on-completion
Alarms: stamp 'last checked' on completion to avoid false positives
2 parents 59092c9 + ec40f18 commit 7f7530d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ struct MissedReadingCondition: AlarmCondition {
1717
// Skip if we have *no* readings
1818
guard let last = data.bgReadings.last else { return false }
1919

20+
guard let lastChecked = Storage.shared.lastBGChecked.value else {
21+
// Never checked, so don't alarm.
22+
return false
23+
}
24+
25+
let checkedAgeSeconds = now.timeIntervalSince(lastChecked)
26+
if checkedAgeSeconds > 360 { // 6 minutes
27+
// The check itself is stale, so the data is unreliable. Don't alarm.
28+
return false
29+
}
30+
2031
let secondsSinceLast = now.timeIntervalSince(last.date)
2132
return secondsSinceLast >= thresholdMinutes * 60
2233
}

LoopFollow/Controllers/Nightscout/BGData.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extension MainViewController {
4646
func webLoadNSBGData(dexData: [ShareGlucoseData] = []) {
4747
// This kicks it out in the instance where dexcom fails but they aren't using NS &&
4848
if !IsNightscoutEnabled() {
49+
Storage.shared.lastBGChecked.value = Date()
4950
return
5051
}
5152

@@ -109,6 +110,8 @@ extension MainViewController {
109110
// if we have Dex data, use it
110111
if !dexData.isEmpty {
111112
self.ProcessDexBGData(data: dexData, sourceName: "Dexcom")
113+
} else {
114+
Storage.shared.lastBGChecked.value = Date()
112115
}
113116
return
114117
}
@@ -121,6 +124,7 @@ extension MainViewController {
121124

122125
guard !data.isEmpty else {
123126
LogManager.shared.log(category: .nightscout, message: "No bg data received. Skipping processing.", limitIdentifier: "No bg data received. Skipping processing.")
127+
Storage.shared.lastBGChecked.value = Date()
124128
return
125129
}
126130

@@ -221,7 +225,10 @@ extension MainViewController {
221225
TaskScheduler.shared.rescheduleTask(id: .minAgoUpdate, to: Date())
222226

223227
let entries = self.bgData
224-
if entries.count < 2 { return } // Protect index out of bounds
228+
if entries.count < 2 { // Protect index out of bounds
229+
Storage.shared.lastBGChecked.value = Date()
230+
return
231+
}
225232

226233
self.updateBGGraph()
227234
self.updateStats()
@@ -264,6 +271,7 @@ extension MainViewController {
264271
stale: Observable.shared.bgStale.value
265272
)
266273
}
274+
Storage.shared.lastBGChecked.value = Date()
267275
}
268276
}
269277
}

LoopFollow/Controllers/Nightscout/DeviceStatus.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import UIKit
77

88
extension MainViewController {
99
func webLoadNSDeviceStatus() {
10-
Storage.shared.lastLoopingChecked.value = Date()
11-
1210
let parameters = ["count": "1"]
1311
NightscoutUtils.executeDynamicRequest(eventType: .deviceStatus, parameters: parameters) { result in
1412
switch result {
1513
case let .success(json):
1614
if let jsonDeviceStatus = json as? [[String: AnyObject]] {
1715
DispatchQueue.main.async {
1816
self.updateDeviceStatusDisplay(jsonDeviceStatus: jsonDeviceStatus)
17+
Storage.shared.lastLoopingChecked.value = Date()
1918
}
2019
} else {
2120
self.handleDeviceStatusError()
@@ -29,6 +28,7 @@ extension MainViewController {
2928
private func handleDeviceStatusError() {
3029
LogManager.shared.log(category: .deviceStatus, message: "Device status fetch failed!", limitIdentifier: "Device status fetch failed!")
3130
DispatchQueue.main.async {
31+
Storage.shared.lastLoopingChecked.value = Date()
3232
TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: Date().addingTimeInterval(10))
3333
self.evaluateNotLooping()
3434
}

LoopFollow/Storage/Storage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class Storage {
160160
var persistentNotificationLastBGTime = StorageValue<Date>(key: "persistentNotificationLastBGTime", defaultValue: .distantPast)
161161

162162
var lastLoopingChecked = StorageValue<Date?>(key: "lastLoopingChecked", defaultValue: nil)
163+
var lastBGChecked = StorageValue<Date?>(key: "lastBGChecked", defaultValue: nil)
163164

164165
var alarmsPosition = StorageValue<TabPosition>(key: "alarmsPosition", defaultValue: .position2)
165166
var remotePosition = StorageValue<TabPosition>(key: "remotePosition", defaultValue: .more)

0 commit comments

Comments
 (0)