RileyLinkConnectionManager.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // RileyLinkConnectionManager.swift
  3. // RileyLinkBLEKit
  4. //
  5. // Created by Pete Schwamb on 8/16/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol RileyLinkConnectionManagerDelegate : AnyObject {
  10. func rileyLinkConnectionManager(_ rileyLinkConnectionManager: RileyLinkConnectionManager, didChange state: RileyLinkConnectionManagerState)
  11. }
  12. public class RileyLinkConnectionManager {
  13. public typealias RawStateValue = [String : Any]
  14. /// The current, serializable state of the manager
  15. public var rawState: RawStateValue {
  16. return state.rawValue
  17. }
  18. public private(set) var state: RileyLinkConnectionManagerState {
  19. didSet {
  20. delegate?.rileyLinkConnectionManager(self, didChange: state)
  21. }
  22. }
  23. public var deviceProvider: RileyLinkDeviceProvider {
  24. return rileyLinkDeviceManager
  25. }
  26. public weak var delegate: RileyLinkConnectionManagerDelegate?
  27. private let rileyLinkDeviceManager: RileyLinkDeviceManager
  28. private var autoConnectIDs: Set<String> {
  29. get {
  30. return state.autoConnectIDs
  31. }
  32. set {
  33. state.autoConnectIDs = newValue
  34. }
  35. }
  36. public init(state: RileyLinkConnectionManagerState) {
  37. self.rileyLinkDeviceManager = RileyLinkDeviceManager(autoConnectIDs: state.autoConnectIDs)
  38. self.state = state
  39. }
  40. public init(autoConnectIDs: Set<String>) {
  41. self.rileyLinkDeviceManager = RileyLinkDeviceManager(autoConnectIDs: autoConnectIDs)
  42. self.state = RileyLinkConnectionManagerState(autoConnectIDs: autoConnectIDs)
  43. }
  44. public convenience init?(rawValue: RawStateValue) {
  45. if let state = RileyLinkConnectionManagerState(rawValue: rawValue) {
  46. self.init(state: state)
  47. } else {
  48. return nil
  49. }
  50. }
  51. public var connectingCount: Int {
  52. return self.autoConnectIDs.count
  53. }
  54. public func shouldConnect(to deviceID: String) -> Bool {
  55. return self.autoConnectIDs.contains(deviceID)
  56. }
  57. public func connect(_ device: RileyLinkDevice) {
  58. autoConnectIDs.insert(device.peripheralIdentifier.uuidString)
  59. rileyLinkDeviceManager.connect(device)
  60. }
  61. public func disconnect(_ device: RileyLinkDevice) {
  62. autoConnectIDs.remove(device.peripheralIdentifier.uuidString)
  63. rileyLinkDeviceManager.disconnect(device)
  64. }
  65. public func setScanningEnabled(_ enabled: Bool) {
  66. rileyLinkDeviceManager.setScanningEnabled(enabled)
  67. }
  68. }
  69. public protocol RileyLinkDeviceProvider: AnyObject {
  70. func getDevices(_ completion: @escaping (_ devices: [RileyLinkDevice]) -> Void)
  71. var idleListeningEnabled: Bool { get }
  72. var timerTickEnabled: Bool { get set }
  73. func deprioritize(_ device: RileyLinkDevice, completion: (() -> Void)?)
  74. func assertIdleListening(forcingRestart: Bool)
  75. var idleListeningState: RileyLinkDevice.IdleListeningState { get set }
  76. var debugDescription: String { get }
  77. }
  78. extension RileyLinkDeviceManager: RileyLinkDeviceProvider {}