LoopStatus.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // LoopStatus.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 7/26/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public struct LoopStatus {
  11. typealias RawValue = [String: Any]
  12. let name: String
  13. let version: String
  14. let timestamp: Date
  15. let iob: IOBStatus?
  16. let cob: COBStatus?
  17. let predicted: PredictedBG?
  18. let automaticDoseRecommendation: AutomaticDoseRecommendation?
  19. let recommendedBolus: Double?
  20. let enacted: LoopEnacted?
  21. let rileylinks: [RileyLinkStatus]?
  22. let failureReason: String?
  23. let currentCorrectionRange: CorrectionRange?
  24. let forecastError: ForecastError?
  25. let testingDetails: [String: Any]?
  26. public init(name: String, version: String, timestamp: Date, iob: IOBStatus? = nil, cob: COBStatus? = nil, predicted: PredictedBG? = nil, automaticDoseRecommendation: AutomaticDoseRecommendation? = nil, recommendedBolus: Double? = nil, enacted: LoopEnacted? = nil, rileylinks: [RileyLinkStatus]? = nil, failureReason: String? = nil, currentCorrectionRange: CorrectionRange? = nil, forecastError: ForecastError? = nil, testingDetails: [String: Any]? = nil) {
  27. self.name = name
  28. self.version = version
  29. self.timestamp = timestamp
  30. self.iob = iob
  31. self.cob = cob
  32. self.predicted = predicted
  33. self.automaticDoseRecommendation = automaticDoseRecommendation
  34. self.recommendedBolus = recommendedBolus
  35. self.enacted = enacted
  36. self.rileylinks = rileylinks
  37. self.failureReason = failureReason
  38. self.currentCorrectionRange = currentCorrectionRange
  39. self.forecastError = forecastError
  40. self.testingDetails = testingDetails
  41. }
  42. public var dictionaryRepresentation: [String: Any] {
  43. var rval = [String: Any]()
  44. rval["name"] = name
  45. rval["version"] = version
  46. rval["timestamp"] = TimeFormat.timestampStrFromDate(timestamp)
  47. if let iob = iob {
  48. rval["iob"] = iob.dictionaryRepresentation
  49. }
  50. if let cob = cob {
  51. rval["cob"] = cob.dictionaryRepresentation
  52. }
  53. if let predicted = predicted {
  54. rval["predicted"] = predicted.dictionaryRepresentation
  55. }
  56. if let automaticDoseRecommendation = automaticDoseRecommendation {
  57. rval["automaticDoseRecommendation"] = automaticDoseRecommendation.dictionaryRepresentation
  58. }
  59. if let recommendedBolus = recommendedBolus {
  60. rval["recommendedBolus"] = recommendedBolus
  61. }
  62. if let enacted = enacted {
  63. rval["enacted"] = enacted.dictionaryRepresentation
  64. }
  65. if let failureReason = failureReason {
  66. rval["failureReason"] = failureReason
  67. }
  68. if let rileylinks = rileylinks {
  69. rval["rileylinks"] = rileylinks.map { $0.dictionaryRepresentation }
  70. }
  71. if let currentCorrectionRange = currentCorrectionRange {
  72. rval["currentCorrectionRange"] = currentCorrectionRange.dictionaryRepresentation
  73. }
  74. if let forecastError = forecastError {
  75. rval["forecastError"] = forecastError.dictionaryRepresentation
  76. }
  77. if let testingDetails = testingDetails {
  78. rval["testingDetails"] = testingDetails
  79. }
  80. return rval
  81. }
  82. init?(rawValue: RawValue) {
  83. guard
  84. let name = rawValue["name"] as? String,
  85. let version = rawValue["version"] as? String,
  86. let timestampStr = rawValue["timestamp"] as? String,
  87. let timestamp = TimeFormat.dateFromTimestamp(timestampStr)
  88. else {
  89. return nil
  90. }
  91. self.name = name
  92. self.version = version
  93. self.timestamp = timestamp
  94. if let iobRaw = rawValue["iob"] as? IOBStatus.RawValue {
  95. self.iob = IOBStatus(rawValue: iobRaw)
  96. } else {
  97. self.iob = nil
  98. }
  99. if let cobRaw = rawValue["cob"] as? COBStatus.RawValue {
  100. self.cob = COBStatus(rawValue: cobRaw)
  101. } else {
  102. self.cob = nil
  103. }
  104. if let predictedRaw = rawValue["predicted"] as? PredictedBG.RawValue {
  105. predicted = PredictedBG(rawValue: predictedRaw)
  106. } else {
  107. predicted = nil
  108. }
  109. if let automaticDoseRecommendationRaw = rawValue["automaticDoseRecommendation"] as? AutomaticDoseRecommendation.RawValue {
  110. automaticDoseRecommendation = AutomaticDoseRecommendation(rawValue: automaticDoseRecommendationRaw)
  111. } else {
  112. automaticDoseRecommendation = nil
  113. }
  114. recommendedBolus = rawValue["recommendedBolus"] as? Double
  115. if let enactedRaw = rawValue["enacted"] as? LoopEnacted.RawValue {
  116. enacted = LoopEnacted(rawValue: enactedRaw)
  117. } else {
  118. enacted = nil
  119. }
  120. if let rileylinksRaw = rawValue["rileylinks"] as? [RileyLinkStatus.RawValue] {
  121. rileylinks = rileylinksRaw.compactMap { RileyLinkStatus(rawValue: $0 ) }
  122. } else {
  123. rileylinks = nil
  124. }
  125. failureReason = rawValue["failureReason"] as? String
  126. if let currentCorrectionRangeRaw = rawValue["currentCorrectionRange"] as? CorrectionRange.RawValue {
  127. currentCorrectionRange = CorrectionRange(rawValue: currentCorrectionRangeRaw)
  128. } else {
  129. currentCorrectionRange = nil
  130. }
  131. if let forecastErrorRaw = rawValue["forecastError"] as? ForecastError.RawValue {
  132. forecastError = ForecastError(rawValue: forecastErrorRaw)
  133. } else {
  134. forecastError = nil
  135. }
  136. testingDetails = rawValue["testingDetails"] as? [String: Any]
  137. }
  138. }