Skip to content

Commit

Permalink
Have the Loop Insulin Delivered start at 0 U despite variable priming…
Browse files Browse the repository at this point in the history
… amounts reported from Pod (#563)

* Exclude the cannula insertion bolus amount from the calculated insulin delivered

* Add a new persistent setupUnitsDelivered PodState variable to save setup amount counted
by Pod as being actually delivered so that the user reported Insulin Delivered starts at 0 U.
  • Loading branch information
itsmojo authored and ps2 committed Dec 17, 2019
1 parent 31e8be4 commit f47c390
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
22 changes: 16 additions & 6 deletions OmniKit/PumpManager/PodCommsSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ public class PodCommsSession {
} catch {} // never critical
}

private func markSetupProgressCompleted(statusResponse: StatusResponse) {
if (podState.setupProgress != .completed) {
podState.setupProgress = .completed
podState.setupUnitsDelivered = statusResponse.insulin // stash the current insulin delivered value as the baseline
log.info("Total setup units delivered: %@", String(describing: statusResponse.insulin))
}
}

public func insertCannula() throws -> TimeInterval {
let insertionWait: TimeInterval = .seconds(Pod.cannulaInsertionUnits / Pod.primeDeliveryRate)

Expand All @@ -345,15 +353,17 @@ public class PodCommsSession {
if podState.setupProgress == .startingInsertCannula || podState.setupProgress == .cannulaInserting {
// We started cannula insertion, but didn't get confirmation somehow, so check status
let status: StatusResponse = try send([GetStatusCommand()])
podState.updateFromStatusResponse(status)
if status.podProgressStatus == .cannulaInserting {
podState.setupProgress = .cannulaInserting
return insertionWait// Not sure when it started, wait full time to be sure
podState.updateFromStatusResponse(status)
return insertionWait // Not sure when it started, wait full time to be sure
}
if status.podProgressStatus.readyForDelivery {
podState.setupProgress = .completed
markSetupProgressCompleted(statusResponse: status)
podState.updateFromStatusResponse(status)
return TimeInterval(0) // Already done; no need to wait
}
podState.updateFromStatusResponse(status)
} else {
// Configure all the non-optional Pod Alarms
let expirationTime = activatedAt + Pod.nominalPodLife
Expand Down Expand Up @@ -382,13 +392,13 @@ public class PodCommsSession {
public func checkInsertionCompleted() throws {
if podState.setupProgress == .cannulaInserting {
let response: StatusResponse = try send([GetStatusCommand()])
podState.updateFromStatusResponse(response)
if response.podProgressStatus.readyForDelivery {
podState.setupProgress = .completed
markSetupProgressCompleted(statusResponse: response)
}
podState.updateFromStatusResponse(response)
}
}

// Throws SetBolusError
public enum DeliveryCommandResult {
case success(statusResponse: StatusResponse)
Expand Down
11 changes: 8 additions & 3 deletions OmniKit/PumpManager/PodInsulinMeasurements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ public struct PodInsulinMeasurements: RawRepresentable, Equatable {
public let delivered: Double
public let reservoirVolume: Double?

public init(statusResponse: StatusResponse, validTime: Date) {
public init(statusResponse: StatusResponse, validTime: Date, setupUnitsDelivered: Double?) {
self.validTime = validTime
self.delivered = statusResponse.insulin - Pod.primeUnits
self.reservoirVolume = statusResponse.reservoirLevel
}
if let setupUnitsDelivered = setupUnitsDelivered {
self.delivered = statusResponse.insulin - setupUnitsDelivered
} else {
// subtract off the fixed setup command values as we don't have an actual value (yet)
self.delivered = max(statusResponse.insulin - Pod.primeUnits - Pod.cannulaInsertionUnits, 0)
}
}

// RawRepresentable
public init?(rawValue: RawValue) {
Expand Down
14 changes: 13 additions & 1 deletion OmniKit/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
public var activatedAt: Date?
public var expiresAt: Date? // set based on StatusResponse timeActive and can change with Pod clock drift and/or system time change

public var setupUnitsDelivered: Double?

public let piVersion: String
public let pmVersion: String
public let lot: UInt32
Expand Down Expand Up @@ -157,7 +159,7 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
self.expiresAt = expiresAtComputed
}
updateDeliveryStatus(deliveryStatus: response.deliveryStatus)
lastInsulinMeasurements = PodInsulinMeasurements(statusResponse: response, validTime: now)
lastInsulinMeasurements = PodInsulinMeasurements(statusResponse: response, validTime: now, setupUnitsDelivered: setupUnitsDelivered)
activeAlertSlots = response.alerts
}

Expand Down Expand Up @@ -262,6 +264,10 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
}
}

if let setupUnitsDelivered = rawValue["setupUnitsDelivered"] as? Double {
self.setupUnitsDelivered = setupUnitsDelivered
}

if let suspended = rawValue["suspended"] as? Bool {
// Migrate old value
if suspended {
Expand Down Expand Up @@ -408,6 +414,11 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
rawValue["expiresAt"] = expiresAt
}

if let setupUnitsDelivered = setupUnitsDelivered {
rawValue["setupUnitsDelivered"] = setupUnitsDelivered
}


if configuredAlerts.count > 0 {
let rawConfiguredAlerts = Dictionary(uniqueKeysWithValues:
configuredAlerts.map { slot, alarm in (String(describing: slot.rawValue), alarm.rawValue) })
Expand All @@ -425,6 +436,7 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
"* address: \(String(format: "%04X", address))",
"* activatedAt: \(String(reflecting: activatedAt))",
"* expiresAt: \(String(reflecting: expiresAt))",
"* setupUnitsDelivered: \(String(reflecting: setupUnitsDelivered))",
"* piVersion: \(piVersion)",
"* pmVersion: \(pmVersion)",
"* lot: \(lot)",
Expand Down

0 comments on commit f47c390

Please sign in to comment.