feat: 完善机巢任务状态展示与景区空境库入口

拍摄任务刷新时保留上次成功数据,失败状态按产品网页展示原因和不可召唤提示;新增受权限控制的景区空境库入口,使用当前景区空态。同步测试与接口说明。

验证:模拟器机巢相关 65 项测试、空境库及首页菜单相关 30 项测试通过。
This commit is contained in:
2026-10-29 17:47:28 +08:00
parent 1173207756
commit e827bf8725
18 changed files with 1055 additions and 98 deletions
@@ -75,11 +75,14 @@ struct DroneDockShootingTaskIdentity: Decodable, Equatable, Sendable {
let waylineStatus: Int?
/// 服务端航线状态文案;执行失败优先于笼统的订单任务执行中。
let waylineStatusLabel: String?
/// 服务端实际记录的任务结果原因,空值不视为具体失败说明。
let resultMessage: String?
/// 已确认嵌套任务字段映射。
private enum CodingKeys: String, CodingKey {
case id, orderNumber = "order_number", scenicID = "scenic_id", status, statusLabel = "status_label"
case waylineStatus = "wayline_status", waylineStatusLabel = "wayline_status_label"
case resultMessage = "result_message"
}
}
@@ -142,6 +145,10 @@ struct DroneDockShootingPlane: Decodable, Equatable, Sendable {
let id: Int?
/// 当前设备序列号。
let snNumber: String?
/// 服务端连接状态;实测 2 为离线,不从电量推导连接状态。
let onlineStatus: Int?
/// 文档工作状态:0 离线、1 空闲、2 拍摄中;不据此推断返航完成。
let workStatus: Int?
/// 服务端工作状态文案。
let workStatusLabel: String?
/// 服务端完整状态文案。
@@ -150,6 +157,7 @@ struct DroneDockShootingPlane: Decodable, Equatable, Sendable {
/// 当前设备字段映射。
private enum CodingKeys: String, CodingKey {
case id, snNumber = "sn_number", workStatusLabel = "work_status_label", statusText = "status_text"
case onlineStatus = "online_status", workStatus = "work_status"
}
}
@@ -34,6 +34,8 @@ enum DroneDockTaskDisplayAction: Equatable, Sendable {
case start
/// 恢复拍摄占位动作,本轮禁用。
case resume
/// 失败后的重新召唤占位动作;尚未接入重新准备流程,始终禁用。
case retry
/// 查看已明确完成任务的订单素材。
case materials
/// 当前无主动作。
@@ -54,6 +56,21 @@ struct DroneDockTaskPresentation: Equatable, Sendable {
let action: DroneDockTaskDisplayAction
/// 主动作名称,无动作时为 nil。
let actionTitle: String?
/// 是否可触发动作;未对接的开始和恢复始终为 false。
/// 是否可触发动作;未对接的开始、恢复和重新召唤始终为 false。
let isActionEnabled: Bool
/// 主动作下方的真实设备限制说明,未提供时不展示。
let actionNote: String?
/// 创建状态展示;动作提示默认为空,既有非失败状态保持原布局。
init(phase: DroneDockTaskDisplayPhase, title: String, message: String, deviceStatus: String,
action: DroneDockTaskDisplayAction, actionTitle: String?, isActionEnabled: Bool, actionNote: String? = nil) {
self.phase = phase
self.title = title
self.message = message
self.deviceStatus = deviceStatus
self.action = action
self.actionTitle = actionTitle
self.isActionEnabled = isActionEnabled
self.actionNote = actionNote
}
}
@@ -8,7 +8,7 @@ final class DroneDockShootingTaskViewModel {
let taskID: Int
/// 进入页面携带的订单摘要,仅作未返回展示字段时的订单文案备用。
let order: DroneDockHomeOrder
/// 当前通过身份检查的服务端快照。
/// 最近一次通过身份检查的服务端快照,刷新期间保留,成功后整体替换。
private(set) var response: DroneDockShootingResponse?
/// 同一页面最多存在一个读取请求。
private(set) var isLoading = false
@@ -31,7 +31,7 @@ final class DroneDockShootingTaskViewModel {
self.currentContext = currentContext
}
/// 页面可见时读取同一任务;失败、取消或身份失效不沿用之前的执行门禁与读数。
/// 页面可见时读取同一任务;刷新及普通失败保留旧快照,身份失效立即清除。
@MainActor
func refresh() async {
guard !isLoading, !identityInvalidated else { return }
@@ -39,16 +39,15 @@ final class DroneDockShootingTaskViewModel {
let requestGeneration = generation
isLoading = true
errorMessage = nil
response = nil
defer {
if generation == requestGeneration {
isLoading = false
if Task.isCancelled { response = nil }
}
}
guard accepts(requestGeneration) else { return }
guard context.canLoad, taskID > 0,
DroneDockHomeMetric.usableText(order.orderNumber) != nil else {
response = nil
errorMessage = "缺少有效任务、订单或景区信息,请返回工作台重新进入"
return
}
@@ -56,23 +55,29 @@ final class DroneDockShootingTaskViewModel {
let value = try await service.shooting(taskID: taskID, liveBattery: true)
guard accepts(requestGeneration) else { return }
guard matchesIdentity(value) else {
response = nil
throw APIError.decodeFailed("拍摄任务返回的任务、订单或景区与当前页面不一致,请重新读取")
}
response = value
} catch {
guard accepts(requestGeneration) else { return }
response = nil
errorMessage = error is CancellationError ? nil : error.localizedDescription
}
}
/// 离开或进入后台时使旧请求失效;再次进入仍读取同一任务,不创建或召唤。
/// 使旧请求失效;同一身份离页或进入后台可保留快照,账号变化始终清除并停止旧任务读取。
@MainActor
func invalidate() {
func invalidate(preservingSnapshot: Bool = false) {
generation &+= 1
isLoading = false
response = nil
if !identityInvalidated { errorMessage = nil }
if !context.hasSameIdentity(as: currentContext()) { identityInvalidated = true }
if identityInvalidated {
response = nil
errorMessage = "账号或景区已变化,请返回工作台重新进入"
} else {
if !preservingSnapshot { response = nil }
errorMessage = nil
}
}
/// 优先使用当前接口标题,缺失时展示当前接口的点位和航线名称。
@@ -97,26 +102,32 @@ final class DroneDockShootingTaskViewModel {
/// 使用服务端阶段标题或任务状态,不从 GET 成功推导到达或拍摄完成。
var statusTitle: String {
if errorMessage != nil { return "任务状态读取失败" }
if isLoading { return "正在获取任务状态" }
if response == nil {
if errorMessage != nil { return "任务状态读取失败" }
if isLoading { return "正在获取任务状态" }
}
if resolvedDisplayPhase().0 == .failed { return "任务执行失败" }
return usable(response?.ui?.title) ?? usable(response?.task?.statusLabel)
?? usable(response?.orderFlightTask?.statusLabel) ?? "状态待确认"
}
/// 保留服务端阶段说明与执行门禁原因;本模块不执行门禁给出的动作。
var statusMessage: String {
if let errorMessage { return errorMessage }
if isLoading { return "正在读取当前拍摄任务" }
if response == nil {
if let errorMessage { return errorMessage }
if isLoading { return "正在读取当前拍摄任务" }
}
if resolvedDisplayPhase().0 == .failed { return failureMessage }
return usable(response?.ui?.message) ?? usable(response?.executeGate?.message) ?? "—"
}
/// 组合可信阶段与本轮可用动作,不从请求成功、执行中或指令列表推断飞行进度。
var presentation: DroneDockTaskPresentation {
if isLoading {
if response == nil, isLoading {
return DroneDockTaskPresentation(phase: .loading, title: "正在获取任务状态", message: "正在读取当前拍摄任务",
deviceStatus: deviceStatusText, action: .none, actionTitle: nil, isActionEnabled: false)
}
if let errorMessage {
if response == nil, let errorMessage {
return DroneDockTaskPresentation(phase: .readError, title: "任务状态读取失败", message: errorMessage,
deviceStatus: deviceStatusText, action: .refresh, actionTitle: "重新读取", isActionEnabled: !identityInvalidated)
}
@@ -139,13 +150,19 @@ final class DroneDockShootingTaskViewModel {
action = .materials
actionTitle = "查看素材"
enabled = true
case .failed:
action = .retry
actionTitle = "重新召唤无人机"
enabled = false
default:
action = .refresh
actionTitle = "刷新状态"
enabled = !identityInvalidated
}
return DroneDockTaskPresentation(phase: phase, title: title, message: message,
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle, isActionEnabled: enabled)
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle,
isActionEnabled: enabled && !isLoading && !identityInvalidated && (errorMessage == nil || action == .refresh),
actionNote: phase == .failed ? retryUnavailableNote : nil)
}
/// 后端六项展示读数,不解析文案反推设备状态或数值单位。
@@ -155,21 +172,21 @@ final class DroneDockShootingTaskViewModel {
windText: text("wind_speed"), rainText: text("rainfall"), signalText: text("signal"), satelliteText: text("satellite"))
}
/// 无有效快照时不展示旧读数;连接 false 自身不被转换为飞行阶段。
/// 仅无快照时使用未知读数;刷新期间继续展示最近一次成功快照。
var isTelemetryStale: Bool { response == nil }
/// 完整服务端 data,供页面展示和联调查看。
var dataJSON: String? { response?.dataJSON }
@MainActor
private func accepts(_ requestGeneration: Int) -> Bool {
guard generation == requestGeneration, !Task.isCancelled else { return false }
guard generation == requestGeneration else { return false }
guard context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
identityInvalidated = true
invalidate()
errorMessage = "账号或景区已变化,请返回工作台重新进入"
return false
}
return true
return !Task.isCancelled
}
private func matchesIdentity(_ value: DroneDockShootingResponse) -> Bool {
@@ -222,12 +239,7 @@ final class DroneDockShootingTaskViewModel {
private func presentationTitle(for phase: DroneDockTaskDisplayPhase, conflict: Bool) -> String {
if conflict { return "状态待确认" }
let tasks = [response?.task, response?.orderFlightTask].compactMap { $0 }
if phase == .failed, let failed = tasks.first(where: { $0.waylineStatus == 91 }) {
return usable(failed.waylineStatusLabel) ?? "任务执行失败"
}
if phase == .failed, let failed = tasks.first(where: { $0.status == 40 }) {
return usable(failed.statusLabel) ?? "任务执行失败"
}
if phase == .failed { return "任务执行失败" }
if phase == .blocked, let cancelled = tasks.first(where: { $0.status == 50 }) {
return usable(cancelled.statusLabel) ?? "任务已取消"
}
@@ -251,6 +263,24 @@ final class DroneDockShootingTaskViewModel {
}
private func usable(_ value: String?) -> String? { DroneDockHomeMetric.usableText(value) }
/// 明确失败后优先展示任务结果原因,再保留执行门禁的实际限制,不用通用阶段标题充当原因。
private var failureMessage: String {
usable(response?.orderFlightTask?.resultMessage) ?? usable(response?.task?.resultMessage)
?? usable(response?.executeGate?.message) ?? usable(response?.ui?.message) ?? "暂未返回具体失败原因"
}
/// 重新召唤尚未接入;只补充明确设备状态,不将开始许可、空闲或失败推断为允许重试。
private var retryUnavailableNote: String {
let plane = response?.currentPlane
if plane?.onlineStatus == 2 || plane?.workStatus == 0 || usable(plane?.workStatusLabel) == "离线" {
return "无人机离线,暂不可召唤"
}
if let status = usable(plane?.workStatusLabel), ["返航中", "正在返航"].contains(status) {
return "无人机返航中,暂不可召唤"
}
return "暂不可重新召唤,请返回准备页确认设备状态"
}
}
/// 任务页六项读数的后端展示文本,保持服务端单位与零值。