import Foundation /// 朱工 live 回执;任务身份与播放参数分别验证,凭证不进入展示 JSON。 struct DroneDockLiveResponse: Decodable, Equatable, Sendable { /// 后端回显的订单飞行任务 ID,不能用请求值补齐。 let orderFlightTaskID: Int /// 设备明细尚未定义字段,保留原 JSON 类型。 let airport: DroneDockHomeJSONValue? /// 无人机明细尚未定义字段,保留原 JSON 类型。 let drone: DroneDockHomeJSONValue? /// 文档中的任务摘要,若提供身份必须与顶层任务一致。 let task: DroneDockShootingTaskIdentity? /// 已锁定机场序列号,不用于客户端选择或重新绑定设备。 let airportSN: String? let planeID: Int? let playable: Bool? let unavailableMessage: String? let play: DroneDockLiveStream? /// 随接口返回的展示读数,不触发额外实时采集。 let metrics: [DroneDockHomeMetric] /// 脱敏回执,不包含 RTC 连接串或 Token。 let dataJSON: String /// live 文档中的已知字段映射。 private enum CodingKeys: String, CodingKey { case orderFlightTaskID = "order_flight_task_id", airport, drone, task case airportSN = "airport_sn", metrics case planeID = "plane_id", playable, play, unavailableMessage = "unavailable_message" } /// 任务 ID 必须明确提供;未知字段及 null 留在完整 JSON 中供后续合同核对。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) orderFlightTaskID = try container.decode(Int.self, forKey: .orderFlightTaskID) airport = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .airport) drone = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .drone) task = try container.decodeIfPresent(DroneDockShootingTaskIdentity.self, forKey: .task) airportSN = try container.decodeIfPresent(String.self, forKey: .airportSN) planeID = try container.decodeIfPresent(Int.self, forKey: .planeID) playable = try container.decodeIfPresent(Bool.self, forKey: .playable) unavailableMessage = try container.decodeIfPresent(String.self, forKey: .unavailableMessage) play = try container.decodeIfPresent(DroneDockLiveStream.self, forKey: .play) metrics = try container.decodeIfPresent([DroneDockHomeMetric].self, forKey: .metrics) ?? [] let raw = try DroneDockHomeJSONValue(from: decoder) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] dataJSON = String(decoding: try encoder.encode(DroneDockLiveJSONSnapshot(value: raw)), as: UTF8.self) } } /// 只负责原样编码解码所得的 JSON,不追加猜测的流媒体参数。 private struct DroneDockLiveJSONSnapshot: Encodable { let value: DroneDockHomeJSONValue /// 保持字典、数组、空值、数值与字符串的原始类型。 func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch value { case .object(let value): try container.encode(value.mapValuesWithKeys { key, value in Self(value: ["url", "token"].contains(key.lowercased()) ? .string("") : value) }) case .array(let value): try container.encode(value.map { Self(value: $0) }) case .string(let value): try container.encode(value) case .number(let value): try container.encode(value) case .bool(let value): try container.encode(value) case .null: try container.encodeNil() } } } /// 图传区域的明确状态;本地视频预览不代表设备连接或真实图传成功。 enum DroneDockLiveState: Equatable, Sendable { case restricted case idle case loading case unavailable(String) case failed(String) case localPreview(URL) case connecting case playing } /// live 的首选流,按 url_type 选择已明确支持的播放器。 struct DroneDockLiveStream: Decodable, Equatable, Sendable { let available: Bool? let role: String? let deviceSN: String? let urlType: String? let url: String? let expiresAt: Int? /// 后端播放字段映射。 private enum CodingKeys: String, CodingKey { case available, role, url case deviceSN = "device_sn", urlType = "url_type", expiresAt = "expire_ts" } } /// 只用于递归脱敏 JSON,避免字典转换时丢失字段名。 private extension Dictionary where Key == String { func mapValuesWithKeys(_ transform: (String, Value) throws -> T) rethrows -> [String: T] { try Dictionary(uniqueKeysWithValues: map { (key, value) in (key, try transform(key, value)) }) } }