PodInfoConfiguredAlerts.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // PodInfoConfiguredAlerts.swift
  3. // OmniKit
  4. //
  5. // Created by Eelke Jager on 16/09/2018.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. // Type 1 Pod Info returns information about the currently configured alerts
  10. public struct PodInfoConfiguredAlerts : PodInfo {
  11. // CMD 1 2 3 4 5 6 7 8 910 1112 1314 1516 1718 1920
  12. // DATA 0 1 2 3 4 5 6 7 8 910 1112 1314 1516 1718
  13. // 02 13 01 XXXX VVVV VVVV VVVV VVVV VVVV VVVV VVVV VVVV
  14. public let podInfoType : PodInfoResponseSubType = .configuredAlerts
  15. public let word_278 : Data
  16. public let alertsActivations : [AlertActivation]
  17. public let data : Data
  18. public struct AlertActivation {
  19. let beepType: BeepType
  20. let unitsLeft: Double
  21. let timeFromPodStart: UInt8
  22. public init(beepType: BeepType, timeFromPodStart: UInt8, unitsLeft: Double) {
  23. self.beepType = beepType
  24. self.timeFromPodStart = timeFromPodStart
  25. self.unitsLeft = unitsLeft
  26. }
  27. }
  28. public init(encodedData: Data) throws {
  29. guard encodedData.count >= 11 else {
  30. throw MessageBlockError.notEnoughData
  31. }
  32. self.word_278 = encodedData[1...2]
  33. let numAlertTypes = 8
  34. let beepType = BeepType.self
  35. var activations = [AlertActivation]()
  36. for alarmType in (0..<numAlertTypes) {
  37. let beepType = beepType.init(rawValue: UInt8(alarmType))
  38. let timeFromPodStart = encodedData[(3 + alarmType * 2)] // Double(encodedData[(5 + alarmType)] & 0x3f)
  39. let unitsLeft = Double(encodedData[(4 + alarmType * 2)]) * Pod.pulseSize
  40. activations.append(AlertActivation(beepType: beepType!, timeFromPodStart: timeFromPodStart, unitsLeft: unitsLeft))
  41. }
  42. alertsActivations = activations
  43. self.data = encodedData
  44. }
  45. }