|
|
@@ -5,10 +5,10 @@ import LoopKit
|
|
|
import LoopKitUI
|
|
|
import Swinject
|
|
|
|
|
|
-protocol TidePoolManager {
|
|
|
- func addTidePoolService(service: Service)
|
|
|
- func getTidePoolServiceUI() -> ServiceUI?
|
|
|
- func getTidePoolPluginHost() -> PluginHost?
|
|
|
+protocol TidepoolManager {
|
|
|
+ func addTidepoolService(service: Service)
|
|
|
+ func getTidepoolServiceUI() -> ServiceUI?
|
|
|
+ func getTidepoolPluginHost() -> PluginHost?
|
|
|
func deleteCarbs(at date: Date, isFPU: Bool?, fpuID: String?, syncID: String)
|
|
|
func deleteInsulin(at date: Date)
|
|
|
// func uploadStatus()
|
|
|
@@ -18,7 +18,7 @@ protocol TidePoolManager {
|
|
|
// func uploadProfileAndSettings(_: Bool)
|
|
|
}
|
|
|
|
|
|
-final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
+final class BaseTidepoolManager: TidepoolManager, Injectable {
|
|
|
@Injected() private var broadcaster: Broadcaster!
|
|
|
@Injected() private var pluginManager: PluginManager!
|
|
|
@Injected() private var glucoseStorage: GlucoseStorage!
|
|
|
@@ -27,53 +27,53 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
@Injected() private var pumpHistoryStorage: PumpHistoryStorage!
|
|
|
|
|
|
private let processQueue = DispatchQueue(label: "BaseNetworkManager.processQueue")
|
|
|
- private var tidePoolService: RemoteDataService? {
|
|
|
+ private var tidepoolService: RemoteDataService? {
|
|
|
didSet {
|
|
|
- if let tidePoolService = tidePoolService {
|
|
|
- rawTidePoolManager = tidePoolService.rawValue
|
|
|
+ if let tidepoolService = tidepoolService {
|
|
|
+ rawTidepoolManager = tidepoolService.rawValue
|
|
|
} else {
|
|
|
- rawTidePoolManager = nil
|
|
|
+ rawTidepoolManager = nil
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @PersistedProperty(key: "TidePoolState") var rawTidePoolManager: Service.RawValue?
|
|
|
+ @PersistedProperty(key: "TidepoolState") var rawTidepoolManager: Service.RawValue?
|
|
|
|
|
|
init(resolver: Resolver) {
|
|
|
injectServices(resolver)
|
|
|
- loadTidePoolManager()
|
|
|
+ loadTidepoolManager()
|
|
|
subscribe()
|
|
|
}
|
|
|
|
|
|
- /// load the TidePool Remote Data Service if available
|
|
|
- fileprivate func loadTidePoolManager() {
|
|
|
- if let rawTidePoolManager = rawTidePoolManager {
|
|
|
- tidePoolService = tidePoolServiceFromRaw(rawTidePoolManager)
|
|
|
- tidePoolService?.serviceDelegate = self
|
|
|
- tidePoolService?.stateDelegate = self
|
|
|
+ /// load the Tidepool Remote Data Service if available
|
|
|
+ fileprivate func loadTidepoolManager() {
|
|
|
+ if let rawTidepoolManager = rawTidepoolManager {
|
|
|
+ tidepoolService = tidepoolServiceFromRaw(rawTidepoolManager)
|
|
|
+ tidepoolService?.serviceDelegate = self
|
|
|
+ tidepoolService?.stateDelegate = self
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// allows to acces to tidePoolService as a simple ServiceUI
|
|
|
- func getTidePoolServiceUI() -> ServiceUI? {
|
|
|
- if let tidePoolService = self.tidePoolService {
|
|
|
- return tidePoolService as! any ServiceUI as ServiceUI
|
|
|
+ /// allows access to tidepoolService as a simple ServiceUI
|
|
|
+ func getTidepoolServiceUI() -> ServiceUI? {
|
|
|
+ if let tidepoolService = self.tidepoolService {
|
|
|
+ return tidepoolService as! any ServiceUI as ServiceUI
|
|
|
} else {
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// get the pluginHost of TidePool
|
|
|
- func getTidePoolPluginHost() -> PluginHost? {
|
|
|
+ /// get the pluginHost of Tidepool
|
|
|
+ func getTidepoolPluginHost() -> PluginHost? {
|
|
|
self as PluginHost
|
|
|
}
|
|
|
|
|
|
- func addTidePoolService(service: Service) {
|
|
|
- tidePoolService = service as! any RemoteDataService as RemoteDataService
|
|
|
+ func addTidepoolService(service: Service) {
|
|
|
+ tidepoolService = service as! any RemoteDataService as RemoteDataService
|
|
|
}
|
|
|
|
|
|
- /// load the TidePool Remote Data Service from raw storage
|
|
|
- private func tidePoolServiceFromRaw(_ rawValue: [String: Any]) -> RemoteDataService? {
|
|
|
+ /// load the Tidepool Remote Data Service from raw storage
|
|
|
+ private func tidepoolServiceFromRaw(_ rawValue: [String: Any]) -> RemoteDataService? {
|
|
|
guard let rawState = rawValue["state"] as? Service.RawStateValue,
|
|
|
let serviceType = pluginManager.getServiceTypeByIdentifier("TidepoolService")
|
|
|
else {
|
|
|
@@ -97,15 +97,15 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
func uploadCarbs() {
|
|
|
let carbs: [CarbsEntry] = carbsStorage.recent()
|
|
|
|
|
|
- guard !carbs.isEmpty, let tidePoolService = self.tidePoolService else { return }
|
|
|
+ guard !carbs.isEmpty, let tidepoolService = self.tidepoolService else { return }
|
|
|
|
|
|
processQueue.async {
|
|
|
- carbs.chunks(ofCount: tidePoolService.carbDataLimit ?? 100).forEach { chunk in
|
|
|
+ carbs.chunks(ofCount: tidepoolService.carbDataLimit ?? 100).forEach { chunk in
|
|
|
|
|
|
let syncCarb: [SyncCarbObject] = Array(chunk).map {
|
|
|
$0.convertSyncCarb()
|
|
|
}
|
|
|
- tidePoolService.uploadCarbData(created: syncCarb, updated: [], deleted: []) { result in
|
|
|
+ tidepoolService.uploadCarbData(created: syncCarb, updated: [], deleted: []) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing carbs data: \(String(describing: error))")
|
|
|
@@ -118,7 +118,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
}
|
|
|
|
|
|
func deleteCarbs(at date: Date, isFPU: Bool?, fpuID: String?, syncID _: String) {
|
|
|
- guard let tidePoolService = self.tidePoolService else { return }
|
|
|
+ guard let tidepoolService = self.tidepoolService else { return }
|
|
|
|
|
|
processQueue.async {
|
|
|
var carbsToDelete: [CarbsEntry] = []
|
|
|
@@ -135,7 +135,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
d.convertSyncCarb(operation: .delete)
|
|
|
}
|
|
|
|
|
|
- tidePoolService.uploadCarbData(created: [], updated: [], deleted: syncCarb) { result in
|
|
|
+ tidepoolService.uploadCarbData(created: [], updated: [], deleted: syncCarb) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing carbs data: \(String(describing: error))")
|
|
|
@@ -149,7 +149,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
func deleteInsulin(at d: Date) {
|
|
|
let allValues = storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self) ?? []
|
|
|
|
|
|
- guard !allValues.isEmpty, let tidePoolService = self.tidePoolService else { return }
|
|
|
+ guard !allValues.isEmpty, let tidepoolService = self.tidepoolService else { return }
|
|
|
|
|
|
var doseDataToDelete: [DoseEntry] = []
|
|
|
|
|
|
@@ -166,7 +166,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
))
|
|
|
|
|
|
processQueue.async {
|
|
|
- tidePoolService.uploadDoseData(created: [], deleted: doseDataToDelete) { result in
|
|
|
+ tidepoolService.uploadDoseData(created: [], deleted: doseDataToDelete) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing Dose delete data: \(String(describing: error))")
|
|
|
@@ -179,7 +179,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
|
|
|
func uploadDose() {
|
|
|
let events = pumpHistoryStorage.recent()
|
|
|
- guard !events.isEmpty, let tidePoolService = self.tidePoolService else { return }
|
|
|
+ guard !events.isEmpty, let tidepoolService = self.tidepoolService else { return }
|
|
|
|
|
|
let eventsBasal = events.filter { $0.type == .tempBasal || $0.type == .tempBasalDuration }
|
|
|
.sorted { $0.timestamp < $1.timestamp }
|
|
|
@@ -297,7 +297,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
}
|
|
|
|
|
|
processQueue.async {
|
|
|
- tidePoolService.uploadDoseData(created: doseDataBasal + boluses, deleted: []) { result in
|
|
|
+ tidepoolService.uploadDoseData(created: doseDataBasal + boluses, deleted: []) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing Dose data: \(String(describing: error))")
|
|
|
@@ -306,7 +306,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- tidePoolService.uploadPumpEventData(pumpEvents) { result in
|
|
|
+ tidepoolService.uploadPumpEventData(pumpEvents) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing Pump Event data: \(String(describing: error))")
|
|
|
@@ -320,19 +320,19 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
func uploadGlucose(device: HKDevice?) {
|
|
|
let glucose: [BloodGlucose] = glucoseStorage.recent()
|
|
|
|
|
|
- guard !glucose.isEmpty, let tidePoolService = self.tidePoolService else { return }
|
|
|
+ guard !glucose.isEmpty, let tidepoolService = self.tidepoolService else { return }
|
|
|
|
|
|
let glucoseWithoutCorrectID = glucose.filter { UUID(uuidString: $0._id) != nil }
|
|
|
|
|
|
processQueue.async {
|
|
|
- glucoseWithoutCorrectID.chunks(ofCount: tidePoolService.glucoseDataLimit ?? 100)
|
|
|
+ glucoseWithoutCorrectID.chunks(ofCount: tidepoolService.glucoseDataLimit ?? 100)
|
|
|
.forEach { chunk in
|
|
|
// all glucose attached with the current device ;-(
|
|
|
|
|
|
let chunkStoreGlucose = Array(chunk).map {
|
|
|
$0.convertStoredGlucoseSample(device: device)
|
|
|
}
|
|
|
- tidePoolService.uploadGlucoseData(chunkStoreGlucose) { result in
|
|
|
+ tidepoolService.uploadGlucoseData(chunkStoreGlucose) { result in
|
|
|
switch result {
|
|
|
case let .failure(error):
|
|
|
debug(.nightscout, "Error synchronizing glucose data: \(String(describing: error))")
|
|
|
@@ -345,7 +345,7 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// force to uploads all data in TidePool Service
|
|
|
+ /// force to uploads all data in Tidepool Service
|
|
|
func forceUploadData(device: HKDevice?) {
|
|
|
uploadDose()
|
|
|
uploadCarbs()
|
|
|
@@ -353,23 +353,23 @@ final class BaseTidePoolManager: TidePoolManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension BaseTidePoolManager: PumpHistoryObserver {
|
|
|
+extension BaseTidepoolManager: PumpHistoryObserver {
|
|
|
func pumpHistoryDidUpdate(_: [PumpHistoryEvent]) {
|
|
|
uploadDose()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension BaseTidePoolManager: CarbsObserver {
|
|
|
+extension BaseTidepoolManager: CarbsObserver {
|
|
|
func carbsDidUpdate(_: [CarbsEntry]) {
|
|
|
uploadCarbs()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension BaseTidePoolManager: TempTargetsObserver {
|
|
|
+extension BaseTidepoolManager: TempTargetsObserver {
|
|
|
func tempTargetsDidUpdate(_: [TempTarget]) {}
|
|
|
}
|
|
|
|
|
|
-extension BaseTidePoolManager: ServiceDelegate {
|
|
|
+extension BaseTidepoolManager: ServiceDelegate {
|
|
|
var hostIdentifier: String {
|
|
|
"com.loopkit.Loop" // To check
|
|
|
}
|
|
|
@@ -404,11 +404,11 @@ extension BaseTidePoolManager: ServiceDelegate {
|
|
|
func deliverRemoteBolus(amountInUnits _: Double) async throws {}
|
|
|
}
|
|
|
|
|
|
-extension BaseTidePoolManager: StatefulPluggableDelegate {
|
|
|
+extension BaseTidepoolManager: StatefulPluggableDelegate {
|
|
|
func pluginDidUpdateState(_: LoopKit.StatefulPluggable) {}
|
|
|
|
|
|
func pluginWantsDeletion(_: LoopKit.StatefulPluggable) {
|
|
|
- tidePoolService = nil
|
|
|
+ tidepoolService = nil
|
|
|
}
|
|
|
}
|
|
|
|