import Foundation /// 朱工 command 接口明确允许的四类安全指令,不接受服务端未知动作直接透传。 enum DroneDockSafetyCommand: String, CaseIterable, Sendable { /// 暂停航线并悬停待命。 case pause = "flighttask_pause" /// 恢复已暂停的航线。 case resume = "flighttask_recovery" /// 立即返航;服务端会中断当前任务并释放飞行锁。 case returnHome = "return_home" /// 取消返航,不代表已恢复或重新创建原拍摄任务。 case cancelReturn = "return_home_cancel" } /// 指令受理回执;仅保留后端返回的身份、读数与状态,不推断设备已完成动作。 struct DroneDockCommandResponse: Decodable, Equatable, Sendable { /// 文档要求回显的订单飞行任务 ID。 let orderFlightTaskID: Int? /// 本次回显的指令码,API 核验其与请求一致。 let command: String? /// 任务实际锁定的机场标识,客户端不提交或覆盖它。 let airportSN: String? /// 司空状态允许任意 JSON 类型;null 与缺失都不构成执行成功证据。 let djiStatus: DroneDockHomeJSONValue? /// 非实时采集的六项读数,不替代 shooting 完整快照。 let metrics: [DroneDockHomeMetric] /// 完整 data 格式化 JSON,供本地模拟联调查看未知字段与原始类型。 let dataJSON: String /// 朱工指令回执字段映射。 private enum CodingKeys: String, CodingKey { case orderFlightTaskID = "order_flight_task_id", command, airportSN = "airport_sn" case djiStatus = "dji_status", metrics } /// 不补造身份、状态或读数,完整 JSON 仍保留实际出现的 null 字段。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) orderFlightTaskID = try container.decodeIfPresent(Int.self, forKey: .orderFlightTaskID) command = try container.decodeIfPresent(String.self, forKey: .command) airportSN = try container.decodeIfPresent(String.self, forKey: .airportSN) djiStatus = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .djiStatus) 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(DroneDockCommandJSONSnapshot(value: raw)), as: UTF8.self) } } /// 将现有 JSON 值完整格式化,不更改指令回执的原始值与结构。 private struct DroneDockCommandJSONSnapshot: Encodable { let value: DroneDockHomeJSONValue /// 保留字符串、数字、布尔、null 和嵌套对象,不添加业务成功推断。 func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch value { case .object(let object): try container.encode(object.mapValues { DroneDockCommandJSONSnapshot(value: $0) }) case .array(let array): try container.encode(array.map { DroneDockCommandJSONSnapshot(value: $0) }) case .string(let text): try container.encode(text) case .number(let number): try container.encode(number) case .bool(let flag): try container.encode(flag) case .null: try container.encodeNil() } } }