IOBStatus.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // IOBStatus.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 7/28/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct IOBStatus {
  10. typealias RawValue = [String: Any]
  11. let timestamp: Date
  12. let iob: Double? // basal iob + bolus iob: can be negative
  13. let basalIOB: Double? // does not include bolus iob
  14. public init(timestamp: Date, iob: Double? = nil, basalIOB: Double? = nil) {
  15. self.timestamp = timestamp
  16. self.iob = iob
  17. self.basalIOB = basalIOB
  18. }
  19. public var dictionaryRepresentation: [String: Any] {
  20. var rval = [String: Any]()
  21. rval["timestamp"] = TimeFormat.timestampStrFromDate(timestamp)
  22. if let iob = iob {
  23. rval["iob"] = iob
  24. }
  25. if let basalIOB = basalIOB {
  26. rval["basaliob"] = basalIOB
  27. }
  28. return rval
  29. }
  30. init?(rawValue: RawValue) {
  31. guard
  32. let timestampStr = rawValue["timestamp"] as? String,
  33. let timestamp = TimeFormat.dateFromTimestamp(timestampStr)
  34. else {
  35. return nil
  36. }
  37. self.timestamp = timestamp
  38. iob = rawValue["iob"] as? Double
  39. basalIOB = rawValue["basaliob"] as? Double
  40. }
  41. }