VacuumSequence.swift (6914B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 // Guided sequence: vacuum, leak test, monitoring and opening. 3 // Pure logic (no Bluetooth, no hidden clock) so that it can be tested: it receives the buttons and 4 // the readings, and returns the orders to carry out. Safety rule: the pump only starts after 5 // the "plug open" confirmation. 6 import Foundation 7 8 struct VacuumSequence: Equatable, Sendable { 9 struct Baseline: Equatable, Sendable { 10 let date: Date 11 let pressurePa: Double 12 let temperatureK: Double? 13 } 14 15 enum Failure: Equatable, Sendable { 16 case vacuumNotReached 17 case leak(risePa: Double) 18 case connectionLost 19 } 20 21 enum Step: Equatable, Sendable { 22 case prepare // iPhone in place, housing closed and locked 23 case openPlug // open the plug (before the pump) 24 case pumping(since: Date) 25 case closePlug 26 case testing(Baseline) // leak test, camera usable 27 case sealed(Baseline) // test passed, continuous monitoring 28 case failed(Failure) 29 case opening(fromPa: Double) // open the plug to let the air back in 30 case opened // vented: the housing can be opened, then turned off 31 case bypassed // dry run, without vacuum (chosen on the touch screen) 32 } 33 34 enum Effect: Equatable, Sendable { 35 case startPump, stopPump, disconnect 36 } 37 38 /// Pump stop: first sample below 80 kPa (observed with the official app). 39 static let vacuumTargetPa = 80_000.0 40 /// Below this threshold at the start, the housing is already under vacuum: go straight to the test. 41 static let alreadyUnderVacuumPa = 85_000.0 42 static let pumpMaxSeconds = 15.0 43 static let testSeconds = 180.0 44 /// Provisional thresholds (a single reference measurement: ~30 Pa/min corrected), to be tuned by trials. 45 static let testMaxRisePa = 500.0 46 static let monitorMaxRisePa = 2_000.0 47 /// Venting detected: +10 kPa since the opening started, or more than 95 kPa. 48 static let openedRisePa = 10_000.0 49 static let openedAbsolutePa = 95_000.0 50 51 private(set) var step: Step = .prepare 52 /// Duration of the leak test; shortened only for the simulated housing. 53 var testDuration = VacuumSequence.testSeconds 54 private(set) var pressurePa: Double? 55 /// Housing unreachable during the test or the dive: the state is kept, monitoring resumes when it comes back. 56 private(set) var linkLost = false 57 private(set) var temperatureC: Double? 58 59 /// Pressure brought back to the temperature at the start of the test (gas law), minus the starting pressure. 60 func correctedRise(since baseline: Baseline) -> Double? { 61 guard let pressurePa else { return nil } 62 var expected = baseline.pressurePa 63 if let t0 = baseline.temperatureK, let temperatureC { 64 expected *= (temperatureC + 273.15) / t0 65 } 66 return pressurePa - expected 67 } 68 69 func testRemaining(now: Date) -> TimeInterval? { 70 guard case .testing(let baseline) = step else { return nil } 71 return max(0, testDuration - now.timeIntervalSince(baseline.date)) 72 } 73 74 /// True when the buttons go to the camera (except the long press right, kept for opening). 75 var cameraHasButtons: Bool { 76 switch step { 77 case .testing, .sealed, .bypassed: true 78 default: false 79 } 80 } 81 82 /// Dry run: only from the start of the sequence, pump stopped. 83 mutating func bypass() { 84 if step == .prepare { step = .bypassed } 85 } 86 87 private func baseline(now: Date) -> Baseline? { 88 guard let pressurePa else { return nil } 89 return Baseline(date: now, pressurePa: pressurePa, temperatureK: temperatureC.map { $0 + 273.15 }) 90 } 91 92 mutating func button(_ event: ButtonEvent, now: Date) -> [Effect] { 93 switch (step, event.button, event.isLong) { 94 case (.prepare, .right, false): 95 if let p = pressurePa, p < Self.alreadyUnderVacuumPa, let b = baseline(now: now) { 96 step = .testing(b) 97 } else { 98 step = .openPlug 99 } 100 case (.openPlug, .right, false): 101 guard pressurePa != nil else { return [] } // no pump without a pressure sensor 102 step = .pumping(since: now) 103 return [.startPump] 104 case (.openPlug, .left, false): 105 step = .prepare 106 case (.pumping, .left, false): 107 step = .openPlug 108 return [.stopPump] 109 case (.closePlug, .right, false): 110 if let b = baseline(now: now) { step = .testing(b) } 111 case (.closePlug, .left, false): 112 step = .openPlug 113 case (.failed, .right, false): 114 step = .prepare 115 case (.testing, .right, true), (.sealed, .right, true), (.failed, .right, true): 116 step = .opening(fromPa: pressurePa ?? 0) 117 case (.bypassed, .right, true): 118 step = .prepare 119 case (.opening, .right, false): 120 step = .opened // manual confirmation when detection is not enough 121 case (.opening, .left, false): 122 step = .prepare 123 case (.opened, .right, false): 124 return [.disconnect] 125 default: 126 break 127 } 128 return [] 129 } 130 131 mutating func sensors(pressurePa: Double?, temperatureC: Double?, now: Date) -> [Effect] { 132 if let pressurePa { self.pressurePa = pressurePa; linkLost = false } 133 if let temperatureC { self.temperatureC = temperatureC } 134 guard let p = self.pressurePa else { return [] } 135 switch step { 136 case .pumping(let since): 137 if p < Self.vacuumTargetPa { 138 step = .closePlug 139 return [.stopPump] 140 } 141 if now.timeIntervalSince(since) > Self.pumpMaxSeconds { 142 step = .failed(.vacuumNotReached) 143 return [.stopPump] 144 } 145 case .testing(let baseline): 146 let rise = correctedRise(since: baseline) ?? 0 147 if rise > Self.testMaxRisePa { 148 step = .failed(.leak(risePa: rise)) 149 } else if now.timeIntervalSince(baseline.date) >= testDuration { 150 step = .sealed(baseline) 151 } 152 case .sealed(let baseline): 153 let rise = correctedRise(since: baseline) ?? 0 154 if rise > Self.monitorMaxRisePa { step = .failed(.leak(risePa: rise)) } 155 case .opening(let fromPa): 156 if p > Self.openedAbsolutePa || p - fromPa > Self.openedRisePa { step = .opened } 157 default: 158 break 159 } 160 return [] 161 } 162 163 mutating func connectionLost() { 164 switch step { 165 case .testing, .sealed: 166 linkLost = true 167 case .pumping, .closePlug: 168 step = .failed(.connectionLost) 169 default: 170 break 171 } 172 } 173 }