feat(drone-dock): 完善真实联调、状态轮询与准备页反馈

开放 Release 真实操作并保留 Debug 拦截;恢复前台 shooting 串行轮询;统一打印机巢请求与完整原值响应;优化长点位名称、未拍摄文案、报备失败及已结束任务提示,移除成功回执 UI。补充相关测试、离线策略检查和联调说明。
This commit is contained in:
2026-11-02 16:26:50 +08:00
parent b21a3b6cb6
commit 2d86ec8cd7
37 changed files with 1298 additions and 411 deletions
@@ -0,0 +1,153 @@
import Foundation
import OSLog
/// 机巢联调日志:按用户要求在 Debug 与 Release 控制台原样记录字段,不做脱敏。
enum DroneDockDiagnostics {
private static let logger = Logger(subsystem: "com.yuanzhixiang.suixinkan", category: "DroneDock")
private static let sessionID = String(UUID().uuidString.prefix(8))
/// 按 UTF-8 字节分段,避免系统日志截断大响应;每段携带同一事件 ID 和序号。
static func write(_ message: String) {
let entry = String(UUID().uuidString.prefix(8))
let chunks = chunks(message)
for (index, chunk) in chunks.enumerated() {
let line = "[DroneDock] session=\(sessionID) entry=\(entry) part=\(index + 1)/\(chunks.count) \(chunk)"
logger.notice("\(line, privacy: .public)")
}
}
/// 页面、业务与 RTC 事件以结构化字段记录。
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 error is DeviceCommandDebugBlockedError { return ["kind": "locally_blocked", "sent": false] }
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 字节,长 JSON 可按 entry 和 part 拼回完整响应。
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["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
fields["flight_blocked"] = DeviceCommandDebugPolicy.blocksFlightRequests
fields["live_blocked"] = DeviceCommandDebugPolicy.blocksLiveRequests
let items = request.url.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: false)?.queryItems } ?? []
fields["query"] = items.map { [$0.name: $0.value ?? ""] }
return DroneDockDiagnostics.json(fields)
}
}