Files
suixinkan_uikit/suixinkan/Features/DroneDock/Models/DroneDockCommandModels.swift
T
lujiuyin e3979e9e32 feat: 完善机巢任务安全控制与状态诊断
同步新版飞控与 WebSocket 回执合同,完善独立指令等待、暂停继续和返航状态;增加真机诊断并优化倒计时与素材库展示。
2026-11-04 17:34:28 +08:00

101 lines
5.6 KiB
Swift

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 {
/// 明确受理标记,仅受理不表示设备已执行。
let accepted: Bool?
/// 回执探测时是否确认设备状态;页面最终仍读取完整 shooting/WS。
let deviceConfirmed: Bool?
/// 本次指令的服务端唯一标识。
let commandID: String?
/// 回显本次点击的幂等键,提供时必须与请求一致。
let clientRequestID: String?
/// 回执顶层的指令进度;完整任务快照位于 flight_control 内。
let pendingCommand: DroneDockCommandProgress?
/// 受理提示原文,用于诊断,不作为展示状态。
let hint: String?
/// 回执所属批次,必须与提交时一致。
let executionBatch: Int64?
/// 回执对应快照版本,用作后续状态核对的最低版本。
let stateVersion: Int64?
/// 设备身份在提供时必须匹配任务锁定设备。
let planeID: Int?
/// 回执中的设备状态不替代完整任务快照。
let flightControl: DroneDockFlightControl?
/// 文档要求回显的订单飞行任务 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 accepted, deviceConfirmed = "device_confirmed", hint, executionBatch = "execution_batch"
case commandID = "command_id", clientRequestID = "client_request_id", pendingCommand = "pending_command"
case stateVersion = "state_version", planeID = "plane_id", flightControl = "flight_control"
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)
accepted = try container.decodeIfPresent(Bool.self, forKey: .accepted)
deviceConfirmed = try container.decodeIfPresent(Bool.self, forKey: .deviceConfirmed)
commandID = try container.decodeIfPresent(String.self, forKey: .commandID)
clientRequestID = try container.decodeIfPresent(String.self, forKey: .clientRequestID)
pendingCommand = try container.decodeIfPresent(DroneDockCommandProgress.self, forKey: .pendingCommand)
hint = try container.decodeIfPresent(String.self, forKey: .hint)
executionBatch = try container.decodeIfPresent(Int64.self, forKey: .executionBatch)
stateVersion = try container.decodeIfPresent(Int64.self, forKey: .stateVersion)
planeID = try container.decodeIfPresent(Int.self, forKey: .planeID)
flightControl = try container.decodeIfPresent(DroneDockFlightControl.self, forKey: .flightControl)
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()
}
}
}