订阅明确无缓存时使用新发起的有效 HTTP 全量响应衔接首包,保留任务、批次和版本校验。 移除 Debug/Release 的设备指令、图传和 RTC 构建拦截,保留后端权限、身份、防重复提交及结果未知保护。 更新开发约定并补充隔离传输、飞控状态和页面回归测试。 验证:iPhone 真机 500 项相关测试通过;iOS Release 无签名编译通过。
143 lines
6.5 KiB
Swift
143 lines
6.5 KiB
Swift
import Foundation
|
|
|
|
/// 机巢联调日志:按用户要求在 Debug 与 Release 控制台原样记录字段,不做脱敏。
|
|
enum DroneDockDiagnostics {
|
|
/// 与其它模块共用简洁出口;内部诊断事件留作测试/业务核对,不在控制台刷屏。
|
|
static func write(_ message: String) {
|
|
APIConsoleDiagnostics.writeDroneDock(message)
|
|
}
|
|
|
|
/// 构造内部结构化事件;控制台仅选择请求、响应及本地传输失败输出。
|
|
static func event(_ name: String, taskID: Int? = nil, fields: [String: Any] = [:]) {
|
|
var fields = fields
|
|
fields["task_id"] = taskID
|
|
fields["event"] = name
|
|
write(json(fields))
|
|
}
|
|
|
|
/// 覆盖机巢全部业务接口、景区空境库及共用 STS;不依赖页面或构建环境开关。
|
|
static func includes(path: String) -> Bool {
|
|
["/api/app/airport/", "/api/yf-handset-app/photog/multi-plane/", "/api/app/scenic-media/"]
|
|
.contains(where: path.hasPrefix) || path == "/api/app/config/get-sts-token"
|
|
}
|
|
|
|
/// JSON 保留完整字段,文本错误页原样记录;二进制正文只记录字节数。
|
|
static func payload(_ data: Data?) -> Any {
|
|
guard let data, !data.isEmpty else { return "<empty>" }
|
|
return (try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed))
|
|
?? String(data: data, encoding: .utf8)
|
|
?? "<binary: \(data.count) bytes>"
|
|
}
|
|
|
|
/// 排序 JSON 便于复制比对,所有字段值保持原样。
|
|
static func json(_ object: Any) -> String {
|
|
guard let data = try? JSONSerialization.data(withJSONObject: object,
|
|
options: [.sortedKeys, .fragmentsAllowed, .withoutEscapingSlashes]),
|
|
let text = String(data: data, encoding: .utf8) else { return "<invalid diagnostic fields>" }
|
|
return text
|
|
}
|
|
|
|
/// 记录业务码、HTTP 状态及原始错误说明,便于与后端回包对应。
|
|
static func errorFields(_ error: Error) -> [String: Any] {
|
|
if error is CancellationError { return ["kind": "cancelled"] }
|
|
if case APIError.serverCode(let code, let message) = error {
|
|
return ["kind": "business_rejected", "code": code, "message": message]
|
|
}
|
|
if case APIError.decodeFailed(let message) = error { return ["kind": "decode_failed", "message": message] }
|
|
if case APIError.httpStatus(let code, let message) = error {
|
|
return ["kind": "http_rejected", "http_status": code, "message": message]
|
|
}
|
|
if case APIError.emptyData = error { return ["kind": "empty_data"] }
|
|
let value = error as NSError
|
|
var fields: [String: Any] = ["kind": "error", "domain": value.domain, "code": value.code,
|
|
"message": value.localizedDescription]
|
|
fields["failing_url"] = value.userInfo[NSURLErrorFailingURLStringErrorKey] as? String
|
|
return fields
|
|
}
|
|
|
|
/// 独立运行时的系统日志按最多 600 字节分段;Xcode 控制台直接输出完整 JSON。
|
|
static func chunks(_ text: String) -> [String] {
|
|
var result: [String] = []
|
|
var current = ""
|
|
var bytes = 0
|
|
for scalar in text.unicodeScalars {
|
|
let next = String(scalar)
|
|
if bytes + next.utf8.count > 600 { result.append(current); current = ""; bytes = 0 }
|
|
current += next
|
|
bytes += next.utf8.count
|
|
}
|
|
if !current.isEmpty { result.append(current) }
|
|
return result.isEmpty ? [""] : result
|
|
}
|
|
|
|
}
|
|
|
|
/// 视频上传和素材下载的网络摘要,保留完整地址和传输状态。
|
|
struct DroneDockFileTransferTrace: Sendable {
|
|
private let requestID = UUID().uuidString
|
|
private let started = ProcessInfo.processInfo.systemUptime
|
|
private let operation: String
|
|
private let method: String
|
|
private let remoteURL: URL?
|
|
|
|
/// 仅在实际文件传输边界创建,记录传入的完整目标地址。
|
|
init(operation: String, method: String, remoteURL: URL?) {
|
|
self.operation = operation
|
|
self.method = method
|
|
self.remoteURL = remoteURL
|
|
}
|
|
|
|
/// 请求、响应和错误共用同一关联 ID;文件响应仅记录状态与大小。
|
|
func message(_ event: String, fields: [String: Any] = [:]) -> String {
|
|
var fields = fields
|
|
fields["event"] = operation + "." + event
|
|
fields["request_id"] = requestID
|
|
fields["elapsed_ms"] = Int((ProcessInfo.processInfo.systemUptime - started) * 1_000)
|
|
fields["source"] = "network"
|
|
fields["method"] = method
|
|
fields["host"] = remoteURL?.host
|
|
fields["url"] = remoteURL?.absoluteString
|
|
return DroneDockDiagnostics.json(fields)
|
|
}
|
|
}
|
|
|
|
/// 一次请求的关联 ID 与耗时;模拟/真实传输明确区分,不把 HTTP 成功当作设备执行成功。
|
|
struct DroneDockRequestTrace {
|
|
private let requestID = UUID().uuidString
|
|
private let started = ProcessInfo.processInfo.systemUptime
|
|
private let request: URLRequest
|
|
private let isSimulation: Bool
|
|
|
|
/// 未在机巢范围的请求不生成日志。
|
|
init?(request: URLRequest, isSimulation: Bool) {
|
|
guard DroneDockDiagnostics.includes(path: request.url?.path ?? "") else { return nil }
|
|
self.request = request
|
|
self.isSimulation = isSimulation
|
|
}
|
|
|
|
/// 保留完整地址、请求头、业务参数与固定身份。
|
|
func message(_ event: String, fields: [String: Any] = [:]) -> String {
|
|
var fields = fields
|
|
fields["event"] = event
|
|
fields["request_id"] = requestID
|
|
fields["elapsed_ms"] = Int((ProcessInfo.processInfo.systemUptime - started) * 1_000)
|
|
fields["source"] = isSimulation ? "local_simulation" : "network"
|
|
fields["host"] = request.url?.host
|
|
fields["path"] = request.url?.path
|
|
fields["method"] = request.httpMethod
|
|
fields["url"] = request.url?.absoluteString
|
|
fields["request_headers"] = request.allHTTPHeaderFields ?? [:]
|
|
fields["request_params"] = APIConsoleFormat.parameters(request)
|
|
fields["app_version"] = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
|
|
fields["app_build"] = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
|
|
#if DEBUG
|
|
fields["build"] = "Debug"
|
|
#else
|
|
fields["build"] = "Release"
|
|
#endif
|
|
let items = request.url.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: false)?.queryItems } ?? []
|
|
fields["query"] = items.map { [$0.name: $0.value ?? ""] }
|
|
return DroneDockDiagnostics.json(fields)
|
|
}
|
|
}
|