generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial blood pressure cuff implementation
- Loading branch information
Showing
6 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// This source file is part of the ENGAGE-HF project based on the Stanford Spezi Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import BluetoothServices | ||
import Foundation | ||
import OSLog | ||
import SpeziBluetooth | ||
|
||
|
||
class BloodPressureCuffDevice: BluetoothDevice, Identifiable { | ||
private static let logger = Logger(subsystem: "ENGAGEHF", category: "BloodPressureCuffDevice") | ||
|
||
@DeviceState(\.id) var id: UUID | ||
@DeviceState(\.name) var name: String? | ||
@DeviceState(\.state) var state: PeripheralState | ||
|
||
@Service var deviceInformation = DeviceInformationService() | ||
|
||
@Service var time = CurrentTimeService() | ||
@Service var bloodPressure = BloodPressureService() | ||
|
||
@DeviceAction(\.connect) var connect | ||
@DeviceAction(\.disconnect) var disconnect | ||
|
||
// TODO: user data service? | ||
// TODO: record access service! | ||
|
||
required init() { | ||
$state | ||
.onChange(perform: handleStateChange) | ||
bloodPressure.$bloodPressureMeasurement | ||
.onChange(perform: processMeasurement) | ||
} | ||
|
||
private func handleStateChange(_ state: PeripheralState) { | ||
guard case .connected = state else { | ||
return | ||
} | ||
|
||
time.ensureUpdatedTime() | ||
} | ||
|
||
private func processMeasurement(_ measurement: BloodPressureMeasurement) { | ||
// TODO: actually do something with it | ||
print("Received new measurement: \(measurement)") | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
ENGAGEHF/Bluetooth/Devices/Services/CurrentTimeService+Update.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// This source file is part of the ENGAGE-HF project based on the Stanford Spezi Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import BluetoothServices | ||
import Foundation | ||
import OSLog | ||
|
||
|
||
extension CurrentTimeService { | ||
private static var logger: Logger { // TODO: how to deal with that? | ||
Logger(subsystem: "edu.stanford.bluetooth", category: "CurrentTimeService") | ||
} | ||
|
||
// TODO: consider moving to SpeziBluetooth! | ||
func ensureUpdatedTime() { // TODO: better name? | ||
// check if time update is necessary | ||
if let currentTime = currentTime, | ||
let deviceTime = currentTime.time.date { | ||
// TODO: what are the default (empty values)? is this check sufficient? (check for zero year, month, day?) | ||
let difference = abs(deviceTime.timeIntervalSinceReferenceDate - Date.now.timeIntervalSinceReferenceDate) | ||
if difference < 5 { | ||
// TODO: better value? | ||
return // we consider 5 second difference accurate enough?) | ||
} | ||
} | ||
|
||
|
||
// update time if it isn't present or if it is outdated | ||
Task { | ||
let exactTime = ExactTime256(from: .now) | ||
do { | ||
try await $currentTime.write(CurrentTime(time: exactTime)) | ||
Self.logger.debug("Updated weight scale device time to \(String(describing: exactTime))") | ||
} catch { | ||
Self.logger.warning("Failed to update current time: \(error)") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters