import Foundation /// 禁飞期间的 Debug 请求边界;模拟器和测试环境均不能视为没有真实设备副作用。 enum DeviceCommandDebugPolicy { /// 所有 Debug 构建禁止真实设备操作,没有启动参数或设置开关可临时放行。 static var blocksLiveRequests: Bool { #if DEBUG true #else false #endif } /// 页面与本地拦截共用的说明,不将未发送的请求表述为服务端执行失败。 static let blockedMessage = "当前禁止真实设备指令,请使用本地模拟调试" /// 在日志和传输之前阻断操作请求;允许的状态读取关闭实时设备采集。 static func prepare(_ request: URLRequest, isLocalSimulation: Bool) throws -> URLRequest { guard blocksLiveRequests, !isLocalSimulation, let url = request.url else { return request } let path = "/" + url.path.split(separator: "/").joined(separator: "/") // 创建虽不直接起飞,但会改变真实订单状态;禁飞调试使用本地待飞任务。 if path == "/api/yf-handset-app/photog/multi-plane/tasks/create" { throw DeviceCommandDebugBlockedError() } guard path == "/api/app/airport" || path.hasPrefix("/api/app/airport/") else { return request } let readablePaths: Set = [ "/api/app/airport/home", "/api/app/airport/plane-status", "/api/app/airport/shooting", "/api/app/airport/status" ] guard request.httpMethod == "GET", readablePaths.contains(path) else { throw DeviceCommandDebugBlockedError() } let liveKey: String switch path { case "/api/app/airport/plane-status", "/api/app/airport/shooting": liveKey = "live_battery" case "/api/app/airport/status": liveKey = "live_metrics" default: return request } guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { throw APIError.invalidURL } var items = components.queryItems ?? [] items.removeAll { $0.name == liveKey } items.append(URLQueryItem(name: liveKey, value: "0")) components.queryItems = items guard let safeURL = components.url else { throw APIError.invalidURL } var safeRequest = request safeRequest.url = safeURL return safeRequest } } /// 明确表示请求在本机被阻断,任何设备指令或任务创建都未发送。 struct DeviceCommandDebugBlockedError: Error, LocalizedError { /// 提示使用本地模拟,不伪造后端错误码或成功回执。 var errorDescription: String? { DeviceCommandDebugPolicy.blockedMessage } }