From 52abc61ad609085c0a17e1a6469129a49a9f0d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Tue, 25 Jun 2024 20:46:38 +0200 Subject: [PATCH 1/3] Show the eventually value and loopstatus --- .../Nightscout/DeviceStatusOpenAPS.swift | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift index 380ce54d..35b545ef 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift @@ -14,12 +14,16 @@ extension MainViewController { 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 { + var wasEnacted = false + if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] { + wasEnacted = true + } + 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)) @@ -42,6 +46,29 @@ extension MainViewController { 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) + + //This is one possible way of showing Pred. with minPredBG /eventualBG + /* + if let reasonString = eventualdata["reason"] as? String { + let regex = try! NSRegularExpression(pattern: "minPredBG (\\d+)") + if let match = regex.firstMatch(in: reasonString, range: NSRange(location: 0, length: reasonString.utf16.count)) { + if let minPredBGRange = Range(match.range(at: 1), in: reasonString) { + let minPredBGString = String(reasonString[minPredBGRange]) + tableData[9].value = "\(bgUnits.toDisplayUnits(String(minPredBGString)))/\(bgUnits.toDisplayUnits(eventualBGStringValue))" + } + } else { + tableData[9].value = bgUnits.toDisplayUnits(eventualBGStringValue) + } + } else { + tableData[9].value = bgUnits.toDisplayUnits(eventualBGStringValue) + }*/ + } + } + var predictioncolor = UIColor.systemGray PredictionLabel.textColor = predictioncolor topPredictionBG = UserDefaultsRepository.minBGScale.value @@ -78,7 +105,27 @@ extension MainViewController { ) } } + + 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) } } } From 8e5a5cb5f9d22e95ac6f279bbcccb6aa91aca008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Mon, 1 Jul 2024 19:43:34 +0200 Subject: [PATCH 2/3] Using min/max for prediction --- .../Nightscout/DeviceStatusOpenAPS.swift | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift index 35b545ef..75df89c9 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift @@ -12,7 +12,7 @@ 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 lastLoopRecord["failureReason"] != nil { LoopStatusLabel.text = "X" @@ -20,17 +20,17 @@ extension MainViewController { if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") } } else { var wasEnacted = false - if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] { + if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject] { wasEnacted = true } - 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 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 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 { @@ -41,38 +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 eventualdata = lastLoopRecord["enacted"] as? [String: AnyObject] { if let eventualBGValue = eventualdata["eventualBG"] as? NSNumber { let eventualBGStringValue = String(describing: eventualBGValue) PredictionLabel.text = bgUnits.toDisplayUnits(eventualBGStringValue) - - //This is one possible way of showing Pred. with minPredBG /eventualBG - /* - if let reasonString = eventualdata["reason"] as? String { - let regex = try! NSRegularExpression(pattern: "minPredBG (\\d+)") - if let match = regex.firstMatch(in: reasonString, range: NSRange(location: 0, length: reasonString.utf16.count)) { - if let minPredBGRange = Range(match.range(at: 1), in: reasonString) { - let minPredBGString = String(reasonString[minPredBGRange]) - tableData[9].value = "\(bgUnits.toDisplayUnits(String(minPredBGString)))/\(bgUnits.toDisplayUnits(eventualBGStringValue))" - } - } else { - tableData[9].value = bgUnits.toDisplayUnits(eventualBGStringValue) - } - } else { - tableData[9].value = 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), @@ -81,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] { @@ -89,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 } @@ -104,9 +95,15 @@ 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 loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String: AnyObject] { if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 { var lastBGTime = lastLoopTime if bgData.count > 0 { From 2eb155948c6a0c5c6fc718bcc769180e7b523e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Mon, 1 Jul 2024 19:53:20 +0200 Subject: [PATCH 3/3] Renamed Pred. to Min/Max --- LoopFollow/ViewControllers/MainViewController.swift | 2 +- LoopFollow/repository/UserDefaults.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LoopFollow/ViewControllers/MainViewController.swift b/LoopFollow/ViewControllers/MainViewController.swift index 5da181dc..dc41ee11 100644 --- a/LoopFollow/ViewControllers/MainViewController.swift +++ b/LoopFollow/ViewControllers/MainViewController.swift @@ -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") diff --git a/LoopFollow/repository/UserDefaults.swift b/LoopFollow/repository/UserDefaults.swift index c5ec1dba..5d36e400 100644 --- a/LoopFollow/repository/UserDefaults.swift +++ b/LoopFollow/repository/UserDefaults.swift @@ -29,7 +29,7 @@ class UserDefaultsRepository { "SAGE", "CAGE", "Rec. Bolus", - "Pred.", + "Min/Max", //Previously "Pred." "Carbs today", "Autosens", "Profile"])