Explorar o código

Adding mmol/l.
Test interval (not working)
Change name

Jon Mårtensson %!s(int64=3) %!d(string=hai) anos
pai
achega
ab4726a6f0

+ 2 - 2
Config.xcconfig

@@ -1,7 +1,7 @@
 APP_DISPLAY_NAME = FreeAPS X
-APP_VERSION = 0.2.9
+APP_VERSION = 0.2.9.1
 APP_BUILD_NUMBER = 1
-BRANCH = DailyStats
+BRANCH = DailyStats test branch
 DEVELOPER_TEAM = ##TEAM_ID##
 BUNDLE_IDENTIFIER = ru.artpancreas.$(DEVELOPMENT_TEAM).FreeAPS
 APP_GROUP_ID = group.com.$(DEVELOPMENT_TEAM).loopkit.LoopGroup

+ 29 - 15
FreeAPS/Sources/APS/APSManager.swift

@@ -759,8 +759,7 @@ final class BaseAPSManager: APSManager, Injectable {
         let branch = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String
         let pump_ = pumpManager?.localizedTitle ?? ""
         let cgm = settingsManager.settings.cgm
-        let file = OpenAPS.Monitor.dailyStats
-        let date_ = Date()
+        var file = OpenAPS.Monitor.dailyStats
         var iPa: Decimal = 75
         if preferences.useCustomPeakTime {
             iPa = preferences.insulinPeakTime
@@ -781,6 +780,7 @@ final class BaseAPSManager: APSManager, Injectable {
         var totalDataAvg: Decimal = 0
         let arraysInjson = stats?.count ?? 0
         // let end = arraysInjson - 1
+        print("arraysInjson: \(arraysInjson)")
         var i = 0
 
         if arraysInjson > 1 {
@@ -800,7 +800,7 @@ final class BaseAPSManager: APSManager, Injectable {
             }
         }
 
-        // HbA1c estimation (%, mmol/mol
+        // HbA1c estimation (%, mmol/mol)
         let NGSPa1CStatisticValue = (46.7 + tir().averageGlucose) / 28.7 // NGSP (%)
         let IFCCa1CStatisticValue = 10.929 *
             (NGSPa1CStatisticValue - 2.152) // IFCC (mmol/mol)  A1C(mmol/mol) = 10.929 * (A1C(%) - 2.15)
@@ -836,14 +836,16 @@ final class BaseAPSManager: APSManager, Injectable {
         }
 
         let HbA1c_string =
-            "Estimated HbA1c (1day): \(roundDecimal(IFCCa1CStatisticValue, 1)) mmol/mol / \(roundDecimal(NGSPa1CStatisticValue, 1)) %. Average Blood Glucose: \(tir().averageGlucose) mg/dl / \(roundDecimal(tir().averageGlucose * 0.0555, 1)) mmol/l." +
+            "Estimated HbA1c (%): \(roundDecimal(NGSPa1CStatisticValue, 1)). Estimated HbA1c (mmol/mol): \(roundDecimal(IFCCa1CStatisticValue, 1))." +
             string7Days + string30Days + stringTotal
 
         let tirString =
-            " \(tir().TIR) %. Time with Hypoglucemia: \(tir().hypos) % (> 10 mmol/l / 180 mg/dl). Time with Hyperglucemia: \(tir().hypers) % (< 4 mmol/l / 72 mg/dl)."
+            "\(tir().TIR) %. Time with Hypoglucemia: \(tir().hypos) % (< 4 mmol/l or 72 mg/dl). Time with Hyperglucemia: \(tir().hypers) % (> 10 mmol/l or 180 mg/dl)."
+
+        let averageBG = tir().averageGlucose
 
         let dailystat = DailyStats(
-            date: date_,
+            date: Date(),
             FAX_Build_Version: version,
             FAX_Build_Number: build ?? "1",
             FAX_Branch: branch ?? "N/A",
@@ -857,7 +859,8 @@ final class BaseAPSManager: APSManager, Injectable {
             TDD: currentTDD ?? 0,
             Carbs_24h: carbTotal,
             TIR: tirString,
-            BG_daily_Average_mg_dl: tir().averageGlucose,
+            BG_daily_Average_mg_dl: averageBG,
+            BG_daily_Average_mmol_l: roundDecimal(averageBG * 0.0555, 2),
             HbA1c: HbA1c_string,
             id: UUID().uuidString
         )
@@ -874,21 +877,32 @@ final class BaseAPSManager: APSManager, Injectable {
         let now = Date()
         let calender = Calendar.current
 
+        file = OpenAPS.Monitor.dailyStats
+
+        let stats_ = storage.retrieve(OpenAPS.Monitor.dailyStats, as: [DailyStats].self)
+
+        let arraysInjson_ = stats_?.count ?? 0
+        var time_ = Date()
+        var timePlus = Date()
+
+        if arraysInjson_ > 0 {
+            time_ = stats_?[0].date ?? Date()
+            timePlus = time_.addingTimeInterval(20.hours.timeInterval)
+        }
+
         // If current local time is 23:41 or later
-        if calender.component(.hour, from: now) > 22,
-           calender.component(.minute, from: now) > 40
+        if calender.component(.hour, from: now) == 0,
+           calender.component(.minute, from: now) > 0
         {
-            if isJSONempty {
+            if isJSONempty || arraysInjson == 0 {
                 storage.save(dailystat, as: file)
-            } else {
+            } else if now > timePlus {
                 storage.transaction { storage in
                     storage.append(dailystat, to: file, uniqBy: \.id)
                     newEntries = storage.retrieve(file, as: [DailyStats].self)?
-                        .filter {
-                            $0.date.addingTimeInterval(1.days.timeInterval) < Date() && $0.date
-                                .addingTimeInterval(120.days.timeInterval) > Date() }
+                        .filter { $0.date.addingTimeInterval(120.days.timeInterval) > Date() }
                         .sorted { $0.date > $1.date } ?? []
-                    storage.save(Array(newEntries), as: file)
+                    storage.save(newEntries, as: OpenAPS.Monitor.dailyStats)
                 }
             }
         }

+ 1 - 1
FreeAPS/Sources/APS/OpenAPS/Constants.swift

@@ -57,7 +57,7 @@ extension OpenAPS {
         static let tdd = "monitor/tdd.json"
         static let tdd_averages = "monitor/tdd_averages.json"
         static let alertHistory = "monitor/alerthistory.json"
-        static let dailyStats = "monitor/daily_stats.json"
+        static let dailyStats = "monitor/dailyStats.json"
     }
 
     enum Enact {

+ 4 - 0
FreeAPS/Sources/Models/DailyStats.swift

@@ -16,6 +16,7 @@ struct DailyStats: JSON, Equatable {
     var Carbs_24h: Decimal
     var TIR: String
     var BG_daily_Average_mg_dl: Decimal
+    var BG_daily_Average_mmol_l: Decimal
     var HbA1c: String
     var id: String
 
@@ -35,6 +36,7 @@ struct DailyStats: JSON, Equatable {
         Carbs_24h: Decimal,
         TIR: String,
         BG_daily_Average_mg_dl: Decimal,
+        BG_daily_Average_mmol_l: Decimal,
         HbA1c: String,
         id: String
     ) {
@@ -53,6 +55,7 @@ struct DailyStats: JSON, Equatable {
         self.Carbs_24h = Carbs_24h
         self.TIR = TIR
         self.BG_daily_Average_mg_dl = BG_daily_Average_mg_dl
+        self.BG_daily_Average_mmol_l = BG_daily_Average_mmol_l
         self.HbA1c = HbA1c
         self.id = id
     }
@@ -75,6 +78,7 @@ extension DailyStats {
         case Carbs_24h
         case TIR
         case BG_daily_Average_mg_dl
+        case BG_daily_Average_mmol_l
         case HbA1c
         case id
     }