feat(drone-dock): 接通任务召唤与状态页并固定拍摄布局
接入 plane-status、tasks/create、shoot 与 shooting,保持同一任务 ID 串接、重复提交保护和只读单次刷新。拍摄任务页固定顶部参数与底部安全控制,中间状态区域适配小屏且不滚动;同步接口记录、脱敏响应及相关测试。
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
import Foundation
|
||||
|
||||
/// 真实拍摄任务页只读状态,只按准备页传入的既有任务 ID 读取 shooting。
|
||||
final class DroneDockShootingTaskViewModel {
|
||||
/// 页面绑定的账号、景区和门店身份。
|
||||
let context: DroneDockHomeContext
|
||||
/// 创建并召唤时使用的同一订单飞行任务 ID。
|
||||
let taskID: Int
|
||||
/// 进入页面携带的订单摘要,仅作未返回展示字段时的订单文案备用。
|
||||
let order: DroneDockHomeOrder
|
||||
/// 当前通过身份检查的服务端快照。
|
||||
private(set) var response: DroneDockShootingResponse?
|
||||
/// 同一页面最多存在一个读取请求。
|
||||
private(set) var isLoading = false
|
||||
/// 读取失败或身份变化时的提示。
|
||||
private(set) var errorMessage: String?
|
||||
|
||||
private let service: any DroneDockShootingServing
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var generation = 0
|
||||
private var identityInvalidated = false
|
||||
|
||||
/// 只注入读取服务,不依赖创建、召唤、开始拍摄或控制服务。
|
||||
init(context: DroneDockHomeContext, taskID: Int, order: DroneDockHomeOrder,
|
||||
service: any DroneDockShootingServing,
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.context = context
|
||||
self.taskID = taskID
|
||||
self.order = order
|
||||
self.service = service
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
/// 页面可见时读取同一任务;失败、取消或身份失效不沿用之前的执行门禁与读数。
|
||||
@MainActor
|
||||
func refresh() async {
|
||||
guard !isLoading, !identityInvalidated else { return }
|
||||
generation &+= 1
|
||||
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 {
|
||||
errorMessage = "缺少有效任务、订单或景区信息,请返回工作台重新进入"
|
||||
return
|
||||
}
|
||||
do {
|
||||
let value = try await service.shooting(taskID: taskID, liveBattery: true)
|
||||
guard accepts(requestGeneration) else { return }
|
||||
guard matchesIdentity(value) else {
|
||||
throw APIError.decodeFailed("拍摄任务返回的任务、订单或景区与当前页面不一致,请重新读取")
|
||||
}
|
||||
response = value
|
||||
} catch {
|
||||
guard accepts(requestGeneration) else { return }
|
||||
response = nil
|
||||
errorMessage = error is CancellationError ? nil : error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 离开或进入后台时使旧请求失效;再次进入仍读取同一任务,不创建或召唤。
|
||||
@MainActor
|
||||
func invalidate() {
|
||||
generation &+= 1
|
||||
isLoading = false
|
||||
response = nil
|
||||
if !identityInvalidated { errorMessage = nil }
|
||||
}
|
||||
|
||||
/// 优先使用当前接口标题,缺失时展示当前接口的点位和航线名称。
|
||||
var title: String {
|
||||
if let title = usable(response?.header?.title) { return title }
|
||||
let names = [usable(response?.scenicSpot?.name), usable(response?.airLine?.name)].compactMap { $0 }
|
||||
return names.isEmpty ? "拍摄任务" : names.joined(separator: " · ")
|
||||
}
|
||||
|
||||
/// 服务端游客和套餐文案优先,后端已返回的套餐不被进入页摘要覆盖。
|
||||
var subtitle: String {
|
||||
let tourist = usable(response?.header?.touristLabel) ?? usable(order.touristPhoneMask)
|
||||
let package = usable(response?.header?.packageName) ?? usable(order.packageName)
|
||||
let parts = [tourist, package].compactMap { $0 }
|
||||
return parts.isEmpty ? "—" : parts.joined(separator: " · ")
|
||||
}
|
||||
|
||||
/// 只展示本任务当前设备状态,不以景区推荐设备替代任务锁定设备。
|
||||
var deviceStatusText: String {
|
||||
usable(response?.currentPlane?.statusText) ?? usable(response?.currentPlane?.workStatusLabel) ?? "—"
|
||||
}
|
||||
|
||||
/// 使用服务端阶段标题或任务状态,不从 GET 成功推导到达或拍摄完成。
|
||||
var statusTitle: String {
|
||||
if errorMessage != nil { return "任务状态读取失败" }
|
||||
if isLoading { 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 "正在读取当前拍摄任务" }
|
||||
return usable(response?.ui?.message) ?? usable(response?.executeGate?.message) ?? "—"
|
||||
}
|
||||
|
||||
/// 组合可信阶段与本轮可用动作,不从请求成功、执行中或指令列表推断飞行进度。
|
||||
var presentation: DroneDockTaskPresentation {
|
||||
if isLoading {
|
||||
return DroneDockTaskPresentation(phase: .loading, title: "正在获取任务状态", message: "正在读取当前拍摄任务",
|
||||
deviceStatus: deviceStatusText, action: .none, actionTitle: nil, isActionEnabled: false)
|
||||
}
|
||||
if let errorMessage {
|
||||
return DroneDockTaskPresentation(phase: .readError, title: "任务状态读取失败", message: errorMessage,
|
||||
deviceStatus: deviceStatusText, action: .refresh, actionTitle: "重新读取", isActionEnabled: !identityInvalidated)
|
||||
}
|
||||
let (phase, conflict) = resolvedDisplayPhase()
|
||||
let message = conflict ? "任务状态信息不一致,请刷新后确认。" + (statusMessage == "—" ? "" : statusMessage) : statusMessage
|
||||
let title = presentationTitle(for: phase, conflict: conflict)
|
||||
let action: DroneDockTaskDisplayAction
|
||||
let actionTitle: String?
|
||||
let enabled: Bool
|
||||
switch phase {
|
||||
case .ready:
|
||||
action = .start
|
||||
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "开始拍摄"
|
||||
enabled = false
|
||||
case .paused where response?.executeGate?.canExecute == true:
|
||||
action = .resume
|
||||
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "继续拍摄"
|
||||
enabled = false
|
||||
case .completed:
|
||||
action = .materials
|
||||
actionTitle = "查看素材"
|
||||
enabled = true
|
||||
default:
|
||||
action = .refresh
|
||||
actionTitle = "刷新状态"
|
||||
enabled = !identityInvalidated
|
||||
}
|
||||
return DroneDockTaskPresentation(phase: phase, title: title, message: message,
|
||||
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle, isActionEnabled: enabled)
|
||||
}
|
||||
|
||||
/// 后端六项展示读数,不解析文案反推设备状态或数值单位。
|
||||
var telemetry: DroneDockShootingTelemetry {
|
||||
func text(_ key: String) -> String { response?.metrics.first(where: { $0.key == key })?.displayText ?? "—" }
|
||||
return DroneDockShootingTelemetry(batteryText: text("battery"), temperatureText: text("env_temp"),
|
||||
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 context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
|
||||
identityInvalidated = true
|
||||
invalidate()
|
||||
errorMessage = "账号或景区已变化,请返回工作台重新进入"
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func matchesIdentity(_ value: DroneDockShootingResponse) -> Bool {
|
||||
let taskIDs = [value.orderFlightTask?.id, value.task?.id].compactMap { $0 }
|
||||
let orderNumbers = [value.orderNumber, value.orderFlightTask?.orderNumber,
|
||||
value.task?.orderNumber, value.header?.orderNumber].compactMap { $0 }
|
||||
let scenicIDs = [value.scenic?.id, value.orderFlightTask?.scenicID,
|
||||
value.task?.scenicID, value.planeStatus?.scenicID].compactMap { $0 }
|
||||
return taskIDs.allSatisfy { $0 == taskID }
|
||||
&& orderNumbers.allSatisfy { $0 == order.orderNumber }
|
||||
&& scenicIDs.allSatisfy { $0 == context.scenicID }
|
||||
}
|
||||
|
||||
/// 只认识显式阶段和已确认任务状态;91 为真实航线执行失败,不把订单状态 20 当作正在拍摄。
|
||||
private func resolvedDisplayPhase() -> (DroneDockTaskDisplayPhase, Bool) {
|
||||
guard let response else { return (.unknown, false) }
|
||||
let tasks = [response.task, response.orderFlightTask].compactMap { $0 }
|
||||
let statuses = Set(tasks.compactMap(\.status))
|
||||
if tasks.contains(where: { $0.waylineStatus == 91 }) || statuses.contains(40) { return (.failed, false) }
|
||||
if statuses.contains(50) { return (.blocked, false) }
|
||||
if statuses.count > 1 { return (.unknown, true) }
|
||||
let known: [String: DroneDockTaskDisplayPhase] = [
|
||||
"navigating": .navigating, "arrived_pending_start": .ready, "shooting": .shooting,
|
||||
"paused": .paused, "returning": .returning, "completed": .completed, "failed": .failed, "blocked": .blocked
|
||||
]
|
||||
let rawPhase = usable(response.ui?.phase)
|
||||
let phase = rawPhase.flatMap { known[$0] }
|
||||
if rawPhase != nil && phase == nil { return (.unknown, false) }
|
||||
if phase == .failed { return (.failed, false) }
|
||||
if statuses.contains(30) {
|
||||
if phase == .returning { return (.returning, false) }
|
||||
if let phase, [.navigating, .ready, .shooting, .paused].contains(phase) { return (.unknown, true) }
|
||||
return (.completed, false)
|
||||
}
|
||||
if phase == .completed {
|
||||
return statuses.isEmpty ? (.completed, false) : (.unknown, true)
|
||||
}
|
||||
if phase == .ready {
|
||||
guard let allowed = response.executeGate?.canExecute else { return (.unknown, false) }
|
||||
if !allowed { return (.blocked, false) }
|
||||
if let action = usable(response.executeGate?.primaryAction), action != "start_shoot" {
|
||||
return (.unknown, true)
|
||||
}
|
||||
}
|
||||
if phase == .paused, response.executeGate?.canExecute == true,
|
||||
response.executeGate?.primaryAction == "blocked" { return (.unknown, true) }
|
||||
return (phase ?? .unknown, false)
|
||||
}
|
||||
|
||||
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 == .blocked, let cancelled = tasks.first(where: { $0.status == 50 }) {
|
||||
return usable(cancelled.statusLabel) ?? "任务已取消"
|
||||
}
|
||||
if phase == .blocked, response?.ui?.phase == "arrived_pending_start" { return "暂不可开始拍摄" }
|
||||
if phase == .completed {
|
||||
return usable(response?.ui?.phase == "completed" ? response?.ui?.title : nil)
|
||||
?? tasks.first(where: { $0.status == 30 }).flatMap { usable($0.statusLabel) } ?? "拍摄已完成"
|
||||
}
|
||||
if phase == .unknown { return "状态待确认" }
|
||||
if let title = usable(response?.ui?.title) { return title }
|
||||
switch phase {
|
||||
case .navigating: return "无人机正在前往拍摄点"
|
||||
case .ready: return "无人机已到达"
|
||||
case .shooting: return "正在拍摄"
|
||||
case .paused: return "拍摄已暂停"
|
||||
case .returning: return "无人机正在返航"
|
||||
case .failed: return "任务执行失败"
|
||||
case .blocked: return "当前任务暂不可执行"
|
||||
default: return "状态待确认"
|
||||
}
|
||||
}
|
||||
|
||||
private func usable(_ value: String?) -> String? { DroneDockHomeMetric.usableText(value) }
|
||||
}
|
||||
|
||||
/// 任务页六项读数的后端展示文本,保持服务端单位与零值。
|
||||
struct DroneDockShootingTelemetry: Equatable, Sendable {
|
||||
/// 电量读数。
|
||||
let batteryText: String
|
||||
/// 环境温度读数。
|
||||
let temperatureText: String
|
||||
/// 风速读数。
|
||||
let windText: String
|
||||
/// 雨量读数。
|
||||
let rainText: String
|
||||
/// 信号状态。
|
||||
let signalText: String
|
||||
/// 搜星状态。
|
||||
let satelliteText: String
|
||||
}
|
||||
Reference in New Issue
Block a user