-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlf_temporary_target.patch
295 lines (283 loc) · 13 KB
/
lf_temporary_target.patch
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
diff --git a/LoopFollow/Controllers/Graphs.swift b/LoopFollow/Controllers/Graphs.swift
index 485dd71..cdb8897 100644
--- a/LoopFollow/Controllers/Graphs.swift
+++ b/LoopFollow/Controllers/Graphs.swift
@@ -267,6 +267,20 @@ extension MainViewController {
lineNote.valueFormatter = ChartYDataValueFormatter()
lineNote.drawValuesEnabled = false
+ // Temporary Target
+ var chartEntryTemporaryTarget = [ChartDataEntry]()
+ let lineTemporaryTarget = LineChartDataSet(entries:chartEntryTemporaryTarget, label: "")
+ lineTemporaryTarget.setDrawHighlightIndicators(false)
+ lineTemporaryTarget.lineWidth = 0
+ lineTemporaryTarget.drawFilledEnabled = true
+ lineTemporaryTarget.fillFormatter = TemporaryTargetFillFormatter()
+ lineTemporaryTarget.fillColor = NSUIColor.systemOrange
+ lineTemporaryTarget.fillAlpha = 0.6
+ lineTemporaryTarget.drawCirclesEnabled = false
+ lineTemporaryTarget.axisDependency = YAxis.AxisDependency.right
+ lineTemporaryTarget.highlightEnabled = true
+ lineTemporaryTarget.drawValuesEnabled = false
+
// Setup the chart data of all lines
let data = LineChartData()
@@ -282,6 +296,7 @@ extension MainViewController {
data.append(lineResume) // Dataset 9
data.append(lineSensor) // Dataset 10
data.append(lineNote) // Dataset 11
+ data.append(lineTemporaryTarget) // Dataset 12
data.setValueFont(UIFont.systemFont(ofSize: 12))
@@ -1142,6 +1157,20 @@ extension MainViewController {
lineNote.valueFormatter = ChartYDataValueFormatter()
lineNote.drawValuesEnabled = false
+ // Temporary Target
+ var chartEntryTemporaryTarget = [ChartDataEntry]()
+ let lineTemporaryTarget = LineChartDataSet(entries:chartEntryTemporaryTarget, label: "")
+ lineTemporaryTarget.setDrawHighlightIndicators(false)
+ lineTemporaryTarget.lineWidth = 0
+ lineTemporaryTarget.drawFilledEnabled = true
+ lineTemporaryTarget.fillFormatter = TemporaryTargetFillFormatter()
+ lineTemporaryTarget.fillColor = NSUIColor.systemOrange
+ lineTemporaryTarget.fillAlpha = 0.6
+ lineTemporaryTarget.drawCirclesEnabled = false
+ lineTemporaryTarget.axisDependency = YAxis.AxisDependency.right
+ lineTemporaryTarget.highlightEnabled = true
+ lineTemporaryTarget.drawValuesEnabled = false
+
// Setup the chart data of all lines
let data = LineChartData()
data.append(lineBG) // Dataset 0
@@ -1156,6 +1185,7 @@ extension MainViewController {
data.append(lineResume) // Dataset 9
data.append(lineSensor) // Dataset 10
data.append(lineNote) // Dataset 11
+ data.append(lineTemporaryTarget) // Dataset 12
BGChartFull.highlightPerDragEnabled = true
BGChartFull.leftAxis.enabled = false
@@ -1240,6 +1270,62 @@ extension MainViewController {
BGChartFull.notifyDataSetChanged()
}
}
+
+ func updateTemporaryTargetGraph() {
+ let dataIndex = 12
+ let yTop: Double = Double(topBG - 25)
+ let yBottom: Double = Double(topBG - 45)
+ let chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
+ let smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
+ chart.clear()
+ smallChart.clear()
+ let thisData = temporaryTargetGraphData
+
+ for i in 0..<thisData.count{
+ let thisItem = thisData[i]
+ var labelText = thisItem.reason + "\r\n"
+ if thisItem.enteredBy.count > 0 {
+ labelText += "\r\nEntered By: " + thisItem.enteredBy
+ }
+
+ // Start Dot
+ // Shift dots 30 seconds to create an empty 0 space between consecutive temps
+ let preStartDot = ChartDataEntry(x: Double(thisItem.date), y: yBottom, data: labelText)
+ BGChart.data?.dataSets[dataIndex].addEntry(preStartDot)
+ if UserDefaultsRepository.smallGraphTreatments.value {
+ BGChartFull.data?.dataSets[dataIndex].addEntry(preStartDot)
+ }
+
+ let startDot = ChartDataEntry(x: Double(thisItem.date + 1), y: yTop, data: labelText)
+ BGChart.data?.dataSets[dataIndex].addEntry(startDot)
+ if UserDefaultsRepository.smallGraphTreatments.value {
+ BGChartFull.data?.dataSets[dataIndex].addEntry(startDot)
+ }
+
+ // End Dot
+ let endDot = ChartDataEntry(x: Double(thisItem.endDate - 2), y: yTop, data: labelText)
+ BGChart.data?.dataSets[dataIndex].addEntry(endDot)
+ if UserDefaultsRepository.smallGraphTreatments.value {
+ BGChartFull.data?.dataSets[dataIndex].addEntry(endDot)
+ }
+
+ // Post end dot
+ let postEndDot = ChartDataEntry(x: Double(thisItem.endDate - 1), y: yBottom, data: labelText)
+ BGChart.data?.dataSets[dataIndex].addEntry(postEndDot)
+ if UserDefaultsRepository.smallGraphTreatments.value {
+ BGChartFull.data?.dataSets[dataIndex].addEntry(postEndDot)
+ }
+ }
+
+ BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
+ BGChart.data?.notifyDataChanged()
+ BGChart.notifyDataSetChanged()
+ if UserDefaultsRepository.smallGraphTreatments.value {
+ BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
+ BGChartFull.data?.notifyDataChanged()
+ BGChartFull.notifyDataSetChanged()
+ }
+ }
func formatPillText(line1: String, time: TimeInterval) -> String {
let dateFormatter = DateFormatter()
diff --git a/LoopFollow/Controllers/NightScout.swift b/LoopFollow/Controllers/NightScout.swift
index e846bfa..c81a0f3 100644
--- a/LoopFollow/Controllers/NightScout.swift
+++ b/LoopFollow/Controllers/NightScout.swift
@@ -995,6 +995,7 @@ extension MainViewController {
var resumePump: [[String:AnyObject]] = []
var pumpSiteChange: [[String:AnyObject]] = []
var cgmSensorStart: [[String:AnyObject]] = []
+ var temporaryTarget: [[String:AnyObject]] = []
for i in 0..<entries.count {
let entry = entries[i] as [String : AnyObject]?
@@ -1023,6 +1024,8 @@ extension MainViewController {
pumpSiteChange.append(entry!)
case "Sensor Start":
cgmSensorStart.append(entry!)
+ case "Temporary Target":
+ temporaryTarget.append(entry!)
default:
print("No Match: \(String(describing: entry))")
}
@@ -1085,6 +1088,13 @@ extension MainViewController {
clearOldSensor()
}
}
+ if temporaryTarget.count > 0 {
+ processTemporaryTarget(entries: temporaryTarget)
+ } else {
+ if temporaryTargetGraphData.count > 0 {
+ clearOldTemporaryTarget()
+ }
+ }
if note.count > 0 {
processNotes(entries: note)
} else {
@@ -1148,6 +1158,12 @@ extension MainViewController {
updateNotes()
}
+ func clearOldTemporaryTarget()
+ {
+ noteGraphData.removeAll()
+ updateTemporaryTargetGraph()
+ }
+
// NS Temp Basal Response Processor
func processNSBasals(entries: [[String:AnyObject]]) {
self.clearLastInfoData(index: 2)
@@ -1749,4 +1765,74 @@ extension MainViewController {
}
}
+
+ // Temporary Target Processor
+ func processTemporaryTarget(entries: [[String:AnyObject]]) {
+ if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Process: Temporary Target") }
+ temporaryTargetGraphData.removeAll()
+
+ let dateFormatter = DateFormatter()
+ dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
+ dateFormatter.locale = Locale(identifier: "en_US")
+ dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
+
+ let graphHours = 24 * UserDefaultsRepository.downloadDays.value
+ var date: String
+ var strippedZone: String
+ for i in 0..<entries.count {
+ let currentEntry = entries[entries.count - 1 - i] as [String : AnyObject]?
+ if currentEntry?["duration"] as! Int > 0 {
+ var duration: Double = 5.0
+ if currentEntry?["created_at"] != nil {
+ date = currentEntry?["created_at"] as! String
+ } else {
+ return
+ }
+ // Fix for FreeAPS milliseconds in timestamp
+ strippedZone = String(date.dropLast())
+ strippedZone = strippedZone.components(separatedBy: ".")[0]
+
+ let dateString = dateFormatter.date(from: strippedZone)
+ var dateTimeStamp = dateString!.timeIntervalSince1970
+ if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) {
+ dateTimeStamp = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
+ }
+
+ if(entries.count - 2 - i >= 0)
+ {
+ let nextEntry = entries[entries.count - 2 - i] as [String : AnyObject]?
+ date = nextEntry?["created_at"] as! String
+ strippedZone = String(date.dropLast())
+ strippedZone = strippedZone.components(separatedBy: ".")[0]
+
+ let endDateString = dateFormatter.date(from: strippedZone)
+ var endDateTimeStamp = endDateString!.timeIntervalSince1970
+ if endDateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) {
+ endDateTimeStamp = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
+ }
+
+ duration = max(min(endDateTimeStamp - dateTimeStamp, 60*60), 0)
+ }
+ else
+ {
+ duration = (currentEntry?["duration"] as? Double)!
+ duration = duration * 60
+ }
+
+ // Skip temporary targets that aren't 5 minutes long. This prevents overlapping that causes bars to not display.
+ if duration < 300 { continue }
+ print(duration)
+ guard let enteredBy = currentEntry?["enteredBy"] as? String else { continue }
+ guard let reason = currentEntry?["reason"] as? String else { continue }
+
+ let endDate = dateTimeStamp + (duration)
+
+ let dot = DataStructs.temporaryTargetStruct(date: dateTimeStamp, endDate: endDate, duration: duration, enteredBy: enteredBy, reason: reason)
+ temporaryTargetGraphData.append(dot)
+ }
+ }
+ if UserDefaultsRepository.graphOtherTreatments.value {
+ updateTemporaryTargetGraph()
+ }
+ }
}
diff --git a/LoopFollow/ViewControllers/MainViewController.swift b/LoopFollow/ViewControllers/MainViewController.swift
index 2421e5a..cd9c626 100644
--- a/LoopFollow/ViewControllers/MainViewController.swift
+++ b/LoopFollow/ViewControllers/MainViewController.swift
@@ -107,6 +107,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
var bolusData: [bolusGraphStruct] = []
var carbData: [carbGraphStruct] = []
var overrideGraphData: [DataStructs.overrideStruct] = []
+ var temporaryTargetGraphData: [DataStructs.temporaryTargetStruct] = []
var predictionData: [ShareGlucoseData] = []
var bgCheckData: [ShareGlucoseData] = []
var suspendGraphData: [DataStructs.timestampOnlyStruct] = []
diff --git a/LoopFollow/helpers/Chart.swift b/LoopFollow/helpers/Chart.swift
index 29fb604..e58851f 100644
--- a/LoopFollow/helpers/Chart.swift
+++ b/LoopFollow/helpers/Chart.swift
@@ -16,6 +16,13 @@ final class OverrideFillFormatter: FillFormatter {
}
}
+final class TemporaryTargetFillFormatter: FillFormatter {
+ func getFillLinePosition(dataSet: Charts.LineChartDataSetProtocol, dataProvider: Charts.LineChartDataProvider) -> CGFloat {
+ return CGFloat(dataSet.entryForIndex(0)!.y)
+ //return 375
+ }
+}
+
final class basalFillFormatter: FillFormatter {
func getFillLinePosition(dataSet: Charts.LineChartDataSetProtocol, dataProvider: Charts.LineChartDataProvider) -> CGFloat {
return 0
diff --git a/LoopFollow/helpers/DataStructs.swift b/LoopFollow/helpers/DataStructs.swift
index 20809b3..1b8f2be 100644
--- a/LoopFollow/helpers/DataStructs.swift
+++ b/LoopFollow/helpers/DataStructs.swift
@@ -48,4 +48,12 @@ class DataStructs {
var sgv: Float
}
+ //Temporary Target Struct
+ struct temporaryTargetStruct: Codable {
+ var date: TimeInterval
+ var endDate: TimeInterval
+ var duration: Double
+ var enteredBy: String
+ var reason: String
+ }
}