Skip to content

Commit

Permalink
ns manager rework to use CD entites
Browse files Browse the repository at this point in the history
  • Loading branch information
polscm32 committed Apr 28, 2024
1 parent bd5057c commit 1bd2866
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 51 deletions.
4 changes: 2 additions & 2 deletions FreeAPS/Sources/Models/NightscoutStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct NightscoutStatus: JSON {

struct OpenAPSStatus: JSON {
let iob: IOBEntry?
let suggested: Suggestion?
let enacted: Suggestion?
let suggested: Determination?
let enacted: Determination?
let version: String
}

Expand Down
2 changes: 0 additions & 2 deletions FreeAPS/Sources/Modules/Home/HomeDataFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ enum Home {
}

protocol HomeProvider: Provider {
var suggestion: Suggestion? { get }
var enactedSuggestion: Suggestion? { get }
func heartbeatNow()
func pumpHistory(hours: Int) -> [PumpHistoryEvent]
func pumpSettings() -> PumpSettings
Expand Down
8 changes: 0 additions & 8 deletions FreeAPS/Sources/Modules/Home/HomeProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ extension Home {
@Injected() var carbsStorage: CarbsStorage!
@Injected() var announcementStorage: AnnouncementsStorage!

var suggestion: Suggestion? {
storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
}

var enactedSuggestion: Suggestion? {
storage.retrieve(OpenAPS.Enact.enacted, as: Suggestion.self)
}

func pumpTimeZone() -> TimeZone? {
apsManager.pumpManager?.status.timeZone
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension NightscoutConfig {
@Injected() var apsManager: APSManager!

let coredataContext = CoreDataStack.shared.backgroundContext

@Published var url = ""
@Published var secret = ""
@Published var message = ""
Expand Down
2 changes: 0 additions & 2 deletions FreeAPS/Sources/Modules/Settings/View/SettingsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ extension Settings {
.navigationLink(to: .configEditor(file: OpenAPS.Settings.profile), from: self)
Text("Carbs")
.navigationLink(to: .configEditor(file: OpenAPS.Monitor.carbHistory), from: self)
Text("Enacted")
.navigationLink(to: .configEditor(file: OpenAPS.Enact.enacted), from: self)
Text("Announcements")
.navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcements), from: self)
Text("Enacted announcements")
Expand Down
210 changes: 203 additions & 7 deletions FreeAPS/Sources/Services/Network/NightscoutManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Combine
import CoreData
import Foundation
import LoopKitUI
import Swinject
Expand Down Expand Up @@ -63,6 +64,8 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {

private let context = CoreDataStack.shared.backgroundContext

private var lastTwoDeterminations: [OrefDetermination]?

init(resolver: Resolver) {
injectServices(resolver)
subscribe()
Expand Down Expand Up @@ -409,15 +412,209 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
}
}

private func fetchDeterminations() {
let fetchRequest: NSFetchRequest<OrefDetermination> = OrefDetermination.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \OrefDetermination.deliverAt, ascending: false)]
fetchRequest.predicate = NSPredicate.predicateFor30MinAgoForDetermination
fetchRequest.fetchLimit = 2
do {
lastTwoDeterminations = try context.fetch(fetchRequest)
debugPrint(
"Home State Model: \(#function) \(DebuggingIdentifiers.succeeded) fetched determinations from core data"
)
} catch {
debugPrint(
"Home State Model: \(#function) \(DebuggingIdentifiers.failed) failed to fetch determinations from core data"
)
}
}

func uploadStatus() {
let iob = storage.retrieve(OpenAPS.Monitor.iob, as: [IOBEntry].self)
var suggested = storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
var enacted = storage.retrieve(OpenAPS.Enact.enacted, as: Suggestion.self)

if (suggested?.timestamp ?? .distantPast) > (enacted?.timestamp ?? .distantPast) {
enacted?.predictions = nil
} else {
suggested?.predictions = nil
let penultimateDetermination = lastTwoDeterminations?.last
let lastDetermination = lastTwoDeterminations?.first

var suggested: Determination?
var enacted: Determination?

if let lastDetermination = lastDetermination, let penultimateDetermination = penultimateDetermination {
if lastDetermination.enacted, penultimateDetermination.enacted {
suggested = Determination(
reason: lastDetermination.reason ?? "",
units: lastDetermination.smbToDeliver?.decimalValue,
insulinReq: lastDetermination.insulinReq?.decimalValue,
eventualBG: Int(truncating: lastDetermination.eventualBG ?? 0),
sensitivityRatio: lastDetermination.sensitivityRatio?.decimalValue,
rate: lastDetermination.rate?.decimalValue,
duration: Int(lastDetermination.duration),
iob: lastDetermination.iob?.decimalValue,
cob: Decimal(lastDetermination.cob),
predictions: nil,
deliverAt: lastDetermination.deliverAt ?? Date(),
carbsReq: Decimal(lastDetermination.carbsRequired),
temp: TempType(rawValue: lastDetermination.temp ?? ""),
bg: lastDetermination.glucose?.decimalValue,
reservoir: lastDetermination.reservoir?.decimalValue,
isf: lastDetermination.insulinSensitivity?.decimalValue,
timestamp: lastDetermination.timestamp,
recieved: lastDetermination.received,
tdd: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
insulin: Insulin(
TDD: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
bolus: lastDetermination.bolus?.decimalValue ?? Decimal(0),
temp_basal: lastDetermination.tempBasal?.decimalValue ?? Decimal(0),
scheduled_basal: lastDetermination.scheduledBasal?.decimalValue ?? Decimal(0)
),
current_target: lastDetermination.currentTarget?.decimalValue ?? Decimal(0),
insulinForManualBolus: lastDetermination.insulinForManualBolus?.decimalValue ?? Decimal(0),
manualBolusErrorString: lastDetermination.manualBolusErrorString?.decimalValue ?? Decimal(0),
minDelta: lastDetermination.minDelta?.decimalValue ?? Decimal(0),
expectedDelta: lastDetermination.expectedDelta?.decimalValue ?? Decimal(0),
minGuardBG: nil, minPredBG: nil, threshold: lastDetermination.treshold?.decimalValue ?? Decimal(0),
carbRatio: lastDetermination.carbRatio?.decimalValue ?? Decimal(0)
)
enacted = Determination(
reason: lastDetermination.reason ?? "",
units: lastDetermination.smbToDeliver?.decimalValue,
insulinReq: lastDetermination.insulinReq?.decimalValue,
eventualBG: Int(truncating: lastDetermination.eventualBG ?? 0),
sensitivityRatio: lastDetermination.sensitivityRatio?.decimalValue,
rate: lastDetermination.rate?.decimalValue,
duration: Int(lastDetermination.duration),
iob: lastDetermination.iob?.decimalValue,
cob: Decimal(lastDetermination.cob),
predictions: nil,
deliverAt: lastDetermination.deliverAt ?? Date(),
carbsReq: Decimal(lastDetermination.carbsRequired),
temp: TempType(rawValue: lastDetermination.temp ?? ""),
bg: lastDetermination.glucose?.decimalValue,
reservoir: lastDetermination.reservoir?.decimalValue,
isf: lastDetermination.insulinSensitivity?.decimalValue,
timestamp: lastDetermination.timestamp,
recieved: lastDetermination.received,
tdd: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
insulin: Insulin(
TDD: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
bolus: lastDetermination.bolus?.decimalValue ?? Decimal(0),
temp_basal: lastDetermination.tempBasal?.decimalValue ?? Decimal(0),
scheduled_basal: lastDetermination.scheduledBasal?.decimalValue ?? Decimal(0)
),
current_target: lastDetermination.currentTarget?.decimalValue ?? Decimal(0),
insulinForManualBolus: lastDetermination.insulinForManualBolus?.decimalValue ?? Decimal(0),
manualBolusErrorString: lastDetermination.manualBolusErrorString?.decimalValue ?? Decimal(0),
minDelta: lastDetermination.minDelta?.decimalValue ?? Decimal(0),
expectedDelta: lastDetermination.expectedDelta?.decimalValue ?? Decimal(0),
minGuardBG: nil, minPredBG: nil, threshold: lastDetermination.treshold?.decimalValue ?? Decimal(0),
carbRatio: lastDetermination.carbRatio?.decimalValue ?? Decimal(0)
)
} else if !lastDetermination.enacted, penultimateDetermination.enacted {
suggested = Determination(
reason: lastDetermination.reason ?? "",
units: lastDetermination.smbToDeliver?.decimalValue,
insulinReq: lastDetermination.insulinReq?.decimalValue,
eventualBG: Int(truncating: lastDetermination.eventualBG ?? 0),
sensitivityRatio: lastDetermination.sensitivityRatio?.decimalValue,
rate: lastDetermination.rate?.decimalValue,
duration: Int(lastDetermination.duration),
iob: lastDetermination.iob?.decimalValue,
cob: Decimal(lastDetermination.cob),
predictions: nil,
deliverAt: lastDetermination.deliverAt ?? Date(),
carbsReq: Decimal(lastDetermination.carbsRequired),
temp: TempType(rawValue: lastDetermination.temp ?? ""),
bg: lastDetermination.glucose?.decimalValue,
reservoir: lastDetermination.reservoir?.decimalValue,
isf: lastDetermination.insulinSensitivity?.decimalValue,
timestamp: lastDetermination.timestamp,
recieved: lastDetermination.received,
tdd: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
insulin: Insulin(
TDD: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
bolus: lastDetermination.bolus?.decimalValue ?? Decimal(0),
temp_basal: lastDetermination.tempBasal?.decimalValue ?? Decimal(0),
scheduled_basal: lastDetermination.scheduledBasal?.decimalValue ?? Decimal(0)
),
current_target: lastDetermination.currentTarget?.decimalValue ?? Decimal(0),
insulinForManualBolus: lastDetermination.insulinForManualBolus?.decimalValue ?? Decimal(0),
manualBolusErrorString: lastDetermination.manualBolusErrorString?.decimalValue ?? Decimal(0),
minDelta: lastDetermination.minDelta?.decimalValue ?? Decimal(0),
expectedDelta: lastDetermination.expectedDelta?.decimalValue ?? Decimal(0),
minGuardBG: nil, minPredBG: nil, threshold: lastDetermination.treshold?.decimalValue ?? Decimal(0),
carbRatio: lastDetermination.carbRatio?.decimalValue ?? Decimal(0)
)
enacted = Determination(
reason: penultimateDetermination.reason ?? "",
units: penultimateDetermination.smbToDeliver?.decimalValue,
insulinReq: penultimateDetermination.insulinReq?.decimalValue,
eventualBG: Int(truncating: penultimateDetermination.eventualBG ?? 0),
sensitivityRatio: penultimateDetermination.sensitivityRatio?.decimalValue,
rate: penultimateDetermination.rate?.decimalValue,
duration: Int(penultimateDetermination.duration),
iob: penultimateDetermination.iob?.decimalValue,
cob: Decimal(penultimateDetermination.cob),
predictions: nil,
deliverAt: penultimateDetermination.deliverAt ?? Date(),
carbsReq: Decimal(penultimateDetermination.carbsRequired),
temp: TempType(rawValue: penultimateDetermination.temp ?? ""),
bg: penultimateDetermination.glucose?.decimalValue,
reservoir: penultimateDetermination.reservoir?.decimalValue,
isf: penultimateDetermination.insulinSensitivity?.decimalValue,
timestamp: penultimateDetermination.timestamp,
recieved: penultimateDetermination.received,
tdd: penultimateDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
insulin: Insulin(
TDD: penultimateDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
bolus: penultimateDetermination.bolus?.decimalValue ?? Decimal(0),
temp_basal: penultimateDetermination.tempBasal?.decimalValue ?? Decimal(0),
scheduled_basal: penultimateDetermination.scheduledBasal?.decimalValue ?? Decimal(0)
),
current_target: penultimateDetermination.currentTarget?.decimalValue ?? Decimal(0),
insulinForManualBolus: penultimateDetermination.insulinForManualBolus?.decimalValue ?? Decimal(0),
manualBolusErrorString: penultimateDetermination.manualBolusErrorString?.decimalValue ?? Decimal(0),
minDelta: penultimateDetermination.minDelta?.decimalValue ?? Decimal(0),
expectedDelta: penultimateDetermination.expectedDelta?.decimalValue ?? Decimal(0),
minGuardBG: nil,
minPredBG: nil,
threshold: penultimateDetermination.treshold?.decimalValue ?? Decimal(0),
carbRatio: penultimateDetermination.carbRatio?.decimalValue ?? Decimal(0)
)
} else if !lastDetermination.enacted, !penultimateDetermination.enacted {
suggested = Determination(
reason: lastDetermination.reason ?? "",
units: lastDetermination.smbToDeliver?.decimalValue,
insulinReq: lastDetermination.insulinReq?.decimalValue,
eventualBG: Int(truncating: lastDetermination.eventualBG ?? 0),
sensitivityRatio: lastDetermination.sensitivityRatio?.decimalValue,
rate: lastDetermination.rate?.decimalValue,
duration: Int(lastDetermination.duration),
iob: lastDetermination.iob?.decimalValue,
cob: Decimal(lastDetermination.cob),
predictions: nil,
deliverAt: lastDetermination.deliverAt ?? Date(),
carbsReq: Decimal(lastDetermination.carbsRequired),
temp: TempType(rawValue: lastDetermination.temp ?? ""),
bg: lastDetermination.glucose?.decimalValue,
reservoir: lastDetermination.reservoir?.decimalValue,
isf: lastDetermination.insulinSensitivity?.decimalValue,
timestamp: lastDetermination.timestamp,
recieved: lastDetermination.received,
tdd: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
insulin: Insulin(
TDD: lastDetermination.totalDailyDose?.decimalValue ?? Decimal(0),
bolus: lastDetermination.bolus?.decimalValue ?? Decimal(0),
temp_basal: lastDetermination.tempBasal?.decimalValue ?? Decimal(0),
scheduled_basal: lastDetermination.scheduledBasal?.decimalValue ?? Decimal(0)
),
current_target: lastDetermination.currentTarget?.decimalValue ?? Decimal(0),
insulinForManualBolus: lastDetermination.insulinForManualBolus?.decimalValue ?? Decimal(0),
manualBolusErrorString: lastDetermination.manualBolusErrorString?.decimalValue ?? Decimal(0),
minDelta: lastDetermination.minDelta?.decimalValue ?? Decimal(0),
expectedDelta: lastDetermination.expectedDelta?.decimalValue ?? Decimal(0),
minGuardBG: nil, minPredBG: nil, threshold: lastDetermination.treshold?.decimalValue ?? Decimal(0),
carbRatio: lastDetermination.carbRatio?.decimalValue ?? Decimal(0)
)
}
}

let loopIsClosed = settingsManager.settings.closedLoop
Expand All @@ -442,7 +639,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
}

let battery = fetchBattery()
// let battery = storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)

var reservoir = Decimal(from: storage.retrieveRaw(OpenAPS.Monitor.reservoir) ?? "0")
if reservoir == 0xDEAD_BEEF {
Expand Down
Loading

0 comments on commit 1bd2866

Please sign in to comment.