Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev into alpha #292

Merged
merged 11 commits into from
Jun 10, 2024
Merged
7 changes: 7 additions & 0 deletions FreeAPS/Sources/APS/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ extension BaseDeviceDataManager: PumpManagerDelegate {
func pumpManagerWillDeactivate(_: PumpManager) {
dispatchPrecondition(condition: .onQueue(processQueue))
pumpManager = nil
broadcaster.notify(PumpDeactivatedObserver.self, on: processQueue) {
$0.pumpDeactivatedDidChange()
}
}

func pumpManager(_: PumpManager, didUpdatePumpRecordsBasalProfileStartEvents _: Bool) {}
Expand Down Expand Up @@ -628,3 +631,7 @@ protocol PumpReservoirObserver {
protocol PumpBatteryObserver {
func pumpBatteryDidChange(_ battery: Battery)
}

protocol PumpDeactivatedObserver {
func pumpDeactivatedDidChange()
}
25 changes: 16 additions & 9 deletions FreeAPS/Sources/Modules/Home/HomeStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ extension Home {
broadcaster.register(EnactedSuggestionObserver.self, observer: self)
broadcaster.register(PumpBatteryObserver.self, observer: self)
broadcaster.register(PumpReservoirObserver.self, observer: self)
broadcaster.register(PumpDeactivatedObserver.self, observer: self)

animatedBackground = settingsManager.settings.animatedBackground

Expand Down Expand Up @@ -169,7 +170,7 @@ extension Home {
} else {
self.setupBattery()
self.setupReservoir()
self.displaypumpStatusHighlightMessage()
self.displayPumpStatusHighlightMessage()
}
}
.store(in: &lifetime)
Expand Down Expand Up @@ -352,13 +353,14 @@ extension Home {

/// Display the eventual status message provided by the manager of the pump
/// Only display if state is warning or critical message else return nil
private func displaypumpStatusHighlightMessage() {
private func displayPumpStatusHighlightMessage(_ didDeactivate: Bool = false) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if let statusHL = self.provider.deviceManager.pumpManager?.pumpStatusHighlight,
statusHL.state == .warning || statusHL.state == .critical
if let statusHighlight = self.provider.deviceManager.pumpManager?.pumpStatusHighlight,
statusHighlight.state == .warning || statusHighlight.state == .critical, !didDeactivate
{
pumpStatusHighlightMessage = (statusHL.state == .warning ? "⚠️\n" : "‼️\n") + statusHL.localizedMessage
pumpStatusHighlightMessage = (statusHighlight.state == .warning ? "⚠️\n" : "‼️\n") + statusHighlight
.localizedMessage
} else {
pumpStatusHighlightMessage = nil
}
Expand Down Expand Up @@ -395,7 +397,8 @@ extension Home.StateModel:
CarbsObserver,
EnactedSuggestionObserver,
PumpBatteryObserver,
PumpReservoirObserver
PumpReservoirObserver,
PumpDeactivatedObserver
{
func glucoseDidUpdate(_: [BloodGlucose]) {
setupGlucose()
Expand Down Expand Up @@ -429,7 +432,7 @@ extension Home.StateModel:
setupBasals()
setupBoluses()
setupSuspensions()
displaypumpStatusHighlightMessage()
displayPumpStatusHighlightMessage()
}

func pumpSettingsDidChange(_: PumpSettings) {
Expand All @@ -455,12 +458,16 @@ extension Home.StateModel:

func pumpBatteryDidChange(_: Battery) {
setupBattery()
displaypumpStatusHighlightMessage()
displayPumpStatusHighlightMessage()
}

func pumpReservoirDidChange(_: Decimal) {
setupReservoir()
displaypumpStatusHighlightMessage()
displayPumpStatusHighlightMessage()
}

func pumpDeactivatedDidChange() {
displayPumpStatusHighlightMessage(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ extension NightscoutConfig {
return NightscoutAPI(url: url, secret: secret)
}

private func getMedianTarget(
lowTargetValue: Decimal,
lowTargetTime: String,
highTarget: [NightscoutTimevalue],
units: GlucoseUnits
) -> Decimal {
if let idx = highTarget.firstIndex(where: { $0.time == lowTargetTime }) {
let median = (lowTargetValue + highTarget[idx].value) / 2
switch units {
case .mgdL:
return Decimal(round(Double(median)))
case .mmolL:
return Decimal(round(Double(median) * 10) / 10)
}
}
return lowTargetValue
}

func importSettings() {
guard let nightscout = nightscoutAPI else {
saveError("Can't access nightscoutAPI")
Expand Down Expand Up @@ -134,8 +152,10 @@ extension NightscoutConfig {
do {
let fetchedProfileStore = try jsonDecoder.decode([FetchedNightscoutProfileStore].self, from: data)
let loop = fetchedProfileStore.first?.enteredBy.contains("Loop")
guard let fetchedProfile: FetchedNightscoutProfile = fetchedProfileStore.first?
.store[loop! ? "Default" : "default"]
guard let fetchedProfile: FetchedNightscoutProfile =
(fetchedProfileStore.first?.store["default"] != nil) ?
fetchedProfileStore.first?.store["default"] :
fetchedProfileStore.first?.store["Default"]
else {
error = "\nCan't find the default Nightscout Profile."
group.leave()
Expand Down Expand Up @@ -220,9 +240,15 @@ extension NightscoutConfig {

let targets = fetchedProfile.target_low
.map { target -> BGTargetEntry in
BGTargetEntry(
low: target.value,
high: target.value,
let median = loop! ? self.getMedianTarget(
lowTargetValue: target.value,
lowTargetTime: target.time,
highTarget: fetchedProfile.target_high,
units: self.units
) : target.value
return BGTargetEntry(
low: median,
high: median,
start: target.time,
offset: self.offset(target.time) / 60
) }
Expand Down
Loading