feat: 完善机巢任务实时状态、双路图传与继续查看入口
This commit is contained in:
@@ -32,6 +32,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private var commandTask: Task<Void, Never>?
|
||||
private var resummonTask: Task<Void, Never>?
|
||||
private var liveTask: Task<Void, Never>?
|
||||
private var liveParameterTask: Task<Void, Never>?
|
||||
private let liveParameterDelay: @Sendable (TimeInterval) async throws -> Void
|
||||
private var liveGeneration = 0
|
||||
private var requestGeneration = 0
|
||||
private var isPageVisible = false
|
||||
@@ -49,7 +51,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
realtimeSession: DroneDockShootingRealtimeSession? = nil,
|
||||
materialsControllerFactory: (@MainActor (DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)? = nil,
|
||||
simulationRecordsProvider: (@MainActor () -> String)? = nil,
|
||||
shootingPollDelay: @escaping @Sendable (UInt64) async throws -> Void = { try await Task.sleep(nanoseconds: $0) }) {
|
||||
shootingPollDelay: @escaping @Sendable (UInt64) async throws -> Void = { try await Task.sleep(nanoseconds: $0) },
|
||||
liveParameterDelay: @escaping @Sendable (TimeInterval) async throws -> Void = { try await Task.sleep(for: .seconds($0)) }) {
|
||||
self.viewModel = viewModel
|
||||
self.feedPlaceholder = livePlayer.map { DroneDockTaskLiveView(rtcPlayer: $0) } ?? DroneDockTaskLiveView()
|
||||
self.realtimeSession = realtimeSession
|
||||
@@ -58,6 +61,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
currentContext: { DroneDockHomeViewController.currentContext() })
|
||||
self.simulationRecordsProvider = simulationRecordsProvider
|
||||
self.shootingPollDelay = shootingPollDelay
|
||||
self.liveParameterDelay = liveParameterDelay
|
||||
self.materialsControllerFactory = materialsControllerFactory ?? { order, context in
|
||||
DroneDockMaterialsViewController(viewModel: DroneDockMaterialsViewModel(
|
||||
context: context, order: order,
|
||||
@@ -87,6 +91,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
commandTask?.cancel()
|
||||
resummonTask?.cancel()
|
||||
liveTask?.cancel()
|
||||
liveParameterTask?.cancel()
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
@@ -188,18 +193,20 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
taskTitleLabel.font = .systemFont(ofSize: compact ? 16 : 17, weight: .semibold)
|
||||
}
|
||||
|
||||
/// 模拟服务明确注入时才允许开始;真实入口仍只提供读取、详情和已有素材。
|
||||
/// 绑定页面操作与主线程播放回调;飞控仍受构建策略、服务注入及任务门禁约束。
|
||||
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) }
|
||||
feedPlaceholder.onRequest = { [weak self] in self?.refreshLiveIfVisible() }
|
||||
feedPlaceholder.onPlaybackEvent = { [weak self] event, sessionID in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.isPageVisible, self.isApplicationActive else { return }
|
||||
self.liveViewModel.receivePlaybackEvent(event, sessionID: sessionID)
|
||||
self.applySnapshot()
|
||||
guard let self, self.isPageVisible, self.isApplicationActive else { return }
|
||||
self.liveViewModel.receivePlaybackEvent(event, sessionID: sessionID)
|
||||
if self.liveViewModel.playbackSessionID == nil {
|
||||
self.liveParameterTask?.cancel()
|
||||
self.liveParameterTask = nil
|
||||
}
|
||||
self.applySnapshot()
|
||||
}
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
@@ -228,7 +235,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
|
||||
private func applySnapshot() {
|
||||
guard isViewLoaded else { return }
|
||||
let loading = isRequesting || viewModel.isLoading || isStartingAction || viewModel.isStartingShooting
|
||||
let loading = (viewModel.response == nil && (isRequesting || viewModel.isLoading))
|
||||
|| isStartingAction || viewModel.isStartingShooting
|
||||
|| isSendingSafetyAction || viewModel.isSendingCommand || isResummoningAction || viewModel.isResummoning
|
||||
feedPlaceholder.apply(liveViewModel)
|
||||
feedPlaceholder.setPlaybackActive(isPageVisible && isApplicationActive)
|
||||
@@ -252,7 +260,7 @@ 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,
|
||||
guard isPageVisible, isApplicationActive,
|
||||
!isStartingAction, !viewModel.isStartingShooting,
|
||||
!isSendingSafetyAction, !viewModel.isSendingCommand,
|
||||
!isResummoningAction, !viewModel.isResummoning else { return }
|
||||
@@ -347,6 +355,16 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
pollTask = nil
|
||||
}
|
||||
|
||||
/// 用户操作优先于后台读取;不停止 Socket,也不解除任何已提交操作的防重锁。
|
||||
private func cancelPendingReadForAction() {
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
refreshTask?.cancel()
|
||||
refreshTask = nil
|
||||
isRequesting = false
|
||||
viewModel.invalidatePendingRefresh()
|
||||
}
|
||||
|
||||
private func cancelRefresh() {
|
||||
wasRealtimeLive = false
|
||||
realtimeSession?.stop()
|
||||
@@ -401,8 +419,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 点击后的同步锁覆盖 Task 调度间隙,回执成功后的单次回查由 ViewModel 协调。
|
||||
private func startShootingIfAllowed() {
|
||||
guard !isStartingAction, !isSendingSafetyAction, !isResummoningAction, viewModel.canStartShooting else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
cancelPendingReadForAction()
|
||||
let generation = requestGeneration
|
||||
isStartingAction = true
|
||||
applySnapshot()
|
||||
@@ -427,9 +444,8 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
private func resummonIfAllowed() {
|
||||
guard allowsControlActions, isPageVisible, isApplicationActive,
|
||||
!isResummoningAction, !isStartingAction, !isSendingSafetyAction,
|
||||
!isRequesting, viewModel.canResummon else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
viewModel.canResummon else { return }
|
||||
cancelPendingReadForAction()
|
||||
let generation = requestGeneration
|
||||
isResummoningAction = true
|
||||
applySnapshot()
|
||||
@@ -456,25 +472,47 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
|
||||
/// 可见且活跃时自动开启一次;失败可手动重试,不随 shooting/Socket 刷新重复请求。
|
||||
private func refreshLiveIfVisible(automatically: Bool = false) {
|
||||
private func refreshLiveIfVisible(automatically: Bool = false, keepingPlayback: Bool = false) {
|
||||
guard !automatically || liveViewModel.loadsAutomatically else { return }
|
||||
guard isPageVisible, isApplicationActive, liveTask == nil, liveViewModel.canLoad else { return }
|
||||
liveParameterTask?.cancel()
|
||||
liveParameterTask = nil
|
||||
logEvent("live.request", fields: ["automatic": automatically])
|
||||
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() }
|
||||
await self.liveViewModel.refresh(keepingPlayback: keepingPlayback) { [weak self] in self?.applySnapshot() }
|
||||
guard self.liveGeneration == generation, self.isPageVisible, self.isApplicationActive,
|
||||
!Task.isCancelled else { return }
|
||||
self.liveTask = nil
|
||||
self.applySnapshot()
|
||||
self.scheduleLiveParameterRefresh()
|
||||
}
|
||||
}
|
||||
|
||||
/// 缺少第二路或凭证临期才重新取参数;补流/续期保留健康房间,不随 shooting 轮询。
|
||||
private func scheduleLiveParameterRefresh() {
|
||||
liveParameterTask?.cancel()
|
||||
liveParameterTask = nil
|
||||
guard isPageVisible, isApplicationActive, let interval = liveViewModel.nextParameterRefreshDelay else { return }
|
||||
let generation = liveGeneration
|
||||
let delay = liveParameterDelay
|
||||
liveParameterTask = Task { @MainActor [weak self] in
|
||||
do { try await delay(interval) } catch { return }
|
||||
guard let self, !Task.isCancelled, self.liveGeneration == generation,
|
||||
self.isPageVisible, self.isApplicationActive,
|
||||
self.liveViewModel.nextParameterRefreshDelay != nil else { return }
|
||||
self.liveParameterTask = nil
|
||||
self.refreshLiveIfVisible(keepingPlayback: true)
|
||||
}
|
||||
}
|
||||
|
||||
/// 离页或进入后台立即释放播放器,使旧图传响应无法恢复播放。
|
||||
private func stopLive() {
|
||||
liveGeneration &+= 1
|
||||
liveParameterTask?.cancel()
|
||||
liveParameterTask = nil
|
||||
liveTask?.cancel()
|
||||
liveTask = nil
|
||||
liveViewModel.invalidate()
|
||||
@@ -487,7 +525,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
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 }
|
||||
!isResummoningAction, viewModel.canSendCommand(command) else { return }
|
||||
if command == .returnHome {
|
||||
let simulated = simulationRecordsProvider != nil
|
||||
let alert = UIAlertController(title: simulated ? "立即返航(本地模拟)" : "立即返航",
|
||||
@@ -507,10 +545,9 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 同步锁覆盖异步调度间隙;界面保留旧快照,只有同任务回查成功才更新阶段。
|
||||
private func sendSafetyCommand(_ command: DroneDockSafetyCommand) {
|
||||
guard allowsControlActions, isPageVisible, isApplicationActive,
|
||||
!isSendingSafetyAction, !isStartingAction, !isResummoningAction, !isRequesting,
|
||||
!isSendingSafetyAction, !isStartingAction, !isResummoningAction,
|
||||
viewModel.canSendCommand(command) else { return }
|
||||
cancelScheduledPoll()
|
||||
requestGeneration &+= 1
|
||||
cancelPendingReadForAction()
|
||||
let generation = requestGeneration
|
||||
isSendingSafetyAction = true
|
||||
applySnapshot()
|
||||
|
||||
Reference in New Issue
Block a user