CalibrationState.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // CalibrationState.swift
  3. // xDripG5
  4. //
  5. // Created by Nate Racklyeft on 8/6/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. public enum CalibrationState: RawRepresentable {
  10. public typealias RawValue = UInt8
  11. public enum State: RawValue {
  12. case stopped = 1
  13. case warmup = 2
  14. case needFirstInitialCalibration = 4
  15. case needSecondInitialCalibration = 5
  16. case ok = 6
  17. case needCalibration7 = 7
  18. case calibrationError8 = 8
  19. case calibrationError9 = 9
  20. case calibrationError10 = 10
  21. case sensorFailure11 = 11
  22. case sensorFailure12 = 12
  23. case calibrationError13 = 13
  24. case needCalibration14 = 14
  25. case sessionFailure15 = 15
  26. case sessionFailure16 = 16
  27. case sessionFailure17 = 17
  28. case questionMarks = 18
  29. }
  30. case known(State)
  31. case unknown(RawValue)
  32. public init(rawValue: RawValue) {
  33. guard let state = State(rawValue: rawValue) else {
  34. self = .unknown(rawValue)
  35. return
  36. }
  37. self = .known(state)
  38. }
  39. public var rawValue: RawValue {
  40. switch self {
  41. case .known(let state):
  42. return state.rawValue
  43. case .unknown(let rawValue):
  44. return rawValue
  45. }
  46. }
  47. public var hasReliableGlucose: Bool {
  48. guard case .known(let state) = self else {
  49. return false
  50. }
  51. switch state {
  52. case .stopped,
  53. .warmup,
  54. .needFirstInitialCalibration,
  55. .needSecondInitialCalibration,
  56. .calibrationError8,
  57. .calibrationError9,
  58. .calibrationError10,
  59. .sensorFailure11,
  60. .sensorFailure12,
  61. .calibrationError13,
  62. .sessionFailure15,
  63. .sessionFailure16,
  64. .sessionFailure17,
  65. .questionMarks:
  66. return false
  67. case .ok, .needCalibration7, .needCalibration14:
  68. return true
  69. }
  70. }
  71. }
  72. extension CalibrationState: Equatable {
  73. public static func ==(lhs: CalibrationState, rhs: CalibrationState) -> Bool {
  74. switch (lhs, rhs) {
  75. case (.known(let lhs), .known(let rhs)):
  76. return lhs == rhs
  77. case (.unknown(let lhs), .unknown(let rhs)):
  78. return lhs == rhs
  79. default:
  80. return false
  81. }
  82. }
  83. }
  84. extension CalibrationState: CustomStringConvertible {
  85. public var description: String {
  86. switch self {
  87. case .known(let state):
  88. return String(describing: state)
  89. case .unknown(let value):
  90. return ".unknown(\(value))"
  91. }
  92. }
  93. }