Parcourir la source

Debug helpers

Jonas Björkert il y a 1 an
Parent
commit
9990e12afe

+ 4 - 0
LoopFollow.xcodeproj/project.pbxproj

@@ -76,6 +76,7 @@
 		DD58171A2D299EF80041FB98 /* DexcomHeartbeatBluetoothDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5817192D299EF40041FB98 /* DexcomHeartbeatBluetoothDevice.swift */; };
 		DD58171C2D299F940041FB98 /* BluetoothDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD58171B2D299F8D0041FB98 /* BluetoothDevice.swift */; };
 		DD58171E2D299FCA0041FB98 /* BluetoothDeviceDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD58171D2D299FC50041FB98 /* BluetoothDeviceDelegate.swift */; };
+		DD5DA27C2DC930D6003D44FC /* GlucoseValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5DA27B2DC930D6003D44FC /* GlucoseValue.swift */; };
 		DD608A082C1F584900F91132 /* DeviceStatusLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */; };
 		DD608A0A2C23593900F91132 /* SMB.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A092C23593900F91132 /* SMB.swift */; };
 		DD608A0C2C27415C00F91132 /* BackgroundAlertManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A0B2C27415C00F91132 /* BackgroundAlertManager.swift */; };
@@ -386,6 +387,7 @@
 		DD5817192D299EF40041FB98 /* DexcomHeartbeatBluetoothDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DexcomHeartbeatBluetoothDevice.swift; sourceTree = "<group>"; };
 		DD58171B2D299F8D0041FB98 /* BluetoothDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothDevice.swift; sourceTree = "<group>"; };
 		DD58171D2D299FC50041FB98 /* BluetoothDeviceDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothDeviceDelegate.swift; sourceTree = "<group>"; };
+		DD5DA27B2DC930D6003D44FC /* GlucoseValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlucoseValue.swift; sourceTree = "<group>"; };
 		DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatusLoop.swift; sourceTree = "<group>"; };
 		DD608A092C23593900F91132 /* SMB.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMB.swift; sourceTree = "<group>"; };
 		DD608A0B2C27415C00F91132 /* BackgroundAlertManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundAlertManager.swift; sourceTree = "<group>"; };
@@ -935,6 +937,7 @@
 		DDCF9A7E2D85FCE6004DF4DD /* Alarm */ = {
 			isa = PBXGroup;
 			children = (
+				DD5DA27B2DC930D6003D44FC /* GlucoseValue.swift */,
 				DD4AFB602DB68BBC00BB593F /* AlarmListView.swift */,
 				DDC7E5412DBD8A1600EB1127 /* AlarmEditing */,
 				DD4AFB482DB576C200BB593F /* AlarmSettingsView.swift */,
@@ -1722,6 +1725,7 @@
 				FCC6886B24898FD800A0279D /* ObservationToken.swift in Sources */,
 				DD4AFB6B2DB6BF2A00BB593F /* Binding+Optional.swift in Sources */,
 				DD608A082C1F584900F91132 /* DeviceStatusLoop.swift in Sources */,
+				DD5DA27C2DC930D6003D44FC /* GlucoseValue.swift in Sources */,
 				DD9ACA062D32AF7900415D8A /* TreatmentsTask.swift in Sources */,
 				DD98F54424BCEFEE0007425A /* ShareClientExtension.swift in Sources */,
 				DD9ED0CE2D35587A000D2A63 /* LogEntry.swift in Sources */,

+ 10 - 2
LoopFollow/Alarm/AlarmData.swift

@@ -8,11 +8,18 @@
 
 import Foundation
 
+struct AlarmData: Codable {
+    let bgReadings: [GlucoseValue]
+    let predictionData: [GlucoseValue]
+    let expireDate: Date?
+}
+
+/*
 struct AlarmData : Encodable, Decodable{
-//    let bgReadings: [ShareGlucoseData]
+    let bgReadings: [ShareGlucoseData]
 //    let iob: Double?
 //    let cob: Double?
-//    let predictionData: [ShareGlucoseData]
+    let predictionData: [ShareGlucoseData]
 //    let latestBoluses: [BolusEntry]
 //    let batteryLevel: Double?
 //    let latestCarbs: [CarbEntry]
@@ -21,3 +28,4 @@ struct AlarmData : Encodable, Decodable{
 //    let pumpVolume: Double?
     let expireDate: Date?
 }
+*/

+ 14 - 0
LoopFollow/Alarm/GlucoseValue.swift

@@ -0,0 +1,14 @@
+//
+//  GlucoseValue.swift
+//  LoopFollow
+//
+//  Created by Jonas Björkert on 2025-05-05.
+//  Copyright © 2025 Jon Fawcett. All rights reserved.
+//
+
+import Foundation
+
+struct GlucoseValue: Codable {
+    let sgv: Int
+    let date: Date
+}

+ 0 - 1
LoopFollow/Controllers/Nightscout/ProfileManager.swift

@@ -107,7 +107,6 @@ final class ProfileManager {
         Storage.shared.deviceToken.value = profileData.deviceToken ?? ""
         if let expirationDate = profileData.expirationDate {
             Storage.shared.expirationDate.value = NightscoutUtils.parseDate(expirationDate)
-            Storage.shared.expirationDate.value = NightscoutUtils.parseDate("2025-04-25T17:54:56.000Z")
         } else {
             Storage.shared.expirationDate.value = nil
         }

+ 2 - 0
LoopFollow/Storage/Observable.swift

@@ -30,5 +30,7 @@ class Observable {
 
     var currentAlarm = ObservableValue<UUID?>(default: nil)
 
+    var debug = ObservableValue<Bool>(default: false)
+
     private init() {}
 }

+ 17 - 7
LoopFollow/Task/AlarmTask.swift

@@ -19,17 +19,27 @@ extension MainViewController {
 
     func alarmTaskAction() {
         DispatchQueue.main.async {
-            var alarmData = AlarmData(
-                expireDate: .distantPast // Storage.shared.expirationDate.value
+            let alarmData = AlarmData(
+                bgReadings: self.bgData
+                    .suffix(5)
+                    .map { GlucoseValue(sgv: $0.sgv, date: Date(timeIntervalSince1970: $0.date)) },
+                predictionData: self.predictionData
+                    .prefix(5)
+                    .map { GlucoseValue(sgv: $0.sgv, date: Date(timeIntervalSince1970: $0.date)) },
+                expireDate: Storage.shared.expirationDate.value
             )
 
-            self.saveLatestAlarmDataToFile(alarmData)
-
-            alarmData = self.loadTestAlarmData() ?? alarmData
+            let finalAlarmData: AlarmData
+            if Observable.shared.debug.value {
+                self.saveLatestAlarmDataToFile(alarmData)
+                finalAlarmData = self.loadTestAlarmData() ?? alarmData
+            } else {
+                finalAlarmData = alarmData
+            }
 
-            LogManager.shared.log(category: .alarm, message: "Checking alarms based on \(alarmData)", isDebug: true)
+            LogManager.shared.log(category: .alarm, message: "Checking alarms based on \(finalAlarmData)", isDebug: true)
 
-            AlarmManager.shared.checkAlarms(data: alarmData)
+            AlarmManager.shared.checkAlarms(data: finalAlarmData)
 
             TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date().addingTimeInterval(60))
         }

+ 38 - 0
LoopFollow/ViewControllers/MainViewController.swift

@@ -125,6 +125,8 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     override func viewDidLoad() {
         super.viewDidLoad()
 
+        loadDebugData()
+
         if ObservableUserDefaults.shared.device.value != "Trio" && Storage.shared.remoteType.value == .trc {
             Storage.shared.remoteType.value = .none
         }
@@ -701,4 +703,40 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     func calculateMaxBgGraphValue() -> Float {
         return max(topBG, topPredictionBG)
     }
+
+    func loadDebugData() {
+        struct DebugData: Codable {
+            let debug: Bool?
+            let url: String?
+            let token: String?
+        }
+
+        let fileManager = FileManager.default
+        let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("debugData.json")
+
+        if fileManager.fileExists(atPath: url.path) {
+            do {
+                let data = try Data(contentsOf: url)
+                let decoder = JSONDecoder()
+                decoder.dateDecodingStrategy = .iso8601
+
+                let debugData = try decoder.decode(DebugData.self, from: data)
+                LogManager.shared.log(category: .alarm, message: "Loaded DebugData from \(url.path)", isDebug: true)
+
+                if let debug = debugData.debug {
+                    Observable.shared.debug.value = debug
+                }
+
+                if let url = debugData.url {
+                    ObservableUserDefaults.shared.url.value = url
+                }
+
+                if let token = debugData.token {
+                    UserDefaultsRepository.token.value = token
+                }
+            } catch {
+                LogManager.shared.log(category: .alarm, message: "Failed to load DebugData: \(error)", isDebug: true)
+            }
+        }
+    }
 }