StatusResponse.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // StatusResponse.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 10/23/17.
  6. // Copyright © 2017 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct StatusResponse : MessageBlock {
  10. public let blockType: MessageBlockType = .statusResponse
  11. public let length: UInt8 = 10
  12. public let deliveryStatus: DeliveryStatus
  13. public let podProgressStatus: PodProgressStatus
  14. public let timeActive: TimeInterval
  15. public let reservoirLevel: Double?
  16. public let insulin: Double
  17. public let bolusNotDelivered: Double
  18. public let lastProgrammingMessageSeqNum: UInt8 // updated by pod for 03, 08, $11, $19, $1A, $1C, $1E & $1F command messages
  19. public let alerts: AlertSet
  20. public let data: Data
  21. public init(encodedData: Data) throws {
  22. if encodedData.count < length {
  23. throw MessageBlockError.notEnoughData
  24. }
  25. data = encodedData.prefix(upTo: Int(length))
  26. guard let deliveryStatus = DeliveryStatus(rawValue: encodedData[1] >> 4) else {
  27. throw MessageError.unknownValue(value: encodedData[1] >> 4, typeDescription: "DeliveryStatus")
  28. }
  29. self.deliveryStatus = deliveryStatus
  30. guard let podProgressStatus = PodProgressStatus(rawValue: encodedData[1] & 0xf) else {
  31. throw MessageError.unknownValue(value: encodedData[1] & 0xf, typeDescription: "PodProgressStatus")
  32. }
  33. self.podProgressStatus = podProgressStatus
  34. let minutes = ((Int(encodedData[7]) & 0x7f) << 6) + (Int(encodedData[8]) >> 2)
  35. self.timeActive = TimeInterval(minutes: Double(minutes))
  36. let highInsulinBits = Int(encodedData[2] & 0xf) << 9
  37. let midInsulinBits = Int(encodedData[3]) << 1
  38. let lowInsulinBits = Int(encodedData[4] >> 7)
  39. self.insulin = Double(highInsulinBits | midInsulinBits | lowInsulinBits) / Pod.pulsesPerUnit
  40. self.lastProgrammingMessageSeqNum = (encodedData[4] >> 3) & 0xf
  41. self.bolusNotDelivered = Double((Int(encodedData[4] & 0x3) << 8) | Int(encodedData[5])) / Pod.pulsesPerUnit
  42. self.alerts = AlertSet(rawValue: ((encodedData[6] & 0x7f) << 1) | (encodedData[7] >> 7))
  43. let reservoirValue = Double((Int(encodedData[8] & 0x3) << 8) + Int(encodedData[9])) / Pod.pulsesPerUnit
  44. if reservoirValue <= Pod.maximumReservoirReading {
  45. self.reservoirLevel = reservoirValue
  46. } else {
  47. self.reservoirLevel = nil
  48. }
  49. }
  50. }
  51. extension StatusResponse: CustomDebugStringConvertible {
  52. public var debugDescription: String {
  53. return "StatusResponse(deliveryStatus:\(deliveryStatus), progressStatus:\(podProgressStatus), timeActive:\(timeActive.stringValue), reservoirLevel:\(String(describing: reservoirLevel)), delivered:\(insulin), bolusNotDelivered:\(bolusNotDelivered), lastProgrammingMessageSeqNum:\(lastProgrammingMessageSeqNum), alerts:\(alerts))"
  54. }
  55. }