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

Backport of OmniXXX fixes & remove extra get pod status commands #780

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Config.xcconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_DISPLAY_NAME = iAPS
APP_VERSION = 4.5.0
APP_VERSION = 4.7.0
APP_BUILD_NUMBER = 1
COPYRIGHT_NOTICE =
DEVELOPER_TEAM = ##TEAM_ID##
Expand Down
26 changes: 13 additions & 13 deletions Dependencies/OmniBLE/OmniBLE/PumpManager/OmniBLEPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ public class OmniBLEPumpManager: DeviceManager {
oldValue = state
let oldStatusEvaluationDate = state.lastStatusChange
let oldHighlight = buildPumpStatusHighlight(for: oldValue, andDate: oldStatusEvaluationDate)
oldStatus = status(for: oldValue)
oldStatus = status(for: oldValue, at: oldStatusEvaluationDate)

returnType = changes(&state)

let newStatusEvaluationDate = Date()
let newStatus = status(for: state)
let newStatus = status(for: state, at: newStatusEvaluationDate)
let newHighlight = buildPumpStatusHighlight(for: state, andDate: newStatusEvaluationDate)

if oldStatus != newStatus || oldHighlight != newHighlight {
Expand Down Expand Up @@ -302,13 +302,13 @@ extension OmniBLEPumpManager {
podStateObservers.removeElement(observer)
}

private func status(for state: OmniBLEPumpManagerState) -> PumpManagerStatus {
private func status(for state: OmniBLEPumpManagerState, at date: Date = Date()) -> PumpManagerStatus {
return PumpManagerStatus(
timeZone: state.timeZone,
device: device(for: state),
pumpBatteryChargeRemaining: nil,
basalDeliveryState: basalDeliveryState(for: state),
bolusState: bolusState(for: state),
basalDeliveryState: basalDeliveryState(for: state, at: date),
bolusState: bolusState(for: state, at: date),
insulinType: state.insulinType,
deliveryIsUncertain: state.podState?.needsCommsRecovery == true
)
Expand Down Expand Up @@ -340,7 +340,7 @@ extension OmniBLEPumpManager {
}
}

private func basalDeliveryState(for state: OmniBLEPumpManagerState) -> PumpManagerStatus.BasalDeliveryState {
private func basalDeliveryState(for state: OmniBLEPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BasalDeliveryState {
guard let podState = state.podState else {
return .active(.distantPast)
}
Expand All @@ -367,7 +367,7 @@ extension OmniBLEPumpManager {
case .disengaging:
return .cancelingTempBasal
case .stable:
if let tempBasal = podState.unfinalizedTempBasal {
if let tempBasal = podState.unfinalizedTempBasal, !tempBasal.isFinished(at: date) {
return .tempBasal(DoseEntry(tempBasal))
}
switch podState.suspendState {
Expand All @@ -379,7 +379,7 @@ extension OmniBLEPumpManager {
}
}

private func bolusState(for state: OmniBLEPumpManagerState) -> PumpManagerStatus.BolusState {
private func bolusState(for state: OmniBLEPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BolusState {
guard let podState = state.podState else {
return .noBolus
}
Expand All @@ -390,7 +390,7 @@ extension OmniBLEPumpManager {
case .disengaging:
return .canceling
case .stable:
if let bolus = podState.unfinalizedBolus {
if let bolus = podState.unfinalizedBolus, !bolus.isFinished(at: date) {
return .inProgress(DoseEntry(bolus))
}
}
Expand Down Expand Up @@ -1841,8 +1841,8 @@ extension OmniBLEPumpManager: PumpManager {
state.bolusEngageState = .engaging
})

if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended != false {
self.log.error("enactBolus: returning pod suspended error for bolus")
if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended == true {
self.log.error("Not enacting bolus because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
}
Expand Down Expand Up @@ -1982,8 +1982,8 @@ extension OmniBLEPumpManager: PumpManager {
return
}

if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended != false {
self.log.info("Not enacting temp basal because podState indicates pod is suspended.")
if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended == true {
self.log.info("Not enacting temp basal because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ public class PodCommsSession {
log.default("bolus: pod is still bolusing")
return DeliveryCommandResult.certainFailure(error: .unfinalizedBolus)
}
// If the pod setup is complete, also confirm that the pod is indeed not suspended
if podState.setupProgress == .completed && statusResponse.deliveryStatus.suspended {
log.default("bolus: pod is suspended")
return DeliveryCommandResult.certainFailure(error: .podSuspended)
}
log.default("bolus: get status response verifies pod is not bolusing and not suspended")
} else {
log.default("bolus: failed to read pod status to verify there is no bolus running")
return DeliveryCommandResult.certainFailure(error: .noResponse)
Expand Down
50 changes: 28 additions & 22 deletions Dependencies/OmniKit/OmniKit/PumpManager/OmnipodPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,25 @@ public class OmnipodPumpManager: RileyLinkPumpManager {
private func setStateWithResult<ReturnType>(_ changes: (_ state: inout OmnipodPumpManagerState) -> ReturnType) -> ReturnType {
var oldValue: OmnipodPumpManagerState!
var returnType: ReturnType!
var shouldNotifyStatusUpdate = false
var oldStatus: PumpManagerStatus?

let newValue = lockedState.mutate { (state) in
oldValue = state
let oldStatusEvaluationDate = state.lastStatusChange
let oldHighlight = buildPumpStatusHighlight(for: oldValue, andDate: oldStatusEvaluationDate)
oldStatus = status(for: oldValue, at: oldStatusEvaluationDate)

returnType = changes(&state)
}

guard oldValue != newValue else {
return returnType
let newStatusEvaluationDate = Date()
let newStatus = status(for: state, at: newStatusEvaluationDate)
let newHighlight = buildPumpStatusHighlight(for: state, andDate: newStatusEvaluationDate)

if oldStatus != newStatus || oldHighlight != newHighlight {
shouldNotifyStatusUpdate = true
state.lastStatusChange = newStatusEvaluationDate
}
}

if oldValue.podState != newValue.podState {
Expand All @@ -173,25 +185,19 @@ public class OmnipodPumpManager: RileyLinkPumpManager {
}
}


// Ideally we ensure that oldValue.rawValue != newValue.rawValue, but the types aren't
// defined as equatable
pumpDelegate.notify { (delegate) in
delegate?.pumpManagerDidUpdateState(self)
}

let oldStatus = status(for: oldValue)
let newStatus = status(for: newValue)

let oldHighlight = buildPumpStatusHighlight(for: oldValue)
let newHighlight = buildPumpStatusHighlight(for: newValue)

if oldStatus != newStatus || oldHighlight != newHighlight {
if let oldStatus = oldStatus, shouldNotifyStatusUpdate {
notifyStatusObservers(oldStatus: oldStatus)
}

return returnType
}

private let lockedState: Locked<OmnipodPumpManagerState>

private let statusObservers = WeakSynchronizedSet<PumpManagerStatusObserver>()
Expand Down Expand Up @@ -431,13 +437,13 @@ extension OmnipodPumpManager {
}
}

private func status(for state: OmnipodPumpManagerState) -> PumpManagerStatus {
private func status(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus {
return PumpManagerStatus(
timeZone: state.timeZone,
device: device(for: state),
pumpBatteryChargeRemaining: nil,
basalDeliveryState: basalDeliveryState(for: state),
bolusState: bolusState(for: state),
basalDeliveryState: basalDeliveryState(for: state, at: date),
bolusState: bolusState(for: state, at: date),
insulinType: state.insulinType,
deliveryIsUncertain: state.podState?.needsCommsRecovery == true
)
Expand Down Expand Up @@ -469,7 +475,7 @@ extension OmnipodPumpManager {
}
}

private func basalDeliveryState(for state: OmnipodPumpManagerState) -> PumpManagerStatus.BasalDeliveryState {
private func basalDeliveryState(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BasalDeliveryState {
guard let podState = state.podState else {
return .active(.distantPast)
}
Expand All @@ -496,7 +502,7 @@ extension OmnipodPumpManager {
case .disengaging:
return .cancelingTempBasal
case .stable:
if let tempBasal = podState.unfinalizedTempBasal {
if let tempBasal = podState.unfinalizedTempBasal, !tempBasal.isFinished(at: date) {
return .tempBasal(DoseEntry(tempBasal))
}
switch podState.suspendState {
Expand All @@ -508,7 +514,7 @@ extension OmnipodPumpManager {
}
}

private func bolusState(for state: OmnipodPumpManagerState) -> PumpManagerStatus.BolusState {
private func bolusState(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BolusState {
guard let podState = state.podState else {
return .noBolus
}
Expand All @@ -519,7 +525,7 @@ extension OmnipodPumpManager {
case .disengaging:
return .canceling
case .stable:
if let bolus = podState.unfinalizedBolus, !bolus.isFinished() {
if let bolus = podState.unfinalizedBolus, !bolus.isFinished(at: date) {
return .inProgress(DoseEntry(bolus))
}
}
Expand Down Expand Up @@ -1856,8 +1862,8 @@ extension OmnipodPumpManager: PumpManager {
state.bolusEngageState = .engaging
})

if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended != false {
self.log.error("enactBolus: returning pod suspended error for bolus")
if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended == true {
self.log.error("Not enacting bolus because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
}
Expand Down Expand Up @@ -1995,8 +2001,8 @@ extension OmnipodPumpManager: PumpManager {
return
}

if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended != false {
self.log.info("Not enacting temp basal because podState indicates pod is suspended.")
if let podState = self.state.podState, podState.isSuspended || podState.lastDeliveryStatusReceived?.suspended == true {
self.log.info("Not enacting temp basal because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public struct OmnipodPumpManagerState: RawRepresentable, Equatable {

internal var tempBasalEngageState: EngageablePumpState = .stable

internal var lastStatusChange: Date = .distantPast

internal var lastPumpDataReportDate: Date?

internal var insulinType: InsulinType?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ public class PodCommsSession {
log.default("bolus: pod is still bolusing")
return DeliveryCommandResult.certainFailure(error: .unfinalizedBolus)
}
// If the pod setup is complete, also confirm that the pod is indeed not suspended
if podState.setupProgress == .completed && statusResponse.deliveryStatus.suspended {
log.default("bolus: pod is suspended")
return DeliveryCommandResult.certainFailure(error: .podSuspended)
}
log.default("bolus: get status response verifies pod is not bolusing and not suspended")
} else {
log.default("bolus: failed to read pod status to verify there is no bolus running")
return DeliveryCommandResult.certainFailure(error: .noResponse)
Expand Down
18 changes: 0 additions & 18 deletions FreeAPS/Sources/APS/APSManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import CoreData
import Foundation
import LoopKit
import LoopKitUI
import OmniBLE
import OmniKit
import RileyLinkKit
import SwiftDate
import SwiftUI
import Swinject
Expand Down Expand Up @@ -1330,26 +1327,11 @@ final class BaseAPSManager: APSManager, Injectable {
bolusReporter?.addObserver(self)
}

private func updateStatus() {
debug(.apsManager, "force update status")
guard let pump = pumpManager else {
return
}

if let omnipod = pump as? OmnipodPumpManager {
omnipod.getPodStatus { _ in }
}
if let omnipodBLE = pump as? OmniBLEPumpManager {
omnipodBLE.getPodStatus { _ in }
}
}

private func clearBolusReporter() {
bolusReporter?.removeObserver(self)
bolusReporter = nil
processQueue.asyncAfter(deadline: .now() + 0.5) {
self.bolusProgress.send(nil)
self.updateStatus()
}
}
}
Expand Down