ResultDailyTotalPumpEvent.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // ResultDailyTotalPumpEvent.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 ResultDailyTotalPumpEvent: PumpEvent {
  10. public let length: Int
  11. public let rawData: Data
  12. public let timestamp: DateComponents
  13. public let totalUnits: Double
  14. public init?(availableData: Data, pumpModel: PumpModel) {
  15. if pumpModel.larger {
  16. length = 10
  17. } else {
  18. length = 7
  19. }
  20. guard length <= availableData.count else {
  21. return nil
  22. }
  23. rawData = availableData.subdata(in: 0..<length)
  24. let strokes = Int(bigEndianBytes: availableData.subdata(in: 3..<5))
  25. totalUnits = Double(strokes) / 40
  26. timestamp = DateComponents(pumpEventBytes: availableData.subdata(in: 5..<7))
  27. }
  28. public var dictionaryRepresentation: [String: Any] {
  29. return [
  30. "_type": "ResultDailyTotal",
  31. "totalUnits": totalUnits,
  32. "validDate": String(format: "%04d-%02d-%02d", timestamp.year!, timestamp.month!, timestamp.day!),
  33. ]
  34. }
  35. }