Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ struct MissedReadingCondition: AlarmCondition {
// Skip if we have *no* readings
guard let last = data.bgReadings.last else { return false }

guard let lastChecked = Storage.shared.lastBGChecked.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
}

let secondsSinceLast = now.timeIntervalSince(last.date)
return secondsSinceLast >= thresholdMinutes * 60
}
Expand Down
10 changes: 9 additions & 1 deletion LoopFollow/Controllers/Nightscout/BGData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extension MainViewController {
func webLoadNSBGData(dexData: [ShareGlucoseData] = []) {
// This kicks it out in the instance where dexcom fails but they aren't using NS &&
if !IsNightscoutEnabled() {
Storage.shared.lastBGChecked.value = Date()
return
}

Expand Down Expand Up @@ -109,6 +110,8 @@ extension MainViewController {
// if we have Dex data, use it
if !dexData.isEmpty {
self.ProcessDexBGData(data: dexData, sourceName: "Dexcom")
} else {
Storage.shared.lastBGChecked.value = Date()
}
return
}
Expand All @@ -121,6 +124,7 @@ extension MainViewController {

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

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

let entries = self.bgData
if entries.count < 2 { return } // Protect index out of bounds
if entries.count < 2 { // Protect index out of bounds
Storage.shared.lastBGChecked.value = Date()
return
}

self.updateBGGraph()
self.updateStats()
Expand Down Expand Up @@ -264,6 +271,7 @@ extension MainViewController {
stale: Observable.shared.bgStale.value
)
}
Storage.shared.lastBGChecked.value = Date()
}
}
}
4 changes: 2 additions & 2 deletions LoopFollow/Controllers/Nightscout/DeviceStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ 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 {
case let .success(json):
if let jsonDeviceStatus = json as? [[String: AnyObject]] {
DispatchQueue.main.async {
self.updateDeviceStatusDisplay(jsonDeviceStatus: jsonDeviceStatus)
Storage.shared.lastLoopingChecked.value = Date()
}
} else {
self.handleDeviceStatusError()
Expand All @@ -29,6 +28,7 @@ extension MainViewController {
private func handleDeviceStatusError() {
LogManager.shared.log(category: .deviceStatus, message: "Device status fetch failed!", limitIdentifier: "Device status fetch failed!")
DispatchQueue.main.async {
Storage.shared.lastLoopingChecked.value = Date()
TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: Date().addingTimeInterval(10))
self.evaluateNotLooping()
}
Expand Down
1 change: 1 addition & 0 deletions LoopFollow/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class Storage {
var persistentNotificationLastBGTime = StorageValue<Date>(key: "persistentNotificationLastBGTime", defaultValue: .distantPast)

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

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