import Foundation nonisolated struct PowerReading: Sendable, Equatable { enum Source: Sendable { case smc, telemetry, instantaneous, average, balance } let watts: Double var source: Source = .telemetry } nonisolated struct BatteryPowerFlow: Sendable { var connected: Bool? var externalSupply: Bool? var charging: Bool? var battery: PowerReading? var input: PowerReading? var load: PowerReading? var adapterCapacity: Double? struct JointSupply: Sendable { let adapter: PowerReading let discharge: PowerReading var batteryFraction: Double { discharge.watts / (adapter.watts + discharge.watts) } } // Classification floor in watts, separate from the resolution of the displayed readings. private static let minimumContribution = 0.5 private(set) var registryJointSupply: JointSupply? var jointSupply: JointSupply? { guard connected == true, let battery, battery.watts <= -Self.minimumContribution else { return nil } if battery.source == .smc { return registryJointSupply } guard let input, input.watts >= Self.minimumContribution else { return nil } // IOPS reports a single primary source. Override its battery-only indication // only if all three direct telemetry channels independently support the split. if externalSupply == false && (input.source != .telemetry || battery.source != .telemetry || load?.source != .telemetry) { return nil } let discharge: PowerReading if battery.source == .telemetry || battery.source == .balance { discharge = PowerReading(watts: -battery.watts, source: battery.source) } else if let load, load.source == .telemetry, input.source == .telemetry { discharge = PowerReading(watts: load.watts - input.watts, source: .balance) } else { return nil } guard discharge.watts >= Self.minimumContribution else { return nil } if let load, load.source == .telemetry, input.source == .telemetry { // Reject conflicting measured channels rather than normalize an unexplained excess. let residual = abs(input.watts + discharge.watts - load.watts) guard residual <= max(1, 0.1 * load.watts) else { return nil } } return JointSupply(adapter: input, discharge: discharge) } mutating func applyBatterySensor(_ watts: Double) { // The gauge may still report the last charging sample just after unplugging. guard watts <= 0 || (externalSupply != false && connected != false) else { return } if battery?.source != .smc { registryJointSupply = jointSupply } battery = PowerReading(watts: watts, source: .smc) // Estimates made from older registry measurements no longer share a sampling window. if load?.source == .balance { load = nil } if input?.source == .balance { input = nil } if connected == false, load == nil, watts < 0 { load = PowerReading(watts: -watts, source: .balance) } } enum State: Sendable { case charging, combined, battery, discharging, external, unknown } var state: State { if let battery { // Differences of two sensors are less precise than a battery-terminal reading. let threshold = battery.source == .balance ? 0.5 : 0.05 if battery.watts >= threshold { return .charging } if battery.watts <= -threshold { if jointSupply != nil { return .combined } if connected == false || externalSupply == false { return .battery } if let input, input.source != .balance { if input.watts < Self.minimumContribution { return .battery } if battery.watts > -Self.minimumContribution { return .external } } return .discharging } } if battery == nil, charging == true, connected != false, externalSupply != false { return .charging } if externalSupply == false { return .battery } if externalSupply == true || (input?.watts ?? 0) > 0 { return .external } if connected == false { return .battery } return .unknown } }