feat(drone-dock): 完善真实联调、状态轮询与准备页反馈
开放 Release 真实操作并保留 Debug 拦截;恢复前台 shooting 串行轮询;统一打印机巢请求与完整原值响应;优化长点位名称、未拍摄文案、报备失败及已结束任务提示,移除成功回执 UI。补充相关测试、离线策略检查和联调说明。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 拍摄任务页:正式入口读取状态并手动开启图传,飞控仅在隔离模拟入口注入。
|
||||
/// 拍摄任务页:前台轮询状态、手动图传;Release 开放已授权控制,Debug 仅模拟控制。
|
||||
@MainActor
|
||||
final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 入口传入的固定任务身份及只读状态,供导航协调和页面测试检查。
|
||||
@@ -23,6 +23,9 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private var bottomInset: CGFloat = -1
|
||||
private let materialsControllerFactory: @MainActor (DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController
|
||||
private let simulationRecordsProvider: (@MainActor () -> String)?
|
||||
private let shootingPollDelay: @Sendable (UInt64) async throws -> Void
|
||||
private var pollTask: Task<Void, Never>?
|
||||
private var pollGeneration = 0
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
private var startTask: Task<Void, Never>?
|
||||
private var commandTask: Task<Void, Never>?
|
||||
@@ -41,12 +44,14 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
init(viewModel: DroneDockShootingTaskViewModel,
|
||||
liveViewModel: DroneDockLiveViewModel? = nil,
|
||||
materialsControllerFactory: (@MainActor (DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)? = nil,
|
||||
simulationRecordsProvider: (@MainActor () -> String)? = nil) {
|
||||
simulationRecordsProvider: (@MainActor () -> String)? = nil,
|
||||
shootingPollDelay: @escaping @Sendable (UInt64) async throws -> Void = { try await Task.sleep(nanoseconds: $0) }) {
|
||||
self.viewModel = viewModel
|
||||
self.liveViewModel = liveViewModel ?? DroneDockLiveViewModel(
|
||||
context: viewModel.context, taskID: viewModel.taskID,
|
||||
currentContext: { DroneDockHomeViewController.currentContext() })
|
||||
self.simulationRecordsProvider = simulationRecordsProvider
|
||||
self.shootingPollDelay = shootingPollDelay
|
||||
self.materialsControllerFactory = materialsControllerFactory ?? { order, context in
|
||||
DroneDockMaterialsViewController(viewModel: DroneDockMaterialsViewModel(
|
||||
context: context, order: order,
|
||||
@@ -61,6 +66,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
deinit {
|
||||
pollTask?.cancel()
|
||||
refreshTask?.cancel()
|
||||
startTask?.cancel()
|
||||
commandTask?.cancel()
|
||||
@@ -186,10 +192,11 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
name: UIApplication.didBecomeActiveNotification, object: nil)
|
||||
}
|
||||
|
||||
/// 每次完整进入任务页只查询一次,不定时重读同一任务。
|
||||
/// 完整进入任务页立即读取,随后仅在前台可见时轮询同一任务。
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
super.viewDidAppear(animated)
|
||||
isPageVisible = true
|
||||
logEvent("page.appeared")
|
||||
isApplicationActive = UIApplication.shared.applicationState == .active
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible(automatically: true)
|
||||
@@ -199,6 +206,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
isPageVisible = false
|
||||
logEvent("page.disappeared")
|
||||
cancelRefresh()
|
||||
stopLive()
|
||||
}
|
||||
@@ -227,6 +235,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
|
||||
private func primaryActionTriggered() {
|
||||
logEvent("action.primary", fields: ["action": String(describing: viewModel.presentation.action),
|
||||
"enabled": viewModel.presentation.isActionEnabled])
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand,
|
||||
@@ -265,12 +275,14 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
/// 页面进入、回到前台或手动刷新时各读一次,请求结束后不安排下一次读取。
|
||||
private func refreshTaskIfVisible() {
|
||||
/// 前台可见时串行读取;轮询失败保留旧快照,下个周期再读,不重复弹出提示。
|
||||
private func refreshTaskIfVisible(isPolling: Bool = false) {
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand,
|
||||
!isResummoningAction, !viewModel.isResummoning else { return }
|
||||
cancelScheduledPoll()
|
||||
logEvent("shooting.refresh", fields: ["trigger": isPolling ? "poll" : "page_or_manual", "live_battery": 0])
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isRequesting = true
|
||||
@@ -283,13 +295,39 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
self.refreshTask = nil
|
||||
self.isRequesting = false
|
||||
self.applySnapshot()
|
||||
if self.viewModel.response != nil, let error = self.viewModel.errorMessage {
|
||||
self.scheduleNextPoll()
|
||||
if !isPolling, self.viewModel.response != nil, let error = self.viewModel.errorMessage {
|
||||
self.showToast("刷新失败,当前显示上次数据:\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 每次读取或控制流程结束后仅安排一个可取消的延时,不持有页面等待计时结束。
|
||||
private func scheduleNextPoll() {
|
||||
cancelScheduledPoll()
|
||||
guard isPageVisible, isApplicationActive, viewModel.canContinuePolling,
|
||||
!isRequesting, !isStartingAction, !isSendingSafetyAction, !isResummoningAction else { return }
|
||||
let generation = pollGeneration
|
||||
let delay = shootingPollDelay
|
||||
let interval = viewModel.pollIntervalNanoseconds
|
||||
logEvent("poll.scheduled", fields: ["delay_ms": interval / 1_000_000])
|
||||
pollTask = Task { @MainActor [weak self] in
|
||||
do { try await delay(interval) } catch { return }
|
||||
guard !Task.isCancelled, let self, self.pollGeneration == generation else { return }
|
||||
self.pollTask = nil
|
||||
self.refreshTaskIfVisible(isPolling: true)
|
||||
}
|
||||
}
|
||||
|
||||
private func cancelScheduledPoll() {
|
||||
if pollTask != nil { logEvent("poll.cancelled") }
|
||||
pollGeneration &+= 1
|
||||
pollTask?.cancel()
|
||||
pollTask = nil
|
||||
}
|
||||
|
||||
private func cancelRefresh() {
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
refreshTask?.cancel()
|
||||
refreshTask = nil
|
||||
@@ -310,6 +348,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 点击后的同步锁覆盖 Task 调度间隙,回执成功后的单次回查由 ViewModel 协调。
|
||||
private func startShootingIfAllowed() {
|
||||
guard !isStartingAction, !isSendingSafetyAction, !isResummoningAction, viewModel.canStartShooting else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isStartingAction = true
|
||||
@@ -322,6 +361,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
self.startTask = nil
|
||||
self.isStartingAction = false
|
||||
self.applySnapshot()
|
||||
self.scheduleNextPoll()
|
||||
if let error = self.viewModel.startErrorMessage {
|
||||
self.showToast(error)
|
||||
} else if let error = self.viewModel.errorMessage {
|
||||
@@ -330,11 +370,12 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 重召唤只在显式模拟入口开放;点击后锁定按钮,使用原任务 ID,并由状态回查更新页面。
|
||||
/// 重召唤使用原任务 ID,回执后再查询状态,避免从受理结果推断设备已到达。
|
||||
private func resummonIfAllowed() {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
guard allowsControlActions, isPageVisible, isApplicationActive,
|
||||
!isResummoningAction, !isStartingAction, !isSendingSafetyAction,
|
||||
!isRequesting, viewModel.canResummon else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isResummoningAction = true
|
||||
@@ -347,12 +388,14 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
self.resummonTask = nil
|
||||
self.isResummoningAction = false
|
||||
self.applySnapshot()
|
||||
self.scheduleNextPoll()
|
||||
if let error = self.viewModel.resummonErrorMessage {
|
||||
self.showToast(error)
|
||||
} else if self.viewModel.hasSubmittedResummon, let error = self.viewModel.errorMessage {
|
||||
self.showToast("模拟重召唤已受理,状态刷新失败:\(error)")
|
||||
self.showToast("重召唤已受理,状态刷新失败:\(error)")
|
||||
} else if self.viewModel.hasSubmittedResummon {
|
||||
self.showToast("本地模拟重召唤已受理,未发送设备指令")
|
||||
self.showToast(self.simulationRecordsProvider != nil
|
||||
? "本地模拟重召唤已受理,未发送设备指令" : "重召唤请求已受理,请等待任务状态更新")
|
||||
} else if let error = self.viewModel.errorMessage {
|
||||
self.showToast(error)
|
||||
}
|
||||
@@ -363,6 +406,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private func refreshLiveIfVisible(automatically: Bool = false) {
|
||||
guard !automatically || liveViewModel.loadsAutomatically else { return }
|
||||
guard isPageVisible, isApplicationActive, liveTask == nil, liveViewModel.canLoad else { return }
|
||||
logEvent("live.request", fields: ["automatic": automatically])
|
||||
liveGeneration &+= 1
|
||||
let generation = liveGeneration
|
||||
liveTask = Task { @MainActor [weak self] in
|
||||
@@ -385,16 +429,20 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
feedPlaceholder.apply(liveViewModel)
|
||||
}
|
||||
|
||||
/// 仅模拟入口响应安全按钮;返航会中断任务,先明确展示其含义再提交一次。
|
||||
/// 返航会中断任务,先明确展示其含义再提交一次;Debug 只开放隔离模拟入口。
|
||||
private func safetyCommandTriggered(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
logEvent("action.safety", fields: ["command": command.rawValue, "enabled": viewModel.canSendCommand(command)])
|
||||
guard allowsControlActions, isPageVisible, isApplicationActive,
|
||||
presentedViewController == nil, !isSendingSafetyAction, !isStartingAction,
|
||||
!isResummoningAction, !isRequesting, viewModel.canSendCommand(command) else { return }
|
||||
if command == .returnHome {
|
||||
let alert = UIAlertController(title: "立即返航(本地模拟)",
|
||||
message: "模拟返航会中断本次任务。取消返航也不会恢复已经中断的任务。\n仅验证本地请求与回执,不向无人机发送指令。", preferredStyle: .alert)
|
||||
let simulated = simulationRecordsProvider != nil
|
||||
let alert = UIAlertController(title: simulated ? "立即返航(本地模拟)" : "立即返航",
|
||||
message: simulated
|
||||
? "模拟返航会中断本次任务。取消返航也不会恢复已经中断的任务。\n仅验证本地请求与回执,不向无人机发送指令。"
|
||||
: "返航会中断本次任务。取消返航也不会恢复已经中断的任务。确定立即返航?", preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
|
||||
alert.addAction(UIAlertAction(title: "模拟返航", style: .destructive) { [weak self] _ in
|
||||
alert.addAction(UIAlertAction(title: simulated ? "模拟返航" : "立即返航", style: .destructive) { [weak self] _ in
|
||||
self?.sendSafetyCommand(command)
|
||||
})
|
||||
present(alert, animated: true)
|
||||
@@ -405,9 +453,10 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
|
||||
/// 同步锁覆盖异步调度间隙;界面保留旧快照,只有同任务回查成功才更新阶段。
|
||||
private func sendSafetyCommand(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
guard allowsControlActions, isPageVisible, isApplicationActive,
|
||||
!isSendingSafetyAction, !isStartingAction, !isResummoningAction, !isRequesting,
|
||||
viewModel.canSendCommand(command) else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isSendingSafetyAction = true
|
||||
@@ -420,12 +469,14 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
self.commandTask = nil
|
||||
self.isSendingSafetyAction = false
|
||||
self.applySnapshot()
|
||||
self.scheduleNextPoll()
|
||||
if let error = self.viewModel.commandErrorMessage {
|
||||
self.showToast(error)
|
||||
} else if let error = self.viewModel.errorMessage {
|
||||
self.showToast("模拟指令已受理,状态刷新失败:\(error)")
|
||||
self.showToast("指令已受理,状态刷新失败:\(error)")
|
||||
} else {
|
||||
self.showToast("本地模拟指令已受理,未发送设备指令")
|
||||
self.showToast(self.simulationRecordsProvider != nil
|
||||
? "本地模拟指令已受理,未发送设备指令" : "指令已受理,请等待任务状态更新")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -445,6 +496,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
|
||||
@objc private func applicationWillResignActive() {
|
||||
logEvent("app.inactive")
|
||||
isApplicationActive = false
|
||||
cancelRefresh()
|
||||
stopLive()
|
||||
@@ -453,9 +505,22 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
@objc private func applicationDidBecomeActive() {
|
||||
guard !isApplicationActive else { return }
|
||||
isApplicationActive = true
|
||||
logEvent("app.active")
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible(automatically: true)
|
||||
}
|
||||
|
||||
private func logEvent(_ name: String, fields: [String: Any] = [:]) {
|
||||
var fields = fields
|
||||
fields["source"] = simulationRecordsProvider == nil ? "task_page" : "local_simulation"
|
||||
fields["scenic_id"] = viewModel.context.scenicID
|
||||
fields["order_number"] = viewModel.order.orderNumber
|
||||
DroneDockDiagnostics.event(name, taskID: viewModel.taskID, fields: fields)
|
||||
}
|
||||
|
||||
private var allowsControlActions: Bool {
|
||||
simulationRecordsProvider != nil || !DeviceCommandDebugPolicy.blocksFlightRequests
|
||||
}
|
||||
}
|
||||
|
||||
/// 以可选择的 JSON 文本展示本地模拟请求与回执,避免与真实接口联调混淆。
|
||||
@@ -555,10 +620,11 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
/// 保持两列固定布局,暂停/继续与返航/取消由快照中的指令白名单决定。
|
||||
func apply(model: DroneDockShootingTaskViewModel, isSimulation: Bool, isInteractive: Bool) {
|
||||
let commands = model.availableSafetyCommands
|
||||
let allowsControls = isSimulation || !DeviceCommandDebugPolicy.blocksFlightRequests
|
||||
pauseCommand = commands.contains(.resume) ? .resume : .pause
|
||||
returnCommand = commands.contains(.cancelReturn) ? .cancelReturn : .returnHome
|
||||
header.text = model.commandOutcomeUnknown ? "安全控制 · 结果待核对"
|
||||
: model.isSendingCommand ? "安全控制 · 本地模拟发送中"
|
||||
: model.isSendingCommand ? (isSimulation ? "安全控制 · 本地模拟发送中" : "安全控制 · 指令发送中")
|
||||
: isSimulation ? "安全控制 · 本地模拟,不发送设备指令" : "安全控制"
|
||||
for (button, command) in [(pauseButton, pauseCommand), (returnButton, returnCommand)] {
|
||||
let title: String
|
||||
@@ -569,10 +635,10 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
case .returnHome: title = "立即返航"; subtitle = "中断任务"
|
||||
case .cancelReturn: title = "取消返航"; subtitle = "不恢复已中断任务"
|
||||
}
|
||||
let enabled = isSimulation && isInteractive && model.canSendCommand(command)
|
||||
let detail = model.activeCommand == command ? "本地模拟发送中…"
|
||||
let enabled = allowsControls && isInteractive && model.canSendCommand(command)
|
||||
let detail = model.activeCommand == command ? (isSimulation ? "本地模拟发送中…" : "指令发送中…")
|
||||
: model.commandOutcomeUnknown ? "结果待核对"
|
||||
: !isSimulation || !commands.contains(command) ? "暂不可用" : subtitle
|
||||
: !allowsControls || !commands.contains(command) ? "暂不可用" : subtitle
|
||||
button.apply(title: title, subtitle: detail, enabled: enabled)
|
||||
button.setControlSymbol(command == .resume ? "play.circle" : command == .pause ? "pause.circle"
|
||||
: command == .cancelReturn ? "xmark.circle" : "arrow.uturn.backward")
|
||||
|
||||
Reference in New Issue
Block a user