TempBasalExtraCommand.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // TempBasalExtraCommand.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 6/6/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct TempBasalExtraCommand : MessageBlock {
  10. public let acknowledgementBeep: Bool
  11. public let completionBeep: Bool
  12. public let programReminderInterval: TimeInterval
  13. public let remainingPulses: Double
  14. public let delayUntilFirstTenthOfPulse: TimeInterval
  15. public let rateEntries: [RateEntry]
  16. public let blockType: MessageBlockType = .tempBasalExtra
  17. public var data: Data {
  18. let beepOptions = (UInt8(programReminderInterval.minutes) & 0x3f) + (completionBeep ? (1<<6) : 0) + (acknowledgementBeep ? (1<<7) : 0)
  19. var data = Data([
  20. blockType.rawValue,
  21. UInt8(8 + rateEntries.count * 6),
  22. beepOptions,
  23. 0
  24. ])
  25. data.appendBigEndian(UInt16(round(remainingPulses * 2) * 5))
  26. if remainingPulses == 0 {
  27. data.appendBigEndian(UInt32(delayUntilFirstTenthOfPulse.hundredthsOfMilliseconds) * 10)
  28. } else {
  29. data.appendBigEndian(UInt32(delayUntilFirstTenthOfPulse.hundredthsOfMilliseconds))
  30. }
  31. for entry in rateEntries {
  32. data.append(entry.data)
  33. }
  34. return data
  35. }
  36. public init(encodedData: Data) throws {
  37. let length = encodedData[1]
  38. let numEntries = (length - 8) / 6
  39. acknowledgementBeep = encodedData[2] & (1<<7) != 0
  40. completionBeep = encodedData[2] & (1<<6) != 0
  41. programReminderInterval = TimeInterval(minutes: Double(encodedData[2] & 0x3f))
  42. remainingPulses = Double(encodedData[4...].toBigEndian(UInt16.self)) / 10.0
  43. let timerCounter = encodedData[6...].toBigEndian(UInt32.self)
  44. if remainingPulses == 0 {
  45. delayUntilFirstTenthOfPulse = TimeInterval(hundredthsOfMilliseconds: Double(timerCounter) / 10)
  46. } else {
  47. delayUntilFirstTenthOfPulse = TimeInterval(hundredthsOfMilliseconds: Double(timerCounter))
  48. }
  49. var entries = [RateEntry]()
  50. for entryIndex in (0..<numEntries) {
  51. let offset = 10 + entryIndex * 6
  52. let totalPulses = Double(encodedData[offset...].toBigEndian(UInt16.self)) / 10.0
  53. let timerCounter = encodedData[(offset+2)...].toBigEndian(UInt32.self)
  54. let delayBetweenPulses: TimeInterval
  55. if totalPulses == 0 {
  56. delayBetweenPulses = TimeInterval(hundredthsOfMilliseconds: Double(timerCounter)) / 10
  57. } else {
  58. delayBetweenPulses = TimeInterval(hundredthsOfMilliseconds: Double(timerCounter))
  59. }
  60. entries.append(RateEntry(totalPulses: totalPulses, delayBetweenPulses: delayBetweenPulses))
  61. }
  62. rateEntries = entries
  63. }
  64. public init(rate: Double, duration: TimeInterval, acknowledgementBeep: Bool = false, completionBeep: Bool = false, programReminderInterval: TimeInterval = 0) {
  65. rateEntries = RateEntry.makeEntries(rate: rate, duration: duration)
  66. remainingPulses = rateEntries[0].totalPulses
  67. delayUntilFirstTenthOfPulse = rateEntries[0].delayBetweenPulses
  68. self.acknowledgementBeep = acknowledgementBeep
  69. self.completionBeep = completionBeep
  70. self.programReminderInterval = programReminderInterval
  71. }
  72. }
  73. extension TempBasalExtraCommand: CustomDebugStringConvertible {
  74. public var debugDescription: String {
  75. return "TempBasalExtraCommand(completionBeep:\(completionBeep), programReminderInterval:\(programReminderInterval.stringValue) remainingPulses:\(remainingPulses), delayUntilNextPulse:\(delayUntilFirstTenthOfPulse.stringValue), rateEntries:\(rateEntries))"
  76. }
  77. }