fix: 适配拍摄任务 WebSocket 分帧订阅回执

This commit is contained in:
2026-11-04 18:23:22 +08:00
parent 9abb5d3bff
commit b3fa6cf7d7
3 changed files with 298 additions and 13 deletions
@@ -1,4 +1,5 @@
import Foundation
import CoreFoundation
/// HTTP 和 WebSocket 共用的快照接收结果;重复包可证明订阅有效,但旧包不能恢复实时状态。
enum DroneDockSnapshotAcceptance {
@@ -8,7 +9,8 @@ enum DroneDockSnapshotAcceptance {
/// 先解析业务码再解析 data,兼容订阅失败时 data=[]。
enum DroneDockShootingSocketMessage {
case opened
case subscribed(taskID: Int, hasSnapshot: Bool)
case subscribed(taskID: Int)
case subscriptionMetadata(taskID: Int, hasSnapshot: Bool)
case snapshot(DroneDockShootingResponse)
case subscriptionRejected(code: Int, message: String)
case rejected(code: Int, message: String)
@@ -27,18 +29,32 @@ enum DroneDockShootingSocketMessage {
if code != 100000 {
let message = root["msg"] as? String ?? "任务订阅失败"
if let body = root["data"] as? [String: Any],
body["type"] as? Int == 310, body["subscribed"] as? Bool == false {
body["type"] as? Int == 310, boolean(body["subscribed"]) == false {
return .subscriptionRejected(code: code, message: message)
}
return .rejected(code: code, message: message)
}
guard let body = root["data"] as? [String: Any] else { throw APIError.invalidResponse }
// 链路就绪和 310 已入站仅供诊断,不能替代正式订阅结果,即使误带了订阅字段。
if let ack = body["ack"] as? String, ack == "ready" || ack == "received" { return .unrelated }
if let subscribed = body["subscribed"] as? Bool, subscribed {
guard let id = body["order_flight_task_id"] as? Int,
let hasSnapshot = body["has_snapshot"] as? Bool else { throw APIError.invalidResponse }
return .subscribed(taskID: id, hasSnapshot: hasSnapshot)
if let ack = body["ack"] as? String {
// 只有正式 subscribe 确认订阅;诊断、元数据和未知 ack 都不能冒充正式结果。
guard body["type"] as? Int == 310 else { return .unrelated }
switch ack {
case "subscribe":
guard let subscribed = boolean(body["subscribed"]) else { throw APIError.invalidResponse }
guard subscribed else {
return .subscriptionRejected(code: code, message: root["msg"] as? String ?? "任务订阅失败")
}
guard let id = body["order_flight_task_id"] as? Int, id > 0 else { throw APIError.invalidResponse }
// 此时尚未读缓存,has_snapshot=null 表示未判定,不等于没有快照。
return .subscribed(taskID: id)
case "subscribe_meta":
guard boolean(body["subscribed"]) == true,
let id = body["order_flight_task_id"] as? Int, id > 0,
let hasSnapshot = boolean(body["has_snapshot"]) else { return .unrelated }
return .subscriptionMetadata(taskID: id, hasSnapshot: hasSnapshot)
default:
return .unrelated
}
}
guard body["action"] as? String == "app_airport_shooting_updated" else { return .unrelated }
guard let params = body["params"] as? [String: Any] else { throw APIError.invalidResponse }
@@ -52,6 +68,12 @@ enum DroneDockShootingSocketMessage {
return .snapshot(value)
}
/// JSON 数值 0/1 不能通过 NSNumber 桥接冒充布尔字段。
private static func boolean(_ value: Any?) -> Bool? {
guard let number = value as? NSNumber, CFGetTypeID(number) == CFBooleanGetTypeID() else { return nil }
return number.boolValue
}
/// 构造已确认的只读订阅协议,不暴露任意 type 的发送入口。
static func subscribe(taskID: Int, token: String) -> String {
DroneDockDiagnostics.json(["type": 310, "params": ["id": taskID, "socket_token": token]])
@@ -138,10 +138,13 @@ final class DroneDockShootingRealtimeSession {
// 记录实际完成本地发送的 310;服务端是否接受仍以 ack 和完整快照为准。
log("request.started", fields: ["body": DroneDockDiagnostics.payload(Data(request.utf8))])
startHeartbeat(generation: current, transport: transport)
case .subscribed(let id, let hasSnapshot):
case .subscribed(let id):
guard subscriptionSent else { throw APIError.decodeFailed("订阅回执先于 310 发送") }
guard id == taskID else { throw APIError.decodeFailed("订阅回执任务不一致") }
subscribed = true
case .subscriptionMetadata(let id, let hasSnapshot):
// 元数据仅描述缓存,不确认订阅、不延长首包期限,也不能降级已有有效快照。
guard subscribed, id == taskID, latestSnapshot == nil else { return }
if !hasSnapshot { update(.fallback("任务缓存尚未就绪,正在通过接口更新")) }
case .snapshot(let snapshot):
guard snapshot.orderFlightTaskID == taskID else {