PumpErrorMessageBody.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // PumpErrorMessageBody.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 5/10/17.
  6. // Copyright © 2017 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public enum PumpErrorCode: UInt8, CustomStringConvertible {
  10. // commandRefused can happen when temp basal type is set incorrectly, during suspended pump, or unfinished prime.
  11. case commandRefused = 0x08
  12. case maxSettingExceeded = 0x09
  13. case bolusInProgress = 0x0c
  14. case pageDoesNotExist = 0x0d
  15. public var description: String {
  16. switch self {
  17. case .commandRefused:
  18. return LocalizedString("Command refused", comment: "Pump error code returned when command refused")
  19. case .maxSettingExceeded:
  20. return LocalizedString("Max setting exceeded", comment: "Pump error code describing max setting exceeded")
  21. case .bolusInProgress:
  22. return LocalizedString("Bolus in progress", comment: "Pump error code when bolus is in progress")
  23. case .pageDoesNotExist:
  24. return LocalizedString("History page does not exist", comment: "Pump error code when invalid history page is requested")
  25. }
  26. }
  27. public var recoverySuggestion: String? {
  28. switch self {
  29. case .commandRefused:
  30. return LocalizedString("Check that the pump is not suspended or priming, or has a percent temp basal type", comment: "Suggestions for diagnosing a command refused pump error")
  31. default:
  32. return nil
  33. }
  34. }
  35. }
  36. public class PumpErrorMessageBody: DecodableMessageBody {
  37. public static let length = 1
  38. let rxData: Data
  39. public let errorCode: PartialDecode<PumpErrorCode, UInt8>
  40. public required init?(rxData: Data) {
  41. self.rxData = rxData
  42. let rawErrorCode = rxData[0]
  43. if let errorCode = PumpErrorCode(rawValue: rawErrorCode) {
  44. self.errorCode = .known(errorCode)
  45. } else {
  46. self.errorCode = .unknown(rawErrorCode)
  47. }
  48. }
  49. public var txData: Data {
  50. return rxData
  51. }
  52. public var description: String {
  53. return "PumpError(\(errorCode))"
  54. }
  55. }