diff --git a/Cartfile b/Cartfile index 64c0d3287..6dbdc6a46 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "LoopKit/LoopKit" == 2.1.3 +github "LoopKit/LoopKit" == 2.1.5 diff --git a/Cartfile.resolved b/Cartfile.resolved index 9e36520f5..49b5e71cd 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1 @@ -github "LoopKit/LoopKit" "v2.1.3" +github "LoopKit/LoopKit" "v2.1.5" diff --git a/Crypto/Info.plist b/Crypto/Info.plist index 6a2e30b13..41f11ed79 100644 --- a/Crypto/Info.plist +++ b/Crypto/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/MinimedKit/Info.plist b/MinimedKit/Info.plist index 01009605d..9fa96d19b 100644 --- a/MinimedKit/Info.plist +++ b/MinimedKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/MinimedKit/PumpManager/MinimedPumpManager.swift b/MinimedKit/PumpManager/MinimedPumpManager.swift index cb1750d1d..4249f5ea8 100644 --- a/MinimedKit/PumpManager/MinimedPumpManager.swift +++ b/MinimedKit/PumpManager/MinimedPumpManager.swift @@ -197,20 +197,13 @@ public class MinimedPumpManager: RileyLinkPumpManager, PumpManager { // How long we should wait before we re-tune the RileyLink let tuneTolerance = TimeInterval(minutes: 14) - let deviceState = deviceStates[device.peripheralIdentifier, default: DeviceState()] - let lastTuned = deviceState.lastTuned ?? .distantPast + let lastTuned = state.lastTuned ?? .distantPast if lastTuned.timeIntervalSinceNow <= -tuneTolerance { pumpOps.runSession(withName: "Tune pump", using: device) { (session) in do { - let scanResult = try session.tuneRadio(current: deviceState.lastValidFrequency) + let scanResult = try session.tuneRadio() self.log.error("Device %{public}@ auto-tuned to %{public}@ MHz", device.name ?? "", String(describing: scanResult.bestFrequency)) - self.queue.async { - self.deviceStates[device.peripheralIdentifier] = DeviceState( - lastTuned: Date(), - lastValidFrequency: scanResult.bestFrequency - ) - } } catch let error { self.log.error("Device %{public}@ auto-tune failed with error: %{public}@", device.name ?? "", String(describing: error)) self.rileyLinkDeviceProvider.deprioritize(device, completion: nil) @@ -262,14 +255,13 @@ public class MinimedPumpManager: RileyLinkPumpManager, PumpManager { device.getStatus { (deviceStatus) in // Trigger device status upload, even if something is wrong with pumpStatus self.queue.async { - let deviceState = self.deviceStates[device.peripheralIdentifier] let pumpManagerStatus = PumpManagerStatus( date: pumpDate, timeZone: timeZone, device: deviceStatus.device(pumpID: self.state.pumpID, pumpModel: self.state.pumpModel), - lastValidFrequency: deviceState?.lastValidFrequency, - lastTuned: deviceState?.lastTuned, + lastValidFrequency: self.state.lastValidFrequency, + lastTuned: self.state.lastTuned, battery: PumpManagerStatus.BatteryStatus(percent: Double(status.batteryRemainingPercent) / 100), isSuspended: nil, isBolusing: nil, @@ -454,13 +446,12 @@ public class MinimedPumpManager: RileyLinkPumpManager, PumpManager { device.getStatus { (deviceStatus) in self.queue.async { - let deviceState = self.deviceStates[device.peripheralIdentifier] let pumpManagerStatus = PumpManagerStatus( date: date, timeZone: session.pump.timeZone, device: deviceStatus.device(pumpID: self.state.pumpID, pumpModel: self.state.pumpModel), - lastValidFrequency: deviceState?.lastValidFrequency, - lastTuned: deviceState?.lastTuned, + lastValidFrequency: self.state.lastValidFrequency, + lastTuned: self.state.lastTuned, battery: PumpManagerStatus.BatteryStatus( voltage: status.batteryVolts, state: { @@ -598,14 +589,15 @@ public class MinimedPumpManager: RileyLinkPumpManager, PumpManager { // MARK: - Configuration // MARK: Pump - + // TODO - public func getStateForDevice(_ device: RileyLinkDevice, completion: @escaping (_ deviceState: DeviceState, _ pumpOps: PumpOps) -> Void) { + public func getOpsForDevice(_ device: RileyLinkDevice, completion: @escaping (_ pumpOps: PumpOps) -> Void) { queue.async { - completion(self.deviceStates[device.peripheralIdentifier, default: DeviceState()], self.pumpOps) + completion(self.pumpOps) } } + public private(set) var pumpOps: PumpOps! /// The user's preferred method of fetching insulin data from the pump diff --git a/MinimedKit/PumpManager/MinimedPumpManagerState.swift b/MinimedKit/PumpManager/MinimedPumpManagerState.swift index 3446cd8b3..dfeb8222f 100644 --- a/MinimedKit/PumpManager/MinimedPumpManagerState.swift +++ b/MinimedKit/PumpManager/MinimedPumpManagerState.swift @@ -26,6 +26,10 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { public var pumpID: String public var pumpRegion: PumpRegion + + public var lastValidFrequency: Measurement? + + public var lastTuned: Date? public var pumpSettings: PumpSettings { get { @@ -42,12 +46,16 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { var state = PumpState() state.pumpModel = pumpModel state.timeZone = timeZone + state.lastValidFrequency = lastValidFrequency + state.lastTuned = lastTuned return state } set { if let model = newValue.pumpModel { pumpModel = model } + lastValidFrequency = newValue.lastValidFrequency + lastTuned = newValue.lastTuned timeZone = newValue.timeZone } } @@ -56,7 +64,7 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { public var timeZone: TimeZone - public init(batteryChemistry: BatteryChemistryType = .alkaline, preferredInsulinDataSource: InsulinDataSource = .pumpHistory, pumpColor: PumpColor, pumpID: String, pumpModel: PumpModel, pumpRegion: PumpRegion, rileyLinkConnectionManagerState: RileyLinkConnectionManagerState?, timeZone: TimeZone) { + public init(batteryChemistry: BatteryChemistryType = .alkaline, preferredInsulinDataSource: InsulinDataSource = .pumpHistory, pumpColor: PumpColor, pumpID: String, pumpModel: PumpModel, pumpRegion: PumpRegion, rileyLinkConnectionManagerState: RileyLinkConnectionManagerState?, timeZone: TimeZone, lastValidFrequency: Measurement? = nil) { self.batteryChemistry = batteryChemistry self.preferredInsulinDataSource = preferredInsulinDataSource self.pumpColor = pumpColor @@ -65,6 +73,7 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { self.pumpRegion = pumpRegion self.rileyLinkConnectionManagerState = rileyLinkConnectionManagerState self.timeZone = timeZone + self.lastValidFrequency = lastValidFrequency } public init?(rawValue: RawValue) { @@ -104,6 +113,13 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { } } + let lastValidFrequency: Measurement? + if let frequencyRaw = rawValue["lastValidFrequency"] as? Double { + lastValidFrequency = Measurement(value: frequencyRaw, unit: .megahertz) + } else { + lastValidFrequency = nil + } + self.init( batteryChemistry: batteryChemistry, preferredInsulinDataSource: insulinDataSource, @@ -112,7 +128,8 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { pumpModel: pumpModel, pumpRegion: pumpRegion, rileyLinkConnectionManagerState: rileyLinkConnectionManagerState, - timeZone: timeZone + timeZone: timeZone, + lastValidFrequency: lastValidFrequency ) } @@ -132,6 +149,11 @@ public struct MinimedPumpManagerState: RawRepresentable, Equatable { if let rileyLinkConnectionManagerState = rileyLinkConnectionManagerState { value["rileyLinkConnectionManagerState"] = rileyLinkConnectionManagerState.rawValue } + + if let frequency = lastValidFrequency?.converted(to: .megahertz) { + value["lastValidFrequency"] = frequency.value + } + return value } } @@ -152,6 +174,7 @@ extension MinimedPumpManagerState: CustomDebugStringConvertible { "pumpID: ✔︎", "pumpModel: \(pumpModel.rawValue)", "pumpRegion: \(pumpRegion)", + "lastValidFrequency: \(String(describing: lastValidFrequency))", "timeZone: \(timeZone)", String(reflecting: rileyLinkConnectionManagerState), ].joined(separator: "\n") diff --git a/MinimedKit/PumpManager/PumpOps.swift b/MinimedKit/PumpManager/PumpOps.swift index abef91cc7..c0c32dbc6 100644 --- a/MinimedKit/PumpManager/PumpOps.swift +++ b/MinimedKit/PumpManager/PumpOps.swift @@ -33,6 +33,8 @@ public class PumpOps { } private var configuredDevices: Set = Set() + + private var sessionDevice: RileyLinkDevice? private let sessionQueue = DispatchQueue(label: "com.rileylink.RileyLinkKit.PumpOps", qos: .utility) @@ -69,8 +71,10 @@ public class PumpOps { device.runSession(withName: name) { (session) in let session = PumpOpsSession(settings: self.pumpSettings, pumpState: self.pumpState, session: session, delegate: self) + self.sessionDevice = device self.configureDevice(device, with: session) block(session) + self.sessionDevice = nil semaphore.signal() } @@ -85,7 +89,7 @@ public class PumpOps { } do { - _ = try session.configureRadio(for: pumpSettings.pumpRegion) + _ = try session.configureRadio(for: pumpSettings.pumpRegion, frequency: pumpState.lastValidFrequency) } catch { // Ignore the error and let the block run anyway return @@ -116,6 +120,15 @@ public class PumpOps { extension PumpOps: PumpOpsSessionDelegate { + + func pumpOpsSessionDidChangeRadioConfig(_ session: PumpOpsSession) { + sessionQueue.async { + if let sessionDevice = self.sessionDevice { + self.configuredDevices = [sessionDevice] + } + } + } + func pumpOpsSession(_ session: PumpOpsSession, didChange state: PumpState) { self.pumpState = state } diff --git a/MinimedKit/PumpManager/PumpState.swift b/MinimedKit/PumpManager/PumpState.swift index 24ed8172b..48a90d340 100644 --- a/MinimedKit/PumpManager/PumpState.swift +++ b/MinimedKit/PumpManager/PumpState.swift @@ -18,6 +18,10 @@ public struct PumpState: RawRepresentable, Equatable { public var awakeUntil: Date? + public var lastValidFrequency: Measurement? + + public var lastTuned: Date? + var isAwake: Bool { if let awakeUntil = awakeUntil { return awakeUntil.timeIntervalSinceNow > 0 @@ -41,10 +45,14 @@ public struct PumpState: RawRepresentable, Equatable { } self.timeZone = timeZone - + if let pumpModelNumber = rawValue["pumpModel"] as? PumpModel.RawValue { pumpModel = PumpModel(rawValue: pumpModelNumber) } + + if let frequencyRaw = rawValue["lastValidFrequency"] as? Double { + lastValidFrequency = Measurement(value: frequencyRaw, unit: .megahertz) + } } public var rawValue: RawValue { @@ -55,6 +63,10 @@ public struct PumpState: RawRepresentable, Equatable { if let pumpModel = pumpModel { rawValue["pumpModel"] = pumpModel.rawValue } + + if let frequency = lastValidFrequency?.converted(to: .megahertz) { + rawValue["lastValidFrequency"] = frequency.value + } return rawValue } @@ -69,6 +81,8 @@ extension PumpState: CustomDebugStringConvertible { "timeZone: \(timeZone)", "pumpModel: \(pumpModel?.rawValue ?? "")", "awakeUntil: \(awakeUntil ?? .distantPast)", + "lastValidFrequency: \(String(describing: lastValidFrequency))", + "lastTuned: \(awakeUntil ?? .distantPast))", "lastWakeAttempt: \(String(describing: lastWakeAttempt))" ].joined(separator: "\n") } diff --git a/MinimedKitTests/Info.plist b/MinimedKitTests/Info.plist index d2194ec9b..7ff571664 100644 --- a/MinimedKitTests/Info.plist +++ b/MinimedKitTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/MinimedKitTests/PumpOpsSynchronousTests.swift b/MinimedKitTests/PumpOpsSynchronousTests.swift index 9b1e91dd7..01bf1570c 100644 --- a/MinimedKitTests/PumpOpsSynchronousTests.swift +++ b/MinimedKitTests/PumpOpsSynchronousTests.swift @@ -393,6 +393,11 @@ extension PumpMessageSenderStub: PumpOpsSessionDelegate { func pumpOpsSession(_ session: PumpOpsSession, didChange state: PumpState) { } + + func pumpOpsSessionDidChangeRadioConfig(_ session: PumpOpsSession) { + + } + } func array(_ timestampedEvents: [TimestampedHistoryEvent], containsPumpEvent pumpEvent: PumpEvent) -> Bool { diff --git a/MinimedKitUI/CommandResponseViewController.swift b/MinimedKitUI/CommandResponseViewController.swift index 91c7c69a1..ec6b30dcc 100644 --- a/MinimedKitUI/CommandResponseViewController.swift +++ b/MinimedKitUI/CommandResponseViewController.swift @@ -297,23 +297,12 @@ extension CommandResponseViewController { } } - static func tuneRadio(ops: PumpOps?, device: RileyLinkDevice, current: Measurement?, measurementFormatter: MeasurementFormatter) -> T { + static func tuneRadio(ops: PumpOps?, device: RileyLinkDevice, measurementFormatter: MeasurementFormatter) -> T { return T { (completionHandler) -> String in ops?.runSession(withName: "Tune pump", using: device) { (session) in let response: String do { - let scanResult = try session.tuneRadio(current: nil) - - NotificationCenter.default.post( - name: .DeviceStateDidChange, - object: device, - userInfo: [ - RileyLinkDevice.notificationDeviceStateKey: DeviceState( - lastTuned: Date(), - lastValidFrequency: scanResult.bestFrequency - ) - ] - ) + let scanResult = try session.tuneRadio() var resultDict: [String: Any] = [:] diff --git a/MinimedKitUI/Info.plist b/MinimedKitUI/Info.plist index b60ff5076..455432913 100644 --- a/MinimedKitUI/Info.plist +++ b/MinimedKitUI/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/MinimedKitUI/MinimedPumpSettingsViewController.swift b/MinimedKitUI/MinimedPumpSettingsViewController.swift index 1cc8a6ff7..5f2afd5fc 100644 --- a/MinimedKitUI/MinimedPumpSettingsViewController.swift +++ b/MinimedKitUI/MinimedPumpSettingsViewController.swift @@ -214,11 +214,10 @@ class MinimedPumpSettingsViewController: RileyLinkSettingsViewController { case .rileyLinks: let device = devicesDataSource.devices[indexPath.row] - pumpManager.getStateForDevice(device) { (deviceState, pumpOps) in + pumpManager.getOpsForDevice(device) { (pumpOps) in DispatchQueue.main.async { let vc = RileyLinkMinimedDeviceTableViewController( device: device, - deviceState: deviceState, pumpOps: pumpOps ) diff --git a/MinimedKitUI/RileyLinkMinimedDeviceTableViewController.swift b/MinimedKitUI/RileyLinkMinimedDeviceTableViewController.swift index 2aaa39d50..2f87ea5a2 100644 --- a/MinimedKitUI/RileyLinkMinimedDeviceTableViewController.swift +++ b/MinimedKitUI/RileyLinkMinimedDeviceTableViewController.swift @@ -20,8 +20,6 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { public let device: RileyLinkDevice - private var deviceState: DeviceState - private let ops: PumpOps private var pumpState: PumpState? { @@ -36,6 +34,10 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { if let cell = cellForRow(.model) { cell.setPumpModel(pumpState?.pumpModel) } + + if let cell = cellForRow(.tune) { + cell.setTuneInfo(lastValidFrequency: pumpState?.lastValidFrequency, lastTuned: pumpState?.lastTuned, measurementFormatter: measurementFormatter, dateFormatter: dateFormatter) + } } } @@ -79,14 +81,14 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { private var appeared = false - public init(device: RileyLinkDevice, deviceState: DeviceState, pumpOps: PumpOps) { + public init(device: RileyLinkDevice, pumpOps: PumpOps) { self.device = device - self.deviceState = deviceState self.ops = pumpOps super.init(style: .grouped) updateDeviceStatus() + pumpOps.getPumpState { (state) in DispatchQueue.main.async { self.pumpState = state @@ -272,6 +274,10 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { return tableView.cellForRow(at: IndexPath(row: row.rawValue, section: Section.pump.rawValue)) } + private func cellForRow(_ row: CommandRow) -> UITableViewCell? { + return tableView.cellForRow(at: IndexPath(row: row.rawValue, section: Section.commands.rawValue)) + } + public override func numberOfSections(in tableView: UITableView) -> Int { return Section.count } @@ -338,14 +344,7 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { switch CommandRow(rawValue: indexPath.row)! { case .tune: - switch (deviceState.lastValidFrequency, deviceState.lastTuned) { - case (let frequency?, let date?): - cell.textLabel?.text = measurementFormatter.string(from: frequency) - cell.setDetailDate(date, formatter: dateFormatter) - default: - cell.textLabel?.text = LocalizedString("Tune Radio Frequency", comment: "The title of the command to re-tune the radio") - } - + cell.setTuneInfo(lastValidFrequency: pumpState?.lastValidFrequency, lastTuned: pumpState?.lastTuned, measurementFormatter: measurementFormatter, dateFormatter: dateFormatter) case .changeTime: cell.textLabel?.text = LocalizedString("Change Time", comment: "The title of the command to change pump time") @@ -448,7 +447,7 @@ public class RileyLinkMinimedDeviceTableViewController: UITableViewController { switch CommandRow(rawValue: indexPath.row)! { case .tune: - vc = .tuneRadio(ops: ops, device: device, current: deviceState.lastValidFrequency, measurementFormatter: measurementFormatter) + vc = .tuneRadio(ops: ops, device: device, measurementFormatter: measurementFormatter) case .changeTime: vc = .changeTime(ops: ops, device: device) case .mySentryPair: @@ -563,4 +562,14 @@ private extension UITableViewCell { detailTextLabel?.text = LocalizedString("Unknown", comment: "The detail text for an unknown pump model") } } + + func setTuneInfo(lastValidFrequency: Measurement?, lastTuned: Date?, measurementFormatter: MeasurementFormatter, dateFormatter: DateFormatter) { + if let frequency = lastValidFrequency, let date = lastTuned { + textLabel?.text = measurementFormatter.string(from: frequency) + setDetailDate(date, formatter: dateFormatter) + } else { + textLabel?.text = LocalizedString("Tune Radio Frequency", comment: "The title of the command to re-tune the radio") + } + } + } diff --git a/MinimedKitUI/Setup/MinimedPumpIDSetupViewController.swift b/MinimedKitUI/Setup/MinimedPumpIDSetupViewController.swift index f2b26129f..c99dbcf32 100644 --- a/MinimedKitUI/Setup/MinimedPumpIDSetupViewController.swift +++ b/MinimedKitUI/Setup/MinimedPumpIDSetupViewController.swift @@ -245,7 +245,7 @@ class MinimedPumpIDSetupViewController: SetupTableViewController { } do { - _ = try session.tuneRadio(current: nil) + _ = try session.tuneRadio() let model = try session.getPumpModel() var isSentrySetUpNeeded = false diff --git a/NightscoutUploadKit/Info.plist b/NightscoutUploadKit/Info.plist index 01009605d..9fa96d19b 100644 --- a/NightscoutUploadKit/Info.plist +++ b/NightscoutUploadKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/NightscoutUploadKitTests/Info.plist b/NightscoutUploadKitTests/Info.plist index a6b29cb70..630acee5c 100644 --- a/NightscoutUploadKitTests/Info.plist +++ b/NightscoutUploadKitTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index 170656fd0..37b550a37 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -131,7 +131,6 @@ 43722FC31CB9F7640038B7F2 /* RileyLinkKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43722FAE1CB9F7630038B7F2 /* RileyLinkKit.framework */; }; 43722FC41CB9F7640038B7F2 /* RileyLinkKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 43722FAE1CB9F7630038B7F2 /* RileyLinkKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 437462391FA9287A00643383 /* RileyLinkDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 437462381FA9287A00643383 /* RileyLinkDevice.swift */; }; - 437F54071FBD52120070FF2C /* DeviceState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 437F54061FBD52120070FF2C /* DeviceState.swift */; }; 437F540A1FBFDAA60070FF2C /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1EAD6BA1C826B92006DBA60 /* Data.swift */; }; 4384C8C91FB941FB00D916E6 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43EBE4501EAD238C0073A0B5 /* TimeInterval.swift */; }; 438D39221D19011700D40CA4 /* PlaceholderPumpEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 438D39211D19011700D40CA4 /* PlaceholderPumpEvent.swift */; }; @@ -734,7 +733,6 @@ 43722FB71CB9F7640038B7F2 /* RileyLinkKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RileyLinkKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 43722FC01CB9F7640038B7F2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 437462381FA9287A00643383 /* RileyLinkDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RileyLinkDevice.swift; sourceTree = ""; }; - 437F54061FBD52120070FF2C /* DeviceState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceState.swift; sourceTree = ""; }; 4384C8C51FB92F8100D916E6 /* HistoryPage+PumpOpsSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HistoryPage+PumpOpsSession.swift"; sourceTree = ""; }; 4384C8C71FB937E500D916E6 /* PumpSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PumpSettings.swift; sourceTree = ""; }; 438D39211D19011700D40CA4 /* PlaceholderPumpEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlaceholderPumpEvent.swift; sourceTree = ""; }; @@ -1475,7 +1473,6 @@ children = ( 43722FB01CB9F7640038B7F2 /* RileyLinkKit.h */, 43722FB21CB9F7640038B7F2 /* Info.plist */, - 437F54061FBD52120070FF2C /* DeviceState.swift */, 43323EA91FA81C1B003FB0FA /* RileyLinkDevice.swift */, 4352A74020DED23000CAC200 /* RileyLinkDeviceManager.swift */, 435D26AF20DA08CE00891C17 /* RileyLinkPumpManager.swift */, @@ -2715,7 +2712,6 @@ 434AB0C71CBCB76400422F4A /* Data.swift in Sources */, 4352A74120DED23100CAC200 /* RileyLinkDeviceManager.swift in Sources */, 431CE7961F9B0F0200255374 /* OSLog.swift in Sources */, - 437F54071FBD52120070FF2C /* DeviceState.swift in Sources */, 435D26B020DA08CE00891C17 /* RileyLinkPumpManager.swift in Sources */, 435D26B920DC83F300891C17 /* Locked.swift in Sources */, ); @@ -3545,7 +3541,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = RileyLinkBLEKit/Info.plist; @@ -3581,7 +3577,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = RileyLinkBLEKit/Info.plist; @@ -3665,7 +3661,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -3705,7 +3701,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -3734,11 +3730,11 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -3771,11 +3767,11 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -3855,12 +3851,12 @@ CLANG_WARN_SUSPICIOUS_MOVES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = Crypto/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -3886,12 +3882,12 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = Crypto/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -3921,7 +3917,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -3960,7 +3956,7 @@ DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -4004,12 +4000,12 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -4041,11 +4037,11 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -4148,7 +4144,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -4215,7 +4211,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = YES; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -4341,11 +4337,11 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_NO_COMMON_BLOCKS = YES; @@ -4371,11 +4367,11 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 42; + CURRENT_PROJECT_VERSION = 43; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 42; + DYLIB_CURRENT_VERSION = 43; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_NO_COMMON_BLOCKS = YES; diff --git a/RileyLink/RileyLink-Info.plist b/RileyLink/RileyLink-Info.plist index 1b6b4db3f..9ff8ac420 100644 --- a/RileyLink/RileyLink-Info.plist +++ b/RileyLink/RileyLink-Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/RileyLinkBLEKit/Info.plist b/RileyLinkBLEKit/Info.plist index b60ff5076..455432913 100644 --- a/RileyLinkBLEKit/Info.plist +++ b/RileyLinkBLEKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/RileyLinkBLEKit/RileyLinkDevice.swift b/RileyLinkBLEKit/RileyLinkDevice.swift index 6062c96d6..abcb67002 100644 --- a/RileyLinkBLEKit/RileyLinkDevice.swift +++ b/RileyLinkBLEKit/RileyLinkDevice.swift @@ -244,6 +244,7 @@ extension RileyLinkDevice { } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { + log.debug("didConnect %@", peripheral) if case .connected = peripheral.state { assertIdleListening(forceRestart: false) assertTimerTick() @@ -255,10 +256,12 @@ extension RileyLinkDevice { } func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { + log.debug("didDisconnectPeripheral %@", peripheral) NotificationCenter.default.post(name: .DeviceConnectionStateDidChange, object: self) } func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { + log.debug("didFailToConnect %@", peripheral) NotificationCenter.default.post(name: .DeviceConnectionStateDidChange, object: self) } } diff --git a/RileyLinkBLEKitTests/Info.plist b/RileyLinkBLEKitTests/Info.plist index 7fecfef89..568fdfa2d 100644 --- a/RileyLinkBLEKitTests/Info.plist +++ b/RileyLinkBLEKitTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleVersion 1 diff --git a/RileyLinkKit/DeviceState.swift b/RileyLinkKit/DeviceState.swift deleted file mode 100644 index f7d51208a..000000000 --- a/RileyLinkKit/DeviceState.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// DeviceState.swift -// RileyLinkKit -// -// Copyright © 2017 Pete Schwamb. All rights reserved. -// - - -public struct DeviceState { - public var lastTuned: Date? - - public var lastValidFrequency: Measurement? - - public init(lastTuned: Date? = nil, lastValidFrequency: Measurement? = nil) { - self.lastTuned = lastTuned - self.lastValidFrequency = lastValidFrequency - } -} - - -extension DeviceState: CustomDebugStringConvertible { - public var debugDescription: String { - return [ - "## DeviceState", - "lastValidFrequency: \(String(describing: lastValidFrequency))", - "lastTuned: \(String(describing: lastTuned))", - ].joined(separator: "\n") - } -} diff --git a/RileyLinkKit/Info.plist b/RileyLinkKit/Info.plist index 01009605d..9fa96d19b 100644 --- a/RileyLinkKit/Info.plist +++ b/RileyLinkKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/RileyLinkKit/PumpOpsSession.swift b/RileyLinkKit/PumpOpsSession.swift index 34b39ad60..51262323a 100644 --- a/RileyLinkKit/PumpOpsSession.swift +++ b/RileyLinkKit/PumpOpsSession.swift @@ -13,6 +13,7 @@ import RileyLinkBLEKit protocol PumpOpsSessionDelegate: class { func pumpOpsSession(_ session: PumpOpsSession, didChange state: PumpState) + func pumpOpsSessionDidChangeRadioConfig(_ session: PumpOpsSession) } @@ -797,11 +798,16 @@ extension PumpOpsSession { /// - PumpOpsError.deviceError /// - PumpOpsError.noResponse /// - PumpOpsError.rfCommsFailure - public func tuneRadio(current: Measurement?) throws -> FrequencyScanResults { + public func tuneRadio() throws -> FrequencyScanResults { let region = self.settings.pumpRegion do { - let results = try scanForPump(in: region.scanFrequencies, current: current) + let results = try scanForPump(in: region.scanFrequencies, fallback: pump.lastValidFrequency) + + pump.lastValidFrequency = results.bestFrequency + pump.lastTuned = Date() + + delegate.pumpOpsSessionDidChangeRadioConfig(self) return results } catch let error as PumpOpsError { @@ -834,7 +840,7 @@ extension PumpOpsSession { /// - Throws: /// - PumpOpsError.deviceError /// - RileyLinkDeviceError - func configureRadio(for region: PumpRegion) throws { + func configureRadio(for region: PumpRegion, frequency: Measurement?) throws { try session.resetRadioConfig() switch region { @@ -855,6 +861,10 @@ extension PumpOpsSession { try session.updateRegister(.mdmcfg0, value: 0x7E) try session.updateRegister(.deviatn, value: 0x15) } + + if let frequency = frequency { + try session.setBaseFrequency(frequency) + } } /// - Throws: @@ -862,7 +872,7 @@ extension PumpOpsSession { /// - PumpOpsError.noResponse /// - PumpOpsError.rfCommsFailure /// - LocalizedError - private func scanForPump(in frequencies: [Measurement], current: Measurement?) throws -> FrequencyScanResults { + private func scanForPump(in frequencies: [Measurement], fallback: Measurement?) throws -> FrequencyScanResults { var trials = [FrequencyTrial]() @@ -904,7 +914,7 @@ extension PumpOpsSession { }) guard sortedTrials.first!.successes > 0 else { - try session.setBaseFrequency(current ?? middleFreq) + try session.setBaseFrequency(fallback ?? middleFreq) throw PumpOpsError.rfCommsFailure("No pump responses during scan") } @@ -912,7 +922,7 @@ extension PumpOpsSession { trials: trials, bestFrequency: sortedTrials.first!.frequency ) - + try session.setBaseFrequency(results.bestFrequency) return results diff --git a/RileyLinkKit/RileyLinkDevice.swift b/RileyLinkKit/RileyLinkDevice.swift index e118d33e8..b8d278535 100644 --- a/RileyLinkKit/RileyLinkDevice.swift +++ b/RileyLinkKit/RileyLinkDevice.swift @@ -8,12 +8,6 @@ import RileyLinkBLEKit -/// Provide a notification contract that clients can use to inform RileyLink UI of changes to DeviceState -extension RileyLinkDevice { - public static let notificationDeviceStateKey = "com.rileylink.RileyLinkKit.RileyLinkDevice.DeviceState" -} - - extension RileyLinkDevice.Status { public var firmwareDescription: String { let versions = [radioFirmwareVersion, bleFirmwareVersion].compactMap { (version: CustomStringConvertible?) -> String? in diff --git a/RileyLinkKit/RileyLinkPumpManager.swift b/RileyLinkKit/RileyLinkPumpManager.swift index 3eb8b6e25..9cd8f3465 100644 --- a/RileyLinkKit/RileyLinkPumpManager.swift +++ b/RileyLinkKit/RileyLinkPumpManager.swift @@ -18,7 +18,6 @@ open class RileyLinkPumpManager { // 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) - NotificationCenter.default.addObserver(self, selector: #selector(deviceStateDidChange(_:)), name: .DeviceStateDidChange, object: nil) } /// Manages all the RileyLinks - access to management is optional @@ -36,9 +35,6 @@ open class RileyLinkPumpManager { // TODO: Put this on each RileyLinkDevice? private var lastTimerTick: Date = .distantPast - // TODO: Isolate to queue - open var deviceStates: [UUID: DeviceState] = [:] - /// Called when one of the connected devices receives a packet outside of a session /// /// - Parameters: @@ -55,7 +51,6 @@ open class RileyLinkPumpManager { "## RileyLinkPumpManager", "rileyLinkConnectionManager: \(String(reflecting: rileyLinkConnectionManager))", "lastTimerTick: \(String(describing: lastTimerTick))", - "deviceStates: \(String(reflecting: deviceStates))", "", String(reflecting: rileyLinkDeviceProvider), ].joined(separator: "\n") @@ -64,18 +59,6 @@ open class RileyLinkPumpManager { // MARK: - RileyLink Updates extension RileyLinkPumpManager { - @objc private func deviceStateDidChange(_ note: Notification) { - guard - let device = note.object as? RileyLinkDevice, - let deviceState = note.userInfo?[RileyLinkDevice.notificationDeviceStateKey] as? RileyLinkKit.DeviceState - else { - return - } - - queue.async { - self.deviceStates[device.peripheralIdentifier] = deviceState - } - } /** Called when a new idle message is received by the RileyLink. diff --git a/RileyLinkKitTests/Info.plist b/RileyLinkKitTests/Info.plist index d2194ec9b..7ff571664 100644 --- a/RileyLinkKitTests/Info.plist +++ b/RileyLinkKitTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion diff --git a/RileyLinkKitUI/Info.plist b/RileyLinkKitUI/Info.plist index b60ff5076..455432913 100644 --- a/RileyLinkKitUI/Info.plist +++ b/RileyLinkKitUI/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/RileyLinkKitUI/RileyLinkDeviceTableViewController.swift b/RileyLinkKitUI/RileyLinkDeviceTableViewController.swift index a4a051f0d..b2f66100a 100644 --- a/RileyLinkKitUI/RileyLinkDeviceTableViewController.swift +++ b/RileyLinkKitUI/RileyLinkDeviceTableViewController.swift @@ -17,8 +17,6 @@ public class RileyLinkDeviceTableViewController: UITableViewController { public let device: RileyLinkDevice - private var deviceState: DeviceState - private var bleRSSI: Int? private var firmwareVersion: String? { @@ -70,7 +68,6 @@ public class RileyLinkDeviceTableViewController: UITableViewController { public init(device: RileyLinkDevice) { self.device = device - self.deviceState = DeviceState(lastTuned: nil, lastValidFrequency: nil) super.init(style: .grouped) diff --git a/RileyLinkTests/RileyLinkTests-Info.plist b/RileyLinkTests/RileyLinkTests-Info.plist index 58e205158..ff71f4bdc 100644 --- a/RileyLinkTests/RileyLinkTests-Info.plist +++ b/RileyLinkTests/RileyLinkTests-Info.plist @@ -13,7 +13,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.0.6 + 2.0.7 CFBundleSignature ???? CFBundleVersion