RileyLinkPumpManager.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // RileyLinkPumpManager.swift
  3. // Loop
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import LoopKit
  8. import RileyLinkBLEKit
  9. open class RileyLinkPumpManager {
  10. public init(rileyLinkDeviceProvider: RileyLinkDeviceProvider,
  11. rileyLinkConnectionManager: RileyLinkConnectionManager? = nil) {
  12. self.rileyLinkDeviceProvider = rileyLinkDeviceProvider
  13. self.rileyLinkConnectionManager = rileyLinkConnectionManager
  14. self.rileyLinkConnectionManagerState = rileyLinkConnectionManager?.state
  15. // Listen for device notifications
  16. NotificationCenter.default.addObserver(self, selector: #selector(receivedRileyLinkPacketNotification(_:)), name: .DevicePacketReceived, object: nil)
  17. NotificationCenter.default.addObserver(self, selector: #selector(receivedRileyLinkTimerTickNotification(_:)), name: .DeviceTimerDidTick, object: nil)
  18. }
  19. /// Manages all the RileyLinks - access to management is optional
  20. public let rileyLinkConnectionManager: RileyLinkConnectionManager?
  21. // TODO: Not thread-safe
  22. open var rileyLinkConnectionManagerState: RileyLinkConnectionManagerState?
  23. /// Access to rileylink devices
  24. public let rileyLinkDeviceProvider: RileyLinkDeviceProvider
  25. // TODO: Put this on each RileyLinkDevice?
  26. private var lastTimerTick = Locked(Date.distantPast)
  27. /// Called when one of the connected devices receives a packet outside of a session
  28. ///
  29. /// - Parameters:
  30. /// - device: The device
  31. /// - packet: The received packet
  32. open func device(_ device: RileyLinkDevice, didReceivePacket packet: RFPacket) { }
  33. open func deviceTimerDidTick(_ device: RileyLinkDevice) { }
  34. open func device(_ device: RileyLinkDevice, didUpdateBattery level: Int) { }
  35. // MARK: - CustomDebugStringConvertible
  36. open var debugDescription: String {
  37. return [
  38. "## RileyLinkPumpManager",
  39. "rileyLinkConnectionManager: \(String(reflecting: rileyLinkConnectionManager))",
  40. "lastTimerTick: \(String(describing: lastTimerTick.value))",
  41. "",
  42. String(reflecting: rileyLinkDeviceProvider),
  43. ].joined(separator: "\n")
  44. }
  45. }
  46. // MARK: - RileyLink Updates
  47. extension RileyLinkPumpManager {
  48. /**
  49. Called when a new idle message is received by the RileyLink.
  50. Only MySentryPumpStatus messages are handled.
  51. - parameter note: The notification object
  52. */
  53. @objc private func receivedRileyLinkPacketNotification(_ note: Notification) {
  54. guard let device = note.object as? RileyLinkDevice,
  55. let packet = note.userInfo?[RileyLinkDevice.notificationPacketKey] as? RFPacket
  56. else {
  57. return
  58. }
  59. device.assertOnSessionQueue()
  60. self.device(device, didReceivePacket: packet)
  61. }
  62. @objc private func receivedRileyLinkTimerTickNotification(_ note: Notification) {
  63. guard let device = note.object as? RileyLinkDevice else {
  64. return
  65. }
  66. self.lastTimerTick.value = Date()
  67. self.deviceTimerDidTick(device)
  68. }
  69. open func connectToRileyLink(_ device: RileyLinkDevice) {
  70. rileyLinkConnectionManager?.connect(device)
  71. }
  72. open func disconnectFromRileyLink(_ device: RileyLinkDevice) {
  73. rileyLinkConnectionManager?.disconnect(device)
  74. }
  75. }
  76. // MARK: - RileyLinkConnectionManagerDelegate
  77. extension RileyLinkPumpManager: RileyLinkConnectionManagerDelegate {
  78. public func rileyLinkConnectionManager(_ rileyLinkConnectionManager: RileyLinkConnectionManager, didChange state: RileyLinkConnectionManagerState) {
  79. self.rileyLinkConnectionManagerState = state
  80. }
  81. }