ReadTempBasalCarelinkMessageBody.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // ReadTempBasalCarelinkMessageBody.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 3/7/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. public class ReadTempBasalCarelinkMessageBody: CarelinkLongMessageBody {
  10. // MMX12 and above
  11. private static let strokesPerUnit = 40
  12. public enum RateType {
  13. case absolute
  14. case percent
  15. }
  16. public let timeRemaining: TimeInterval
  17. public let rate: Double
  18. public let rateType: RateType
  19. public required init?(rxData: Data) {
  20. guard rxData.count == type(of: self).length else {
  21. return nil
  22. }
  23. let rawRateType: UInt8 = rxData[1]
  24. switch rawRateType {
  25. case 0:
  26. rateType = .absolute
  27. let strokes = Int(bigEndianBytes: rxData.subdata(in: 3..<5))
  28. rate = Double(strokes) / Double(type(of: self).strokesPerUnit)
  29. case 1:
  30. rateType = .percent
  31. let rawRate: UInt8 = rxData[2]
  32. rate = Double(rawRate)
  33. default:
  34. return nil
  35. }
  36. let minutesRemaining = Int(bigEndianBytes: rxData.subdata(in: 5..<7))
  37. timeRemaining = TimeInterval(minutesRemaining * 60)
  38. super.init(rxData: rxData)
  39. }
  40. public required init?(rxData: NSData) {
  41. fatalError("init(rxData:) has not been implemented")
  42. }
  43. }