feat(drone-dock): 添加安全控制接口本地模拟调试

接入 command 请求合同与底部暂停、继续、返航、取消返航交互;支持同任务单次回查、防重复提交及未知结果锁。保留真实指令禁飞拦截,补充隔离测试、模拟回执与截图。
This commit is contained in:
2026-10-30 10:32:31 +08:00
parent b95b251de1
commit 20877b425c
21 changed files with 2668 additions and 43 deletions
@@ -1,6 +1,6 @@
import Foundation
/// 拍摄任务状态及可选开拍流程,始终复用同一任务;禁飞调试仅为本地模拟注入开拍服务。
/// 拍摄任务状态及可选控制流程,始终复用同一任务;禁飞调试仅为本地模拟注入操作服务。
final class DroneDockShootingTaskViewModel {
/// 页面绑定的账号、景区和门店身份。
let context: DroneDockHomeContext
@@ -24,25 +24,42 @@ final class DroneDockShootingTaskViewModel {
private(set) var hasSubmittedStart = false
/// 同任务开拍回执;受理成功不直接替代 shooting 的完整状态快照。
private(set) var startResponse: DroneDockStartShootResponse?
/// 指令提交及其后单次回查期间锁定控制动作,旧状态继续展示。
private(set) var isSendingCommand = false
/// 当前正在提交或核对的指令;结束后清空。
private(set) var activeCommand: DroneDockSafetyCommand?
/// 最近一次明确受理且身份一致的指令回执,不作为设备已执行的证据。
private(set) var commandResponse: DroneDockCommandResponse?
/// 指令业务拒绝、本地拦截或结果待核对提示。
private(set) var commandErrorMessage: String?
/// 已发但结果不明确时保持锁定,普通读取不能自动解除或重发。
private(set) var commandOutcomeUnknown = false
private let service: any DroneDockShootingServing
private let startService: (any DroneDockStartShooting)?
private let commandService: (any DroneDockCommandSending)?
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
private var identityInvalidated = false
private var startGeneration = 0
private var didSendStart = false
private var commandGeneration = 0
private var didSendCommand = false
// 回查仍提供的已受理指令分别保留防重锁,避免另一条指令受理覆盖旧锁。
private var pendingAcceptedCommands: Set<DroneDockSafetyCommand> = []
/// 默认只读;只有显式注入开拍服务才提供开拍动作,当前正式入口不注入该服务。
/// 默认只读;仅显式注入模拟操作服务时开放对应动作,当前正式入口不注入。
init(context: DroneDockHomeContext, taskID: Int, order: DroneDockHomeOrder,
service: any DroneDockShootingServing,
startService: (any DroneDockStartShooting)? = nil,
commandService: (any DroneDockCommandSending)? = nil,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.taskID = taskID
self.order = order
self.service = service
self.startService = startService
self.commandService = commandService
self.currentContext = currentContext
}
@@ -53,8 +70,9 @@ final class DroneDockShootingTaskViewModel {
}
@MainActor
private func refreshSnapshot(allowWhileStarting: Bool) async {
guard !isLoading, !identityInvalidated, !isStartingShooting || allowWhileStarting else { return }
private func refreshSnapshot(allowWhileStarting: Bool, allowWhileCommand: Bool = false) async {
guard !isLoading, !identityInvalidated, !isStartingShooting || allowWhileStarting,
!isSendingCommand || allowWhileCommand else { return }
generation &+= 1
let requestGeneration = generation
isLoading = true
@@ -79,6 +97,7 @@ final class DroneDockShootingTaskViewModel {
throw APIError.decodeFailed("拍摄任务返回的任务、订单或景区与当前页面不一致,请重新读取")
}
response = value
pendingAcceptedCommands.formIntersection(availableSafetyCommands)
} catch {
guard accepts(requestGeneration) else { return }
errorMessage = error is CancellationError ? nil : error.localizedDescription
@@ -89,6 +108,7 @@ final class DroneDockShootingTaskViewModel {
@MainActor
func invalidate(preservingSnapshot: Bool = false) {
invalidateStartShooting()
invalidateCommand()
generation &+= 1
isLoading = false
if !context.hasSameIdentity(as: currentContext()) { identityInvalidated = true }
@@ -96,6 +116,9 @@ final class DroneDockShootingTaskViewModel {
response = nil
startResponse = nil
startErrorMessage = nil
commandResponse = nil
commandErrorMessage = nil
pendingAcceptedCommands.removeAll()
errorMessage = "账号或景区已变化,请返回工作台重新进入"
} else {
if !preservingSnapshot { response = nil }
@@ -106,8 +129,9 @@ final class DroneDockShootingTaskViewModel {
/// 只在有效读取明确到达、锁定飞机并允许 start_shoot 后开放;默认真实页面保持禁用。
var canStartShooting: Bool {
startService != nil && context.canLoad && taskID > 0 && !identityInvalidated
&& !isLoading && !isStartingShooting && errorMessage == nil
&& !isLoading && !isStartingShooting && !isSendingCommand && errorMessage == nil
&& !hasSubmittedStart && !startOutcomeUnknown
&& !commandOutcomeUnknown && pendingAcceptedCommands.isEmpty
&& resolvedDisplayPhase().0 == .ready
&& response?.executeGate?.canExecute == true
&& response?.executeGate?.primaryAction == "start_shoot"
@@ -180,6 +204,93 @@ final class DroneDockShootingTaskViewModel {
didSendStart = false
}
/// 只识别当前快照明确提供的四种指令,未知值忽略,同一指令只展示一次。
var availableSafetyCommands: [DroneDockSafetyCommand] {
var commands: [DroneDockSafetyCommand] = []
for item in response?.ui?.commands ?? [] {
guard let raw = item.command, let command = DroneDockSafetyCommand(rawValue: raw),
!commands.contains(command) else { continue }
commands.append(command)
}
return commands
}
/// 安全指令不借用开拍门禁;需明确任务、锁定飞机、当前指令及注入的本地服务。
func canSendCommand(_ command: DroneDockSafetyCommand) -> Bool {
commandService != nil && context.canLoad && taskID > 0 && !identityInvalidated
&& !isLoading && !isStartingShooting && !isSendingCommand && errorMessage == nil
&& !startOutcomeUnknown && !commandOutcomeUnknown && !pendingAcceptedCommands.contains(command)
&& (response?.orderFlightTask?.id == taskID || response?.task?.id == taskID)
&& (response?.currentPlane?.id ?? 0) > 0
&& availableSafetyCommands.contains(command)
}
/// 只提交当前任务的一次指令,受理后单次回查;不轮询、不改派飞机、不自动重试。
@MainActor
func sendCommand(_ command: DroneDockSafetyCommand,
onStateChange: @escaping @MainActor () -> Void = {}) async {
guard canSendCommand(command), let commandService else { return }
guard context.hasSameIdentity(as: currentContext()) else {
identityInvalidated = true
invalidate()
onStateChange()
return
}
guard !Task.isCancelled else { return }
commandGeneration &+= 1
let requestGeneration = commandGeneration
isSendingCommand = true
activeCommand = command
didSendCommand = false
commandResponse = nil
commandErrorMessage = nil
onStateChange()
defer {
if commandGeneration == requestGeneration {
if Task.isCancelled && didSendCommand && commandResponse == nil { markCommandUnknown() }
isSendingCommand = false
activeCommand = nil
didSendCommand = false
onStateChange()
}
}
guard acceptsCommand(requestGeneration) else { return }
do {
didSendCommand = true
let value = try await commandService.sendCommand(taskID: taskID, command: command)
guard acceptsCommand(requestGeneration) else { return }
guard value.orderFlightTaskID == taskID, value.command == command.rawValue else {
throw APIError.decodeFailed("安全指令回执与当前任务或指令不一致,请先核对任务状态")
}
commandResponse = value
pendingAcceptedCommands.insert(command)
commandOutcomeUnknown = false
onStateChange()
guard acceptsCommand(requestGeneration) else { return }
await refreshSnapshot(allowWhileStarting: false, allowWhileCommand: true)
} catch {
if error is DeviceCommandDebugBlockedError, commandGeneration == requestGeneration { didSendCommand = false }
guard acceptsCommand(requestGeneration) else { return }
if error is DeviceCommandDebugBlockedError {
commandErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if case APIError.serverCode = error {
commandErrorMessage = error.localizedDescription
} else {
markCommandUnknown(reason: error.localizedDescription)
}
}
}
/// 离页或后台取消控制流程;已发而未受理时保留未知结果锁,忽略迟到回执。
@MainActor
func invalidateCommand() {
if isSendingCommand && didSendCommand && commandResponse == nil { markCommandUnknown() }
commandGeneration &+= 1
isSendingCommand = false
activeCommand = nil
didSendCommand = false
}
/// 优先使用当前接口标题,缺失时展示当前接口的点位和航线名称。
var title: String {
if let title = usable(response?.header?.title) { return title }
@@ -211,12 +322,13 @@ final class DroneDockShootingTaskViewModel {
?? usable(response?.orderFlightTask?.statusLabel) ?? "状态待确认"
}
/// 保留服务端阶段说明;未确认的开拍结果明确提示核对,不伪装为正在拍摄。
/// 保留服务端阶段说明;操作结果未知时优先提示核对,不伪装为设备已执行。
var statusMessage: String {
if response == nil {
if let errorMessage { return errorMessage }
if isLoading { return "正在读取当前拍摄任务" }
}
if let commandErrorMessage { return commandErrorMessage }
if resolvedDisplayPhase().0 == .failed { return failureMessage }
if startOutcomeUnknown, let startErrorMessage { return startErrorMessage }
if resolvedDisplayPhase().0 == .ready {
@@ -243,7 +355,7 @@ final class DroneDockShootingTaskViewModel {
let actionTitle: String?
let enabled: Bool
switch phase {
case .ready where hasSubmittedStart || startOutcomeUnknown:
case .ready where hasSubmittedStart || startOutcomeUnknown || commandOutcomeUnknown:
action = .refresh
actionTitle = "刷新状态"
enabled = !identityInvalidated
@@ -271,7 +383,7 @@ final class DroneDockShootingTaskViewModel {
}
return DroneDockTaskPresentation(phase: phase, title: title, message: message,
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle,
isActionEnabled: enabled && !isLoading && !isStartingShooting && !identityInvalidated && (errorMessage == nil || action == .refresh),
isActionEnabled: enabled && !isLoading && !isStartingShooting && !isSendingCommand && !identityInvalidated && (errorMessage == nil || action == .refresh),
actionNote: phase == .failed ? retryUnavailableNote : nil)
}
@@ -310,6 +422,22 @@ final class DroneDockShootingTaskViewModel {
return !Task.isCancelled
}
@MainActor
private func acceptsCommand(_ requestGeneration: Int) -> Bool {
guard commandGeneration == requestGeneration else { return false }
guard context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
identityInvalidated = true
invalidate()
return false
}
return !Task.isCancelled
}
private func markCommandUnknown(reason: String? = nil) {
commandOutcomeUnknown = true
commandErrorMessage = (reason.map { "\($0)。" } ?? "") + "安全指令结果待核对,请刷新任务状态,勿重复提交"
}
private func markStartUnknown(reason: String? = nil) {
startOutcomeUnknown = true
startErrorMessage = (reason.map { "\($0)。" } ?? "") + "开始拍摄结果待核对,请刷新任务状态,勿重复提交"