@@ -12,21 +12,25 @@ import UIKit
1212extension MainViewController {
1313 func DeviceStatusOpenAPS( formatter: ISO8601DateFormatter , lastDeviceStatus: [ String : AnyObject ] ? , lastLoopRecord: [ String : AnyObject ] ) {
1414
15- if let lastLoopTime = formatter. date ( from: ( lastDeviceStatus ? [ " created_at " ] as! String ) ) ? . timeIntervalSince1970 {
15+ if let lastLoopTime = formatter. date ( from: ( lastDeviceStatus ? [ " created_at " ] as! String ) ) ? . timeIntervalSince1970 {
1616 UserDefaultsRepository . alertLastLoopTime. value = lastLoopTime
17- if UserDefaultsRepository . debugLog. value { self . writeDebugLog ( value: " lastLoopTime: " + String( lastLoopTime) ) }
1817 if lastLoopRecord [ " failureReason " ] != nil {
1918 LoopStatusLabel . text = " X "
2019 latestLoopStatusString = " X "
2120 if UserDefaultsRepository . debugLog. value { self . writeDebugLog ( value: " Loop Failure: X " ) }
2221 } else {
23- if let iobdata = lastLoopRecord [ " iob " ] as? [ String : AnyObject ] {
24- tableData [ 0 ] . value = String ( format : " %.2f " , ( iobdata [ " iob " ] as! Double ) )
25- latestIOB = String ( format : " %.2f " , ( iobdata [ " iob " ] as! Double ) )
22+ var wasEnacted = false
23+ if let enacted = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
24+ wasEnacted = true
2625 }
27- if let cobdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
28- tableData [ 1 ] . value = String ( format: " %.0f " , cobdata [ " COB " ] as! Double )
29- latestCOB = String ( format: " %.0f " , cobdata [ " COB " ] as! Double )
26+
27+ if let iobdata = lastLoopRecord [ " iob " ] as? [ String : AnyObject ] {
28+ tableData [ 0 ] . value = String ( format: " %.2f " , ( iobdata [ " iob " ] as! Double ) )
29+ latestIOB = String ( format: " %.2f " , ( iobdata [ " iob " ] as! Double ) )
30+ }
31+ if let cobdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
32+ tableData [ 1 ] . value = String ( format: " %.0f " , cobdata [ " COB " ] as! Double )
33+ latestCOB = String ( format: " %.0f " , cobdata [ " COB " ] as! Double )
3034 }
3135 if let recbolusdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] ,
3236 let insulinReq = recbolusdata [ " insulinReq " ] as? Double {
@@ -37,15 +41,22 @@ extension MainViewController {
3741 UserDefaultsRepository . deviceRecBolus. value = 0
3842 }
3943
40- if let autosensdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
44+ if let autosensdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
4145 let sens = autosensdata [ " sensitivityRatio " ] as! Double * 100.0
42- tableData [ 11 ] . value = String ( format: " %.0f " , sens) + " % "
46+ tableData [ 11 ] . value = String ( format: " %.0f " , sens) + " % "
47+ }
48+
49+ if let eventualdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] {
50+ if let eventualBGValue = eventualdata [ " eventualBG " ] as? NSNumber {
51+ let eventualBGStringValue = String ( describing: eventualBGValue)
52+ PredictionLabel . text = bgUnits. toDisplayUnits ( eventualBGStringValue)
53+ }
4354 }
4455
4556 var predictioncolor = UIColor . systemGray
4657 PredictionLabel . textColor = predictioncolor
4758 topPredictionBG = UserDefaultsRepository . minBGScale. value
48- if let enactdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] ,
59+ if let enactdata = lastLoopRecord [ " enacted " ] as? [ String : AnyObject ] ,
4960 let predbgdata = enactdata [ " predBGs " ] as? [ String : AnyObject ] {
5061 let predictionTypes : [ ( type: String , colorName: String , dataIndex: Int ) ] = [
5162 ( " ZT " , " ZT " , 12 ) ,
@@ -54,6 +65,9 @@ extension MainViewController {
5465 ( " UAM " , " UAM " , 15 )
5566 ]
5667
68+ var minPredBG = Double . infinity
69+ var maxPredBG = - Double. infinity
70+
5771 for (type, colorName, dataIndex) in predictionTypes {
5872 var predictionData = [ ShareGlucoseData] ( )
5973 if let graphdata = predbgdata [ type] as? [ Double ] {
@@ -62,7 +76,11 @@ extension MainViewController {
6276
6377 for i in 0 ... toLoad {
6478 if i < graphdata. count {
65- let prediction = ShareGlucoseData ( sgv: Int ( round ( graphdata [ i] ) ) , date: predictionTime, direction: " flat " )
79+ let predictionValue = graphdata [ i]
80+ minPredBG = min ( minPredBG, predictionValue)
81+ maxPredBG = max ( maxPredBG, predictionValue)
82+
83+ let prediction = ShareGlucoseData ( sgv: Int ( round ( predictionValue) ) , date: predictionTime, direction: " flat " )
6684 predictionData. append ( prediction)
6785 predictionTime += 300
6886 }
@@ -77,8 +95,34 @@ extension MainViewController {
7795 color: color
7896 )
7997 }
98+
99+ if minPredBG != Double . infinity && maxPredBG != - Double. infinity {
100+ tableData [ 9 ] . value = " \( bgUnits. toDisplayUnits ( String ( minPredBG) ) ) / \( bgUnits. toDisplayUnits ( String ( maxPredBG) ) ) "
101+ } else {
102+ tableData [ 9 ] . value = " N/A "
103+ }
104+ }
105+
106+ if let loopStatus = lastLoopRecord [ " recommendedTempBasal " ] as? [ String : AnyObject ] {
107+ if let tempBasalTime = formatter. date ( from: ( loopStatus [ " timestamp " ] as! String ) ) ? . timeIntervalSince1970 {
108+ var lastBGTime = lastLoopTime
109+ if bgData. count > 0 {
110+ lastBGTime = bgData [ bgData. count - 1 ] . date
111+ }
112+ if tempBasalTime > lastBGTime && !wasEnacted {
113+ LoopStatusLabel . text = " ⏀ "
114+ latestLoopStatusString = " ⏀ "
115+ } else {
116+ LoopStatusLabel . text = " ↻ "
117+ latestLoopStatusString = " ↻ "
118+ }
119+ }
120+ } else {
121+ LoopStatusLabel . text = " ↻ "
122+ latestLoopStatusString = " ↻ "
80123 }
81124 }
125+ evaluateNotLooping ( lastLoopTime: lastLoopTime)
82126 }
83127 }
84128}
0 commit comments