Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt LoopFollow to Trio 1.0’s Updated Device Status Upload Behavior #368

Merged
merged 5 commits into from
Feb 1, 2025
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
87 changes: 45 additions & 42 deletions LoopFollow/Controllers/Nightscout/DeviceStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ import UIKit
import Charts

extension MainViewController {
// NS Device Status Web Call
func webLoadNSDeviceStatus() {
let count = ObservableUserDefaults.shared.device.value == "Trio" && Observable.shared.isLastDeviceStatusSuggested.value ? "5" : "1"
if count != "1" {
LogManager.shared.log(category: .deviceStatus, message: "Fetching \(count) device status records")
}

let parameters: [String: String] = ["count": count]
let parameters: [String: String] = ["count": "1"]
NightscoutUtils.executeDynamicRequest(eventType: .deviceStatus, parameters: parameters) { result in
switch result {
case .success(let json):
Expand All @@ -40,45 +34,52 @@ extension MainViewController {
DispatchQueue.main.async {
TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: Date().addingTimeInterval(10))
}

evaluateNotLooping()
}

func evaluateNotLooping(lastLoopTime: TimeInterval) {
if let statusStackView = LoopStatusLabel.superview as? UIStackView {
if ((TimeInterval(Date().timeIntervalSince1970) - lastLoopTime) / 60) > 15 {
IsNotLooping = true
// Change the distribution to 'fill' to allow manual resizing of arranged subviews
statusStackView.distribution = .fill

// Hide PredictionLabel and expand LoopStatusLabel to fill the entire stack view
PredictionLabel.isHidden = true
LoopStatusLabel.frame = CGRect(x: 0, y: 0, width: statusStackView.frame.width, height: statusStackView.frame.height)

// Update LoopStatusLabel's properties to display Not Looping
LoopStatusLabel.textAlignment = .center
LoopStatusLabel.text = "⚠️ Not Looping!"
LoopStatusLabel.textColor = UIColor.systemYellow
LoopStatusLabel.font = UIFont.boldSystemFont(ofSize: 18)

func evaluateNotLooping() {
guard let statusStackView = LoopStatusLabel.superview as? UIStackView else { return }

let now = TimeInterval(Date().timeIntervalSince1970)
let lastLoopTime = UserDefaultsRepository.alertLastLoopTime.value
let isAlarmEnabled = UserDefaultsRepository.alertNotLoopingActive.value
let nonLoopingTimeThreshold: TimeInterval

if isAlarmEnabled {
nonLoopingTimeThreshold = Double(UserDefaultsRepository.alertNotLooping.value * 60)
} else {
nonLoopingTimeThreshold = 15 * 60
}

if IsNightscoutEnabled(), (now - lastLoopTime) >= nonLoopingTimeThreshold, lastLoopTime > 0 {
IsNotLooping = true
statusStackView.distribution = .fill

PredictionLabel.isHidden = true
LoopStatusLabel.frame = CGRect(x: 0, y: 0, width: statusStackView.frame.width, height: statusStackView.frame.height)

LoopStatusLabel.textAlignment = .center
LoopStatusLabel.text = "⚠️ Not Looping!"
LoopStatusLabel.textColor = UIColor.systemYellow
LoopStatusLabel.font = UIFont.boldSystemFont(ofSize: 18)

} else {
IsNotLooping = false
statusStackView.distribution = .fillEqually
PredictionLabel.isHidden = false

LoopStatusLabel.textAlignment = .right
LoopStatusLabel.font = UIFont.systemFont(ofSize: 17)

if UserDefaultsRepository.forceDarkMode.value {
LoopStatusLabel.textColor = UIColor.white
} else {
IsNotLooping = false
// Restore the original distribution and visibility of labels
statusStackView.distribution = .fillEqually
PredictionLabel.isHidden = false

// Reset LoopStatusLabel's properties
LoopStatusLabel.textAlignment = .right
LoopStatusLabel.font = UIFont.systemFont(ofSize: 17)

if UserDefaultsRepository.forceDarkMode.value {
LoopStatusLabel.textColor = UIColor.white
} else {
LoopStatusLabel.textColor = UIColor.black
}
LoopStatusLabel.textColor = UIColor.black
}
}
latestLoopTime = lastLoopTime
}

// NS Device Status Response Processor
func updateDeviceStatusDisplay(jsonDeviceStatus: [[String:AnyObject]]) {
infoManager.clearInfoData(types: [.iob, .cob, .override, .battery, .pump, .target, .isf, .carbRatio, .updated, .recBolus, .tdd])
Expand Down Expand Up @@ -146,12 +147,12 @@ extension MainViewController {

// OpenAPS - handle new data
if let lastLoopRecord = lastDeviceStatus?["openaps"] as! [String : AnyObject]? {
DeviceStatusOpenAPS(formatter: formatter, lastDeviceStatus: lastDeviceStatus, lastLoopRecord: lastLoopRecord, jsonDeviceStatus: jsonDeviceStatus)
DeviceStatusOpenAPS(formatter: formatter, lastDeviceStatus: lastDeviceStatus, lastLoopRecord: lastLoopRecord)
}

// Start the timer based on the timestamp
let now = dateTimeUtils.getNowTimeIntervalUTC()
let secondsAgo = now - latestLoopTime
let secondsAgo = now - UserDefaultsRepository.alertLastLoopTime.value

DispatchQueue.main.async {
if secondsAgo >= (20 * 60) {
Expand Down Expand Up @@ -185,6 +186,8 @@ extension MainViewController {
)
}
}

evaluateNotLooping()
LogManager.shared.log(category: .deviceStatus, message: "Update Device Status done", isDebug: true)
}
}
5 changes: 2 additions & 3 deletions LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension MainViewController {
func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
ObservableUserDefaults.shared.device.value = "Loop"
if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970 {
let previousLastLoopTime = UserDefaultsRepository.alertLastLoopTime.value
UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
if let failure = lastLoopRecord["failureReason"] {
LoopStatusLabel.text = "X"
Expand Down Expand Up @@ -66,7 +67,7 @@ extension MainViewController {
let prediction = predictdata["values"] as! [Double]
PredictionLabel.text = Localizer.toDisplayUnits(String(Int(prediction.last!)))
PredictionLabel.textColor = UIColor.systemPurple
if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
if UserDefaultsRepository.downloadPrediction.value && previousLastLoopTime < lastLoopTime {
predictionData.removeAll()
var predictionTime = lastLoopTime
let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
Expand Down Expand Up @@ -123,8 +124,6 @@ extension MainViewController {
}

}

evaluateNotLooping(lastLoopTime: lastLoopTime)
}
}
}
Loading