feat(drone-dock): 接入图传与重召唤的本地模拟流程
新增 live 接口、状态展示和本地视频预览;支持同任务重新召唤、单次回查和防重,保留禁飞拦截。同步测试、模拟证据、后端接口确认文档及景区空境库范围约定。 验证:168 个相关用例最终均通过;最后复跑 38 项零失败。真实设备指令未发送。
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 拍摄任务页:真实入口只读,本地模拟入口可验证开拍与底部安全指令合同。
|
||||
/// 拍摄任务页:真实入口只读,本地模拟入口可验证图传、重召唤、开拍与安全指令合同。
|
||||
@MainActor
|
||||
final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 入口传入的固定任务身份及只读状态,供导航协调和页面测试检查。
|
||||
let viewModel: DroneDockShootingTaskViewModel
|
||||
/// 图传使用独立状态,刷新与播放不会影响任务阶段或固定布局。
|
||||
let liveViewModel: DroneDockLiveViewModel
|
||||
|
||||
private let topView = UIView()
|
||||
private let summaryView = UIView()
|
||||
private let taskTitleLabel = DroneDockStyle.label(size: 17, weight: .semibold)
|
||||
private let subtitleLabel = DroneDockStyle.label(size: 12, color: DroneDockStyle.secondary)
|
||||
private let feedPlaceholder = DroneDockTaskFeedPlaceholderView()
|
||||
private let feedPlaceholder = DroneDockTaskLiveView()
|
||||
private let telemetryView = DroneShootingTaskTelemetryView()
|
||||
private let statusView = DroneDockTaskStatusView()
|
||||
private let safetyView = DroneDockTaskSafetyView()
|
||||
@@ -24,18 +26,26 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
private var startTask: Task<Void, Never>?
|
||||
private var commandTask: Task<Void, Never>?
|
||||
private var resummonTask: Task<Void, Never>?
|
||||
private var liveTask: Task<Void, Never>?
|
||||
private var liveGeneration = 0
|
||||
private var requestGeneration = 0
|
||||
private var isPageVisible = false
|
||||
private var isApplicationActive = false
|
||||
private var isRequesting = false
|
||||
private var isStartingAction = false
|
||||
private var isSendingSafetyAction = false
|
||||
private var isResummoningAction = false
|
||||
|
||||
/// 构造时只保存依赖,首次 GET 必须等页面完整出现后才发送。
|
||||
init(viewModel: DroneDockShootingTaskViewModel,
|
||||
liveViewModel: DroneDockLiveViewModel? = nil,
|
||||
materialsControllerFactory: (@MainActor (DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)? = nil,
|
||||
simulationRecordsProvider: (@MainActor () -> String)? = nil) {
|
||||
self.viewModel = viewModel
|
||||
self.liveViewModel = liveViewModel ?? DroneDockLiveViewModel(
|
||||
context: viewModel.context, taskID: viewModel.taskID,
|
||||
currentContext: { DroneDockHomeViewController.currentContext() })
|
||||
self.simulationRecordsProvider = simulationRecordsProvider
|
||||
self.materialsControllerFactory = materialsControllerFactory ?? { order, context in
|
||||
DroneDockMaterialsViewController(viewModel: DroneDockMaterialsViewModel(
|
||||
@@ -54,6 +64,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
refreshTask?.cancel()
|
||||
startTask?.cancel()
|
||||
commandTask?.cancel()
|
||||
resummonTask?.cancel()
|
||||
liveTask?.cancel()
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
@@ -160,6 +172,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
statusView.onPrimaryAction = { [weak self] in self?.primaryActionTriggered() }
|
||||
statusView.onDetails = { [weak self] in self?.showStatusDetails() }
|
||||
safetyView.onCommand = { [weak self] command in self?.safetyCommandTriggered(command) }
|
||||
feedPlaceholder.onRequest = { [weak self] in self?.refreshLiveIfVisible() }
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive),
|
||||
@@ -172,6 +185,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
isPageVisible = true
|
||||
isApplicationActive = UIApplication.shared.applicationState == .active
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible()
|
||||
}
|
||||
|
||||
/// 离页立即取消未完成的读取,使迟到响应失效。
|
||||
@@ -179,12 +193,15 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
super.viewWillDisappear(animated)
|
||||
isPageVisible = false
|
||||
cancelRefresh()
|
||||
stopLive()
|
||||
}
|
||||
|
||||
private func applySnapshot() {
|
||||
guard isViewLoaded else { return }
|
||||
let loading = isRequesting || viewModel.isLoading || isStartingAction || viewModel.isStartingShooting
|
||||
|| isSendingSafetyAction || viewModel.isSendingCommand
|
||||
|| isSendingSafetyAction || viewModel.isSendingCommand || isResummoningAction || viewModel.isResummoning
|
||||
feedPlaceholder.apply(liveViewModel)
|
||||
feedPlaceholder.setPlaybackActive(isPageVisible && isApplicationActive)
|
||||
taskTitleLabel.text = viewModel.title
|
||||
subtitleLabel.text = viewModel.subtitle
|
||||
let telemetry = viewModel.telemetry
|
||||
@@ -205,7 +222,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private func primaryActionTriggered() {
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand else { return }
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand,
|
||||
!isResummoningAction, !viewModel.isResummoning else { return }
|
||||
let state = viewModel.presentation
|
||||
guard state.isActionEnabled else { return }
|
||||
switch state.action {
|
||||
@@ -213,10 +231,12 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
refreshTaskIfVisible()
|
||||
case .start:
|
||||
startShootingIfAllowed()
|
||||
case .retry:
|
||||
resummonIfAllowed()
|
||||
case .materials:
|
||||
guard let navigationController, navigationController.topViewController === self else { return }
|
||||
navigationController.pushViewController(materialsControllerFactory(viewModel.order, viewModel.context), animated: true)
|
||||
case .resume, .retry, .none:
|
||||
case .resume, .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -230,6 +250,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
if let error = viewModel.startErrorMessage { texts.append(error) }
|
||||
if let error = viewModel.commandErrorMessage { texts.append(error) }
|
||||
if let error = viewModel.resummonErrorMessage { texts.append(error) }
|
||||
var distinct: [String] = []
|
||||
for text in texts where !distinct.contains(text) { distinct.append(text) }
|
||||
let alert = UIAlertController(title: "任务状态详情", message: distinct.joined(separator: "\n\n"), preferredStyle: .alert)
|
||||
@@ -241,7 +262,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private func refreshTaskIfVisible() {
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand else { return }
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand,
|
||||
!isResummoningAction, !viewModel.isResummoning else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isRequesting = true
|
||||
@@ -268,6 +290,9 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
startTask = nil
|
||||
commandTask?.cancel()
|
||||
commandTask = nil
|
||||
resummonTask?.cancel()
|
||||
resummonTask = nil
|
||||
isResummoningAction = false
|
||||
isSendingSafetyAction = false
|
||||
isStartingAction = false
|
||||
isRequesting = false
|
||||
@@ -277,7 +302,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
|
||||
/// 点击后的同步锁覆盖 Task 调度间隙,回执成功后的单次回查由 ViewModel 协调。
|
||||
private func startShootingIfAllowed() {
|
||||
guard !isStartingAction, !isSendingSafetyAction, viewModel.canStartShooting else { return }
|
||||
guard !isStartingAction, !isSendingSafetyAction, !isResummoningAction, viewModel.canStartShooting else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isStartingAction = true
|
||||
@@ -298,11 +323,65 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 重召唤只在显式模拟入口开放;点击后锁定按钮,使用原任务 ID,并由状态回查更新页面。
|
||||
private func resummonIfAllowed() {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
!isResummoningAction, !isStartingAction, !isSendingSafetyAction,
|
||||
!isRequesting, viewModel.canResummon else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isResummoningAction = true
|
||||
applySnapshot()
|
||||
resummonTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await self.viewModel.resummon { [weak self] in self?.applySnapshot() }
|
||||
guard self.isPageVisible, self.isApplicationActive,
|
||||
self.requestGeneration == generation, !Task.isCancelled else { return }
|
||||
self.resummonTask = nil
|
||||
self.isResummoningAction = false
|
||||
self.applySnapshot()
|
||||
if let error = self.viewModel.resummonErrorMessage {
|
||||
self.showToast(error)
|
||||
} else if self.viewModel.hasSubmittedResummon, let error = self.viewModel.errorMessage {
|
||||
self.showToast("模拟重召唤已受理,状态刷新失败:\(error)")
|
||||
} else if self.viewModel.hasSubmittedResummon {
|
||||
self.showToast("本地模拟重召唤已受理,未发送设备指令")
|
||||
} else if let error = self.viewModel.errorMessage {
|
||||
self.showToast(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 图传只读取一次,不轮询;正式入口没有图传服务,不能向设备发出开流请求。
|
||||
private func refreshLiveIfVisible() {
|
||||
guard isPageVisible, isApplicationActive, liveTask == nil, liveViewModel.canLoad else { return }
|
||||
liveGeneration &+= 1
|
||||
let generation = liveGeneration
|
||||
liveTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await self.liveViewModel.refresh { [weak self] in self?.applySnapshot() }
|
||||
guard self.liveGeneration == generation, self.isPageVisible, self.isApplicationActive,
|
||||
!Task.isCancelled else { return }
|
||||
self.liveTask = nil
|
||||
self.applySnapshot()
|
||||
}
|
||||
}
|
||||
|
||||
/// 离页或进入后台立即释放播放器,使旧图传响应无法恢复播放。
|
||||
private func stopLive() {
|
||||
liveGeneration &+= 1
|
||||
liveTask?.cancel()
|
||||
liveTask = nil
|
||||
liveViewModel.invalidate()
|
||||
feedPlaceholder.setPlaybackActive(false)
|
||||
feedPlaceholder.apply(liveViewModel)
|
||||
}
|
||||
|
||||
/// 仅模拟入口响应安全按钮;返航会中断任务,先明确展示其含义再提交一次。
|
||||
private func safetyCommandTriggered(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
presentedViewController == nil, !isSendingSafetyAction, !isStartingAction,
|
||||
!isRequesting, viewModel.canSendCommand(command) else { return }
|
||||
!isResummoningAction, !isRequesting, viewModel.canSendCommand(command) else { return }
|
||||
if command == .returnHome {
|
||||
let alert = UIAlertController(title: "立即返航(本地模拟)",
|
||||
message: "模拟返航会中断本次任务。取消返航也不会恢复已经中断的任务。\n仅验证本地请求与回执,不向无人机发送指令。", preferredStyle: .alert)
|
||||
@@ -319,7 +398,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 同步锁覆盖异步调度间隙;界面保留旧快照,只有同任务回查成功才更新阶段。
|
||||
private func sendSafetyCommand(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
!isSendingSafetyAction, !isStartingAction, !isRequesting,
|
||||
!isSendingSafetyAction, !isStartingAction, !isResummoningAction, !isRequesting,
|
||||
viewModel.canSendCommand(command) else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
@@ -360,12 +439,14 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
@objc private func applicationWillResignActive() {
|
||||
isApplicationActive = false
|
||||
cancelRefresh()
|
||||
stopLive()
|
||||
}
|
||||
|
||||
@objc private func applicationDidBecomeActive() {
|
||||
guard !isApplicationActive else { return }
|
||||
isApplicationActive = true
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,34 +582,3 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
buttonHeight?.update(offset: compact ? 56 : 62)
|
||||
}
|
||||
}
|
||||
|
||||
/// 真实图传尚未接入时的中性占位,不使用演示图片、播放进度或虚假连接状态。
|
||||
@MainActor
|
||||
private final class DroneDockTaskFeedPlaceholderView: UIView {
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
backgroundColor = UIColor(hex: 0x172033)
|
||||
accessibilityIdentifier = "drone.task.remote.feedPlaceholder"
|
||||
let icon = UIImageView(image: UIImage(systemName: "video.slash"))
|
||||
icon.contentMode = .scaleAspectFit
|
||||
icon.tintColor = UIColor.white.withAlphaComponent(0.6)
|
||||
let title = DroneDockStyle.label("图传暂未接入", size: 15, weight: .medium, color: .white)
|
||||
let detail = DroneDockStyle.label("任务状态将在下方更新", size: 12, color: .white.withAlphaComponent(0.65))
|
||||
let stack = UIStackView(arrangedSubviews: [icon, title, detail])
|
||||
stack.axis = .vertical
|
||||
stack.spacing = 9
|
||||
stack.alignment = .center
|
||||
addSubview(stack)
|
||||
icon.snp.makeConstraints { make in make.size.equalTo(31) }
|
||||
stack.snp.makeConstraints { make in
|
||||
make.center.equalToSuperview()
|
||||
make.leading.greaterThanOrEqualToSuperview().offset(20)
|
||||
make.trailing.lessThanOrEqualToSuperview().offset(-20)
|
||||
}
|
||||
isAccessibilityElement = true
|
||||
accessibilityLabel = "图传暂未接入,任务状态将在下方更新"
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user