Skip to content

Miscellaneous Omnipod code improvements & cleanup #135

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 4 commits into from
Dec 10, 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
52 changes: 41 additions & 11 deletions OmniBLE/OmnipodCommon/Pod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ public struct Pod {
public static let reservoirCapacity: Double = 200

// Supported basal rates
// Eros minimum scheduled basal rate is 0.05 U/H while for Dash supports 0 U/H.
// Would need to have this value based on productID to be able to share this file with Eros.
// Eros minimum scheduled basal rate is 0.05 U/hr while Dash supports 0 U/hr.
public static let supportedBasalRates: [Double] = (0...600).map { Double($0) / Double(pulsesPerUnit) }

// The internal basal rate used for non-Eros pods
// Would need to have this value based on productID to be able to share this file with Eros.
// Supported temp basal rates
// Both Eros and Dash support a minimum temp basal rate of 0 U/hr.
public static let supportedTempBasalRates: [Double] = (0...600).map { Double($0) / Double(pulsesPerUnit) }

// The internal basal rate used for zero basal rates
// Eros uses 0.0 while Dash uses a near zero rate
public static let zeroBasalRate: Double = nearZeroBasalRate

// Maximum number of basal schedule entries supported
Expand All @@ -85,13 +88,13 @@ public struct Pod {
public static let defaultExpirationReminderOffset = TimeInterval(hours: 2)
public static let expirationReminderAlertMinHoursBeforeExpiration = 1
public static let expirationReminderAlertMaxHoursBeforeExpiration = 24

// Threshold used to display pod end of life warnings
public static let timeRemainingWarningThreshold = TimeInterval(days: 1)

// Default low reservoir alert limit in Units
public static let defaultLowReservoirReminder: Double = 10

// Allowed Low Reservoir reminder values
public static let allowedLowReservoirReminderValues = Array(stride(from: 1, through: 50, by: 1))
}
Expand All @@ -111,19 +114,46 @@ public enum DeliveryStatus: UInt8, CustomStringConvertible {
case extendedBolusAndTempBasal = 10

public var suspended: Bool {
return self == .suspended || self == .priming || self == .extendedBolusWhileSuspended
// returns true if both the tempBasal and basal bits are clear
let suspendedStates: Set<DeliveryStatus> = [
.suspended,
.priming,
.extendedBolusWhileSuspended,
]
return suspendedStates.contains(self)
}

public var bolusing: Bool {
return self == .bolusInProgress || self == .bolusAndTempBasal || self == .extendedBolusRunning || self == .extendedBolusAndTempBasal || self == .priming || self == .extendedBolusWhileSuspended
// returns true if either the immediateBolus or extendedBolus bits are set
let bolusingStates: Set<DeliveryStatus> = [
.priming,
.bolusInProgress,
.bolusAndTempBasal,
.extendedBolusWhileSuspended,
.extendedBolusRunning,
.extendedBolusAndTempBasal,
]
return bolusingStates.contains(self)
}

public var tempBasalRunning: Bool {
return self == .tempBasalRunning || self == .bolusAndTempBasal || self == .extendedBolusAndTempBasal
// returns true if the tempBasal bit is set
let tempBasalRunningStates: Set<DeliveryStatus> = [
.tempBasalRunning,
.bolusAndTempBasal,
.extendedBolusAndTempBasal,
]
return tempBasalRunningStates.contains(self)
}

public var extendedBolusRunning: Bool {
return self == .extendedBolusRunning || self == .extendedBolusAndTempBasal || self == .extendedBolusWhileSuspended
// returns true if the extendedBolus bit is set
let extendedBolusRunningStates: Set<DeliveryStatus> = [
.extendedBolusWhileSuspended,
.extendedBolusRunning,
.extendedBolusAndTempBasal,
]
return extendedBolusRunningStates.contains(self)
}

public var description: String {
Expand Down
6 changes: 2 additions & 4 deletions OmniBLE/PumpManager/OmniBLEPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ extension OmniBLEPumpManager: PumpManager {
})

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")
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 @@ -2033,7 +2033,7 @@ extension OmniBLEPumpManager: PumpManager {
return
}

guard status.deliveryStatus != .suspended else {
guard !status.deliveryStatus.suspended else {
self.log.info("Canceling temp basal because status return indicates pod is suspended!")
completion(.communication(PodCommsError.podSuspended))
return
Expand Down Expand Up @@ -2422,12 +2422,10 @@ extension OmniBLEPumpManager: PumpManager {

extension OmniBLEPumpManager: MessageLogger {
func didSend(_ message: Data) {
log.default("didSend: %{public}@", message.hexadecimalString)
self.logDeviceCommunication(message.hexadecimalString, type: .send)
}

func didReceive(_ message: Data) {
log.default("didReceive: %{public}@", message.hexadecimalString)
self.logDeviceCommunication(message.hexadecimalString, type: .receive)
}

Expand Down
6 changes: 4 additions & 2 deletions OmniBLE/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
if deliveryStatus.tempBasalRunning && unfinalizedTempBasal == nil { // active temp basal that we aren't tracking
// unfinalizedTempBasal = UnfinalizedDose(tempBasalRate: 0, startTime: Date(), duration: .minutes(30), isHighTemp: false, scheduledCertainty: .certain, insulinType: insulinType)
}
if deliveryStatus != .suspended && isSuspended { // active basal that we aren't tracking
if !deliveryStatus.suspended && isSuspended { // active basal that we aren't tracking
let resumeStartTime = Date()
suspendState = .resumed(resumeStartTime)
unfinalizedResume = UnfinalizedDose(resumeStartTime: resumeStartTime, scheduledCertainty: .certain, insulinType: insulinType)
Expand Down Expand Up @@ -570,7 +570,7 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
"* expiresAt: \(String(reflecting: expiresAt))",
"* podTime: \(podTime.timeIntervalStr)",
"* podTimeUpdated: \(String(reflecting: podTimeUpdated))",
"* setupUnitsDelivered: \(String(reflecting: setupUnitsDelivered))",
"* setupUnitsDelivered: \(setupUnitsDelivered == nil ? "?" : setupUnitsDelivered!.twoDecimals) U",
"* firmwareVersion: \(firmwareVersion)",
"* bleFirmwareVersion: \(bleFirmwareVersion)",
"* lotNo: \(lotNo)",
Expand All @@ -583,6 +583,8 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
"* unfinalizedResume: \(String(describing: unfinalizedResume))",
"* finalizedDoses: \(String(describing: finalizedDoses))",
"* activeAlertsSlots: \(alertSetString(alertSet: activeAlertSlots))",
"* delivered: \(lastInsulinMeasurements == nil ? "?" : lastInsulinMeasurements!.delivered.twoDecimals) U",
"* reservoirLevel: \(lastInsulinMeasurements == nil || lastInsulinMeasurements!.reservoirLevel == nil || lastInsulinMeasurements!.reservoirLevel == Pod.reservoirLevelAboveThresholdMagicNumber ? "50+" : lastInsulinMeasurements!.reservoirLevel!.twoDecimals) U",
"* messageTransportState: \(String(describing: messageTransportState))",
"* setupProgress: \(setupProgress)",
"* primeFinishTime: \(String(describing: primeFinishTime))",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ class OmniBLESettingsViewModel: ObservableObject {
}

public var allowedTempBasalRates: [Double] {
return Pod.supportedBasalRates.filter { $0 <= pumpManager.state.maximumTempBasalRate }
return Pod.supportedTempBasalRates.filter { $0 <= pumpManager.state.maximumTempBasalRate }
}
}

Expand Down