| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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
- // 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"])
- }
- }
- // 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
- ]
- }
- }
- func returnType() -> ReturnType {
- switch self {
- case .makeProfile:
- return .dictionary
- case .iob:
- return .array
- case .meal:
- return .dictionary
- case .autosens:
- return .dictionary
- }
- }
- }
|