forked from tidepool-org/LoopAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmOutput.swift
More file actions
74 lines (64 loc) · 2.39 KB
/
AlgorithmOutput.swift
File metadata and controls
74 lines (64 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// AlgorithmOutput.swift
//
//
// Created by Pete Schwamb on 10/13/23.
//
import Foundation
public struct AlgorithmOutput<CarbEntryType: CarbEntry> {
public var recommendationResult: Result<LoopAlgorithmDoseRecommendation,Error>
public var predictedGlucose: [PredictedGlucoseValue]
public var effects: LoopAlgorithmEffects<CarbEntryType>
public var dosesRelativeToBasal: [BasalRelativeDose]
public var activeInsulin: Double?
public var activeCarbs: Double?
public init(
recommendationResult: Result<LoopAlgorithmDoseRecommendation, Error>,
predictedGlucose: [PredictedGlucoseValue],
effects: LoopAlgorithmEffects<CarbEntryType>,
dosesRelativeToBasal: [BasalRelativeDose],
activeInsulin: Double? = nil,
activeCarbs: Double? = nil
) {
self.recommendationResult = recommendationResult
self.predictedGlucose = predictedGlucose
self.effects = effects
self.dosesRelativeToBasal = dosesRelativeToBasal
self.activeInsulin = activeInsulin
self.activeCarbs = activeCarbs
}
public var recommendation: LoopAlgorithmDoseRecommendation? {
switch recommendationResult {
case .success(let rec):
return rec
case .failure:
return nil
}
}
}
public typealias LoopAlgorithmOutputFixture = AlgorithmOutput<FixtureCarbEntry>
extension LoopAlgorithmOutputFixture: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch recommendationResult {
case .success(let recommendation):
try container.encode(recommendation, forKey: .recommendation)
case .failure(let error):
try container.encode(String(describing: error), forKey: .error)
}
try container.encode(predictedGlucose, forKey: .predictedGlucose)
try container.encode(effects, forKey: .effects)
try container.encode(dosesRelativeToBasal, forKey: .dosesRelativeToBasal)
try container.encode(activeInsulin, forKey: .activeInsulin)
try container.encode(activeCarbs, forKey: .activeCarbs)
}
private enum CodingKeys: String, CodingKey {
case recommendation
case error
case predictedGlucose
case effects
case dosesRelativeToBasal
case activeInsulin
case activeCarbs
}
}