From 7b78d797d640f1e8197c837d87497133731e606d Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Tue, 13 Aug 2024 16:11:09 +0200 Subject: [PATCH] Minor modifications --- Sources/SpeziDevices/HealthMeasurements.swift | 2 -- .../Devices/OmronBloodPressureCuff.swift | 24 ++++++++++++------- .../SpeziOmron/Devices/OmronWeightScale.swift | 13 ++++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Sources/SpeziDevices/HealthMeasurements.swift b/Sources/SpeziDevices/HealthMeasurements.swift index 1281db1..9ee8159 100644 --- a/Sources/SpeziDevices/HealthMeasurements.swift +++ b/Sources/SpeziDevices/HealthMeasurements.swift @@ -156,7 +156,6 @@ public final class HealthMeasurements: @unchecked Sendable { ) { device[keyPath: keyPath].$weightMeasurement.onChange { @MainActor [weak self, weak device] measurement in guard let self, let device, case .connected = device.state else { - // TODO: we now just assume connected for all devices? return } let service = device[keyPath: keyPath] @@ -180,7 +179,6 @@ public final class HealthMeasurements: @unchecked Sendable { // make sure to not capture the device device[keyPath: keyPath].$bloodPressureMeasurement.onChange { @MainActor [weak self, weak device] measurement in guard let self, let device, case .connected = device.state else { - // TODO: we now just assume connected for all devices? return } let service = device[keyPath: keyPath] diff --git a/Sources/SpeziOmron/Devices/OmronBloodPressureCuff.swift b/Sources/SpeziOmron/Devices/OmronBloodPressureCuff.swift index c9fbcd8..734fe1b 100644 --- a/Sources/SpeziOmron/Devices/OmronBloodPressureCuff.swift +++ b/Sources/SpeziOmron/Devices/OmronBloodPressureCuff.swift @@ -53,6 +53,8 @@ public final class OmronBloodPressureCuff: BluetoothDevice, Identifiable, OmronH @Dependency(HealthMeasurements.self) private var measurements: HealthMeasurements? @Dependency(PairedDevices.self) private var pairedDevices: PairedDevices? + @SpeziBluetooth private var didReceiveFirstTimeNotification = false + /// Initialize the device. public required init() {} @@ -76,11 +78,14 @@ public final class OmronBloodPressureCuff: BluetoothDevice, Identifiable, OmronH } } - private func handleStateChange(_ state: PeripheralState) async { + @SpeziBluetooth + private func handleStateChange(_ state: PeripheralState) { logger.debug("\(Self.self) changed state to \(state).") - if case .connected = state, - case .transferMode = manufacturerData?.pairingMode { - time.synchronizeDeviceTime() + switch state { + case .connecting, .connected: + break + case .disconnected, .disconnecting: + didReceiveFirstTimeNotification = false } } @@ -89,12 +94,15 @@ public final class OmronBloodPressureCuff: BluetoothDevice, Identifiable, OmronH pairedDevices?.signalDevicePaired(self) } - @MainActor - private func handleCurrentTimeChange(_ time: CurrentTime) { + @SpeziBluetooth + private func handleCurrentTimeChange(_ time: CurrentTime) async { logger.debug("Received updated device time for \(self.label) is \(String(describing: time))") - let paired = pairedDevices?.signalDevicePaired(self) - if paired == true { + // for Omron we take that as a signal that device is paired + await pairedDevices?.signalDevicePaired(self) + + if !didReceiveFirstTimeNotification { + didReceiveFirstTimeNotification = true self.time.synchronizeDeviceTime() } } diff --git a/Sources/SpeziOmron/Devices/OmronWeightScale.swift b/Sources/SpeziOmron/Devices/OmronWeightScale.swift index 888f9db..126790f 100644 --- a/Sources/SpeziOmron/Devices/OmronWeightScale.swift +++ b/Sources/SpeziOmron/Devices/OmronWeightScale.swift @@ -41,7 +41,7 @@ public final class OmronWeightScale: BluetoothDevice, Identifiable, OmronHealthD @Dependency(HealthMeasurements.self) private var measurements: HealthMeasurements? @Dependency(PairedDevices.self) private var pairedDevices: PairedDevices? - private var didReceiveFirstTimeNotification = false + @MainActor private var didReceiveFirstTimeNotification = false /// Initialize the device. public required init() {} @@ -63,7 +63,8 @@ public final class OmronWeightScale: BluetoothDevice, Identifiable, OmronHealthD } } - private func handleStateChange(_ state: PeripheralState) async { + @MainActor + private func handleStateChange(_ state: PeripheralState) { logger.debug("\(Self.self) changed state to \(state).") switch state { case .connecting, .connected: @@ -78,10 +79,12 @@ public final class OmronWeightScale: BluetoothDevice, Identifiable, OmronHealthD // TODO: only update the first time, do we have that web page still open??? logger.debug("Received updated device time for \(self.label): \(String(describing: time))") - // TODO: filter for notifications happening while being in disconnected state? + // TODO: pairing issue is still there + // - if Bluetooth entry is removed: two notifications while one is always delivered instantly! + // - if already paired in Bluetooth menu, only one notification while still connecting! - // for Omron we take that as a signal that device is paired // TODO: does this work now for all weight scales? - let didPair = pairedDevices?.signalDevicePaired(self) == true // TODO: do we need to result still? + // for Omron we take that as a signal that device is paired + pairedDevices?.signalDevicePaired(self) if !didReceiveFirstTimeNotification { didReceiveFirstTimeNotification = true