Skip to content

Commit

Permalink
Merge pull request loopandlearn#368 from loopandlearn/enacted-suggested
Browse files Browse the repository at this point in the history
Adapt LoopFollow to Trio 1.0’s Updated Device Status Upload Behavior
  • Loading branch information
bjorkert authored Feb 1, 2025
2 parents 51d3013 + e8abd99 commit 4f33e44
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 261 deletions.
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 @@ -20,6 +20,7 @@ extension MainViewController {
}

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 @@ -71,7 +72,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 @@ -128,8 +129,6 @@ extension MainViewController {
}

}

evaluateNotLooping(lastLoopTime: lastLoopTime)
}
}
}
Loading

0 comments on commit 4f33e44

Please sign in to comment.