-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LOOP-873 Upload Loop glucose and carbohydrate data to Tidepool backend (
#7) - https://tidepool.atlassian.net/browse/LOOP-873 - Upload carbohydrate and glucose data to Tidepool backend - Delete carbohydrate data from Tidepool backend - Add DeletedCarbEntry extension to generate TSelector - Add StoredCarbEntry extension to generate TDatum - Add StoredGlucose extension to generate TDatum - Add default origin for main bundle
- Loading branch information
Darin Krauss
authored
Apr 24, 2020
1 parent
88720e3
commit 752e510
Showing
8 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// DeletedCarbEntry.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 4/1/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import LoopKit | ||
import TidepoolKit | ||
|
||
extension DeletedCarbEntry { | ||
var selector: TDatum.Selector? { | ||
guard let syncIdentifier = syncIdentifier else { | ||
return nil | ||
} | ||
return TDatum.Selector(origin: TDatum.Selector.Origin(id: syncIdentifier)) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// HKUnit.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 3/18/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import HealthKit | ||
|
||
extension HKUnit { | ||
public static let milligramsPerDeciliter: HKUnit = { | ||
return HKUnit.gramUnit(with: .milli).unitDivided(by: .literUnit(with: .deci)) | ||
}() | ||
|
||
public static let millimolesPerLiter: HKUnit = { | ||
return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: .liter()) | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// StoredCarbEntry.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 4/1/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import LoopKit | ||
import TidepoolKit | ||
|
||
extension StoredCarbEntry { | ||
var datum: TDatum? { | ||
guard syncIdentifier != nil else { | ||
return nil | ||
} | ||
return TFoodDatum(time: datumTime, name: datumName, nutrition: datumNutrition).adorn(withOrigin: datumOrigin) | ||
} | ||
|
||
private var datumTime: Date { startDate } | ||
|
||
private var datumName: String? { foodType } | ||
|
||
private var datumNutrition: TFoodDatum.Nutrition { | ||
return TFoodDatum.Nutrition(carbohydrate: datumCarbohydrate, estimatedAbsorptionDuration: datumEstimatedAbsorptionDuration) | ||
} | ||
|
||
private var datumCarbohydrate: TFoodDatum.Nutrition.Carbohydrate { | ||
return TFoodDatum.Nutrition.Carbohydrate(net: quantity.doubleValue(for: .gram()), units: .grams) | ||
} | ||
|
||
private var datumEstimatedAbsorptionDuration: Int? { | ||
guard let absorptionTime = absorptionTime else { | ||
return nil | ||
} | ||
return Int(absorptionTime) | ||
} | ||
|
||
private var datumOrigin: TOrigin? { | ||
guard let syncIdentifier = syncIdentifier else { | ||
return nil | ||
} | ||
if !createdByCurrentApp { | ||
return TOrigin(id: syncIdentifier, name: "com.apple.HealthKit", type: .service) | ||
} | ||
return TOrigin(id: syncIdentifier) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// StoredGlucoseSample.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 4/2/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import LoopKit | ||
import TidepoolKit | ||
|
||
extension StoredGlucoseSample { | ||
var datum: TDatum { | ||
if isDisplayOnly { | ||
return TCalibrationDeviceEventDatum(time: datumTime, value: datumValue, units: datumUnits).adorn(withOrigin: datumOrigin) | ||
} else { | ||
return TCBGDatum(time: datumTime, value: datumValue, units: datumUnits).adorn(withOrigin: datumOrigin) | ||
} | ||
} | ||
|
||
private var datumTime: Date { startDate } | ||
|
||
private var datumValue: Double { quantity.doubleValue(for: .milligramsPerDeciliter) } | ||
|
||
private var datumUnits: TBloodGlucose.Units { .milligramsPerDeciliter } | ||
|
||
private var datumOrigin: TOrigin { | ||
if !provenanceIdentifier.isEmpty && provenanceIdentifier != Bundle.main.bundleIdentifier { | ||
return TOrigin(id: syncIdentifier, name: provenanceIdentifier, type: .application) | ||
} | ||
return TOrigin(id: syncIdentifier) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// TDatum.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 4/3/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import TidepoolKit | ||
|
||
extension TDatum { | ||
func adorn(withOrigin origin: TOrigin?) -> TDatum { | ||
self.origin = origin | ||
return self | ||
} | ||
} | ||
|
||
extension TDatum: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
guard let data = try? encoder.encode(self) else { | ||
return "error: failure to encode datum as data" | ||
} | ||
guard let string = String(data: data, encoding: .utf8) else { | ||
return "error: failure to encode data as string" | ||
} | ||
return string | ||
} | ||
} | ||
|
||
fileprivate let encoder: JSONEncoder = { | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] | ||
encoder.dateEncodingStrategy = .tidepool | ||
return encoder | ||
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// TOrigin.swift | ||
// TidepoolServiceKit | ||
// | ||
// Created by Darin Krauss on 4/1/20. | ||
// Copyright © 2020 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import TidepoolKit | ||
|
||
extension TOrigin { | ||
init(id: String) { | ||
self.init(id: id, name: Bundle.main.bundleIdentifier, version: Bundle.main.semanticVersion, type: .application) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters