// // ADSBDataSource.swift // LearnMapKit // // Created for MVVM refactoring // import Foundation import Combine // MARK: - Data Source Protocol protocol ADSBDataSource: AnyObject { var dataStream: AnyPublisher { get } var connectionState: AnyPublisher { get } var configuration: DataSourceConfiguration { get } func configure(with config: DataSourceConfiguration) async func start() async throws func stop() async func reconnect() async throws } // MARK: - Data Events enum ADSBDataEvent { case aircraftIdentification(address: Int, icaoName: String) case aircraftPosition(address: Int, latitude: Double, longitude: Double) case aircraftAltitude(address: Int, altitude: Int) case rawMessage(String) case error(Error) } // MARK: - Data Source State enum DataSourceState: Equatable { case idle case configuring case connecting case connected case disconnected case error(String) static func == (lhs: DataSourceState, rhs: DataSourceState) -> Bool { switch (lhs, rhs) { case (.idle, .idle), (.configuring, .configuring), (.connecting, .connecting), (.connected, .connected), (.disconnected, .disconnected): return true case let (.error(lhsError), .error(rhsError)): return lhsError == rhsError default: return false } } } // MARK: - Configuration protocol DataSourceConfiguration { var sourceType: DataSourceType { get } } enum DataSourceType { case file(path: String) case network(host: String, port: Int) case test(mockData: [String]) } struct FileDataSourceConfiguration: DataSourceConfiguration { let sourceType: DataSourceType let filePath: String let processRate: Int // messages per second init(filePath: String, processRate: Int = 120) { self.filePath = filePath self.processRate = processRate self.sourceType = .file(path: filePath) } } struct NetworkDataSourceConfiguration: DataSourceConfiguration { let sourceType: DataSourceType let hostname: String let port: Int let reconnectAttempts: Int let timeoutInterval: TimeInterval init(hostname: String, port: Int, reconnectAttempts: Int = 3, timeoutInterval: TimeInterval = 10.0) { self.hostname = hostname self.port = port self.reconnectAttempts = reconnectAttempts self.timeoutInterval = timeoutInterval self.sourceType = .network(host: hostname, port: port) } }