feat: 接入拍摄任务实时订阅并完善日志与封面

This commit is contained in:
2026-11-03 09:06:38 +08:00
parent 687088326c
commit 980d16da9b
27 changed files with 1790 additions and 64 deletions
@@ -38,6 +38,24 @@ struct DroneDockPurchasedSpot: Decodable, Equatable, Sendable {
/// 购买范围内的航线,按服务端顺序展示。
let airLines: [DroneDockPurchasedAirLine]
/// 卡片优先使用点位封面,缺失或无效时取本点位已购航线的第一张有效样片封面。
var displayCoverURL: URL? {
if let url = Self.imageURL(coverURL) { return url }
for route in airLines {
for sample in route.samples {
if let url = Self.imageURL(sample.coverURL) { return url }
}
}
return nil
}
private static func imageURL(_ value: String?) -> URL? {
guard let text = DroneDockHomeMetric.usableText(value), let url = URL(string: text),
let scheme = url.scheme?.lowercased(), ["https", "http"].contains(scheme),
let host = url.host, !host.isEmpty else { return nil }
return url
}
/// 张工购买点位字段,不接收 workbench 的 name 作为替代。
private enum CodingKeys: String, CodingKey {
case id = "scenic_spot_id", name = "scenic_spot_name", available
@@ -2,6 +2,20 @@ import Foundation
/// 朱工 shooting 的只读快照;完整 data 用于展示,不表示已召唤或已开始拍摄。
struct DroneDockShootingResponse: Decodable, Equatable, Sendable {
/// HTTP/推送共用的顶层任务身份。
let orderFlightTaskID: Int?
/// 同一执行批次下递增的完整快照版本。
let stateVersion: Int64?
/// 当前航线执行批次,先比较批次再比较版本,防止重召唤后的旧数据回退。
let executionBatch: Int64?
/// 第一版只接收 full 完整推送。
let snapshotKind: String?
/// 后端状态写入时间,不能作为设备读数采集时间。
let stateUpdatedAt: String?
/// 设备读数采集时间,用于显示来源和核对缓存。
let metricsUpdatedAt: String?
/// 设备缓存过期时保留数值,但标为非实时读数。
let metricsStale: Bool?
/// 后端若明确提供订单号则保留,不由请求上下文伪造响应身份。
let orderNumber: String?
/// 实测返回的订单飞行任务身份;存在时必须与请求上下文核对。
@@ -31,6 +45,9 @@ struct DroneDockShootingResponse: Decodable, Equatable, Sendable {
/// 只映射已知展示字段,不假设响应中的任务 ID 位置。
private enum CodingKeys: String, CodingKey {
case orderFlightTaskID = "order_flight_task_id", stateVersion = "state_version"
case executionBatch = "execution_batch", snapshotKind = "snapshot_kind"
case stateUpdatedAt = "state_updated_at", metricsUpdatedAt = "metrics_updated_at", metricsStale = "metrics_stale"
case orderNumber = "order_number", orderFlightTask = "order_flight_task", task
case scenic, scenicSpot = "scenic_spot", airLine = "air_line"
case header, currentPlane = "current_plane", executeGate = "execute_gate", ui
@@ -40,6 +57,13 @@ struct DroneDockShootingResponse: Decodable, Equatable, Sendable {
/// 可选字段缺失或为 null 时保持未知;不把缺失执行门禁推断为允许。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderFlightTaskID = try container.decodeIfPresent(Int.self, forKey: .orderFlightTaskID)
stateVersion = try container.decodeIfPresent(Int64.self, forKey: .stateVersion)
executionBatch = try container.decodeIfPresent(Int64.self, forKey: .executionBatch)
snapshotKind = try container.decodeIfPresent(String.self, forKey: .snapshotKind)
stateUpdatedAt = try container.decodeIfPresent(String.self, forKey: .stateUpdatedAt)
metricsUpdatedAt = try container.decodeIfPresent(String.self, forKey: .metricsUpdatedAt)
metricsStale = try container.decodeIfPresent(Bool.self, forKey: .metricsStale)
orderNumber = try container.decodeIfPresent(String.self, forKey: .orderNumber)
orderFlightTask = try container.decodeIfPresent(DroneDockShootingTaskIdentity.self, forKey: .orderFlightTask)
task = try container.decodeIfPresent(DroneDockShootingTaskIdentity.self, forKey: .task)
@@ -0,0 +1,49 @@
import Foundation
/// HTTP 和 WebSocket 共用的快照接收结果;重复包可证明订阅有效,但旧包不能恢复实时状态。
enum DroneDockSnapshotAcceptance {
case applied, duplicate, outdated, invalid
}
/// 先解析业务码再解析 data,兼容订阅失败时 data=[]。
enum DroneDockShootingSocketMessage {
case subscribed(taskID: Int, hasSnapshot: Bool)
case snapshot(DroneDockShootingResponse)
case rejected(code: Int, message: String)
case unrelated
/// WebSocket 成功码为 0,不使用 HTTP 的 100000 Envelope。
static func decode(_ data: Data) throws -> Self {
guard let root = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let code = root["code"] as? Int else { throw APIError.invalidResponse }
if code != 0 {
return .rejected(code: code, message: root["msg"] as? String ?? "任务订阅失败")
}
guard let body = root["data"] as? [String: Any] else { throw APIError.invalidResponse }
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)
}
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)
}
/// 构造已确认的只读订阅协议,不暴露任意 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]])
}
}