ReadSettingsCarelinkMessageBody.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: CarelinkLongMessageBody {
  44. private static let maxBolusMultiplier: Double = 10
  45. private static let maxBasalMultiplier: Double = 40
  46. public let maxBasal: Double
  47. public let maxBolus: Double
  48. public let insulinActionCurveHours: Int
  49. public let selectedBasalProfile: BasalProfile
  50. public required init?(rxData: Data) {
  51. guard rxData.count == type(of: self).length else {
  52. return nil
  53. }
  54. let newer = rxData[0] == 25 // x23
  55. let maxBolusTicks: UInt8
  56. let maxBasalTicks: UInt16
  57. if newer {
  58. maxBolusTicks = rxData[7]
  59. maxBasalTicks = rxData[8..<10].toBigEndian(UInt16.self)
  60. } else {
  61. maxBolusTicks = rxData[6]
  62. maxBasalTicks = rxData[7..<9].toBigEndian(UInt16.self)
  63. }
  64. maxBolus = Double(maxBolusTicks) / type(of: self).maxBolusMultiplier
  65. maxBasal = Double(maxBasalTicks) / type(of: self).maxBasalMultiplier
  66. let rawSelectedBasalProfile: UInt8 = rxData[12]
  67. selectedBasalProfile = BasalProfile(rawValue: rawSelectedBasalProfile)
  68. let rawInsulinActionCurveHours: UInt8 = rxData[18]
  69. insulinActionCurveHours = Int(rawInsulinActionCurveHours)
  70. super.init(rxData: rxData)
  71. }
  72. public required init?(rxData: NSData) {
  73. fatalError("init(rxData:) has not been implemented")
  74. }
  75. }
  76. extension ReadSettingsCarelinkMessageBody: DictionaryRepresentable {
  77. public var dictionaryRepresentation: [String: Any] {
  78. return [:]
  79. }
  80. }