remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

DiveRecorder.swift (4331B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 
      4 /// Records a dive in progress: feeds the computer and keeps the profile for the logbook.
      5 public struct DiveRecorder: Sendable {
      6     public private(set) var computer: DiveComputer
      7     public let start: Date
      8     public let surfaceIntervalBefore: Double?
      9     /// Name of the computer written in the logbook entry (the app that records the dive).
     10     public var computerModel: String
     11     /// Minimum gap between two profile points (s). Alarms are logged at every sample.
     12     public var sampleInterval: Double = 2
     13 
     14     private var t0: Double?
     15     private var samples: [DiveSample] = []
     16     private var events: [DiveEvent] = []
     17     private var activeWarnings = Set<DiveComputer.Warning>()
     18     private var lastLogged: [DiveComputer.Warning: Double] = [:]
     19     /// An alarm that flickers around its threshold is logged only once per period (s).
     20     public var eventRepeatInterval: Double = 30
     21     private var latest: DiveSample?
     22     public private(set) var marks: [DiveMark] = []
     23 
     24     /// Starts a dive. Tissues come from the logbook (repetitive dive) when it holds a recent one.
     25     public init(configuration: DiveComputer.Configuration, surfacePressure: Double,
     26                 start: Date, logbook: Logbook? = nil, computerModel: String = "Open Rémora") {
     27         self.computerModel = computerModel
     28         let previous = logbook?.tissues(at: start, surfacePressure: surfacePressure)
     29         self.computer = DiveComputer(configuration: configuration, surfacePressure: surfacePressure,
     30                                      tissues: previous?.tissues)
     31         self.start = start
     32         self.surfaceIntervalBefore = previous?.interval
     33     }
     34 
     35     /// `time`: monotonic clock in seconds; `pressure`: absolute bar; `temperature`: °C.
     36     @discardableResult
     37     public mutating func record(time: Double, pressure: Double, temperature: Double? = nil) -> DiveComputer.Status {
     38         computer.update(time: time, pressure: pressure)
     39         let status = computer.status
     40         if t0 == nil { t0 = time }
     41         let elapsed = time - (t0 ?? time)
     42 
     43         for w in status.warnings.subtracting(activeWarnings) {
     44             if let before = lastLogged[w], elapsed - before < eventRepeatInterval { continue }
     45             lastLogged[w] = elapsed
     46             events.append(DiveEvent(time: elapsed, warning: w))
     47         }
     48         activeWarnings = status.warnings
     49 
     50         let stop = status.plan.stops.first
     51         let sample = DiveSample(time: elapsed, depth: status.depth, temperature: temperature,
     52                                 ceiling: status.ceilingDepth,
     53                                 noDecompressionLimit: status.plan.noDecompressionLimit,
     54                                 stopDepth: stop?.depth, stopMinutes: stop?.minutes)
     55         latest = sample
     56         if samples.isEmpty || elapsed - samples[samples.count - 1].time >= sampleInterval {
     57             samples.append(sample)
     58         }
     59         return status
     60     }
     61 
     62     /// Marks the current point of the dive. Nil before the first sample.
     63     @discardableResult
     64     public mutating func mark(heading: Double? = nil) -> DiveMark? {
     65         guard let latest else { return nil }
     66         let mark = DiveMark(time: latest.time, depth: latest.depth, heading: heading)
     67         marks.append(mark)
     68         return mark
     69     }
     70 
     71     /// Ends the dive and produces the logbook entry (number assigned by `Logbook.add`).
     72     public func finish() -> DiveLog {
     73         // The last sample received always closes the profile (back at the surface).
     74         var samples = samples
     75         if let latest, latest.time > (samples.last?.time ?? -1) { samples.append(latest) }
     76         var dive = log(samples: samples)
     77         if !marks.isEmpty { dive.marks = marks }
     78         return dive
     79     }
     80 
     81     private func log(samples: [DiveSample]) -> DiveLog {
     82         let c = computer.configuration
     83         return DiveLog(id: UUID(), source: .remora, computerModel: computerModel, number: 0, start: start, surfacePressure: computer.scale.surfacePressure,
     84                        surfaceIntervalBefore: surfaceIntervalBefore, gas: c.gas,
     85                        gradientFactors: c.deco.gradientFactors, water: c.water,
     86                        samples: samples, events: events.sorted { $0.time < $1.time },
     87                        tissuesAfter: computer.tissues)
     88     }
     89 }