|
|
@@ -84,6 +84,24 @@ extension NightscoutConfig {
|
|
|
return NightscoutAPI(url: url, secret: secret)
|
|
|
}
|
|
|
|
|
|
+ private func getMedianTarget(
|
|
|
+ lowTargetValue: Decimal,
|
|
|
+ lowTargetTime: String,
|
|
|
+ highTarget: [NightscoutTimevalue],
|
|
|
+ units: GlucoseUnits
|
|
|
+ ) -> Decimal {
|
|
|
+ if let idx = highTarget.firstIndex(where: { $0.time == lowTargetTime }) {
|
|
|
+ let median = (lowTargetValue + highTarget[idx].value) / 2
|
|
|
+ switch units {
|
|
|
+ case .mgdL:
|
|
|
+ return Decimal(round(Double(median)))
|
|
|
+ case .mmolL:
|
|
|
+ return Decimal(round(Double(median) * 10) / 10)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return lowTargetValue
|
|
|
+ }
|
|
|
+
|
|
|
func importSettings() {
|
|
|
guard let nightscout = nightscoutAPI else {
|
|
|
saveError("Can't access nightscoutAPI")
|
|
|
@@ -220,9 +238,14 @@ extension NightscoutConfig {
|
|
|
|
|
|
let targets = fetchedProfile.target_low
|
|
|
.map { target -> BGTargetEntry in
|
|
|
- BGTargetEntry(
|
|
|
- low: target.value,
|
|
|
- high: target.value,
|
|
|
+ let median = loop! ? self.getMedianTarget(
|
|
|
+ lowTargetValue: target.value,
|
|
|
+ lowTargetTime: target.time,
|
|
|
+ highTarget: fetchedProfile.target_high, units: self.units
|
|
|
+ ) : target.value
|
|
|
+ return BGTargetEntry(
|
|
|
+ low: median,
|
|
|
+ high: median,
|
|
|
start: target.time,
|
|
|
offset: self.offset(target.time) / 60
|
|
|
) }
|