DeviceStatusOpenAPS.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import Foundation
  2. import UIKit
  3. extension MainViewController {
  4. func DeviceStatusOpenAPS(formatter: ISO8601DateFormatter, lastDeviceStatus: [String: AnyObject]?, lastLoopRecord: [String: AnyObject]) {
  5. if let lastLoopTime = formatter.date(from: (lastDeviceStatus?["created_at"] as! String))?.timeIntervalSince1970 {
  6. UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
  7. if lastLoopRecord["failureReason"] != nil {
  8. LoopStatusLabel.text = "X"
  9. latestLoopStatusString = "X"
  10. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
  11. } else {
  12. var wasEnacted = false
  13. if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject] {
  14. wasEnacted = true
  15. }
  16. /*
  17. ISF
  18. */
  19. let profileISF = profileManager.currentISF()
  20. var enactedISF: String?
  21. if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject],
  22. let enactedISFValue = enacted["ISF"] as? Double {
  23. enactedISF = Localizer.formatLocalDouble(enactedISFValue)
  24. }
  25. if let profileISF = profileISF, let enactedISF = enactedISF, profileISF != enactedISF {
  26. infoManager.updateInfoData(type: .isf, value: "\(profileISF) → \(enactedISF)")
  27. } else if let profileISF = profileISF {
  28. infoManager.updateInfoData(type: .isf, value: profileISF)
  29. }
  30. /*
  31. Carb Ratio (CR)
  32. */
  33. let profileCR = profileManager.currentCarbRatio()
  34. var enactedCR: String?
  35. if let reasonString = lastLoopRecord["enacted"]?["reason"] as? String {
  36. let pattern = "CR: (\\d+(?:\\.\\d+)?)"
  37. if let regex = try? NSRegularExpression(pattern: pattern) {
  38. let nsString = reasonString as NSString
  39. if let match = regex.firstMatch(in: reasonString, range: NSRange(location: 0, length: nsString.length)) {
  40. let crString = nsString.substring(with: match.range(at: 1))
  41. if let crValue = Double(crString) {
  42. enactedCR = Localizer.formatToLocalizedString(crValue)
  43. }
  44. }
  45. }
  46. }
  47. if let profileCR = profileCR, let enactedCR = enactedCR, profileCR != enactedCR {
  48. infoManager.updateInfoData(type: .carbRatio, value: "\(profileCR) → \(enactedCR)")
  49. } else if let profileCR = profileCR {
  50. infoManager.updateInfoData(type: .carbRatio, value: profileCR)
  51. }
  52. if let iobdata = lastLoopRecord["iob"] as? [String: AnyObject],
  53. let iobValue = iobdata["iob"] as? Double {
  54. let formattedIOB = String(format: "%.2f", iobValue)
  55. infoManager.updateInfoData(type: .iob, value: formattedIOB)
  56. latestIOB = formattedIOB
  57. }
  58. if let cobdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  59. let cobValue = cobdata["COB"] as? Double {
  60. let formattedCOB = String(format: "%.0f", cobValue)
  61. infoManager.updateInfoData(type: .cob, value: formattedCOB)
  62. latestCOB = formattedCOB
  63. }
  64. if let recbolusdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  65. let insulinReq = recbolusdata["insulinReq"] as? Double {
  66. let formattedRecBolus = String(format: "%.2fU", insulinReq)
  67. infoManager.updateInfoData(type: .recBolus, value: formattedRecBolus)
  68. UserDefaultsRepository.deviceRecBolus.value = insulinReq
  69. } else {
  70. infoManager.updateInfoData(type: .recBolus, value: "N/A")
  71. UserDefaultsRepository.deviceRecBolus.value = 0
  72. }
  73. if let autosensdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  74. let sens = autosensdata["sensitivityRatio"] as? Double {
  75. let formattedSens = String(format: "%.0f", sens * 100.0) + "%"
  76. infoManager.updateInfoData(type: .autosens, value: formattedSens)
  77. }
  78. if let eventualdata = lastLoopRecord["enacted"] as? [String: AnyObject] {
  79. if let eventualBGValue = eventualdata["eventualBG"] as? NSNumber {
  80. let eventualBGStringValue = String(describing: eventualBGValue)
  81. PredictionLabel.text = Localizer.toDisplayUnits(eventualBGStringValue)
  82. }
  83. }
  84. if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject],
  85. let currentTarget = enacted["current_target"] as? Double {
  86. let formattedTarget = Localizer.toDisplayUnits(String(currentTarget))
  87. infoManager.updateInfoData(type: .target, value: formattedTarget)
  88. }
  89. var predictioncolor = UIColor.systemGray
  90. PredictionLabel.textColor = predictioncolor
  91. topPredictionBG = UserDefaultsRepository.minBGScale.value
  92. if let enactdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  93. let predbgdata = enactdata["predBGs"] as? [String: AnyObject] {
  94. let predictionTypes: [(type: String, colorName: String, dataIndex: Int)] = [
  95. ("ZT", "ZT", 12),
  96. ("IOB", "Insulin", 13),
  97. ("COB", "LoopYellow", 14),
  98. ("UAM", "UAM", 15)
  99. ]
  100. var minPredBG = Double.infinity
  101. var maxPredBG = -Double.infinity
  102. for (type, colorName, dataIndex) in predictionTypes {
  103. var predictionData = [ShareGlucoseData]()
  104. if let graphdata = predbgdata[type] as? [Double] {
  105. var predictionTime = lastLoopTime
  106. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  107. for i in 0...toLoad {
  108. if i < graphdata.count {
  109. let predictionValue = graphdata[i]
  110. minPredBG = min(minPredBG, predictionValue)
  111. maxPredBG = max(maxPredBG, predictionValue)
  112. let prediction = ShareGlucoseData(sgv: Int(round(predictionValue)), date: predictionTime, direction: "flat")
  113. predictionData.append(prediction)
  114. predictionTime += 300
  115. }
  116. }
  117. }
  118. let color = UIColor(named: colorName) ?? UIColor.systemPurple
  119. updatePredictionGraphGeneric(
  120. dataIndex: dataIndex,
  121. predictionData: predictionData,
  122. chartLabel: type,
  123. color: color
  124. )
  125. }
  126. if minPredBG != Double.infinity && maxPredBG != -Double.infinity {
  127. let value = "\(Localizer.toDisplayUnits(String(minPredBG)))/\(Localizer.toDisplayUnits(String(maxPredBG)))"
  128. infoManager.updateInfoData(type: .minMax, value: value)
  129. } else {
  130. infoManager.updateInfoData(type: .minMax, value: "N/A")
  131. }
  132. }
  133. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String: AnyObject] {
  134. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  135. var lastBGTime = lastLoopTime
  136. if bgData.count > 0 {
  137. lastBGTime = bgData[bgData.count - 1].date
  138. }
  139. if tempBasalTime > lastBGTime && !wasEnacted {
  140. LoopStatusLabel.text = "⏀"
  141. latestLoopStatusString = "⏀"
  142. } else {
  143. LoopStatusLabel.text = "↻"
  144. latestLoopStatusString = "↻"
  145. }
  146. }
  147. } else {
  148. LoopStatusLabel.text = "↻"
  149. latestLoopStatusString = "↻"
  150. }
  151. }
  152. evaluateNotLooping(lastLoopTime: lastLoopTime)
  153. }
  154. }
  155. }