通过 airport/home 读取当前景区和门店的设备与统计,隔离失效会话回包,未知遥测显示破折号。完善状态圆点、分割线和卫星图标;订单详情等待后端测试数据。 验证:iPhone 11 上 49 项相关测试、样式调整后 9 项界面回归,以及测试样例匿名化后 20 项测试均通过。真实联调响应、日志和截图仅保留在本地。
232 lines
11 KiB
Swift
232 lines
11 KiB
Swift
import Foundation
|
|
|
|
/// 朱工首页接口的已验证结构;数组缺失或类型异常视为解析错误,不伪装成空列表。
|
|
struct DroneDockHomeResponse: Decodable, Equatable, Sendable {
|
|
/// 服务端返回的数据所属景区。
|
|
let scenicID: Int
|
|
/// 当前景区的机巢设备,按真实非空响应的字段读取。
|
|
let airports: [DroneDockHomeAirport]
|
|
/// 订单原始记录;不将未知字段猜测成演示订单。
|
|
let orders: [DroneDockHomeRecord]
|
|
/// 今日数据缺失时保留未知,不生成零值统计。
|
|
let today: DroneDockHomeToday?
|
|
|
|
/// 已验证的首页顶层字段名。
|
|
private enum CodingKeys: String, CodingKey {
|
|
case scenicID = "scenic_id"
|
|
case airports, orders, today
|
|
}
|
|
|
|
/// 严格读取首页骨架,兼容服务端使用整数或整数字符串表示景区 ID。
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
scenicID = try container.decode(DroneDockHomeInteger.self, forKey: .scenicID).value
|
|
guard scenicID > 0 else {
|
|
throw DecodingError.dataCorruptedError(forKey: .scenicID, in: container, debugDescription: "景区 ID 必须为正整数")
|
|
}
|
|
airports = try container.decode([DroneDockHomeAirport].self, forKey: .airports)
|
|
guard Set(airports.map(\.id)).count == airports.count else {
|
|
throw DecodingError.dataCorruptedError(forKey: .airports, in: container, debugDescription: "设备 ID 重复,无法确定设备身份")
|
|
}
|
|
orders = try container.decode([DroneDockHomeRecord].self, forKey: .orders)
|
|
today = try container.decodeIfPresent(DroneDockHomeToday.self, forKey: .today)
|
|
}
|
|
}
|
|
|
|
/// 首页设备卡的数据,只表达后端展示信息,不推导起飞或拍摄许可。
|
|
struct DroneDockHomeAirport: Decodable, Equatable, Sendable {
|
|
/// 后端设备稳定 ID,用于 Diffable 项身份。
|
|
let id: Int
|
|
/// 后端设备名称。
|
|
let name: String
|
|
/// 后端设备序列号,不将它与尚未确认的飞控参数混用。
|
|
let snNumber: String?
|
|
/// 后端设备所属景区 ID。
|
|
let scenicAreaID: Int?
|
|
/// 原始在线状态;尚未取得完整枚举约定。
|
|
let onlineStatus: Int?
|
|
/// 原始工作状态;不据此决定设备可否执行任务。
|
|
let workStatus: Int?
|
|
/// 后端工作状态展示文案。
|
|
let workStatusLabel: String?
|
|
/// 原始接单配置;不等同于本次起飞许可。
|
|
let isReceiveOrder: Int?
|
|
/// 任务类型原始值;当前样本为 null,暂不假定其枚举或值类型。
|
|
let taskType: DroneDockHomeJSONValue?
|
|
/// 后端读数列表,展示时按稳定 key 匹配,不依赖返回顺序。
|
|
let metrics: [DroneDockHomeMetric]
|
|
|
|
/// 已实测的设备字段名。
|
|
private enum CodingKeys: String, CodingKey {
|
|
case id, name, metrics
|
|
case snNumber = "sn_number"
|
|
case scenicAreaID = "scenic_area_id"
|
|
case onlineStatus = "online_status"
|
|
case workStatus = "work_status"
|
|
case workStatusLabel = "work_status_label"
|
|
case isReceiveOrder = "is_receive_order"
|
|
case taskType = "task_type"
|
|
}
|
|
|
|
/// 读取实际设备结构;身份缺失时报错,缺失遥测保持未知。
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
id = try container.decode(DroneDockHomeInteger.self, forKey: .id).value
|
|
guard id > 0 else {
|
|
throw DecodingError.dataCorruptedError(forKey: .id, in: container, debugDescription: "设备 ID 必须为正整数")
|
|
}
|
|
name = try container.decode(String.self, forKey: .name)
|
|
snNumber = try container.decodeIfPresent(String.self, forKey: .snNumber)
|
|
scenicAreaID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .scenicAreaID)?.value
|
|
onlineStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .onlineStatus)?.value
|
|
workStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .workStatus)?.value
|
|
workStatusLabel = try container.decodeIfPresent(String.self, forKey: .workStatusLabel)
|
|
isReceiveOrder = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .isReceiveOrder)?.value
|
|
taskType = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .taskType)
|
|
metrics = try container.decodeIfPresent([DroneDockHomeMetric].self, forKey: .metrics) ?? []
|
|
}
|
|
|
|
/// 状态标签使用后端文案;缺失、空白或占位文案不显示成空闲。
|
|
var statusTitle: String { DroneDockHomeMetric.usableText(workStatusLabel) ?? "状态待确认" }
|
|
|
|
/// 只映射已验证的空闲状态样本,其余保持中性色,不新增业务状态推断。
|
|
var statusTone: DroneDockStatusTone { workStatus == 1 && statusTitle == "空闲" ? .green : .gray }
|
|
|
|
/// 返回指定读数的后端展示值,保留单位;未知值不从 raw 猜测生成。
|
|
func metricText(for key: String) -> String {
|
|
metrics.first { $0.key == key }?.displayText ?? "—"
|
|
}
|
|
|
|
/// 仅在电量原始值为 0~100 的真实数字时绘制环形进度,零电量与未知分开。
|
|
var batteryProgress: Double? {
|
|
guard let metric = metrics.first(where: { $0.key == "battery" }),
|
|
case let .number(value)? = metric.raw, value >= 0, value <= 100 else { return nil }
|
|
return NSDecimalNumber(decimal: value).doubleValue / 100
|
|
}
|
|
}
|
|
|
|
/// 首页单项设备读数,区分可展示的后端文案和未经解释的原始值。
|
|
struct DroneDockHomeMetric: Decodable, Equatable, Sendable {
|
|
/// 后端读数稳定键,如 battery、env_temp。
|
|
let key: String
|
|
/// 后端读数名称。
|
|
let label: String?
|
|
/// 后端原始读数;除合法数值电量环外不据此推测单位或设备许可。
|
|
let raw: DroneDockHomeJSONValue?
|
|
/// 后端格式化展示值。
|
|
let value: String?
|
|
|
|
/// 统一未知占位,但保留有效零值及服务端单位。
|
|
var displayText: String { Self.usableText(value) ?? "—" }
|
|
|
|
/// 将空白和仅由破折号组成的占位文案判为未知。
|
|
static func usableText(_ value: String?) -> String? {
|
|
guard let value else { return nil }
|
|
let text = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !text.isEmpty, !text.allSatisfy({ "-–—−― ".contains($0) }) else { return nil }
|
|
return text
|
|
}
|
|
}
|
|
|
|
/// 首页今日统计;保留服务端字段语义,未知项不显示为零。
|
|
struct DroneDockHomeToday: Decodable, Equatable, Sendable {
|
|
/// 服务端的今日订单数量。
|
|
let orderCount: Int?
|
|
/// 服务端的今日订单金额,使用十进制金额避免二进制浮点误差。
|
|
let orderAmount: Decimal?
|
|
/// 服务端 finished_shoot 数值,其业务统计口径仍需与后端核对。
|
|
let finishedShoot: Int?
|
|
|
|
/// 已验证的统计字段名。
|
|
private enum CodingKeys: String, CodingKey {
|
|
case orderCount = "order_count"
|
|
case orderAmount = "order_amount"
|
|
case finishedShoot = "finished_shoot"
|
|
}
|
|
|
|
/// 兼容数字和数字字符串;缺失或 null 保持 nil,非法文本拒绝解析。
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
orderCount = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .orderCount)?.value
|
|
orderAmount = try container.decodeIfPresent(DroneDockHomeDecimal.self, forKey: .orderAmount)?.value
|
|
finishedShoot = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .finishedShoot)?.value
|
|
}
|
|
}
|
|
|
|
/// 尚未取得真实样本的订单行,保留对象字段供后续精确接入和诊断。
|
|
struct DroneDockHomeRecord: Decodable, Equatable, Sendable {
|
|
/// 原始对象键值,不猜测订单状态或拍摄次数的字段别名。
|
|
let fields: [String: DroneDockHomeJSONValue]
|
|
|
|
/// 只接受 JSON 对象作为列表记录,拒绝 null、标量和嵌套数组。
|
|
init(from decoder: Decoder) throws {
|
|
fields = try decoder.singleValueContainer().decode([String: DroneDockHomeJSONValue].self)
|
|
}
|
|
}
|
|
|
|
/// 保留未知记录的 JSON 结构;数字采用 Decimal,不参与未确认业务计算。
|
|
indirect enum DroneDockHomeJSONValue: Decodable, Equatable, Sendable {
|
|
/// JSON 对象。
|
|
case object([String: DroneDockHomeJSONValue])
|
|
/// JSON 数组。
|
|
case array([DroneDockHomeJSONValue])
|
|
/// JSON 字符串。
|
|
case string(String)
|
|
/// JSON 数字。
|
|
case number(Decimal)
|
|
/// JSON 布尔值。
|
|
case bool(Bool)
|
|
/// JSON 空值。
|
|
case null
|
|
|
|
/// 按 JSON 实际类型解码,不把字符串自动转换为其他类型。
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
if container.decodeNil() { self = .null }
|
|
else if let value = try? container.decode(Bool.self) { self = .bool(value) }
|
|
else if let value = try? container.decode(String.self) { self = .string(value) }
|
|
else if let value = try? container.decode(Decimal.self) { self = .number(value) }
|
|
else if let value = try? container.decode([String: DroneDockHomeJSONValue].self) { self = .object(value) }
|
|
else { self = .array(try container.decode([DroneDockHomeJSONValue].self)) }
|
|
}
|
|
}
|
|
|
|
/// 已知整数统计字段的严格兼容解码器。
|
|
private struct DroneDockHomeInteger: 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: "需要整数或整数字符串")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 已知金额字段的十进制兼容解码器,拒绝只含部分合法数字的字符串。
|
|
private struct DroneDockHomeDecimal: Decodable {
|
|
let value: Decimal
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
if let text = try? container.decode(String.self) {
|
|
let normalized = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard normalized.range(of: #"^-?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$"#, options: .regularExpression) != nil,
|
|
let number = Decimal(string: normalized, locale: Locale(identifier: "en_US_POSIX")),
|
|
!number.isNaN else {
|
|
throw DecodingError.dataCorruptedError(in: container, debugDescription: "金额必须为有效十进制数")
|
|
}
|
|
value = number
|
|
} else {
|
|
value = try container.decode(Decimal.self)
|
|
guard !value.isNaN else {
|
|
throw DecodingError.dataCorruptedError(in: container, debugDescription: "金额必须为有效十进制数")
|
|
}
|
|
}
|
|
}
|
|
}
|