| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // RileyLinkPumpManager.swift
- // Loop
- //
- // Copyright © 2018 LoopKit Authors. All rights reserved.
- //
- import LoopKit
- import RileyLinkBLEKit
- open class RileyLinkPumpManager {
-
- public init(rileyLinkDeviceProvider: RileyLinkDeviceProvider,
- rileyLinkConnectionManager: RileyLinkConnectionManager? = nil) {
-
- self.rileyLinkDeviceProvider = rileyLinkDeviceProvider
- self.rileyLinkConnectionManager = rileyLinkConnectionManager
- self.rileyLinkConnectionManagerState = rileyLinkConnectionManager?.state
-
- // Listen for device notifications
- NotificationCenter.default.addObserver(self, selector: #selector(receivedRileyLinkPacketNotification(_:)), name: .DevicePacketReceived, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(receivedRileyLinkTimerTickNotification(_:)), name: .DeviceTimerDidTick, object: nil)
- }
-
- /// Manages all the RileyLinks - access to management is optional
- public let rileyLinkConnectionManager: RileyLinkConnectionManager?
- // TODO: Not thread-safe
- open var rileyLinkConnectionManagerState: RileyLinkConnectionManagerState?
-
- /// Access to rileylink devices
- public let rileyLinkDeviceProvider: RileyLinkDeviceProvider
- // TODO: Put this on each RileyLinkDevice?
- private var lastTimerTick = Locked(Date.distantPast)
- /// Called when one of the connected devices receives a packet outside of a session
- ///
- /// - Parameters:
- /// - device: The device
- /// - packet: The received packet
- open func device(_ device: RileyLinkDevice, didReceivePacket packet: RFPacket) { }
- open func deviceTimerDidTick(_ device: RileyLinkDevice) { }
- open func device(_ device: RileyLinkDevice, didUpdateBattery level: Int) { }
- // MARK: - CustomDebugStringConvertible
-
- open var debugDescription: String {
- return [
- "## RileyLinkPumpManager",
- "rileyLinkConnectionManager: \(String(reflecting: rileyLinkConnectionManager))",
- "lastTimerTick: \(String(describing: lastTimerTick.value))",
- "",
- String(reflecting: rileyLinkDeviceProvider),
- ].joined(separator: "\n")
- }
- }
- // MARK: - RileyLink Updates
- extension RileyLinkPumpManager {
- /**
- Called when a new idle message is received by the RileyLink.
- Only MySentryPumpStatus messages are handled.
- - parameter note: The notification object
- */
- @objc private func receivedRileyLinkPacketNotification(_ note: Notification) {
- guard let device = note.object as? RileyLinkDevice,
- let packet = note.userInfo?[RileyLinkDevice.notificationPacketKey] as? RFPacket
- else {
- return
- }
- device.assertOnSessionQueue()
- self.device(device, didReceivePacket: packet)
- }
- @objc private func receivedRileyLinkTimerTickNotification(_ note: Notification) {
- guard let device = note.object as? RileyLinkDevice else {
- return
- }
- self.lastTimerTick.value = Date()
- self.deviceTimerDidTick(device)
- }
-
- open func connectToRileyLink(_ device: RileyLinkDevice) {
- rileyLinkConnectionManager?.connect(device)
- }
- open func disconnectFromRileyLink(_ device: RileyLinkDevice) {
- rileyLinkConnectionManager?.disconnect(device)
- }
-
- }
- // MARK: - RileyLinkConnectionManagerDelegate
- extension RileyLinkPumpManager: RileyLinkConnectionManagerDelegate {
- public func rileyLinkConnectionManager(_ rileyLinkConnectionManager: RileyLinkConnectionManager, didChange state: RileyLinkConnectionManagerState) {
- self.rileyLinkConnectionManagerState = state
- }
- }
|