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
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ extension AlertConfiguration {
case .unitsRemaining(let volume):
let ticks = UInt16(volume / Pod.pulseSize / 2)
data.appendBigEndian(ticks)
case .timeUntilAlert(let duration):
let minutes = UInt16(duration.minutes)
case .timeUntilAlert(let secondsUntilAlert):
// round the time to alert to the nearest minute
let minutes = UInt16((secondsUntilAlert + 30).minutes)
data.appendBigEndian(minutes)
}
data.append(beepRepeat.rawValue)
Expand Down
14 changes: 11 additions & 3 deletions OmniKit/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl

public let address: UInt32
fileprivate var nonceState: NonceState
public var activatedAt: Date?
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)
Expand Down Expand Up @@ -144,11 +144,19 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
}

public mutating func updateFromStatusResponse(_ response: StatusResponse) {
let now = Date()
let activatedAtComputed = now - response.timeActive
if activatedAt == nil {
self.activatedAt = Date() - response.timeActive
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
}
updateDeliveryStatus(deliveryStatus: response.deliveryStatus)
lastInsulinMeasurements = PodInsulinMeasurements(statusResponse: response, validTime: Date())
lastInsulinMeasurements = PodInsulinMeasurements(statusResponse: response, validTime: now)
activeAlertSlots = response.alerts
}

Expand Down