RileyLinkPumpManager.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // MARK: - CustomDebugStringConvertible
  35. open var debugDescription: String {
  36. return [
  37. "## RileyLinkPumpManager",
  38. "rileyLinkConnectionManager: \(String(reflecting: rileyLinkConnectionManager))",
  39. "lastTimerTick: \(String(describing: lastTimerTick.value))",
  40. "",
  41. String(reflecting: rileyLinkDeviceProvider),
  42. ].joined(separator: "\n")
  43. }
  44. }
  45. // MARK: - RileyLink Updates
  46. extension RileyLinkPumpManager {
  47. /**
  48. Called when a new idle message is received by the RileyLink.
  49. Only MySentryPumpStatus messages are handled.
  50. - parameter note: The notification object
  51. */
  52. @objc private func receivedRileyLinkPacketNotification(_ note: Notification) {
  53. guard let device = note.object as? RileyLinkDevice,
  54. let packet = note.userInfo?[RileyLinkDevice.notificationPacketKey] as? RFPacket
  55. else {
  56. return
  57. }
  58. device.assertOnSessionQueue()
  59. self.device(device, didReceivePacket: packet)
  60. }
  61. @objc private func receivedRileyLinkTimerTickNotification(_ note: Notification) {
  62. guard let device = note.object as? RileyLinkDevice else {
  63. return
  64. }
  65. self.lastTimerTick.value = Date()
  66. self.deviceTimerDidTick(device)
  67. }
  68. open func connectToRileyLink(_ device: RileyLinkDevice) {
  69. rileyLinkConnectionManager?.connect(device)
  70. }
  71. open func disconnectFromRileyLink(_ device: RileyLinkDevice) {
  72. rileyLinkConnectionManager?.disconnect(device)
  73. }
  74. }
  75. // MARK: - RileyLinkConnectionManagerDelegate
  76. extension RileyLinkPumpManager: RileyLinkConnectionManagerDelegate {
  77. public func rileyLinkConnectionManager(_ rileyLinkConnectionManager: RileyLinkConnectionManager, didChange state: RileyLinkConnectionManagerState) {
  78. self.rileyLinkConnectionManagerState = state
  79. }
  80. }