DeviceStatusOpenAPS.swift 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastLoopTime: " + String(lastLoopTime)) }
  15. if lastLoopRecord["failureReason"] != nil {
  16. LoopStatusLabel.text = "X"
  17. latestLoopStatusString = "X"
  18. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
  19. } else {
  20. if let iobdata = lastLoopRecord["iob"] as? [String:AnyObject] {
  21. tableData[0].value = String(format:"%.2f", (iobdata["iob"] as! Double))
  22. latestIOB = String(format:"%.2f", (iobdata["iob"] as! Double))
  23. }
  24. if let cobdata = lastLoopRecord["enacted"] as? [String:AnyObject] {
  25. tableData[1].value = String(format:"%.0f", cobdata["COB"] as! Double)
  26. latestCOB = String(format:"%.0f", cobdata["COB"] as! Double)
  27. }
  28. if let recbolusdata = lastLoopRecord["enacted"] as? [String: AnyObject],
  29. let insulinReq = recbolusdata["insulinReq"] as? Double {
  30. tableData[8].value = String(format: "%.2fU", insulinReq)
  31. UserDefaultsRepository.deviceRecBolus.value = insulinReq
  32. } else {
  33. tableData[8].value = "N/A"
  34. UserDefaultsRepository.deviceRecBolus.value = 0
  35. }
  36. if let autosensdata = lastLoopRecord["enacted"] as? [String:AnyObject] {
  37. let sens = autosensdata["sensitivityRatio"] as! Double * 100.0
  38. tableData[11].value = String(format:"%.0f", sens) + "%"
  39. }
  40. var predictioncolor = UIColor.systemGray
  41. PredictionLabel.textColor = predictioncolor
  42. topPredictionBG = UserDefaultsRepository.minBGScale.value
  43. if let enactdata = lastLoopRecord["enacted"] as? [String:AnyObject],
  44. let predbgdata = enactdata["predBGs"] as? [String: AnyObject] {
  45. let predictionTypes: [(type: String, colorName: String, dataIndex: Int)] = [
  46. ("ZT", "ZT", 12),
  47. ("IOB", "Insulin", 13),
  48. ("COB", "LoopYellow", 14),
  49. ("UAM", "UAM", 15)
  50. ]
  51. for (type, colorName, dataIndex) in predictionTypes {
  52. var predictionData = [ShareGlucoseData]()
  53. if let graphdata = predbgdata[type] as? [Double] {
  54. var predictionTime = lastLoopTime
  55. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  56. for i in 0...toLoad {
  57. if i < graphdata.count {
  58. let prediction = ShareGlucoseData(sgv: Int(round(graphdata[i])), date: predictionTime, direction: "flat")
  59. predictionData.append(prediction)
  60. predictionTime += 300
  61. }
  62. }
  63. }
  64. let color = UIColor(named: colorName) ?? UIColor.systemPurple
  65. updatePredictionGraphGeneric(
  66. dataIndex: dataIndex,
  67. predictionData: predictionData,
  68. chartLabel: type,
  69. color: color
  70. )
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }