Parcourir la source

Fix upload and import functions (#379)

* Fix offset() function for importSettings so that it calculates second properly
Fix uploadProfile so that it uploads seconds ( we store everything as minutes )

* If uploadProfile fails, output to the log file which retrieve call is
failing, as that that should never happen, but points to a missing .json
file that needs to be regenerated.

This let's you know which one, and the 'fix' is to go into Settings and
save the 'file' pointed to in the log file.

* Refactor

---------

Co-authored-by: Jon Mårtensson <jon.m@live.se>
Marc G. Fournier il y a 2 ans
Parent
commit
fdf2d9c196

+ 5 - 5
FreeAPS/Sources/Modules/NightscoutConfig/NightscoutConfigStateModel.swift

@@ -174,7 +174,7 @@ extension NightscoutConfig {
                                 }
                                 }
                                 return CarbRatioEntry(
                                 return CarbRatioEntry(
                                     start: carbratio.time,
                                     start: carbratio.time,
-                                    offset: (carbratio.timeAsSeconds ?? self.offset(carbratio.time)) / 60,
+                                    offset: self.offset(carbratio.time) / 60,
                                     ratio: carbratio.value
                                     ratio: carbratio.value
                                 ) }
                                 ) }
                         let carbratiosProfile = CarbRatios(units: CarbUnit.grams, schedule: carbratios)
                         let carbratiosProfile = CarbRatios(units: CarbUnit.grams, schedule: carbratios)
@@ -195,7 +195,7 @@ extension NightscoutConfig {
                                 }
                                 }
                                 return BasalProfileEntry(
                                 return BasalProfileEntry(
                                     start: basal.time,
                                     start: basal.time,
-                                    minutes: (basal.timeAsSeconds ?? self.offset(basal.time)) / 60,
+                                    minutes: self.offset(basal.time) / 60,
                                     rate: basal.value
                                     rate: basal.value
                                 ) }
                                 ) }
                         // DASH pumps can have 0U/h basal rates but don't import if total basals (24 hours) amount to 0 U.
                         // DASH pumps can have 0U/h basal rates but don't import if total basals (24 hours) amount to 0 U.
@@ -213,7 +213,7 @@ extension NightscoutConfig {
                         let sensitivities = fetchedProfile.sens.map { sensitivity -> InsulinSensitivityEntry in
                         let sensitivities = fetchedProfile.sens.map { sensitivity -> InsulinSensitivityEntry in
                             InsulinSensitivityEntry(
                             InsulinSensitivityEntry(
                                 sensitivity: self.units == .mmolL ? sensitivity.value : sensitivity.value.asMgdL,
                                 sensitivity: self.units == .mmolL ? sensitivity.value : sensitivity.value.asMgdL,
-                                offset: (sensitivity.timeAsSeconds ?? self.offset(sensitivity.time)) / 60,
+                                offset: self.offset(sensitivity.time) / 60,
                                 start: sensitivity.time
                                 start: sensitivity.time
                             )
                             )
                         }
                         }
@@ -236,7 +236,7 @@ extension NightscoutConfig {
                                     low: self.units == .mmolL ? target.value : target.value.asMgdL,
                                     low: self.units == .mmolL ? target.value : target.value.asMgdL,
                                     high: self.units == .mmolL ? target.value : target.value.asMgdL,
                                     high: self.units == .mmolL ? target.value : target.value.asMgdL,
                                     start: target.time,
                                     start: target.time,
-                                    offset: (target.timeAsSeconds ?? self.offset(target.time)) / 60
+                                    offset: self.offset(target.time) / 60
                                 ) }
                                 ) }
                         let targetsProfile = BGTargets(
                         let targetsProfile = BGTargets(
                             units: self.units,
                             units: self.units,
@@ -308,7 +308,7 @@ extension NightscoutConfig {
         func offset(_ string: String) -> Int {
         func offset(_ string: String) -> Int {
             let hours = Int(string.prefix(2)) ?? 0
             let hours = Int(string.prefix(2)) ?? 0
             let minutes = Int(string.suffix(2)) ?? 0
             let minutes = Int(string.suffix(2)) ?? 0
-            return hours * 60 + minutes * 60
+            return ((hours * 60) + minutes) * 60
         }
         }
 
 
         func saveError(_ string: String) {
         func saveError(_ string: String) {

+ 26 - 15
FreeAPS/Sources/Services/Network/NightscoutManager.swift

@@ -482,15 +482,28 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
     }
     }
 
 
     func uploadProfileAndSettings(_ force: Bool) {
     func uploadProfileAndSettings(_ force: Bool) {
-        // These should be modified anyways and not the defaults
-        guard let sensitivities = storage.retrieve(OpenAPS.Settings.insulinSensitivities, as: InsulinSensitivities.self),
-              let basalProfile = storage.retrieve(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self),
-              let carbRatios = storage.retrieve(OpenAPS.Settings.carbRatios, as: CarbRatios.self),
-              let targets = storage.retrieve(OpenAPS.Settings.bgTargets, as: BGTargets.self),
-              let preferences = storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self),
-              let settings = storage.retrieve(OpenAPS.FreeAPS.settings, as: FreeAPSSettings.self)
-        else {
-            debug(.nightscout, "NightscoutManager uploadProfile Not all settings found to build profile!")
+        guard let sensitivities = storage.retrieve(OpenAPS.Settings.insulinSensitivities, as: InsulinSensitivities.self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading insulinSensitivities")
+            return
+        }
+        guard let settings = storage.retrieve(OpenAPS.FreeAPS.settings, as: FreeAPSSettings.self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading settings")
+            return
+        }
+        guard let preferences = storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading preferences")
+            return
+        }
+        guard let targets = storage.retrieve(OpenAPS.Settings.bgTargets, as: BGTargets.self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading bgTargets")
+            return
+        }
+        guard let carbRatios = storage.retrieve(OpenAPS.Settings.carbRatios, as: CarbRatios.self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading carbRatios")
+            return
+        }
+        guard let basalProfile = storage.retrieve(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self) else {
+            debug(.nightscout, "NightscoutManager uploadProfile: error loading basalProfile")
             return
             return
         }
         }
 
 
@@ -498,32 +511,30 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
             NightscoutTimevalue(
             NightscoutTimevalue(
                 time: String(item.start.prefix(5)),
                 time: String(item.start.prefix(5)),
                 value: item.sensitivity,
                 value: item.sensitivity,
-                timeAsSeconds: item.offset
+                timeAsSeconds: item.offset * 60
             )
             )
         }
         }
-
         let target_low = targets.targets.map { item -> NightscoutTimevalue in
         let target_low = targets.targets.map { item -> NightscoutTimevalue in
             NightscoutTimevalue(
             NightscoutTimevalue(
                 time: String(item.start.prefix(5)),
                 time: String(item.start.prefix(5)),
                 value: item.low,
                 value: item.low,
-                timeAsSeconds: item.offset
+                timeAsSeconds: item.offset * 60
             )
             )
         }
         }
         let target_high = targets.targets.map { item -> NightscoutTimevalue in
         let target_high = targets.targets.map { item -> NightscoutTimevalue in
             NightscoutTimevalue(
             NightscoutTimevalue(
                 time: String(item.start.prefix(5)),
                 time: String(item.start.prefix(5)),
                 value: item.high,
                 value: item.high,
-                timeAsSeconds: item.offset
+                timeAsSeconds: item.offset * 60
             )
             )
         }
         }
         let cr = carbRatios.schedule.map { item -> NightscoutTimevalue in
         let cr = carbRatios.schedule.map { item -> NightscoutTimevalue in
             NightscoutTimevalue(
             NightscoutTimevalue(
                 time: String(item.start.prefix(5)),
                 time: String(item.start.prefix(5)),
                 value: item.ratio,
                 value: item.ratio,
-                timeAsSeconds: item.offset
+                timeAsSeconds: item.offset * 60
             )
             )
         }
         }
-
         let basal = basalProfile.map { item -> NightscoutTimevalue in
         let basal = basalProfile.map { item -> NightscoutTimevalue in
             NightscoutTimevalue(
             NightscoutTimevalue(
                 time: String(item.start.prefix(5)),
                 time: String(item.start.prefix(5)),