Basal.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import Foundation
  2. struct Basal {
  3. static func basalLookup(_ basalProfile: [BasalProfileEntry], now: Date? = nil) throws -> Decimal? {
  4. let nowDate = now ?? Date()
  5. // Original had a sort but it was a no-op if 'i' wasn't present, so we can skip it
  6. let basalProfileData = basalProfile
  7. guard let lastBasalRate = basalProfileData.last?.rate, lastBasalRate != 0 else {
  8. warning(.openAPS, "Warning: bad basal schedule \(basalProfile)")
  9. return nil
  10. }
  11. // Look for matching time slot
  12. for (curr, next) in zip(basalProfileData, basalProfileData.dropFirst()) {
  13. if try nowDate.isMinutesFromMidnightWithinRange(lowerBound: curr.minutes, upperBound: next.minutes) {
  14. return curr.rate.rounded(scale: 3)
  15. }
  16. }
  17. // If no matching slot found, return last basal rate
  18. return lastBasalRate.rounded(scale: 3)
  19. }
  20. static func maxDailyBasal(_ basalProfile: [BasalProfileEntry]) -> Decimal? {
  21. guard let maxBasal = basalProfile.map(\.rate).max() else {
  22. return nil
  23. }
  24. // In Javascript Number is floating point, so we don't need to do
  25. // the * 1000 / 1000
  26. return maxBasal
  27. }
  28. }