forked from ps2/NightscoutService
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStoredSettings.swift
More file actions
153 lines (133 loc) · 5.15 KB
/
StoredSettings.swift
File metadata and controls
153 lines (133 loc) · 5.15 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// StoredSettings.swift
// NightscoutServiceKit
//
// Created by Darin Krauss on 10/17/19.
// Copyright © 2019 LoopKit Authors. All rights reserved.
//
import HealthKit
import LoopKit
import NightscoutKit
extension AutomaticDosingStrategy {
var name: String {
switch self {
case .automaticBolus:
return "automaticBolus"
case .tempBasalOnly:
return "tempBasalOnly"
}
}
init?(name: String) {
switch name {
case "automaticBolus":
self = .automaticBolus
case "tempBasalOnly":
self = .tempBasalOnly
default:
return nil
}
}
}
extension StoredSettings {
var loopSettings: NightscoutKit.LoopSettings? {
guard let bloodGlucoseUnit = bloodGlucoseUnit else {
return nil
}
var nightscoutPreMealTargetRange: ClosedRange<Double>?
if let preMealTargetRange = preMealTargetRange?.doubleRange(for: bloodGlucoseUnit) {
nightscoutPreMealTargetRange = ClosedRange(
uncheckedBounds: (
lower: preMealTargetRange.minValue,
upper: preMealTargetRange.maxValue))
}
return NightscoutKit.LoopSettings(
dosingEnabled: dosingEnabled,
overridePresets: overridePresets.map { $0.nsScheduleOverride(for: bloodGlucoseUnit) },
scheduleOverride: nil,
minimumBGGuard: suspendThreshold?.quantity.doubleValue(for: bloodGlucoseUnit),
preMealTargetRange: nightscoutPreMealTargetRange,
maximumBasalRatePerHour: maximumBasalRatePerHour,
maximumBolus: maximumBolus,
deviceToken: deviceToken,
bundleIdentifier: Bundle.main.bundleIdentifier,
dosingStrategy: automaticDosingStrategy.name)
}
var profile: ProfileSet.Profile? {
guard let basalRateSchedule = basalRateSchedule,
let carbRatioSchedule = carbRatioSchedule,
let glucoseTargetRangeSchedule = glucoseTargetRangeSchedule,
let insulinSensitivitySchedule = insulinSensitivitySchedule?.schedule(for: glucoseTargetRangeSchedule.unit)
else
{
return nil
}
let targetLowItems = glucoseTargetRangeSchedule.items.map { item -> ProfileSet.ScheduleItem in
return ProfileSet.ScheduleItem(offset: item.startTime, value: item.value.minValue)
}
let targetHighItems = glucoseTargetRangeSchedule.items.map { item -> ProfileSet.ScheduleItem in
return ProfileSet.ScheduleItem(offset: item.startTime, value: item.value.maxValue)
}
return ProfileSet.Profile(
timezone: basalRateSchedule.timeZone,
dia: .hours(6),
sensitivity: insulinSensitivitySchedule.items.scheduleItems,
carbratio: carbRatioSchedule.items.scheduleItems,
basal: basalRateSchedule.items.scheduleItems,
targetLow: targetLowItems,
targetHigh: targetHighItems,
units: glucoseTargetRangeSchedule.unit.shortLocalizedUnitString(avoidLineBreaking: false))
}
var profileSet: ProfileSet? {
guard let bloodGlucoseUnit = bloodGlucoseUnit, let profile = profile, let loopSettings = loopSettings else {
return nil
}
return ProfileSet(
startDate: date,
units: bloodGlucoseUnit.shortLocalizedUnitString(avoidLineBreaking: false),
enteredBy: "Loop",
defaultProfile: "Default",
store: ["Default": profile],
settings: loopSettings,
syncIdentifier: syncIdentifier.uuidString)
}
}
fileprivate extension Array where Element == RepeatingScheduleValue<Double> {
var scheduleItems: [ProfileSet.ScheduleItem] {
return map { item -> ProfileSet.ScheduleItem in
return ProfileSet.ScheduleItem(offset: item.startTime, value: item.value)
}
}
}
// String conversion methods, adapted from https://stackoverflow.com/questions/40276322/hex-binary-string-conversion-in-swift/40278391#40278391
fileprivate extension Data {
init?(hexadecimalString: String) {
self.init(capacity: hexadecimalString.utf16.count / 2)
// Convert 0 ... 9, a ... f, A ...F to their decimal value,
// return nil for all other input characters
func decodeNibble(u: UInt16) -> UInt8? {
switch u {
case 0x30 ... 0x39: // '0'-'9'
return UInt8(u - 0x30)
case 0x41 ... 0x46: // 'A'-'F'
return UInt8(u - 0x41 + 10) // 10 since 'A' is 10, not 0
case 0x61 ... 0x66: // 'a'-'f'
return UInt8(u - 0x61 + 10) // 10 since 'a' is 10, not 0
default:
return nil
}
}
var even = true
var byte: UInt8 = 0
for c in hexadecimalString.utf16 {
guard let val = decodeNibble(u: c) else { return nil }
if even {
byte = val << 4
} else {
byte += val
self.append(byte)
}
even = !even
}
guard even else { return nil }
}
}