ReadSettingsCarelinkMessageBody.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // ReadSettingsCarelinkMessageBody.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 12/26/15.
  6. // Copyright © 2015 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. public enum BasalProfile {
  10. typealias RawValue = UInt8
  11. case standard
  12. case profileA
  13. case profileB
  14. init(rawValue: UInt8) {
  15. switch rawValue {
  16. case 1:
  17. self = .profileA
  18. case 2:
  19. self = .profileB
  20. default:
  21. self = .standard
  22. }
  23. }
  24. var rawValue: RawValue {
  25. switch self {
  26. case .standard:
  27. return 0
  28. case .profileA:
  29. return 1
  30. case .profileB:
  31. return 2
  32. }
  33. }
  34. }
  35. /**
  36. Describes the response to the Read Settings command from the pump
  37. See: [Decocare Class](https://github.com/bewest/decoding-carelink/blob/master/decocare/commands.py#L1223)
  38. ```
  39. -- ------ -- 00 01 02 03 04 05 06 07 0809 10 11 12 13141516171819 20 21 2223 24 25 26 27 282930313233 343536 --
  40. a7 594040 c0 19 00 01 00 01 01 00 96 008c 00 00 00 00000064010400 14 00 1901 01 01 00 00 000000000000 000000 00000000000000000000000000000000000000000000000000000000 e9
  41. ```
  42. */
  43. public class ReadSettingsCarelinkMessageBody: DecodableMessageBody {
  44. public static var length: Int = 65
  45. public var txData: Data
  46. private static let maxBolusMultiplier: Double = 10
  47. private static let maxBasalMultiplier: Double = 40
  48. public let maxBasal: Double
  49. public let maxBolus: Double
  50. public let insulinActionCurveHours: Int
  51. public let selectedBasalProfile: BasalProfile
  52. public required init?(rxData: Data) {
  53. guard rxData.count == type(of: self).length else {
  54. return nil
  55. }
  56. let newer = rxData[0] == 25 // x23
  57. let maxBolusTicks: UInt8
  58. let maxBasalTicks: UInt16
  59. if newer {
  60. maxBolusTicks = rxData[7]
  61. maxBasalTicks = rxData[8..<10].toBigEndian(UInt16.self)
  62. } else {
  63. maxBolusTicks = rxData[6]
  64. maxBasalTicks = rxData[7..<9].toBigEndian(UInt16.self)
  65. }
  66. maxBolus = Double(maxBolusTicks) / type(of: self).maxBolusMultiplier
  67. maxBasal = Double(maxBasalTicks) / type(of: self).maxBasalMultiplier
  68. let rawSelectedBasalProfile: UInt8 = rxData[12]
  69. selectedBasalProfile = BasalProfile(rawValue: rawSelectedBasalProfile)
  70. let rawInsulinActionCurveHours: UInt8 = rxData[18]
  71. insulinActionCurveHours = Int(rawInsulinActionCurveHours)
  72. txData = rxData
  73. }
  74. public var description: String {
  75. return "ReadSettings(maxBasal:\(maxBasal), maxBolus:\(maxBolus), insulinActionCurveHours:\(insulinActionCurveHours), selectedBasalProfile:\(selectedBasalProfile))"
  76. }
  77. }