DeviceStatusOpenAPS.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // DeviceStatusOpenAPS.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-05-19.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension MainViewController {
  11. func DeviceStatusOpenAPS(formatter: ISO8601DateFormatter, lastDeviceStatus: [String: AnyObject]?, lastLoopRecord: [String: AnyObject]) {
  12. if let lastLoopTime = formatter.date(from: (lastDeviceStatus?["created_at"] as! String))?.timeIntervalSince1970 {
  13. UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
  14. if lastLoopRecord["failureReason"] != nil {
  15. LoopStatusLabel.text = "X"
  16. latestLoopStatusString = "X"
  17. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
  18. } else {
  19. var wasEnacted = false
  20. if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject] {
  21. wasEnacted = true
  22. }
  23. if let iobdata = lastLoopRecord["iob"] as? [String: AnyObject] {
  24. tableData[0].value = String(format: "%.2f", (iobdata["iob"] as! Double))
  25. latestIOB = String(format: "%.2f", (iobdata["iob"] as! Double))
  26. }
  27. if let cobdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
  28. tableData[1].value = String(format: "%.0f", cobdata["COB"] as! Double)
  29. latestCOB = String(format: "%.0f", cobdata["COB"] as! Double)
  30. }
  31. if let recbolusdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  32. let insulinReq = recbolusdata["insulinReq"] as? Double {
  33. tableData[8].value = String(format: "%.2fU", insulinReq)
  34. UserDefaultsRepository.deviceRecBolus.value = insulinReq
  35. } else {
  36. tableData[8].value = "N/A"
  37. UserDefaultsRepository.deviceRecBolus.value = 0
  38. }
  39. if let autosensdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
  40. let sens = autosensdata["sensitivityRatio"] as! Double * 100.0
  41. tableData[11].value = String(format: "%.0f", sens) + "%"
  42. }
  43. if let eventualdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
  44. if let eventualBGValue = eventualdata["eventualBG"] as? NSNumber {
  45. let eventualBGStringValue = String(describing: eventualBGValue)
  46. PredictionLabel.text = bgUnits.toDisplayUnits(eventualBGStringValue)
  47. }
  48. }
  49. var predictioncolor = UIColor.systemGray
  50. PredictionLabel.textColor = predictioncolor
  51. topPredictionBG = UserDefaultsRepository.minBGScale.value
  52. if let enactdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  53. let predbgdata = enactdata["predBGs"] as? [String: AnyObject] {
  54. let predictionTypes: [(type: String, colorName: String, dataIndex: Int)] = [
  55. ("ZT", "ZT", 12),
  56. ("IOB", "Insulin", 13),
  57. ("COB", "LoopYellow", 14),
  58. ("UAM", "UAM", 15)
  59. ]
  60. var minPredBG = Double.infinity
  61. var maxPredBG = -Double.infinity
  62. for (type, colorName, dataIndex) in predictionTypes {
  63. var predictionData = [ShareGlucoseData]()
  64. if let graphdata = predbgdata[type] as? [Double] {
  65. var predictionTime = lastLoopTime
  66. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  67. for i in 0...toLoad {
  68. if i < graphdata.count {
  69. let predictionValue = graphdata[i]
  70. minPredBG = min(minPredBG, predictionValue)
  71. maxPredBG = max(maxPredBG, predictionValue)
  72. let prediction = ShareGlucoseData(sgv: Int(round(predictionValue)), date: predictionTime, direction: "flat")
  73. predictionData.append(prediction)
  74. predictionTime += 300
  75. }
  76. }
  77. }
  78. let color = UIColor(named: colorName) ?? UIColor.systemPurple
  79. updatePredictionGraphGeneric(
  80. dataIndex: dataIndex,
  81. predictionData: predictionData,
  82. chartLabel: type,
  83. color: color
  84. )
  85. }
  86. if minPredBG != Double.infinity && maxPredBG != -Double.infinity {
  87. tableData[9].value = "\(bgUnits.toDisplayUnits(String(minPredBG)))/\(bgUnits.toDisplayUnits(String(maxPredBG)))"
  88. } else {
  89. tableData[9].value = "N/A"
  90. }
  91. }
  92. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String: AnyObject] {
  93. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  94. var lastBGTime = lastLoopTime
  95. if bgData.count > 0 {
  96. lastBGTime = bgData[bgData.count - 1].date
  97. }
  98. if tempBasalTime > lastBGTime && !wasEnacted {
  99. LoopStatusLabel.text = "⏀"
  100. latestLoopStatusString = "⏀"
  101. } else {
  102. LoopStatusLabel.text = "↻"
  103. latestLoopStatusString = "↻"
  104. }
  105. }
  106. } else {
  107. LoopStatusLabel.text = "↻"
  108. latestLoopStatusString = "↻"
  109. }
  110. }
  111. evaluateNotLooping(lastLoopTime: lastLoopTime)
  112. }
  113. }
  114. }