Преглед на файлове

Merge pull request #376 from dsnallfot/estimated-delay

Add rileylink offset calculations
Jonas Björkert преди 1 година
родител
ревизия
840980b9df
променени са 2 файла, в които са добавени 21 реда и са изтрити 14 реда
  1. 12 8
      LoopFollow/BackgroundRefresh/BT/BLEManager.swift
  2. 9 6
      LoopFollow/Controllers/Nightscout/BGData.swift

+ 12 - 8
LoopFollow/BackgroundRefresh/BT/BLEManager.swift

@@ -216,8 +216,8 @@ extension BLEManager: BluetoothDeviceDelegate {
 
 extension BLEManager {
     /// Returns the expected sensor fetch offset as a formatted string ("mm:ss (fetch delay: XX sec)")
-    /// for Dexcom devices. The expected offset is computed as the sensor's schedule offset plus the polling delay.
-    /// The device’s lastSeen time is used (mod 300) to calculate the effective delay between when the sensor value
+    /// for Dexcom and RileyLink devices. The expected offset is computed as the sensor's schedule offset plus the polling delay.
+    /// The device’s lastSeen time is used (mod cycleDuration) to calculate the effective delay between when the sensor value
     /// becomes available and when the fetch is actually triggered.
     func expectedSensorFetchOffsetString(for device: BLEDevice) -> String? {
         // Determine the device type using your BackgroundRefreshType matching.
@@ -225,8 +225,8 @@ extension BLEManager {
             return nil
         }
 
-        // We only calculate this for Dexcom (G7) devices.
-        if matchedType == .dexcom {
+        // We calculate this for Dexcom and RileyLink devices.
+        if matchedType == .dexcom || matchedType == .rileyLink {
             // Return nil if the sensor schedule offset hasn't been set.
             guard let sensorOffset = Storage.shared.sensorScheduleOffset.value else {
                 return nil
@@ -238,21 +238,25 @@ extension BLEManager {
             // T_expected: the time (in seconds) after the sensor reading when the value is available.
             let expectedOffset = sensorOffset + pollingDelay
 
-            // Compute the device’s heartbeat offset within the 5-minute (300 sec) cycle.
+            // Determine the cycle duration based on the device type.
+            let cycleDuration: TimeInterval = (matchedType == .rileyLink) ? 60 : 300
+
+            // Compute the device’s heartbeat offset within the appropriate cycle.
             let calendar = Calendar(identifier: .gregorian)
             let startOfDay = calendar.startOfDay(for: device.lastSeen)
-            let heartbeatOffset = device.lastSeen.timeIntervalSince(startOfDay).truncatingRemainder(dividingBy: 300)
+            let heartbeatOffset = device.lastSeen.timeIntervalSince(startOfDay).truncatingRemainder(dividingBy: cycleDuration)
 
             // Calculate effective delay:
             // If the heartbeat happens after the sensor value is available, delay = heartbeatOffset - expectedOffset.
             // Otherwise, the fetch will occur on the next cycle:
-            // delay = (heartbeatOffset + 300) - expectedOffset.
+            // delay = (heartbeatOffset + cycleDuration) - expectedOffset.
             let effectiveDelay: TimeInterval = (heartbeatOffset >= expectedOffset)
             ? (heartbeatOffset - expectedOffset)
-            : (heartbeatOffset + 300 - expectedOffset)
+            : (heartbeatOffset + cycleDuration - expectedOffset)
 
             return "\(Int(effectiveDelay)) sec"
         }
         return nil
     }
 }
+

+ 9 - 6
LoopFollow/Controllers/Nightscout/BGData.swift

@@ -135,8 +135,11 @@ extension MainViewController {
         // secondsAgo is how old the newest reading is
         let secondsAgo = now - sensorTimestamp
 
-        // Compute the current sensor schedule offset.
-        let currentOffset = sensorScheduleOffset(for: sensorTimestamp)
+        // Determine the cycle duration based on device type.
+        let cycleDuration: TimeInterval = (Storage.shared.backgroundRefreshType.value == .rileyLink) ? 60 : 300
+
+        // Compute the current sensor schedule offset using the appropriate cycle.
+        let currentOffset = sensorScheduleOffset(for: sensorTimestamp, cycle: cycleDuration)
 
         if Storage.shared.sensorScheduleOffset.value != currentOffset {
             Storage.shared.sensorScheduleOffset.value = currentOffset
@@ -212,9 +215,9 @@ extension MainViewController {
     }
 
     /// Computes the sensor schedule offset (in seconds) for a given time interval.
-    /// The offset is the remainder (in seconds) of the time elapsed since midnight (UTC) divided by 300 seconds.
-    /// For example, if the sensor reports a time that corresponds to 13:06:30, the offset is 90 seconds.
-    func sensorScheduleOffset(for timeInterval: TimeInterval) -> TimeInterval {
+    /// The offset is the remainder (in seconds) of the time elapsed since midnight (UTC)
+    /// divided by the given cycle length (default 300 seconds).
+    func sensorScheduleOffset(for timeInterval: TimeInterval, cycle: TimeInterval = 300) -> TimeInterval {
         var calendar = Calendar(identifier: .gregorian)
         // Use UTC to be consistent with our sensor timestamps.
         calendar.timeZone = TimeZone(secondsFromGMT: 0)!
@@ -222,7 +225,7 @@ extension MainViewController {
         let date = Date(timeIntervalSince1970: timeInterval)
         let startOfDay = calendar.startOfDay(for: date)
         let secondsSinceStartOfDay = date.timeIntervalSince(startOfDay)
-        return secondsSinceStartOfDay.truncatingRemainder(dividingBy: 300)
+        return secondsSinceStartOfDay.truncatingRemainder(dividingBy: cycle)
     }
 
     func updateServerText(with serverText: String? = nil) {