Skip to content

Commit

Permalink
add basal to tins
Browse files Browse the repository at this point in the history
  • Loading branch information
mountrcg committed May 18, 2024
1 parent 11abad8 commit b1dbafd
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions FreeAPS/Sources/Modules/Home/HomeStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,21 +335,61 @@ extension Home {
let date = Date()
let calendar = Calendar.current
let offset = hours

var offsetComponents = DateComponents()
// offsetComponents.hour = -offset.rawValue
offsetComponents.hour = -Int(offset)

let startTime = calendar.date(byAdding: offsetComponents, to: date)!
// print("******************")
// print("TINS calc start time: \(startTime)")

let bolusesForCurrentDay = boluses.filter { $0.timestamp >= startTime && $0.type == .bolus }
guard let basalInsulinForCurrentDay = getDeliveredBasal() else { return "" }

/// add all boluses for specified time together
let totalBoluses = bolusesForCurrentDay.map { $0.amount ?? 0 }.reduce(0, +)
/// final TINS value, i.e. boluses AND delivered basal
let total = totalBoluses + basalInsulinForCurrentDay
debug(.default, "totalBoluses: \(totalBoluses)")
debug(.default, "basalInsulinForCurrentDay: \(basalInsulinForCurrentDay)")

let totalBolus = bolusesForCurrentDay.map { $0.amount ?? 0 }.reduce(0, +)
let roundedTotalBolus = Decimal(round(100 * Double(totalBolus)) / 100)
calculatedTins = AllFormatters.numberFormatter.string(from: total as NSNumber) ?? "NaN"

return calculatedTins
}

private func getDeliveredBasal() -> Decimal? {
let date = Date()
let calendar = Calendar.current
let offset = hours
var offsetComponents = DateComponents()
offsetComponents.hour = -Int(offset)
let startTime = calendar.date(byAdding: offsetComponents, to: date)!

/// retrieve basal entries in defined time
let basalEntriesForCurrentDay = tempBasals.filter { $0.timestamp >= startTime && $0.type == .tempBasal }

/// sort basal entries in order to prepare for the duration calculation
let basalEntriesForCurrentDaySorted = basalEntriesForCurrentDay.sorted { $0.timestamp < $1.timestamp }
/// empty array to append basal entries
var basalDurations: [Double] = []
/// calculate every basal entries duration
for (index, basalEntry) in basalEntriesForCurrentDaySorted.enumerated() {
/// proof if a next entry exists
if index + 1 < basalEntriesForCurrentDaySorted.count {
let nextEntry = basalEntriesForCurrentDaySorted[index + 1]
/// calculate difference of timestamps, result is in seconds
let durationInSeconds = nextEntry.timestamp.timeIntervalSince(basalEntry.timestamp)
let durationInHours = durationInSeconds / 3600 /// conversion to hours
basalDurations.append(durationInHours)
}
}
/// calculate how much insulin was given, i.e. rate * duration for every entry
let basalInsulinForCurrentDay = zip(basalEntriesForCurrentDaySorted, basalDurations).map { basalEntry, duration in
if let rate = basalEntry.rate {
return rate * Decimal(duration)
} else {
return 0
}
}.reduce(0, +)

return "\(roundedTotalBolus)"
return basalInsulinForCurrentDay > 0 ? basalInsulinForCurrentDay : nil
}

private func setupSuspensions() {
Expand Down

0 comments on commit b1dbafd

Please sign in to comment.