Skip to content

Fix for pump manager returns bogus podSuspended #141

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

Merged
merged 3 commits into from
Feb 12, 2025
Merged
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
69 changes: 67 additions & 2 deletions OmniBLE/PumpManager/OmniBLEPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,27 @@ extension OmniBLEPumpManager {
}
}

// If the last delivery status received is invalid or there is an unacknowledged command, execute a getStatus command
// for the current PodCommsSession. If the getStatus fails, return its error to be passed on to the higher level.
// Return nil if comms looks OK or the getStatus was successful.
private func tryToValidateComms(session: PodCommsSession) -> LocalizedError? {

// Since we're already connected for this session, if we have a delivery status and no unacknowledged command, return nil
if self.state.podState?.lastDeliveryStatusReceived != nil && self.state.podState?.unacknowledgedCommand == nil {
return nil
}

// Attempt to do a getStatus to try to resolve any outstanding comms issues
do {
let _ = try session.getStatus()
self.log.debug("### tryToValidateComms getStatus resolved all pending comms issues")
return nil
} catch let error {
self.log.debug("### tryToValidateComms getStatus failed, returning: %@", error.localizedDescription)
return error as? LocalizedError
}
}

// MARK: - Pump Commands

public func getPodStatus(completion: ((_ result: PumpManagerResult<StatusResponse>) -> Void)? = nil) {
Expand Down Expand Up @@ -1163,6 +1184,11 @@ extension OmniBLEPumpManager {
switch result {
case .success(let session):
do {
if let error = self.tryToValidateComms(session: session) {
completion(.communication(error))
return
}

let beep = self.silencePod ? false : self.beepPreference.shouldBeepForManualCommand
let _ = try session.setTime(timeZone: timeZone, basalSchedule: self.state.basalSchedule, date: Date(), acknowledgementBeep: beep)
self.clearSuspendReminder()
Expand Down Expand Up @@ -1216,6 +1242,11 @@ extension OmniBLEPumpManager {
do {
switch result {
case .success(let session):
if let error = self.tryToValidateComms(session: session) {
completion(error)
return
}

let scheduleOffset = timeZone.scheduleOffset(forDate: Date())
let result = session.cancelDelivery(deliveryType: .all)
switch result {
Expand Down Expand Up @@ -1454,6 +1485,11 @@ extension OmniBLEPumpManager {
self.podComms.runSession(withName: name) { (result) in
switch result {
case .success(let session):
if let error = self.tryToValidateComms(session: session) {
completion(.communication(error))
return
}

// enable/disable Pod completion beep state for any unfinalized manual insulin delivery
let enabled = newPreference.shouldBeepForManualCommand
let beepType: BeepType = enabled ? .bipBip : .noBeepNonCancel
Expand Down Expand Up @@ -1504,6 +1540,11 @@ extension OmniBLEPumpManager {
return
}

if let error = self.tryToValidateComms(session: session) {
completion(.communication(error))
return
}

guard let configuredAlerts = self.state.podState?.configuredAlerts,
let activeAlertSlots = self.state.podState?.activeAlertSlots,
let reservoirLevel = self.state.podState?.lastInsulinMeasurements?.reservoirLevel?.rawValue else
Expand Down Expand Up @@ -1707,6 +1748,11 @@ extension OmniBLEPumpManager: PumpManager {
state.suspendEngageState = .engaging
})

if let error = self.tryToValidateComms(session: session) {
completion(error)
return
}

// Use a beepBlock for the confirmation beep to avoid getting 3 beeps using cancel command beeps!
let beepBlock = self.beepMessageBlock(beepType: .beeeeeep)
let result = session.suspendDelivery(suspendReminder: suspendReminder, silent: self.silencePod, beepBlock: beepBlock)
Expand Down Expand Up @@ -1753,6 +1799,11 @@ extension OmniBLEPumpManager: PumpManager {
state.suspendEngageState = .disengaging
})

if let error = self.tryToValidateComms(session: session) {
completion(error)
return
}

do {
let scheduleOffset = self.state.timeZone.scheduleOffset(forDate: Date())
let beep = self.silencePod ? false : self.beepPreference.shouldBeepForManualCommand
Expand Down Expand Up @@ -1848,7 +1899,14 @@ extension OmniBLEPumpManager: PumpManager {
state.bolusEngageState = .engaging
})

guard let podState = self.state.podState, !podState.isSuspended && podState.lastDeliveryStatusReceived?.suspended == false else {
if let error = self.tryToValidateComms(session: session) {
completion(.communication(error))
return
}

// Use a lastDeliveryStatusReceived?.suspended != true test here to not return a pod suspended failure if
// there is not a valid last delivery status (which shouldn't even happen now with tryToValidateComms()).
guard let podState = self.state.podState, !podState.isSuspended && podState.lastDeliveryStatusReceived?.suspended != true else {
self.log.info("Not enacting bolus because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
Expand Down Expand Up @@ -1989,7 +2047,14 @@ extension OmniBLEPumpManager: PumpManager {
return
}

guard let podState = self.state.podState, !podState.isSuspended && podState.lastDeliveryStatusReceived?.suspended == false else {
if let error = self.tryToValidateComms(session: session) {
completion(.communication(error))
return
}

// Use a lastDeliveryStatusReceived?.suspended != true test here to not return a pod suspended failure if
// there is not a valid last delivery status (which shouldn't even happen now with tryToValidateComms()).
guard let podState = self.state.podState, !podState.isSuspended && podState.lastDeliveryStatusReceived?.suspended != true else {
self.log.info("Not enacting temp basal because podState or last status received indicates pod is suspended")
completion(.deviceState(PodCommsError.podSuspended))
return
Expand Down