Files
suixinkan_uikit/suixinkan/Core/Networking/DeviceCommandDebugPolicy.swift
T
lujiuyin 2d86ec8cd7 feat(drone-dock): 完善真实联调、状态轮询与准备页反馈
开放 Release 真实操作并保留 Debug 拦截;恢复前台 shooting 串行轮询;统一打印机巢请求与完整原值响应;优化长点位名称、未拍摄文案、报备失败及已结束任务提示,移除成功回执 UI。补充相关测试、离线策略检查和联调说明。
2026-11-02 16:26:50 +08:00

69 lines
3.3 KiB
Swift

import Foundation
/// 调试请求边界:Debug 禁止真实设备操作,Release 按用户授权开放已对接的操作。
enum DeviceCommandDebugPolicy {
/// 2026-11-02 用户授权 Release 真机联调;Debug 仍无运行时放行开关。
static var blocksFlightRequests: Bool {
#if DEBUG
true
#else
false
#endif
}
/// 所有 Debug 构建禁止真实设备操作,没有启动参数或设置开关可临时放行。
static var blocksLiveRequests: Bool {
#if DEBUG
true
#else
false
#endif
}
/// 页面与本地拦截共用的说明,不将未发送的请求表述为服务端执行失败。
static let blockedMessage = "当前禁止真实设备指令,请使用本地模拟调试"
/// 在日志和传输之前阻断操作请求;允许的状态读取关闭实时设备采集。
static func prepare(_ request: URLRequest, isLocalSimulation: Bool) throws -> URLRequest {
guard !isLocalSimulation, let url = request.url else { return request }
let path = "/" + url.path.split(separator: "/").joined(separator: "/")
// Debug 创建也必须拦截,避免调试残留真实待飞任务。
if path == "/api/yf-handset-app/photog/multi-plane/tasks/create" {
guard !blocksFlightRequests, request.httpMethod == "POST" else { throw DeviceCommandDebugBlockedError() }
return request
}
guard path == "/api/app/airport" || path.hasPrefix("/api/app/airport/") else { return request }
let flightPaths: Set<String> = ["/api/app/airport/shoot", "/api/app/airport/start-shoot", "/api/app/airport/command"]
if !blocksFlightRequests, request.httpMethod == "POST", flightPaths.contains(path) { return request }
// 仅允许已确认的图传和飞控接口;未知设备操作仍阻断。
if request.httpMethod == "GET", path == "/api/app/airport/live", !blocksLiveRequests { return request }
let readablePaths: Set<String> = [
"/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 }
}