Переглянути джерело

Reduced heartbeat logging

Jonas Björkert 1 рік тому
батько
коміт
11dc1c397f

+ 29 - 1
LoopFollow/BackgroundRefresh/BT/BLEManager.swift

@@ -41,6 +41,7 @@ class BLEManager: NSObject, ObservableObject {
         if let device = activeDevice {
             device.disconnect()
             activeDevice = nil
+            device.lastHeartbeatTime = nil
         }
         Storage.shared.selectedBLEDevice.value = nil
     }
@@ -142,7 +143,34 @@ extension BLEManager: BluetoothDeviceDelegate {
     }
 
     func heartBeat() {
-        LogManager.shared.log(category: .bluetooth, message: "Heartbeat triggered")
+        guard let device = activeDevice else {
+            return
+        }
+
+        let now = Date()
+        guard let expectedInterval = device.expectedHeartbeatInterval() else {
+            LogManager.shared.log(category: .bluetooth, message: "Heartbeat triggered")
+            device.lastHeartbeatTime = now
+            TaskScheduler.shared.checkTasksNow()
+            return
+        }
+
+        let marginPercentage: Double = 0.15 // 15% margin
+        let margin = expectedInterval * marginPercentage
+        let threshold = expectedInterval + margin
+
+        if let last = device.lastHeartbeatTime {
+            let elapsedTime = now.timeIntervalSince(last)
+            if elapsedTime > threshold {
+                let delay = elapsedTime - expectedInterval
+                LogManager.shared.log(category: .bluetooth, message: "Heartbeat triggered (Delayed by \(String(format: "%.1f", delay)) seconds)")
+            }
+        } else {
+            LogManager.shared.log(category: .bluetooth, message: "Heartbeat triggered (First heartbeat)")
+        }
+
+        device.lastHeartbeatTime = now
+
         TaskScheduler.shared.checkTasksNow()
     }
 }

+ 6 - 0
LoopFollow/BackgroundRefresh/BT/BluetoothDevice.swift

@@ -24,8 +24,10 @@ class BluetoothDevice: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
     private var receiveCharacteristic:CBCharacteristic?
     private let maxTimeToWaitForPeripheralResponse = 5.0
     private var connectTimeOutTimer: Timer?
+    var lastHeartbeatTime: Date?
 
     init(address:String, name:String?, CBUUID_Advertisement:String?, servicesCBUUIDs:[CBUUID]?, CBUUID_ReceiveCharacteristic:String, bluetoothDeviceDelegate: BluetoothDeviceDelegate) {
+        self.lastHeartbeatTime = nil
         self.deviceAddress = address
         self.deviceName = name
 
@@ -320,4 +322,8 @@ class BluetoothDevice: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
             }
         }
     }
+
+    func expectedHeartbeatInterval() -> TimeInterval? {
+        return nil
+    }
 }

+ 4 - 0
LoopFollow/BackgroundRefresh/BT/Devices/DexcomG7HeartbeatBluetoothDevice.swift

@@ -42,4 +42,8 @@ class DexcomG7HeartbeatBluetoothDevice: BluetoothDevice {
         super.centralManager(central, didDisconnectPeripheral: peripheral, error: error)
         self.bluetoothDeviceDelegate?.heartBeat()
     }
+
+    override func expectedHeartbeatInterval() -> TimeInterval? {
+        return 5 * 60 // 5 minutes in seconds
+    }
 }

+ 4 - 0
LoopFollow/BackgroundRefresh/BT/Devices/RileyLinkHeartbeatBluetoothDevice.swift

@@ -47,4 +47,8 @@ class RileyLinkHeartbeatBluetoothDevice: BluetoothDevice {
 
         self.bluetoothDeviceDelegate?.heartBeat()
     }
+
+    override func expectedHeartbeatInterval() -> TimeInterval? {
+        return 60
+    }
 }