import Foundation /// 朱工提供的景区无人机状态快照,推荐设备由服务端选择。 struct DroneDockPlaneStatusResponse: Decodable, Equatable, Sendable { /// 本次状态所属景区,必须与请求一致。 let scenicID: Int /// 当前景区的设备状态;空数组表示没有设备。 let planes: [DroneDockPlaneStatus] /// 服务端推荐设备,缺失时不在客户端另选一台。 let recommend: DroneDockPlaneStatus? /// 服务端是否存在空闲设备,未知时不当作 false。 let hasIdle: Bool? /// 服务端是否存在拍摄中的设备。 let hasShooting: Bool? /// 景区连接状态,缺失时仍为未知。 let connected: Bool? /// 服务端建议的轮询间隔;实际定时由页面生命周期控制。 let pollHintMS: Int? /// 保留 plane-status 原始字段映射,不复用首页的不同状态枚举。 private enum CodingKeys: String, CodingKey { case scenicID = "scenic_id", planes, recommend case hasIdle = "has_idle", hasShooting = "has_shooting", connected, pollHintMS = "poll_hint_ms" } /// 校验景区和设备身份,不把缺失或错误列表当作正常空态。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) scenicID = try container.decode(DroneDockPlaneStatusInteger.self, forKey: .scenicID).value guard scenicID > 0 else { throw DecodingError.dataCorruptedError(forKey: .scenicID, in: container, debugDescription: "景区 ID 必须为正整数") } planes = try container.decode([DroneDockPlaneStatus].self, forKey: .planes) guard Set(planes.map(\.id)).count == planes.count else { throw DecodingError.dataCorruptedError(forKey: .planes, in: container, debugDescription: "无人机 ID 重复") } recommend = try container.decodeIfPresent(DroneDockPlaneStatus.self, forKey: .recommend) hasIdle = try container.decodeIfPresent(Bool.self, forKey: .hasIdle) hasShooting = try container.decodeIfPresent(Bool.self, forKey: .hasShooting) connected = try container.decodeIfPresent(Bool.self, forKey: .connected) pollHintMS = try container.decodeIfPresent(DroneDockPlaneStatusInteger.self, forKey: .pollHintMS)?.value } } /// 单台无人机的工作状态和电量,仅供准备页展示,不代表召唤许可。 struct DroneDockPlaneStatus: Decodable, Equatable, Sendable { /// 后端设备稳定身份。 let id: Int /// 设备序列号,不作为飞行任务 ID 使用。 let snNumber: String? /// 后端设备名称。 let name: String? /// 0 离线、1 空闲、2 拍摄中;其他整数原样保留。 let workStatus: Int? /// 后端工作状态名称。 let workStatusLabel: String? /// 原始电量百分比,0 与缺失分开表示。 let batteryPercent: Int? /// 后端格式化电量文案。 let batteryLabel: String? /// 后端设备可飞标记,不据此启用当前订单的召唤按钮。 let canFly: Bool? /// 准备页底栏优先展示的完整状态文案。 let statusText: String? /// 后端字段映射。 private enum CodingKeys: String, CodingKey { case id, snNumber = "sn_number", name, workStatus = "work_status", workStatusLabel = "work_status_label" case batteryPercent = "battery_percent", batteryLabel = "battery_label", canFly = "can_fly", statusText = "status_text" } /// 状态允许未知,设备身份必须明确;不从电量或工作状态推导 can_fly。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(DroneDockPlaneStatusInteger.self, forKey: .id).value guard id > 0 else { throw DecodingError.dataCorruptedError(forKey: .id, in: container, debugDescription: "无人机 ID 必须为正整数") } snNumber = try container.decodeIfPresent(String.self, forKey: .snNumber) name = try container.decodeIfPresent(String.self, forKey: .name) workStatus = try container.decodeIfPresent(DroneDockPlaneStatusInteger.self, forKey: .workStatus)?.value workStatusLabel = try container.decodeIfPresent(String.self, forKey: .workStatusLabel) batteryPercent = try container.decodeIfPresent(DroneDockPlaneStatusInteger.self, forKey: .batteryPercent)?.value batteryLabel = try container.decodeIfPresent(String.self, forKey: .batteryLabel) canFly = try container.decodeIfPresent(Bool.self, forKey: .canFly) statusText = try container.decodeIfPresent(String.self, forKey: .statusText) } } /// 兼容整数与整数字符串,拒绝布尔值、浮点截断及非法数字。 private struct DroneDockPlaneStatusInteger: Decodable { let value: Int init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() if let number = try? container.decode(Int.self) { value = number } else if let text = try? container.decode(String.self), let number = Int(text) { value = number } else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "需要整数或整数字符串") } } }