Skip to content
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
1 change: 1 addition & 0 deletions OmniKit/PumpManager/OmnipodPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ extension OmnipodPumpManager {
var podState = PodState(address: address, piVersion: "jumpstarted", pmVersion: "jumpstarted", lot: lot, tid: tid)
podState.setupProgress = .podConfigured
podState.activatedAt = start
podState.expiresAt = start + .hours(72)

let fault = mockFault ? try? PodInfoFaultEvent(encodedData: Data(hexadecimalString: "020d0000000e00c36a020703ff020900002899080082")!) : nil
podState.fault = fault
Expand Down
30 changes: 20 additions & 10 deletions OmniKit/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl

public let address: UInt32
fileprivate var nonceState: NonceState
public var activatedAt: Date? // set based on StatusResponse timeActive and can change due to Pod clock drift and/or a system time change

public var expiresAt: Date? {
return activatedAt?.addingTimeInterval(Pod.serviceDuration - Pod.endOfServiceImminentWindow - Pod.expirationAdvisoryWindow)
}
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 let piVersion: String
public let pmVersion: String
Expand Down Expand Up @@ -148,12 +146,15 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
let activatedAtComputed = now - response.timeActive
if activatedAt == nil {
self.activatedAt = activatedAtComputed
} else if let currActivatedAt = self.activatedAt,
(activatedAtComputed < currActivatedAt || activatedAtComputed > currActivatedAt + TimeInterval(minutes: 1)) {
// The computed activatedAt time is earlier than or more than a minute later than the current activatedAt time,
// so use the computed activatedAt time instead to handle Pod clock drift and/or system time changes issues.
// The more than a minute later test prevents oscillation of activatedAt based on the timing of the responses.
self.activatedAt = activatedAtComputed
}
let expiresAtComputed = activatedAtComputed + (Pod.serviceDuration - Pod.endOfServiceImminentWindow - Pod.expirationAdvisoryWindow)
if expiresAt == nil {
self.expiresAt = expiresAtComputed
} else if expiresAtComputed < self.expiresAt! || expiresAtComputed > (self.expiresAt! + TimeInterval(minutes: 1)) {
// The computed expiresAt time is earlier than or more than a minute later than the current expiresAt time,
// so use the computed expiresAt time instead to handle Pod clock drift and/or system time changes issues.
// The more than a minute later test prevents oscillation of expiresAt based on the timing of the responses.
self.expiresAt = expiresAtComputed
}
updateDeliveryStatus(deliveryStatus: response.deliveryStatus)
lastInsulinMeasurements = PodInsulinMeasurements(statusResponse: response, validTime: now)
Expand Down Expand Up @@ -254,6 +255,11 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl

if let activatedAt = rawValue["activatedAt"] as? Date {
self.activatedAt = activatedAt
if let expiresAt = rawValue["expiresAt"] as? Date {
self.expiresAt = expiresAt
} else {
self.expiresAt = activatedAt + (Pod.serviceDuration - Pod.endOfServiceImminentWindow - Pod.expirationAdvisoryWindow)
}
}

if let suspended = rawValue["suspended"] as? Bool {
Expand Down Expand Up @@ -398,6 +404,10 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
rawValue["activatedAt"] = activatedAt
}

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

if configuredAlerts.count > 0 {
let rawConfiguredAlerts = Dictionary(uniqueKeysWithValues:
configuredAlerts.map { slot, alarm in (String(describing: slot.rawValue), alarm.rawValue) })
Expand Down