完善机巢任务实时同步并开放双构建真实操作
订阅明确无缓存时使用新发起的有效 HTTP 全量响应衔接首包,保留任务、批次和版本校验。 移除 Debug/Release 的设备指令、图传和 RTC 构建拦截,保留后端权限、身份、防重复提交及结果未知保护。 更新开发约定并补充隔离传输、飞控状态和页面回归测试。 验证:iPhone 真机 500 项相关测试通过;iOS Release 无签名编译通过。
This commit is contained in:
@@ -49,10 +49,10 @@ final class DroneDockLiveViewModel {
|
||||
self.allowsDevicePlayback = allowsDevicePlayback
|
||||
self.now = now
|
||||
self.currentContext = currentContext
|
||||
state = service == nil ? .restricted : .idle
|
||||
state = service == nil ? .unavailable("当前页面未配置图传服务") : .idle
|
||||
}
|
||||
|
||||
/// 仅有明确注入服务且身份有效时允许发起请求;正式禁飞入口始终为 false。
|
||||
/// 有明确注入服务且身份有效时允许发起请求。
|
||||
var canLoad: Bool { service != nil && !isLoading && !identityInvalidated && context.canLoad && taskID > 0 }
|
||||
|
||||
/// 仅返回明确注入且已经过本地文件检查的模拟视频,不使用后端未定义字段。
|
||||
@@ -64,7 +64,6 @@ final class DroneDockLiveViewModel {
|
||||
/// 图传区域主文案,不把接口受理视为设备连接成功。
|
||||
var title: String {
|
||||
switch state {
|
||||
case .restricted: return "当前调试已停用实时图传"
|
||||
case .idle: return "实时图传"
|
||||
case .loading: return "正在获取图传信息"
|
||||
case .unavailable: return "图传暂不可用"
|
||||
@@ -78,7 +77,6 @@ final class DroneDockLiveViewModel {
|
||||
/// 缺失协议时说明真实限制,不给示例视频贴上实时来源。
|
||||
var message: String {
|
||||
switch state {
|
||||
case .restricted: return "禁飞调试期间不启动设备图传"
|
||||
case .idle: return allowsDevicePlayback ? "正在准备连接图传" : "按当前任务获取图传信息"
|
||||
case .loading: return "请稍候"
|
||||
case .unavailable(let reason), .failed(let reason): return reason
|
||||
@@ -93,7 +91,6 @@ final class DroneDockLiveViewModel {
|
||||
guard service != nil else { return nil }
|
||||
if isLoading { return allowsDevicePlayback ? nil : "正在加载…" }
|
||||
switch state {
|
||||
case .restricted: return nil
|
||||
case .idle: return nil
|
||||
case .loading: return allowsDevicePlayback ? nil : "正在加载…"
|
||||
case .failed, .unavailable: return "重试"
|
||||
@@ -194,7 +191,7 @@ final class DroneDockLiveViewModel {
|
||||
isLoading = false
|
||||
response = nil
|
||||
clearPlayback()
|
||||
state = service == nil ? .restricted : .idle
|
||||
state = service == nil ? .unavailable("当前页面未配置图传服务") : .idle
|
||||
}
|
||||
|
||||
/// 只接受当前播放器会话的事件;首帧之前不显示已连接,旧回调不能恢复画面。
|
||||
|
||||
@@ -59,7 +59,6 @@ final class DroneDockPreparationViewModel {
|
||||
private let planeStatusService: any DroneDockPlaneStatusServing
|
||||
private let taskCreationService: any DroneDockTaskCreating
|
||||
private let shootService: any DroneDockShootServing
|
||||
private let allowsTaskSubmission: Bool
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var generation = 0
|
||||
private var planeStatusGeneration = 0
|
||||
@@ -80,7 +79,6 @@ final class DroneDockPreparationViewModel {
|
||||
planeStatusService: any DroneDockPlaneStatusServing,
|
||||
taskCreationService: any DroneDockTaskCreating,
|
||||
shootService: any DroneDockShootServing,
|
||||
allowsTaskSubmission: Bool = true,
|
||||
diagnostics: DroneDockTaskDiagnostics = DroneDockTaskDiagnostics(),
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.diagnostics = diagnostics
|
||||
@@ -90,7 +88,6 @@ final class DroneDockPreparationViewModel {
|
||||
self.planeStatusService = planeStatusService
|
||||
self.taskCreationService = taskCreationService
|
||||
self.shootService = shootService
|
||||
self.allowsTaskSubmission = allowsTaskSubmission
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
@@ -106,14 +103,14 @@ final class DroneDockPreparationViewModel {
|
||||
"created_task_id": DroneDockTaskTraceFields.value(createdTask?.taskID),
|
||||
"is_creating": isCreatingTask, "is_summoning": isSummoning,
|
||||
"creation_outcome_unknown": taskCreationOutcomeUnknown, "summon_outcome_unknown": summonOutcomeUnknown,
|
||||
"allows_task_submission": allowsTaskSubmission, "primary_action_enabled": isPrimaryActionEnabled]
|
||||
"primary_action_enabled": isPrimaryActionEnabled]
|
||||
diagnostics.record(event, taskID: createdTask?.taskID ?? 0,
|
||||
fields: summary.merging(fields) { _, new in new })
|
||||
}
|
||||
|
||||
/// 当前阶段只创建待飞任务,不以设备电量或位置作为创建任务条件。
|
||||
var canCreateTask: Bool {
|
||||
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
|
||||
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
|
||||
!hasCreatedTask, !taskCreationOutcomeUnknown, errorMessage == nil,
|
||||
let info = projectInfo, info.order.canCreateTask == true,
|
||||
let total = info.order.shotTotal, total > 0,
|
||||
@@ -125,9 +122,6 @@ final class DroneDockPreparationViewModel {
|
||||
return info.order.orderNumber == number && info.order.projectID == projectID
|
||||
}
|
||||
|
||||
/// 当前入口是否被禁飞限制禁止提交,用于提供独立模拟入口而不放开真实写入。
|
||||
var isTaskSubmissionBlocked: Bool { !allowsTaskSubmission }
|
||||
|
||||
/// 提交中和已有回执/不确定结果时冻结点位航线,避免显示与任务归属不一致。
|
||||
var isTaskSelectionLocked: Bool {
|
||||
isCreatingTask || isSummoning || isPerformingPrimaryAction || hasCreatedTask || taskCreationOutcomeUnknown
|
||||
@@ -149,7 +143,7 @@ final class DroneDockPreparationViewModel {
|
||||
|
||||
/// 首次允许创建再召唤;明确业务拒绝后可对同任务重试,成功和未知结果锁定。
|
||||
var isPrimaryActionEnabled: Bool {
|
||||
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
|
||||
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
|
||||
!isSummoning, !isPerformingPrimaryAction, !taskCreationOutcomeUnknown,
|
||||
!hasSubmittedSummon, !summonOutcomeUnknown, !summonRequiresTaskReview else { return false }
|
||||
if let createdTask { return createdTask.taskID > 0 }
|
||||
@@ -160,8 +154,7 @@ final class DroneDockPreparationViewModel {
|
||||
@MainActor
|
||||
func prepareAndSummon(onStateChange: @escaping @MainActor () -> Void = {}) async {
|
||||
recordPreparationEvent("preparation.primary_requested")
|
||||
// 禁飞调试在整条链开始前阻断,避免先残留真实待飞任务再拦截召唤。
|
||||
guard allowsTaskSubmission, isPrimaryActionEnabled else { return }
|
||||
guard isPrimaryActionEnabled else { return }
|
||||
primaryActionGeneration &+= 1
|
||||
let actionGeneration = primaryActionGeneration
|
||||
isPerformingPrimaryAction = true
|
||||
@@ -180,7 +173,7 @@ final class DroneDockPreparationViewModel {
|
||||
/// 对明确创建成功的任务发送一次召唤;只有明确业务拒绝允许之后再次提交。
|
||||
@MainActor
|
||||
private func summonCreatedTask(onStateChange: @escaping @MainActor () -> Void) async {
|
||||
guard allowsTaskSubmission, !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
|
||||
guard !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
|
||||
!hasSubmittedSummon, !summonOutcomeUnknown,
|
||||
let taskID = createdTask?.taskID, taskID > 0 else { return }
|
||||
summonGeneration &+= 1
|
||||
@@ -211,14 +204,9 @@ final class DroneDockPreparationViewModel {
|
||||
hasSubmittedSummon = true
|
||||
summonOutcomeUnknown = false
|
||||
} catch {
|
||||
if error is DeviceCommandDebugBlockedError, summonGeneration == requestGeneration {
|
||||
didSendSummon = false
|
||||
}
|
||||
guard acceptsSummon(requestGeneration, taskID: taskID) else { return }
|
||||
recordPreparationEvent("summon.failed_or_unknown", fields: ["error": DroneDockDiagnostics.errorFields(error)])
|
||||
if error is DeviceCommandDebugBlockedError {
|
||||
shootErrorMessage = DeviceCommandDebugPolicy.blockedMessage
|
||||
} else if case let APIError.serverCode(code, message) = error {
|
||||
if case let APIError.serverCode(code, message) = error {
|
||||
shootErrorCode = code
|
||||
shootErrorMessage = message
|
||||
summonRequiresTaskReview = DroneDockPreparationFailure.requiresTaskReview(message)
|
||||
@@ -249,7 +237,7 @@ final class DroneDockPreparationViewModel {
|
||||
@MainActor
|
||||
func createTask(onStateChange: @escaping @MainActor () -> Void = {}) async {
|
||||
recordPreparationEvent("create.requested", fields: ["allowed": canCreateTask])
|
||||
guard allowsTaskSubmission, canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
|
||||
guard canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
|
||||
let number = order.orderNumber, let projectID = order.projectID else { return }
|
||||
taskCreationGeneration &+= 1
|
||||
let requestGeneration = taskCreationGeneration
|
||||
@@ -300,14 +288,9 @@ final class DroneDockPreparationViewModel {
|
||||
hasCreatedTask = true
|
||||
taskCreationOutcomeUnknown = false
|
||||
} catch {
|
||||
if error is DeviceCommandDebugBlockedError, taskCreationGeneration == requestGeneration {
|
||||
didSendTaskCreation = false
|
||||
}
|
||||
guard acceptsTaskCreation(requestGeneration) else { return }
|
||||
recordPreparationEvent("create.failed_or_unknown", fields: ["error": DroneDockDiagnostics.errorFields(error)])
|
||||
if error is DeviceCommandDebugBlockedError {
|
||||
taskCreationErrorMessage = DeviceCommandDebugPolicy.blockedMessage
|
||||
} else if didSendTaskCreation {
|
||||
if didSendTaskCreation {
|
||||
if case APIError.serverCode = error {
|
||||
taskCreationErrorMessage = error.localizedDescription
|
||||
} else {
|
||||
@@ -484,7 +467,6 @@ final class DroneDockPreparationViewModel {
|
||||
if isCreatingTask { return "正在创建任务…" }
|
||||
if isSummoning { return "正在召唤无人机…" }
|
||||
if hasSubmittedSummon || summonRequiresTaskReview { return "查看拍摄任务" }
|
||||
if !allowsTaskSubmission { return "仅限模拟调试" }
|
||||
if summonOutcomeUnknown { return "召唤结果待核对" }
|
||||
if hasCreatedTask { return shootErrorMessage == nil ? "确认并召唤" : "重试召唤" }
|
||||
if taskCreationOutcomeUnknown { return "结果待核对" }
|
||||
@@ -497,7 +479,6 @@ final class DroneDockPreparationViewModel {
|
||||
if isCreatingTask { return "正在创建待飞任务,请勿重复点击" }
|
||||
if isSummoning { return "任务已创建,正在提交召唤请求" }
|
||||
if creationContextInvalidated, let errorMessage { return errorMessage }
|
||||
if !allowsTaskSubmission && !hasSubmittedSummon { return DeviceCommandDebugPolicy.blockedMessage }
|
||||
if createdTask != nil {
|
||||
if hasSubmittedSummon { return "召唤请求已提交,请等待任务状态更新" }
|
||||
if let summonFailure { return summonFailure.message }
|
||||
|
||||
@@ -108,9 +108,7 @@ final class DroneDockShootingTaskViewModel {
|
||||
self.hasInitialTravelReceipt = context.canLoad && initialSummonReceipt?.confirmsTravel(
|
||||
taskID: taskID, orderNumber: order.orderNumber, scenicID: context.scenicID) == true
|
||||
recordTaskEvent("task.created", fields: ["initial_travel_receipt": hasInitialTravelReceipt,
|
||||
"summon_receipt": DroneDockTaskTraceFields.summon(initialSummonReceipt),
|
||||
"debug_flight_blocked": DeviceCommandDebugPolicy.blocksFlightRequests,
|
||||
"debug_live_blocked": DeviceCommandDebugPolicy.blocksLiveRequests])
|
||||
"summon_receipt": DroneDockTaskTraceFields.summon(initialSummonReceipt)])
|
||||
}
|
||||
|
||||
/// 页面与业务层共享会话标识,串联准备页导航、快照和按钮事件。
|
||||
@@ -154,6 +152,12 @@ final class DroneDockShootingTaskViewModel {
|
||||
await refreshSnapshot(allowWhileStarting: false)
|
||||
}
|
||||
|
||||
/// 无缓存订阅只认此调用新读取且采纳的 HTTP 结果;读取未发起、失败或旧版本均返回 nil。
|
||||
@MainActor
|
||||
func refreshForRealtimeSeed() async -> DroneDockHTTPSnapshotResult? {
|
||||
await refreshSnapshot(allowWhileStarting: false)
|
||||
}
|
||||
|
||||
/// 操作提交前仅作废在途读取,保留快照和操作防重锁;迟到的旧读取不能覆盖操作回查。
|
||||
@MainActor
|
||||
func invalidatePendingRefresh() {
|
||||
@@ -174,10 +178,11 @@ final class DroneDockShootingTaskViewModel {
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@discardableResult
|
||||
private func refreshSnapshot(allowWhileStarting: Bool, allowWhileCommand: Bool = false,
|
||||
allowWhileResummoning: Bool = false) async {
|
||||
allowWhileResummoning: Bool = false) async -> DroneDockHTTPSnapshotResult? {
|
||||
guard !isLoading, !identityInvalidated, readAccessError == nil, !isStartingShooting || allowWhileStarting,
|
||||
!isResummoning || allowWhileResummoning else { return }
|
||||
!isResummoning || allowWhileResummoning else { return nil }
|
||||
generation &+= 1
|
||||
let requestGeneration = generation
|
||||
let revisionAtRequest = snapshotRevision
|
||||
@@ -189,12 +194,12 @@ final class DroneDockShootingTaskViewModel {
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
guard accepts(requestGeneration) else { return }
|
||||
guard accepts(requestGeneration) else { return nil }
|
||||
guard context.canLoad, taskID > 0,
|
||||
DroneDockHomeMetric.usableText(order.orderNumber) != nil else {
|
||||
response = nil
|
||||
errorMessage = "缺少有效任务、订单或景区信息,请返回工作台重新进入"
|
||||
return
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
let value = try await service.shooting(taskID: taskID, liveBattery: false)
|
||||
@@ -202,24 +207,31 @@ final class DroneDockShootingTaskViewModel {
|
||||
recordTaskEvent("snapshot.http_ignored", fields: ["read_generation": requestGeneration,
|
||||
"current_generation": generation, "reason": "cancelled_or_stale_read",
|
||||
"incoming": DroneDockTaskTraceFields.snapshot(value)])
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if applyTaskSnapshot(value, source: .http) == .invalid {
|
||||
let acceptance = applyTaskSnapshot(value, source: .http)
|
||||
if acceptance == .invalid {
|
||||
response = nil
|
||||
throw APIError.decodeFailed("拍摄任务返回的任务、订单或景区与当前页面不一致,请重新读取")
|
||||
}
|
||||
switch acceptance {
|
||||
case .applied, .duplicate:
|
||||
return DroneDockHTTPSnapshotResult(snapshot: value, acceptance: acceptance)
|
||||
case .outdated, .invalid: return nil
|
||||
}
|
||||
} catch {
|
||||
guard accepts(requestGeneration) else { return }
|
||||
guard accepts(requestGeneration) else { return nil }
|
||||
if Self.stopsTaskReading(error) {
|
||||
stopReading(message: error.localizedDescription)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
// 迟到 HTTP 失败不能覆盖请求期间已收到的有效实时状态。
|
||||
guard snapshotRevision == revisionAtRequest else { return }
|
||||
guard snapshotRevision == revisionAtRequest else { return nil }
|
||||
if !(error is CancellationError) { errorMessage = error.localizedDescription }
|
||||
recordTaskEvent("snapshot.http_failed", fields: [
|
||||
"retained_snapshot": response != nil, "error": DroneDockDiagnostics.errorFields(error)
|
||||
])
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,7 +365,7 @@ final class DroneDockShootingTaskViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
/// 只在有效读取明确到达、锁定飞机并允许 start_shoot 后开放;Debug 真实页面保持禁用。
|
||||
/// 只在有效读取明确到达、锁定飞机并允许 start_shoot 后开放。
|
||||
var canStartShooting: Bool {
|
||||
startService != nil && context.canLoad && taskID > 0 && !identityInvalidated
|
||||
&& hasControlSnapshotIdentity
|
||||
@@ -418,11 +430,8 @@ final class DroneDockShootingTaskViewModel {
|
||||
guard acceptsStart(requestGeneration) else { return }
|
||||
await refreshSnapshot(allowWhileStarting: true)
|
||||
} catch {
|
||||
if error is DeviceCommandDebugBlockedError, startGeneration == requestGeneration { didSendStart = false }
|
||||
guard acceptsStart(requestGeneration) else { return }
|
||||
if error is DeviceCommandDebugBlockedError {
|
||||
startErrorMessage = DeviceCommandDebugPolicy.blockedMessage
|
||||
} else if case APIError.serverCode = error {
|
||||
if case APIError.serverCode = error {
|
||||
startErrorMessage = error.localizedDescription
|
||||
} else {
|
||||
markStartUnknown(reason: error.localizedDescription)
|
||||
@@ -629,10 +638,7 @@ final class DroneDockShootingTaskViewModel {
|
||||
fields: ["reason": "stale_task_context", "error": DroneDockDiagnostics.errorFields(error)])
|
||||
return
|
||||
}
|
||||
if error is DeviceCommandDebugBlockedError {
|
||||
commandOperations[operation.command]?.phase = .finished
|
||||
commandOperations[operation.command]?.error = DeviceCommandDebugPolicy.blockedMessage
|
||||
} else if case APIError.serverCode = error {
|
||||
if case APIError.serverCode = error {
|
||||
commandOperations[operation.command]?.phase = .finished
|
||||
commandOperations[operation.command]?.error = error.localizedDescription
|
||||
} else {
|
||||
@@ -731,11 +737,8 @@ final class DroneDockShootingTaskViewModel {
|
||||
guard acceptsResummon(requestGeneration) else { return }
|
||||
await refreshSnapshot(allowWhileStarting: false, allowWhileResummoning: true)
|
||||
} catch {
|
||||
if error is DeviceCommandDebugBlockedError, resummonGeneration == requestGeneration { didSendResummon = false }
|
||||
guard acceptsResummon(requestGeneration) else { return }
|
||||
if error is DeviceCommandDebugBlockedError {
|
||||
resummonErrorMessage = DeviceCommandDebugPolicy.blockedMessage
|
||||
} else if case APIError.serverCode = error {
|
||||
if case APIError.serverCode = error {
|
||||
didSendResummon = false
|
||||
resummonErrorMessage = error.localizedDescription
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user