Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Getting insulin dosing data from pump event history
  • Loading branch information
loudnate committed Aug 28, 2016
commit 4753b4833d8821e7d0a9658e101b090f17913d71
8 changes: 4 additions & 4 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
4354003A1C9FB81100D5819C /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DE92501C541832001FFDE1 /* UIColor.swift */; };
43649A631C7A347F00523D7F /* CollectionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43649A621C7A347F00523D7F /* CollectionType.swift */; };
436A0DA51D236A2A00104B24 /* LoopError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 436A0DA41D236A2A00104B24 /* LoopError.swift */; };
436FACEC1D0BA246004E2427 /* SegmentedControlTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 436FACEB1D0BA246004E2427 /* SegmentedControlTableViewCell.swift */; };
436FACEE1D0BA636004E2427 /* InsulinDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 436FACED1D0BA636004E2427 /* InsulinDataSource.swift */; };
43776F901B8022E90074EA36 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43776F8F1B8022E90074EA36 /* AppDelegate.swift */; };
43776F971B8022E90074EA36 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 43776F951B8022E90074EA36 /* Main.storyboard */; };
Expand Down Expand Up @@ -133,6 +132,7 @@
43F41C351D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F41C341D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift */; };
43F41C371D3BF32400C11ED6 /* UIAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F41C361D3BF32400C11ED6 /* UIAlertController.swift */; };
43F4EF1D1BA2A57600526CE1 /* DiagnosticLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F4EF1C1BA2A57600526CE1 /* DiagnosticLogger.swift */; };
43F5173D1D713DB0000FA422 /* RadioSelectionTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F5173C1D713DB0000FA422 /* RadioSelectionTableViewController.swift */; };
43F5C2C91B929C09003EB13D /* HealthKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F5C2C81B929C09003EB13D /* HealthKit.framework */; };
43F5C2DB1B92A5E1003EB13D /* SettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F5C2DA1B92A5E1003EB13D /* SettingsTableViewController.swift */; };
43F78D261C8FC000002152D1 /* DoseMath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F78D251C8FC000002152D1 /* DoseMath.swift */; };
Expand Down Expand Up @@ -270,7 +270,6 @@
435400331C9F878D00D5819C /* SetBolusUserInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetBolusUserInfo.swift; sourceTree = "<group>"; };
43649A621C7A347F00523D7F /* CollectionType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionType.swift; sourceTree = "<group>"; };
436A0DA41D236A2A00104B24 /* LoopError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoopError.swift; sourceTree = "<group>"; };
436FACEB1D0BA246004E2427 /* SegmentedControlTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentedControlTableViewCell.swift; sourceTree = "<group>"; };
436FACED1D0BA636004E2427 /* InsulinDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsulinDataSource.swift; sourceTree = "<group>"; };
43776F8C1B8022E90074EA36 /* Loop.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Loop.app; sourceTree = BUILT_PRODUCTS_DIR; };
43776F8F1B8022E90074EA36 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = AppDelegate.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -355,6 +354,7 @@
43F41C341D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartPointsTouchHighlightLayerViewCache.swift; sourceTree = "<group>"; };
43F41C361D3BF32400C11ED6 /* UIAlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIAlertController.swift; sourceTree = "<group>"; };
43F4EF1C1BA2A57600526CE1 /* DiagnosticLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiagnosticLogger.swift; sourceTree = "<group>"; };
43F5173C1D713DB0000FA422 /* RadioSelectionTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadioSelectionTableViewController.swift; sourceTree = "<group>"; };
43F5C2C81B929C09003EB13D /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; };
43F5C2D41B92A4A6003EB13D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
43F5C2D61B92A4DC003EB13D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -624,6 +624,7 @@
4315D2861CA5CC3B00589052 /* CarbEntryEditTableViewController.swift */,
43DBF0581C93F73800B3C386 /* CarbEntryTableViewController.swift */,
4302F4E21D4EA54200F0FCAF /* InsulinDeliveryTableViewController.swift */,
43F5173C1D713DB0000FA422 /* RadioSelectionTableViewController.swift */,
43F5C2DA1B92A5E1003EB13D /* SettingsTableViewController.swift */,
43E3449E1B9D68E900C85C07 /* StatusTableViewController.swift */,
4302F4E01D4E9C8900F0FCAF /* TextFieldTableViewController.swift */,
Expand All @@ -648,7 +649,6 @@
437CEEBD1CD6E0CB003C8C80 /* LoopCompletionHUDView.swift */,
438DADC71CDE8F8B007697A5 /* LoopStateView.swift */,
437CEEC71CD84CBB003C8C80 /* ReservoirVolumeHUDView.swift */,
436FACEB1D0BA246004E2427 /* SegmentedControlTableViewCell.swift */,
43A5676A1C96155700334FAC /* SwitchTableViewCell.swift */,
434F54621D28DD80002A9274 /* ValidatingIndicatorView.swift */,
);
Expand Down Expand Up @@ -1003,7 +1003,6 @@
43E2D8C81D208D5B004DA55F /* KeychainManager+Loop.swift in Sources */,
4346D1E71C77F5FE00ABAFE3 /* ChartTableViewCell.swift in Sources */,
437CEEE41CDE5C0A003C8C80 /* UIImage.swift in Sources */,
436FACEC1D0BA246004E2427 /* SegmentedControlTableViewCell.swift in Sources */,
43DBF0591C93F73800B3C386 /* CarbEntryTableViewController.swift in Sources */,
43DE925C1C547A20001FFDE1 /* WatchContext.swift in Sources */,
43F41C351D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift in Sources */,
Expand All @@ -1016,6 +1015,7 @@
438849EA1D297CB6003B3F23 /* NightscoutService.swift in Sources */,
437CCADC1D284B830075D2C3 /* ButtonTableViewCell.swift in Sources */,
4315D2871CA5CC3B00589052 /* CarbEntryEditTableViewController.swift in Sources */,
43F5173D1D713DB0000FA422 /* RadioSelectionTableViewController.swift in Sources */,
4331E0781C85302200FBE832 /* CGPoint.swift in Sources */,
43DBF04C1C93B8D700B3C386 /* BolusViewController.swift in Sources */,
4328E0351CFC0AE100E199AA /* WatchDataManager.swift in Sources */,
Expand Down
37 changes: 1 addition & 36 deletions Loop/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,6 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="SegmentedControlTableViewCell" id="qVa-bG-wlH" customClass="SegmentedControlTableViewCell" customModule="Loop" customModuleProvider="target">
<rect key="frame" x="0.0" y="201.5" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="qVa-bG-wlH" id="5qS-48-yNM">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="249" verticalHuggingPriority="251" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ZgD-7x-5Wg">
<rect key="frame" x="15" y="8" width="493" height="27"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="LrK-Yi-kdn">
<rect key="frame" x="516" y="7" width="69" height="29"/>
<segments>
<segment title="On"/>
<segment title="Off"/>
</segments>
</segmentedControl>
</subviews>
<constraints>
<constraint firstItem="ZgD-7x-5Wg" firstAttribute="bottom" secondItem="5qS-48-yNM" secondAttribute="bottomMargin" priority="750" id="Fik-ru-b1R"/>
<constraint firstItem="LrK-Yi-kdn" firstAttribute="leading" secondItem="ZgD-7x-5Wg" secondAttribute="trailing" constant="8" symbolic="YES" id="XJH-Sy-MdY"/>
<constraint firstItem="LrK-Yi-kdn" firstAttribute="centerY" secondItem="5qS-48-yNM" secondAttribute="centerY" id="qod-JV-8ip"/>
<constraint firstItem="ZgD-7x-5Wg" firstAttribute="top" secondItem="5qS-48-yNM" secondAttribute="topMargin" id="rxe-38-gFO"/>
<constraint firstItem="ZgD-7x-5Wg" firstAttribute="leading" secondItem="5qS-48-yNM" secondAttribute="leadingMargin" constant="7" id="uwV-Bp-8gq"/>
<constraint firstAttribute="trailingMargin" secondItem="LrK-Yi-kdn" secondAttribute="trailing" constant="7" id="zMJ-bc-2B1"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="segmentedControl" destination="LrK-Yi-kdn" id="e99-v4-aTF"/>
<outlet property="titleLabel" destination="ZgD-7x-5Wg" id="iDE-Jk-fkZ"/>
</connections>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="4cb-Yq-02o" id="KZ7-dm-dst"/>
Expand Down Expand Up @@ -245,7 +210,7 @@
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="399" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="01f-Du-MVi">
<rect key="frame" x="0.0" y="5" width="120" height="32.5"/>
<fontDescription key="fontDescription" type="system" weight="light" pointSize="27"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
Expand Down
33 changes: 9 additions & 24 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,6 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter

var latestPumpStatusFromMySentry: MySentryPumpStatusMessageBody?

// TODO: Expose this on DoseStore
var latestReservoirValue: ReservoirValue? {
didSet {
if let oldValue = oldValue, newValue = latestReservoirValue {
latestReservoirVolumeDrop = oldValue.unitVolume - newValue.unitVolume
}
}
}

// The last change in reservoir volume. Useful in detecting rewind events.
// TODO: Expose this on DoseStore
private var latestReservoirVolumeDrop: Double = 0

/**
Handles receiving a MySentry status message, which are only posted by MM x23 pumps.

Expand Down Expand Up @@ -242,15 +229,13 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
- parameter timeLeft: The approximate time before the reservoir is empty
*/
private func updateReservoirVolume(units: Double, atDate date: NSDate, withTimeLeft timeLeft: NSTimeInterval?) {
doseStore.addReservoirValue(units, atDate: date) { (newValue, previousValue, error) -> Void in
doseStore.addReservoirValue(units, atDate: date) { (newValue, previousValue, areStoredValuesContinuous, error) -> Void in
if let error = error {
self.logger.addError(error, fromSource: "DoseStore")
return
}

self.latestReservoirValue = newValue

if self.preferredInsulinDataSource == .PumpHistory {
if self.preferredInsulinDataSource == .pumpHistory || !areStoredValuesContinuous {
self.fetchPumpHistory()
} else {
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.PumpStatusUpdatedNotification, object: self)
Expand Down Expand Up @@ -322,7 +307,8 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
let pumpStatusAgeTolerance = rileyLinkManager.idleListeningEnabled ? NSTimeInterval(minutes: 11) : NSTimeInterval(minutes: 4)

// If we don't yet have pump status, or it's old, poll for it.
if latestReservoirValue == nil || latestReservoirValue!.startDate.timeIntervalSinceNow <= -pumpStatusAgeTolerance {
if doseStore.lastReservoirValue == nil ||
doseStore.lastReservoirValue!.startDate.timeIntervalSinceNow <= -pumpStatusAgeTolerance {
readPumpData { (result) in
let nsPumpStatus: NightscoutUploadKit.PumpStatus?
switch result {
Expand Down Expand Up @@ -404,19 +390,18 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
}

// If we don't have recent pump data, or the pump was recently rewound, read new pump data before bolusing.
if latestReservoirValue == nil ||
latestReservoirVolumeDrop < 0 ||
latestReservoirValue!.startDate.timeIntervalSinceNow <= NSTimeInterval(minutes: -5)
if doseStore.lastReservoirValue == nil ||
doseStore.lastReservoirVolumeDrop < 0 ||
doseStore.lastReservoirValue!.startDate.timeIntervalSinceNow <= NSTimeInterval(minutes: -5)
{
readPumpData { (result) in
switch result {
case .Success(let (status, date)):
self.doseStore.addReservoirValue(status.reservoir, atDate: date) { (newValue, _, error) in
self.doseStore.addReservoirValue(status.reservoir, atDate: date) { (newValue, _, _, error) in
if let error = error {
self.logger.addError(error, fromSource: "Bolus")
completion(error: error)
} else {
self.latestReservoirValue = newValue
setBolus()
}
}
Expand Down Expand Up @@ -685,7 +670,7 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
}

/// The user's preferred method of fetching insulin data from the pump
var preferredInsulinDataSource = NSUserDefaults.standardUserDefaults().preferredInsulinDataSource ?? .PumpHistory {
var preferredInsulinDataSource = NSUserDefaults.standardUserDefaults().preferredInsulinDataSource ?? .pumpHistory {
didSet {
NSUserDefaults.standardUserDefaults().preferredInsulinDataSource = preferredInsulinDataSource
}
Expand Down
5 changes: 2 additions & 3 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ final class LoopDataManager {
private func updatePredictedGlucoseAndRecommendedBasal() throws {
guard let
glucose = self.deviceDataManager.glucoseStore?.latestGlucose,
pumpStatusDate = self.deviceDataManager.latestReservoirValue?.startDate
else
{
pumpStatusDate = self.deviceDataManager.doseStore.lastReservoirValue?.startDate
else {
self.predictedGlucose = nil
throw LoopError.MissingDataError("Cannot predict glucose due to missing input data")
}
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/WatchDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate {
}

let glucose = deviceDataManager.glucoseStore?.latestGlucose
let reservoir = deviceDataManager.latestReservoirValue
let reservoir = deviceDataManager.doseStore.lastReservoirValue

deviceDataManager.loopManager.getLoopStatus { (predictedGlucose, recommendedTempBasal, lastTempBasal, lastLoopCompleted, insulinOnBoard, error) in

Expand Down
17 changes: 14 additions & 3 deletions Loop/Models/InsulinDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//

import Foundation

enum InsulinDataSource: Int {
case PumpHistory
case Reservoir

enum InsulinDataSource: Int, CustomStringConvertible {
case pumpHistory = 0
case reservoir

var description: String {
switch self {
case .pumpHistory:
return NSLocalizedString("Event History", comment: "Describing the pump history insulin data source")
case .reservoir:
return NSLocalizedString("Reservoir", comment: "Describing the reservoir insulin data source")
}
}
}
Loading