|
|
@@ -268,3 +268,133 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
|
|
|
storeEvents(events)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+extension NightscoutTreatment {
|
|
|
+ init?(event: PumpHistoryEvent, tempBasalDuration: PumpHistoryEvent? = nil) {
|
|
|
+ var basalDurationEvent: PumpHistoryEvent?
|
|
|
+ if tempBasalDuration != nil, tempBasalDuration?.timestamp == event.timestamp, event.type == .tempBasal,
|
|
|
+ tempBasalDuration?.type == .tempBasalDuration
|
|
|
+ {
|
|
|
+ basalDurationEvent = tempBasalDuration
|
|
|
+ }
|
|
|
+ switch event.type {
|
|
|
+ case .tempBasal:
|
|
|
+ self.init(
|
|
|
+ duration: basalDurationEvent?.durationMin,
|
|
|
+ rawDuration: basalDurationEvent,
|
|
|
+ rawRate: event,
|
|
|
+ absolute: event.rate,
|
|
|
+ rate: event.rate,
|
|
|
+ eventType: .nsTempBasal,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: nil,
|
|
|
+ insulin: nil,
|
|
|
+ notes: nil,
|
|
|
+ carbs: nil,
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ case .bolus:
|
|
|
+ let eventType = determineBolusEventType(for: event)
|
|
|
+ self.init(
|
|
|
+ duration: event.duration,
|
|
|
+ rawDuration: nil,
|
|
|
+ rawRate: nil,
|
|
|
+ absolute: nil,
|
|
|
+ rate: nil,
|
|
|
+ eventType: eventType,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: event,
|
|
|
+ insulin: event.amount,
|
|
|
+ notes: nil,
|
|
|
+ carbs: nil,
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ case .journalCarbs:
|
|
|
+ self.init(
|
|
|
+ duration: nil,
|
|
|
+ rawDuration: nil,
|
|
|
+ rawRate: nil,
|
|
|
+ absolute: nil,
|
|
|
+ rate: nil,
|
|
|
+ eventType: .nsCarbCorrection,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: nil,
|
|
|
+ insulin: nil,
|
|
|
+ notes: nil,
|
|
|
+ carbs: Decimal(event.carbInput ?? 0),
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ case .prime:
|
|
|
+ self.init(
|
|
|
+ duration: event.duration,
|
|
|
+ rawDuration: nil,
|
|
|
+ rawRate: nil,
|
|
|
+ absolute: nil,
|
|
|
+ rate: nil,
|
|
|
+ eventType: .nsSiteChange,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: event,
|
|
|
+ insulin: nil,
|
|
|
+ notes: nil,
|
|
|
+ carbs: nil,
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ case .rewind:
|
|
|
+ self.init(
|
|
|
+ duration: nil,
|
|
|
+ rawDuration: nil,
|
|
|
+ rawRate: nil,
|
|
|
+ absolute: nil,
|
|
|
+ rate: nil,
|
|
|
+ eventType: .nsInsulinChange,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: nil,
|
|
|
+ insulin: nil,
|
|
|
+ notes: nil,
|
|
|
+ carbs: nil,
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ case .pumpAlarm:
|
|
|
+ self.init(
|
|
|
+ duration: 30, // minutes
|
|
|
+ rawDuration: nil,
|
|
|
+ rawRate: nil,
|
|
|
+ absolute: nil,
|
|
|
+ rate: nil,
|
|
|
+ eventType: .nsAnnouncement,
|
|
|
+ createdAt: event.timestamp,
|
|
|
+ enteredBy: NightscoutTreatment.local,
|
|
|
+ bolus: nil,
|
|
|
+ insulin: nil,
|
|
|
+ notes: "Alarm \(String(describing: event.note)) \(event.type)",
|
|
|
+ carbs: nil,
|
|
|
+ fat: nil,
|
|
|
+ protein: nil,
|
|
|
+ targetTop: nil,
|
|
|
+ targetBottom: nil
|
|
|
+ )
|
|
|
+ default:
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|