Logbook.swift (5760B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 4 /// One recorded profile point (for the chart and the export). 5 public struct DiveSample: Sendable, Hashable, Codable { 6 public let time: Double // s since the start of the dive 7 public let depth: Double // m 8 public let temperature: Double? // °C (water) 9 public let ceiling: Double // m (0 = no ceiling) 10 public let noDecompressionLimit: Double? // min; nil = unlimited; 0 = in decompression 11 public let stopDepth: Double? // next mandatory stop, m 12 public let stopMinutes: Int? 13 } 14 15 /// Alarm or warning raised during the dive. 16 public struct DiveEvent: Sendable, Hashable, Codable { 17 public let time: Double // s 18 public let warning: DiveComputer.Warning 19 } 20 21 /// A point marked by the diver during the dive (Action button): something to find again on the profile. 22 public struct DiveMark: Sendable, Hashable, Codable { 23 public let time: Double // s 24 public let depth: Double // m 25 /// Magnetic heading of the device at that moment, degrees (nil when unknown). 26 public let heading: Double? 27 28 public init(time: Double, depth: Double, heading: Double? = nil) { 29 self.time = time 30 self.depth = depth 31 self.heading = heading 32 } 33 } 34 35 /// One dive of the logbook. 36 public struct DiveLog: Sendable, Hashable, Codable, Identifiable { 37 /// Where the dive comes from: recorded by Open Rémora, imported from a Subsurface logbook, 38 /// or simulated (demo: never counted in tissue loading or waiting times). 39 public enum Source: String, Sendable, Hashable, Codable { case remora, subsurface, simulation } 40 41 public var id: UUID 42 public var source: Source = .remora 43 /// Computer that recorded the dive (imported dives: model as noted by Subsurface). 44 public var computerModel: String? 45 public var number: Int // number in the logbook (1, 2, …) 46 public let start: Date 47 public let surfacePressure: Double // bar 48 public let surfaceIntervalBefore: Double? // min; nil = first dive (saturated tissues) 49 public let gas: Gas 50 public let gradientFactors: GradientFactors? // nil: imported dive 51 public let water: Water 52 public let samples: [DiveSample] 53 public let events: [DiveEvent] 54 public let tissuesAfter: TissueState? // nil: imported dive 55 // Entered afterwards in the logbook 56 public var site: String? 57 public var notes: String? 58 public var latitude: Double? 59 public var longitude: Double? 60 /// Points marked by the diver; nil in logbooks written before marks existed. 61 public var marks: [DiveMark]? 62 63 public var duration: Double { samples.last?.time ?? 0 } // s 64 public var maxDepth: Double { samples.map(\.depth).max() ?? 0 } 65 public var minTemperature: Double? { samples.compactMap(\.temperature).min() } 66 public var hadDecompression: Bool { samples.contains { $0.stopDepth != nil } } 67 /// Recorded violations: ceiling broken, ascent too fast, critical ppO2. 68 public var violations: [DiveEvent] { 69 events.filter { [.ceilingViolated, .ascentTooFast, .ppO2Critical].contains($0.warning) } 70 } 71 public var end: Date { start.addingTimeInterval(duration) } 72 73 /// Time-weighted average depth. 74 public var averageDepth: Double { 75 guard samples.count > 1, duration > 0 else { return samples.first?.depth ?? 0 } 76 var area = 0.0 77 for (a, b) in zip(samples, samples.dropFirst()) { area += (a.depth + b.depth) / 2 * (b.time - a.time) } 78 return area / duration 79 } 80 } 81 82 /// The logbook: dives in order, and the tissue state for the next dive. 83 public struct Logbook: Sendable, Hashable, Codable { 84 public private(set) var dives: [DiveLog] = [] 85 86 public init() {} 87 88 /// Real dives (demo simulations are excluded from the computations). 89 public var realDives: [DiveLog] { dives.filter { $0.source != .simulation } } 90 91 public mutating func add(_ dive: DiveLog) { 92 var d = dive 93 d.number = (dives.map(\.number).max() ?? 0) + 1 94 dives.append(d) 95 dives.sort { $0.start < $1.start } 96 } 97 98 /// Adds imported dives, keeping their number; skips those already present 99 /// (same start within one minute). Returns the number of dives added. 100 @discardableResult 101 public mutating func merge(_ imported: [DiveLog]) -> Int { 102 var added = 0 103 for d in imported where !dives.contains(where: { abs($0.start.timeIntervalSince(d.start)) < 60 }) { 104 dives.append(d) 105 added += 1 106 } 107 dives.sort { $0.start < $1.start } 108 return added 109 } 110 111 public mutating func update(_ dive: DiveLog) { 112 guard let i = dives.firstIndex(where: { $0.id == dive.id }) else { return } 113 dives[i] = dive 114 } 115 116 public mutating func remove(id: UUID) { dives.removeAll { $0.id == id } } 117 118 /// Tissues at `date`, from the last dive and the current surface pressure. 119 /// `nil` when there is no recent dive (more than 72 h): the diver is considered saturated at the surface. 120 public func tissues(at date: Date, surfacePressure: Double) -> (tissues: TissueState, interval: Double)? { 121 // Known limitation: a more recent imported dive (without tissues) is not taken into account. 122 guard let last = realDives.last(where: { $0.tissuesAfter != nil }), let after = last.tissuesAfter 123 else { return nil } 124 let minutes = date.timeIntervalSince(last.end) / 60 125 guard minutes >= 0, minutes < 72 * 60 else { return nil } 126 return (DiveComputer.surfaceInterval(after, minutes: minutes, 127 surfacePressure: surfacePressure), minutes) 128 } 129 }