| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import Foundation
- /// After the port from Javascript to Swift is complete, we should remove the logging module:
- /// https://github.com/nightscout/Trio-dev/issues/293
- enum OrefFunctionResult {
- case success(RawJSON)
- case failure(Error)
- func returnOrThrow() throws -> RawJSON {
- switch self {
- case let .success(json): return json
- case let .failure(error): throw error
- }
- }
- }
- enum OrefFunction: String, Codable {
- enum ReturnType {
- case array
- case dictionary
- }
- case autosens
- case iob
- case meal
- case makeProfile
- case determineBasal
- // since we're removing some keys from our Profile that exist in Javascript
- // we need to let the difference function know which keys to ignore when
- // calculating differences
- func keysToIgnore() -> Set<String> {
- switch self {
- case .makeProfile:
- return Set(["calc_glucose_noise", "enableEnliteBgproxy", "exercise_mode", "offline_hotspot"])
- case .iob:
- // we're only checking the first result for now
- return Set(stride(from: 1, to: 48, by: 1).map { String("[\($0)]") })
- case .meal:
- // These aren't used by downstream calculations, so we
- // can ignore them in our comparison
- return Set(["maxDeviation", "minDeviation", "allDeviations", "bwCarbs", "bwFound", "journalCarbs", "nsCarbs"])
- case .autosens:
- return Set(["deviationsUnsorted"])
- case .determineBasal:
- // FIXME: Adjust as we go
- return Set([
- // Not calculating yet
- "id",
- "units",
- "insulinReq",
- "rate",
- "duration",
- "deliverAt",
- "carbsReq",
- "temp",
- "reservoir",
- "ISF",
- "current_target",
- "TDD",
- "insulinForManualBolus",
- "manualBolusErrorString",
- "minDelta",
- "CR",
- "received",
- "reason",
- // in JS but not in Swift
- "tick",
- "BGI",
- "target_bg",
- "deviation",
- // in Swift but not in JS
- "timestamp",
- "minGuardBG",
- "minPredBG"
- ])
- }
- }
- // Some values might be slightly different due to Double vs Decimal
- // and minor algorithmic differences
- func approximateMatchingNumbers() -> [String: Double] {
- switch self {
- case .makeProfile:
- return [:]
- case .iob:
- // for iob we can get rounding errors because of Double vs Decimal
- // so we leave a little extra room for our comparisons
- return [
- "iob": 0.1,
- "activity": 0.01,
- "basaliob": 0.25,
- "bolusiob": 0.25,
- "netbasalinsulin": 0.25,
- "bolusinsulin": 0.25,
- // Please see this issue for context on duration:
- // https://github.com/nightscout/Trio-dev/issues/453
- "duration": 120
- ]
- case .meal:
- return [
- "carbs": 0.1,
- "mealCOB": 10,
- "currentDeviation": 1,
- "slopeFromMaxDeviation": 0.25,
- "slopeFromMinDeviation": 0.25,
- "lastCarbTime": 1
- ]
- case .autosens:
- return [
- "ratio": 0.011,
- "newisf": 1.5
- ]
- case .determineBasal:
- return [:]
- }
- }
- func returnType() -> ReturnType {
- switch self {
- case .makeProfile:
- return .dictionary
- case .iob:
- return .array
- case .meal:
- return .dictionary
- case .autosens:
- return .dictionary
- case .determineBasal:
- return .dictionary
- }
- }
- }
|