TempBasalPumpEvent.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // TempBasalPumpEvent.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 3/8/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct TempBasalPumpEvent: TimestampedPumpEvent {
  10. public enum RateType : String {
  11. case Absolute = "absolute"
  12. case Percent = "percent"
  13. }
  14. public let length: Int
  15. public let rawData: Data
  16. public let rateType: RateType
  17. public let rate: Double
  18. public let timestamp: DateComponents
  19. public let wasRemotelyTriggered: Bool
  20. public init?(availableData: Data, pumpModel: PumpModel) {
  21. length = 8
  22. func d(_ idx: Int) -> Int {
  23. return Int(availableData[idx])
  24. }
  25. guard length <= availableData.count else {
  26. return nil
  27. }
  28. rawData = availableData.subdata(in: 0..<length)
  29. rateType = (d(7) >> 3) == 0 ? .Absolute : .Percent
  30. if rateType == .Absolute {
  31. rate = Double(((d(7) & 0b111) << 8) + d(1)) / 40.0
  32. } else {
  33. rate = Double(d(1))
  34. }
  35. timestamp = DateComponents(pumpEventData: availableData, offset: 2)
  36. wasRemotelyTriggered = availableData[5] & 0b01000000 != 0
  37. }
  38. public var dictionaryRepresentation: [String: Any] {
  39. return [
  40. "_type": "TempBasal",
  41. "rate": rate,
  42. "temp": rateType.rawValue,
  43. "wasRemotelyTriggered": wasRemotelyTriggered,
  44. ]
  45. }
  46. public var description: String {
  47. switch rateType {
  48. case .Absolute:
  49. return String(format: LocalizedString("Temporary Basal: %1$.3f U/hour", comment: "The format string description of a TempBasalPumpEvent. (1: The rate of the temp basal in minutes)"), rate)
  50. case .Percent:
  51. return String(format: LocalizedString("Temporary Basal: %1$d%%", comment: "The format string description of a TempBasalPumpEvent. (1: The rate of the temp basal in percent)"), Int(rate))
  52. }
  53. }
  54. }