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

Show the eventually value and loopstatus #309

Merged
merged 3 commits into from
Jul 2, 2024
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
68 changes: 56 additions & 12 deletions LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ import UIKit
extension MainViewController {
func DeviceStatusOpenAPS(formatter: ISO8601DateFormatter, lastDeviceStatus: [String: AnyObject]?, lastLoopRecord: [String: AnyObject]) {

if let lastLoopTime = formatter.date(from: (lastDeviceStatus?["created_at"] as! String))?.timeIntervalSince1970 {
if let lastLoopTime = formatter.date(from: (lastDeviceStatus?["created_at"] as! String))?.timeIntervalSince1970 {
UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastLoopTime: " + String(lastLoopTime)) }
if lastLoopRecord["failureReason"] != nil {
LoopStatusLabel.text = "X"
latestLoopStatusString = "X"
if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
} else {
if let iobdata = lastLoopRecord["iob"] as? [String:AnyObject] {
tableData[0].value = String(format:"%.2f", (iobdata["iob"] as! Double))
latestIOB = String(format:"%.2f", (iobdata["iob"] as! Double))
var wasEnacted = false
if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject] {
wasEnacted = true
}
if let cobdata = lastLoopRecord["enacted"] as? [String:AnyObject] {
tableData[1].value = String(format:"%.0f", cobdata["COB"] as! Double)
latestCOB = String(format:"%.0f", cobdata["COB"] as! Double)

if let iobdata = lastLoopRecord["iob"] as? [String: AnyObject] {
tableData[0].value = String(format: "%.2f", (iobdata["iob"] as! Double))
latestIOB = String(format: "%.2f", (iobdata["iob"] as! Double))
}
if let cobdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
tableData[1].value = String(format: "%.0f", cobdata["COB"] as! Double)
latestCOB = String(format: "%.0f", cobdata["COB"] as! Double)
}
if let recbolusdata = lastLoopRecord["enacted"] as? [String: AnyObject],
let insulinReq = recbolusdata["insulinReq"] as? Double {
Expand All @@ -37,15 +41,22 @@ extension MainViewController {
UserDefaultsRepository.deviceRecBolus.value = 0
}

if let autosensdata = lastLoopRecord["enacted"] as? [String:AnyObject] {
if let autosensdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
let sens = autosensdata["sensitivityRatio"] as! Double * 100.0
tableData[11].value = String(format:"%.0f", sens) + "%"
tableData[11].value = String(format: "%.0f", sens) + "%"
}

if let eventualdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
if let eventualBGValue = eventualdata["eventualBG"] as? NSNumber {
let eventualBGStringValue = String(describing: eventualBGValue)
PredictionLabel.text = bgUnits.toDisplayUnits(eventualBGStringValue)
}
}

var predictioncolor = UIColor.systemGray
PredictionLabel.textColor = predictioncolor
topPredictionBG = UserDefaultsRepository.minBGScale.value
if let enactdata = lastLoopRecord["enacted"] as? [String:AnyObject],
if let enactdata = lastLoopRecord["enacted"] as? [String: AnyObject],
let predbgdata = enactdata["predBGs"] as? [String: AnyObject] {
let predictionTypes: [(type: String, colorName: String, dataIndex: Int)] = [
("ZT", "ZT", 12),
Expand All @@ -54,6 +65,9 @@ extension MainViewController {
("UAM", "UAM", 15)
]

var minPredBG = Double.infinity
var maxPredBG = -Double.infinity

for (type, colorName, dataIndex) in predictionTypes {
var predictionData = [ShareGlucoseData]()
if let graphdata = predbgdata[type] as? [Double] {
Expand All @@ -62,7 +76,11 @@ extension MainViewController {

for i in 0...toLoad {
if i < graphdata.count {
let prediction = ShareGlucoseData(sgv: Int(round(graphdata[i])), date: predictionTime, direction: "flat")
let predictionValue = graphdata[i]
minPredBG = min(minPredBG, predictionValue)
maxPredBG = max(maxPredBG, predictionValue)

let prediction = ShareGlucoseData(sgv: Int(round(predictionValue)), date: predictionTime, direction: "flat")
predictionData.append(prediction)
predictionTime += 300
}
Expand All @@ -77,8 +95,34 @@ extension MainViewController {
color: color
)
}

if minPredBG != Double.infinity && maxPredBG != -Double.infinity {
tableData[9].value = "\(bgUnits.toDisplayUnits(String(minPredBG)))/\(bgUnits.toDisplayUnits(String(maxPredBG)))"
} else {
tableData[9].value = "N/A"
}
}

if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String: AnyObject] {
if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
var lastBGTime = lastLoopTime
if bgData.count > 0 {
lastBGTime = bgData[bgData.count - 1].date
}
if tempBasalTime > lastBGTime && !wasEnacted {
LoopStatusLabel.text = "⏀"
latestLoopStatusString = "⏀"
} else {
LoopStatusLabel.text = "↻"
latestLoopStatusString = "↻"
}
}
} else {
LoopStatusLabel.text = "↻"
latestLoopStatusString = "↻"
}
}
evaluateNotLooping(lastLoopTime: lastLoopTime)
}
}
}
2 changes: 1 addition & 1 deletion LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
UserDefaultsRepository.infoNames.value.append("SAGE")
UserDefaultsRepository.infoNames.value.append("CAGE")
UserDefaultsRepository.infoNames.value.append("Rec. Bolus")
UserDefaultsRepository.infoNames.value.append("Pred.")
UserDefaultsRepository.infoNames.value.append("Min/Max") //Previously "Pred."
UserDefaultsRepository.infoNames.value.append("Carbs today")
UserDefaultsRepository.infoNames.value.append("Autosens")
UserDefaultsRepository.infoNames.value.append("Profile")
Expand Down
2 changes: 1 addition & 1 deletion LoopFollow/repository/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserDefaultsRepository {
"SAGE",
"CAGE",
"Rec. Bolus",
"Pred.",
"Min/Max", //Previously "Pred."
"Carbs today",
"Autosens",
"Profile"])
Expand Down