feat(drone-dock): 添加安全控制接口本地模拟调试
接入 command 请求合同与底部暂停、继续、返航、取消返航交互;支持同任务单次回查、防重复提交及未知结果锁。保留真实指令禁飞拦截,补充隔离测试、模拟回执与截图。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 拍摄任务页:真实入口只读,本地模拟入口可注入开始拍摄服务验证接口流程。
|
||||
/// 拍摄任务页:真实入口只读,本地模拟入口可验证开拍与底部安全指令合同。
|
||||
@MainActor
|
||||
final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 入口传入的固定任务身份及只读状态,供导航协调和页面测试检查。
|
||||
@@ -23,11 +23,13 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private let simulationRecordsProvider: (@MainActor () -> String)?
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
private var startTask: Task<Void, Never>?
|
||||
private var commandTask: Task<Void, Never>?
|
||||
private var requestGeneration = 0
|
||||
private var isPageVisible = false
|
||||
private var isApplicationActive = false
|
||||
private var isRequesting = false
|
||||
private var isStartingAction = false
|
||||
private var isSendingSafetyAction = false
|
||||
|
||||
/// 构造时只保存依赖,首次 GET 必须等页面完整出现后才发送。
|
||||
init(viewModel: DroneDockShootingTaskViewModel,
|
||||
@@ -51,6 +53,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
deinit {
|
||||
refreshTask?.cancel()
|
||||
startTask?.cancel()
|
||||
commandTask?.cancel()
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
@@ -156,6 +159,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
override func bindActions() {
|
||||
statusView.onPrimaryAction = { [weak self] in self?.primaryActionTriggered() }
|
||||
statusView.onDetails = { [weak self] in self?.showStatusDetails() }
|
||||
safetyView.onCommand = { [weak self] command in self?.safetyCommandTriggered(command) }
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive),
|
||||
@@ -180,6 +184,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private func applySnapshot() {
|
||||
guard isViewLoaded else { return }
|
||||
let loading = isRequesting || viewModel.isLoading || isStartingAction || viewModel.isStartingShooting
|
||||
|| isSendingSafetyAction || viewModel.isSendingCommand
|
||||
taskTitleLabel.text = viewModel.title
|
||||
subtitleLabel.text = viewModel.subtitle
|
||||
let telemetry = viewModel.telemetry
|
||||
@@ -190,6 +195,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
phase: .loading, title: "正在获取任务状态", message: "正在读取当前拍摄任务",
|
||||
deviceStatus: "—", action: .none, actionTitle: nil, isActionEnabled: false) : viewModel.presentation
|
||||
statusView.apply(value, isRefreshing: loading)
|
||||
safetyView.apply(model: viewModel, isSimulation: simulationRecordsProvider != nil,
|
||||
isInteractive: isPageVisible && isApplicationActive && !loading)
|
||||
statusView.isUserInteractionEnabled = isPageVisible && isApplicationActive && !loading
|
||||
navigationItem.rightBarButtonItems?.forEach { $0.isEnabled = isPageVisible && isApplicationActive && !loading }
|
||||
navigationItem.rightBarButtonItem?.accessibilityValue = loading ? "正在刷新" : nil
|
||||
@@ -197,7 +204,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
|
||||
private func primaryActionTriggered() {
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting else { return }
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand else { return }
|
||||
let state = viewModel.presentation
|
||||
guard state.isActionEnabled else { return }
|
||||
switch state.action {
|
||||
@@ -221,6 +229,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
texts.append("刷新失败,当前显示上次数据:\(error)")
|
||||
}
|
||||
if let error = viewModel.startErrorMessage { texts.append(error) }
|
||||
if let error = viewModel.commandErrorMessage { 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)
|
||||
@@ -231,7 +240,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 页面进入、回到前台或手动刷新时各读一次,请求结束后不安排下一次读取。
|
||||
private func refreshTaskIfVisible() {
|
||||
guard isPageVisible, isApplicationActive, !isRequesting, !viewModel.isLoading,
|
||||
!isStartingAction, !viewModel.isStartingShooting else { return }
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isRequesting = true
|
||||
@@ -256,6 +266,9 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
refreshTask = nil
|
||||
startTask?.cancel()
|
||||
startTask = nil
|
||||
commandTask?.cancel()
|
||||
commandTask = nil
|
||||
isSendingSafetyAction = false
|
||||
isStartingAction = false
|
||||
isRequesting = false
|
||||
viewModel.invalidate(preservingSnapshot: true)
|
||||
@@ -264,7 +277,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
|
||||
/// 点击后的同步锁覆盖 Task 调度间隙,回执成功后的单次回查由 ViewModel 协调。
|
||||
private func startShootingIfAllowed() {
|
||||
guard !isStartingAction, viewModel.canStartShooting else { return }
|
||||
guard !isStartingAction, !isSendingSafetyAction, viewModel.canStartShooting else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isStartingAction = true
|
||||
@@ -285,6 +298,51 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 仅模拟入口响应安全按钮;返航会中断任务,先明确展示其含义再提交一次。
|
||||
private func safetyCommandTriggered(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
presentedViewController == nil, !isSendingSafetyAction, !isStartingAction,
|
||||
!isRequesting, viewModel.canSendCommand(command) else { return }
|
||||
if command == .returnHome {
|
||||
let alert = UIAlertController(title: "立即返航(本地模拟)",
|
||||
message: "模拟返航会中断本次任务。取消返航也不会恢复已经中断的任务。\n仅验证本地请求与回执,不向无人机发送指令。", preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
|
||||
alert.addAction(UIAlertAction(title: "模拟返航", style: .destructive) { [weak self] _ in
|
||||
self?.sendSafetyCommand(command)
|
||||
})
|
||||
present(alert, animated: true)
|
||||
} else {
|
||||
sendSafetyCommand(command)
|
||||
}
|
||||
}
|
||||
|
||||
/// 同步锁覆盖异步调度间隙;界面保留旧快照,只有同任务回查成功才更新阶段。
|
||||
private func sendSafetyCommand(_ command: DroneDockSafetyCommand) {
|
||||
guard simulationRecordsProvider != nil, isPageVisible, isApplicationActive,
|
||||
!isSendingSafetyAction, !isStartingAction, !isRequesting,
|
||||
viewModel.canSendCommand(command) else { return }
|
||||
requestGeneration &+= 1
|
||||
let generation = requestGeneration
|
||||
isSendingSafetyAction = true
|
||||
applySnapshot()
|
||||
commandTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await self.viewModel.sendCommand(command) { [weak self] in self?.applySnapshot() }
|
||||
guard self.isPageVisible, self.isApplicationActive,
|
||||
self.requestGeneration == generation, !Task.isCancelled else { return }
|
||||
self.commandTask = nil
|
||||
self.isSendingSafetyAction = false
|
||||
self.applySnapshot()
|
||||
if let error = self.viewModel.commandErrorMessage {
|
||||
self.showToast(error)
|
||||
} else if let error = self.viewModel.errorMessage {
|
||||
self.showToast("模拟指令已受理,状态刷新失败:\(error)")
|
||||
} else {
|
||||
self.showToast("本地模拟指令已受理,未发送设备指令")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 模拟入口专用记录页,内容由内存传输提供,不请求后端日志或设备。
|
||||
@objc private func showSimulationRecords() {
|
||||
guard let simulationRecordsProvider, presentedViewController == nil else { return }
|
||||
@@ -349,9 +407,13 @@ final class DroneDockSimulationRecordsViewController: BaseViewController {
|
||||
@objc private func close() { dismiss(animated: true) }
|
||||
}
|
||||
|
||||
/// 固定底部的安全控制占位,尚未接入控制接口时不提供可执行的飞控动作。
|
||||
/// 固定底部的安全控制,只在隔离模拟入口按当前接口提供的指令展示可用操作。
|
||||
@MainActor
|
||||
private final class DroneDockTaskSafetyView: UIView {
|
||||
/// 将用户选择的白名单指令交给页面,不直接调用任何服务。
|
||||
var onCommand: ((DroneDockSafetyCommand) -> Void)?
|
||||
private var pauseCommand = DroneDockSafetyCommand.pause
|
||||
private var returnCommand = DroneDockSafetyCommand.returnHome
|
||||
private let header = DroneDockStyle.label("安全控制", size: 11, color: DroneDockStyle.secondary)
|
||||
private let pauseButton = DroneShootingTaskSafetyButton(symbol: "pause.circle", color: UIColor(hex: 0xEC982A), filled: true)
|
||||
private let returnButton = DroneShootingTaskSafetyButton(symbol: "arrow.uturn.backward", color: UIColor(hex: 0xD85762), filled: false)
|
||||
@@ -361,7 +423,7 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
private var buttonGap: Constraint?
|
||||
private var buttonHeight: Constraint?
|
||||
|
||||
/// 复用安全按钮样式,按钮始终保留占位与明确的不可用状态。
|
||||
/// 复用安全按钮样式,初始没有状态数据时保持禁用。
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
backgroundColor = .white
|
||||
@@ -384,6 +446,8 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
returnButton.accessibilityIdentifier = "drone.task.remote.return"
|
||||
pauseButton.apply(title: "紧急暂停", subtitle: "暂不可用", enabled: false)
|
||||
returnButton.apply(title: "立即返航", subtitle: "暂不可用", enabled: false)
|
||||
pauseButton.addTarget(self, action: #selector(pauseTapped), for: .touchUpInside)
|
||||
returnButton.addTarget(self, action: #selector(returnTapped), for: .touchUpInside)
|
||||
header.snp.makeConstraints { make in
|
||||
headerTop = make.top.equalToSuperview().offset(10).constraint
|
||||
make.leading.trailing.equalToSuperview().inset(16)
|
||||
@@ -399,6 +463,36 @@ private final class DroneDockTaskSafetyView: UIView {
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
/// 保持两列固定布局,暂停/继续与返航/取消由快照中的指令白名单决定。
|
||||
func apply(model: DroneDockShootingTaskViewModel, isSimulation: Bool, isInteractive: Bool) {
|
||||
let commands = model.availableSafetyCommands
|
||||
pauseCommand = commands.contains(.resume) ? .resume : .pause
|
||||
returnCommand = commands.contains(.cancelReturn) ? .cancelReturn : .returnHome
|
||||
header.text = model.commandOutcomeUnknown ? "安全控制 · 结果待核对"
|
||||
: model.isSendingCommand ? "安全控制 · 本地模拟发送中"
|
||||
: isSimulation ? "安全控制 · 本地模拟,不发送设备指令" : "安全控制"
|
||||
for (button, command) in [(pauseButton, pauseCommand), (returnButton, returnCommand)] {
|
||||
let title: String
|
||||
let subtitle: String
|
||||
switch command {
|
||||
case .pause: title = "紧急暂停"; subtitle = "悬停待命"
|
||||
case .resume: title = "继续拍摄"; subtitle = "恢复航线"
|
||||
case .returnHome: title = "立即返航"; subtitle = "中断任务"
|
||||
case .cancelReturn: title = "取消返航"; subtitle = "不恢复已中断任务"
|
||||
}
|
||||
let enabled = isSimulation && isInteractive && model.canSendCommand(command)
|
||||
let detail = model.activeCommand == command ? "本地模拟发送中…"
|
||||
: model.commandOutcomeUnknown ? "结果待核对"
|
||||
: !isSimulation || !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")
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func pauseTapped() { guard pauseButton.isEnabled else { return }; onCommand?(pauseCommand) }
|
||||
@objc private func returnTapped() { guard returnButton.isEnabled else { return }; onCommand?(returnCommand) }
|
||||
|
||||
/// 高度仅依设备空间切换,不随任务状态、按钮文案或网络响应变化。
|
||||
func configure(compact: Bool) {
|
||||
headerTop?.update(offset: compact ? 8 : 10)
|
||||
|
||||
Reference in New Issue
Block a user