订阅明确无缓存时使用新发起的有效 HTTP 全量响应衔接首包,保留任务、批次和版本校验。 移除 Debug/Release 的设备指令、图传和 RTC 构建拦截,保留后端权限、身份、防重复提交及结果未知保护。 更新开发约定并补充隔离传输、飞控状态和页面回归测试。 验证:iPhone 真机 500 项相关测试通过;iOS Release 无签名编译通过。
94 lines
4.9 KiB
Swift
94 lines
4.9 KiB
Swift
import Foundation
|
|
import CoreFoundation
|
|
|
|
/// HTTP 和 WebSocket 共用的快照接收结果;重复包可证明订阅有效,但旧包不能恢复实时状态。
|
|
enum DroneDockSnapshotAcceptance {
|
|
case applied, duplicate, outdated, invalid
|
|
}
|
|
|
|
/// 本次 HTTP 实际读取且已通过业务校验的结果,不用页面保留的旧快照证明订阅首包。
|
|
struct DroneDockHTTPSnapshotResult {
|
|
let snapshot: DroneDockShootingResponse
|
|
let acceptance: DroneDockSnapshotAcceptance
|
|
}
|
|
|
|
/// 先解析业务码再解析 data,兼容订阅失败时 data=[]。
|
|
enum DroneDockShootingSocketMessage {
|
|
case opened
|
|
case subscribed(taskID: Int)
|
|
case subscriptionMetadata(taskID: Int, hasSnapshot: Bool, executionBatch: Int?)
|
|
case snapshot(DroneDockShootingResponse)
|
|
case subscriptionRejected(code: Int, message: String)
|
|
case rejected(code: Int, message: String)
|
|
case unrelated
|
|
|
|
/// 按新版协议解析 100000 成功包,欢迎帧仅用于触发 310,不代表订阅成功。
|
|
static func decode(_ data: Data) throws -> Self {
|
|
// 服务端建连先发 Opened。它不代表 310 订阅成功,也不能取消订阅超时或 HTTP 兜底。
|
|
if String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) == "Opened" {
|
|
return .opened
|
|
}
|
|
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
|
if json as? String == "Opened" { return .opened }
|
|
guard let root = json as? [String: Any],
|
|
let code = root["code"] as? Int else { throw APIError.invalidResponse }
|
|
if code != 100000 {
|
|
let message = root["msg"] as? String ?? "任务订阅失败"
|
|
if let body = root["data"] as? [String: Any],
|
|
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 }
|
|
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,
|
|
executionBatch: (body["execution_batch"] as? Int).flatMap { $0 > 0 ? $0 : nil })
|
|
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 }
|
|
let value = try JSONDecoder().decode(DroneDockShootingResponse.self,
|
|
from: JSONSerialization.data(withJSONObject: params))
|
|
guard value.orderFlightTaskID != nil, value.snapshotKind == "full",
|
|
let version = value.stateVersion, version > 0,
|
|
let batch = value.executionBatch, batch >= 0, value.ui?.phase != nil else {
|
|
throw APIError.decodeFailed("任务推送缺少完整快照或版本信息")
|
|
}
|
|
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]])
|
|
}
|
|
|
|
/// 离页尽力取消订阅;断开连接也会清除服务端 fd 订阅。
|
|
static func unsubscribe(taskID: Int) -> String {
|
|
DroneDockDiagnostics.json(["type": 311, "params": ["id": taskID]])
|
|
}
|
|
}
|