DeviceStatusOpenAPS.swift 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if let enactdata = lastLoopRecord["enacted"] as? [String:AnyObject],
  43. let predbgdata = enactdata["predBGs"] as? [String: AnyObject] {
  44. let predictionTypes: [(type: String, colorName: String, dataIndex: Int)] = [
  45. ("COB", "LoopYellow", 12),
  46. ("IOB", "Insulin", 13),
  47. ("UAM", "UAM", 14),
  48. ("ZT", "ZT", 15)
  49. ]
  50. for (type, colorName, dataIndex) in predictionTypes {
  51. if let graphdata = predbgdata[type] as? [Double] {
  52. var predictionData = [ShareGlucoseData]()
  53. var predictionTime = lastLoopTime
  54. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  55. for i in 0...toLoad {
  56. if i < graphdata.count {
  57. let prediction = ShareGlucoseData(sgv: Int(round(graphdata[i])), date: predictionTime, direction: "flat")
  58. predictionData.append(prediction)
  59. predictionTime += 300
  60. }
  61. }
  62. if let color = UIColor(named: colorName) {
  63. updatePredictionGraphGeneric(
  64. dataIndex: dataIndex,
  65. predictionData: predictionData,
  66. chartLabel: type,
  67. color: color
  68. )
  69. } else {
  70. updatePredictionGraphGeneric(
  71. dataIndex: dataIndex,
  72. predictionData: predictionData,
  73. chartLabel: type,
  74. color: UIColor.systemPurple
  75. )
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }