LoopEnacted.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // LoopEnacted.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 LoopEnacted {
  10. typealias RawValue = [String: Any]
  11. let rate: Double?
  12. let duration: TimeInterval?
  13. let timestamp: Date
  14. let received: Bool
  15. let bolusVolume: Double
  16. public init(rate: Double?, duration: TimeInterval?, timestamp: Date, received: Bool, bolusVolume: Double = 0) {
  17. self.rate = rate
  18. self.duration = duration
  19. self.timestamp = timestamp
  20. self.received = received
  21. self.bolusVolume = bolusVolume
  22. }
  23. public var dictionaryRepresentation: [String: Any] {
  24. var rval = [String: Any]()
  25. rval["rate"] = rate
  26. if let duration = duration {
  27. rval["duration"] = duration / 60.0
  28. }
  29. rval["timestamp"] = TimeFormat.timestampStrFromDate(timestamp)
  30. rval["received"] = received
  31. rval["bolusVolume"] = bolusVolume
  32. return rval
  33. }
  34. init?(rawValue: RawValue) {
  35. guard
  36. let rate = rawValue["rate"] as? Double,
  37. let durationMinutes = rawValue["duration"] as? Double,
  38. let timestampStr = rawValue["timestamp"] as? String,
  39. let timestamp = TimeFormat.dateFromTimestamp(timestampStr),
  40. let received = rawValue["received"] as? Bool
  41. else {
  42. return nil
  43. }
  44. self.rate = rate
  45. self.duration = TimeInterval(minutes: durationMinutes)
  46. self.timestamp = timestamp
  47. self.received = received
  48. self.bolusVolume = rawValue["bolusVolume"] as? Double ?? 0
  49. }
  50. }