CancelDeliveryCommand.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // CancelDeliveryCommand.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/23/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct CancelDeliveryCommand : NonceResyncableMessageBlock {
  10. public let blockType: MessageBlockType = .cancelDelivery
  11. // ID1:1f00ee84 PTYPE:PDM SEQ:26 ID2:1f00ee84 B9:ac BLEN:7 MTYPE:1f05 BODY:e1f78752078196 CRC:03
  12. // Cancel bolus
  13. // 1f 05 be1b741a 64 - 1U
  14. // 1f 05 a00a1a95 64 - 1U over 1hr
  15. // 1f 05 ff52f6c8 64 - 1U immediate, 1U over 1hr
  16. // Cancel temp basal
  17. // 1f 05 f76d34c4 62 - 30U/hr
  18. // 1f 05 156b93e8 62 - ?
  19. // 1f 05 62723698 62 - 0%
  20. // 1f 05 2933db73 62 - 03ea
  21. // Suspend is a Cancel delivery, followed by a configure alerts command (0x19)
  22. // 1f 05 50f02312 03 191050f02312580f000f06046800001e0302
  23. // Deactivate pod:
  24. // 1f 05 e1f78752 07
  25. public struct DeliveryType: OptionSet, Equatable {
  26. public let rawValue: UInt8
  27. public static let none = DeliveryType()
  28. public static let basal = DeliveryType(rawValue: 1 << 0)
  29. public static let tempBasal = DeliveryType(rawValue: 1 << 1)
  30. public static let bolus = DeliveryType(rawValue: 1 << 2)
  31. public static let allButBasal: DeliveryType = [.tempBasal, .bolus]
  32. public static let all: DeliveryType = [.none, .basal, .tempBasal, .bolus]
  33. public init(rawValue: UInt8) {
  34. self.rawValue = rawValue
  35. }
  36. }
  37. public let deliveryType: DeliveryType
  38. public let beepType: BeepType
  39. public var nonce: UInt32
  40. public var data: Data {
  41. var data = Data([
  42. blockType.rawValue,
  43. 5,
  44. ])
  45. data.appendBigEndian(nonce)
  46. data.append((beepType.rawValue << 4) + deliveryType.rawValue)
  47. return data
  48. }
  49. public init(encodedData: Data) throws {
  50. if encodedData.count < 7 {
  51. throw MessageBlockError.notEnoughData
  52. }
  53. self.nonce = encodedData[2...].toBigEndian(UInt32.self)
  54. self.deliveryType = DeliveryType(rawValue: encodedData[6] & 0xf)
  55. self.beepType = BeepType(rawValue: encodedData[6] >> 4)!
  56. }
  57. public init(nonce: UInt32, deliveryType: DeliveryType, beepType: BeepType) {
  58. self.nonce = nonce
  59. self.deliveryType = deliveryType
  60. self.beepType = beepType
  61. }
  62. }
  63. extension CancelDeliveryCommand: CustomDebugStringConvertible {
  64. public var debugDescription: String {
  65. return "CancelDeliveryCommand(nonce:\(Data(bigEndian: nonce).hexadecimalString), deliveryType:\(deliveryType), beepType:\(beepType))"
  66. }
  67. }