Skip to content
Draft
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
12 changes: 11 additions & 1 deletion OmnipodKit/Bluetooth/BlePodComms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,11 @@ class BlePodComms: PodComms {
}
}

func bleRunSession(withName name: String, _ block: @escaping (_ result: SessionRunResult) -> Void) {
func bleRunSession(
withName name: String,
onFault: @escaping (_ session: PodCommsSession) -> Void,
_ block: @escaping (_ result: SessionRunResult) -> Void
) {

guard let manager = manager, manager.peripheral.state == .connected else {
block(.failure(PodCommsError.podNotConnected))
Expand All @@ -654,6 +658,12 @@ class BlePodComms: PodComms {

let podSession = PodCommsSession(podState: self.podState!, transport: transport, delegate: self)
block(.success(session: podSession))

// If the pod is faulted, call the onFault
// handler to save any updated dose info.
if self.podState?.isFaulted == true {
onFault(podSession)
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions OmnipodKit/Eros/ErosPodComms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@ class ErosPodComms: PodComms {
}
}

func erosRunSession(withName name: String, using deviceSelector: @escaping (_ completion: @escaping (_ device: RileyLinkDevice?) -> Void) -> Void, _ block: @escaping (_ result: SessionRunResult) -> Void)
{
func erosRunSession(
withName name: String,
using deviceSelector: @escaping (_ completion: @escaping (_ device: RileyLinkDevice?) -> Void) -> Void,
onFault: @escaping (_ session: PodCommsSession) -> Void,
_ block: @escaping (_ result: SessionRunResult) -> Void
) {
deviceSelector { (device) in
guard let device = device else {
block(.failure(PodCommsError.noRileyLinkAvailable))
Expand All @@ -352,6 +356,12 @@ class ErosPodComms: PodComms {
erosPodMessageTransport.messageLogger = self.messageLogger
let podSession = PodCommsSession(podState: self.podState!, transport: erosPodMessageTransport, delegate: self)
block(.success(session: podSession))

// If the pod is faulted, call the onFault
// handler to save any updated dose info.
if self.podState?.isFaulted == true {
onFault(podSession)
}
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions OmnipodKit/PumpManager/OmniPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1513,15 +1513,33 @@ extension OmniPumpManager {
// Used to serialize a set of Pod Commands for a given session - vectors to correct version
private func runSession(withName name: String, _ block: @escaping (_ result: PodComms.SessionRunResult) -> Void) {
if let blePodComms = self.podComms as? BlePodComms {
blePodComms.bleRunSession(withName: name, block)
blePodComms.bleRunSession(withName: name, onFault: onFault, block)
} else if let erosPodComms = self.podComms as? ErosPodComms {
let device = self.rileyLinkDeviceProvider.firstConnectedDevice
erosPodComms.erosRunSession(withName: name, using: device, block)
erosPodComms.erosRunSession(withName: name, using: device, onFault: onFault, block)
} else {
block(.failure(.diagnosticMessage(str: OmniPumpManagerError.podTypeNotConfigured.localizedDescription)))
}
}

/// Called at the end of the PodComms RunSession manager sequence if a pod fault has been
/// detected to ensure the any updated dose information is saved immediately after the pod fault
/// is detected reported to the app as doses may contained interrupted doses. This will better
/// handle various cases where a pod fault has have detected during some operation, but the
/// calling flow has to rely on some later flow (e.g., in forgetPod() or handlePodUpdatesAsNeeded())
/// to actually handle getting the interrupted doses saved after the pod fault has occurred.
func onFault(session: PodCommsSession) {
if state.podState?.isFaulted == true,
let dosesToStore = state.podState?.dosesToStore,
dosesToStore.count > 0
{
self.log.default("@@@ onFault storing doses: %{public}@", String(describing: dosesToStore))
session.dosesForStorage() { (doses) -> Bool in
return store(doses: doses, in: session)
}
}
}

// Shared handler for getPodStatus() and post-connnect() that does a getStatus()
// (unless canOptimize is true and a StatusResponse had been recently received)
// and other associated actions that need to be regularly performed.
Expand Down