feat: 完善机巢任务安全控制与状态诊断
同步新版飞控与 WebSocket 回执合同,完善独立指令等待、暂停继续和返航状态;增加真机诊断并优化倒计时与素材库展示。
This commit is contained in:
@@ -54,6 +54,7 @@ final class DroneDockPreparationViewModel {
|
||||
outcomeUnknown: summonOutcomeUnknown, code: shootErrorCode)
|
||||
}
|
||||
|
||||
private let diagnostics: DroneDockTaskDiagnostics
|
||||
private let service: any DroneDockPreparationServing
|
||||
private let planeStatusService: any DroneDockPlaneStatusServing
|
||||
private let taskCreationService: any DroneDockTaskCreating
|
||||
@@ -80,7 +81,9 @@ final class DroneDockPreparationViewModel {
|
||||
taskCreationService: any DroneDockTaskCreating,
|
||||
shootService: any DroneDockShootServing,
|
||||
allowsTaskSubmission: Bool = true,
|
||||
diagnostics: DroneDockTaskDiagnostics = DroneDockTaskDiagnostics(),
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.diagnostics = diagnostics
|
||||
self.context = context
|
||||
self.order = order
|
||||
self.service = service
|
||||
@@ -91,6 +94,23 @@ final class DroneDockPreparationViewModel {
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
/// 准备页会话可与后续任务页入口事件关联,区分重新进入和同一次召唤。
|
||||
var diagnosticSessionID: String { diagnostics.sessionID }
|
||||
|
||||
/// 准备阶段日志只观察现有选择、门禁与请求结果,不发起任何额外请求。
|
||||
func recordPreparationEvent(_ event: String, fields: [String: Any] = [:]) {
|
||||
let summary: [String: Any] = ["source": "preparation", "scenic_id": context.scenicID,
|
||||
"order_number": DroneDockTaskTraceFields.value(order.orderNumber),
|
||||
"spot_id": DroneDockTaskTraceFields.value(selectedSpotID),
|
||||
"air_line_id": DroneDockTaskTraceFields.value(selectedRouteID),
|
||||
"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]
|
||||
diagnostics.record(event, taskID: createdTask?.taskID ?? 0,
|
||||
fields: summary.merging(fields) { _, new in new })
|
||||
}
|
||||
|
||||
/// 当前阶段只创建待飞任务,不以设备电量或位置作为创建任务条件。
|
||||
var canCreateTask: Bool {
|
||||
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
|
||||
@@ -139,6 +159,7 @@ final class DroneDockPreparationViewModel {
|
||||
/// 主按钮串接创建与召唤;已有任务跳过创建,成功后的导航由页面处理。
|
||||
@MainActor
|
||||
func prepareAndSummon(onStateChange: @escaping @MainActor () -> Void = {}) async {
|
||||
recordPreparationEvent("preparation.primary_requested")
|
||||
// 禁飞调试在整条链开始前阻断,避免先残留真实待飞任务再拦截召唤。
|
||||
guard allowsTaskSubmission, isPrimaryActionEnabled else { return }
|
||||
primaryActionGeneration &+= 1
|
||||
@@ -180,10 +201,13 @@ final class DroneDockPreparationViewModel {
|
||||
guard acceptsSummon(requestGeneration, taskID: taskID) else { return }
|
||||
do {
|
||||
didSendSummon = true
|
||||
recordPreparationEvent("summon.sending")
|
||||
let response = try await shootService.shoot(taskID: taskID)
|
||||
guard acceptsSummon(requestGeneration, taskID: taskID) else { return }
|
||||
shootResponse = response
|
||||
DroneDockDiagnostics.event("summon.receipt_accepted", taskID: taskID)
|
||||
recordPreparationEvent("summon.receipt_accepted", fields: ["receipt": DroneDockTaskTraceFields.summon(response),
|
||||
"travel_transition_allowed": response.confirmsTravel(taskID: taskID, orderNumber: order.orderNumber,
|
||||
scenicID: context.scenicID)])
|
||||
hasSubmittedSummon = true
|
||||
summonOutcomeUnknown = false
|
||||
} catch {
|
||||
@@ -191,6 +215,7 @@ final class DroneDockPreparationViewModel {
|
||||
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 {
|
||||
@@ -223,6 +248,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,
|
||||
let number = order.orderNumber, let projectID = order.projectID else { return }
|
||||
taskCreationGeneration &+= 1
|
||||
@@ -261,6 +287,7 @@ final class DroneDockPreparationViewModel {
|
||||
}
|
||||
guard acceptsTaskCreation(requestGeneration) else { return }
|
||||
didSendTaskCreation = true
|
||||
recordPreparationEvent("create.sending")
|
||||
let response = try await taskCreationService.createTask(scenicID: context.scenicID,
|
||||
orderNumber: number, spotID: spotID, airLineID: routeID)
|
||||
guard acceptsTaskCreation(requestGeneration) else { return }
|
||||
@@ -269,7 +296,7 @@ final class DroneDockPreparationViewModel {
|
||||
throw APIError.decodeFailed("创建回执与当前订单或项目不一致,请核对任务")
|
||||
}
|
||||
createdTask = response
|
||||
DroneDockDiagnostics.event("create.receipt_accepted", taskID: response.taskID)
|
||||
recordPreparationEvent("create.receipt_accepted", fields: ["receipt_task_id": DroneDockTaskTraceFields.value(response.taskID)])
|
||||
hasCreatedTask = true
|
||||
taskCreationOutcomeUnknown = false
|
||||
} catch {
|
||||
@@ -277,6 +304,7 @@ final class DroneDockPreparationViewModel {
|
||||
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 {
|
||||
@@ -580,16 +608,16 @@ final class DroneDockPreparationViewModel {
|
||||
}
|
||||
|
||||
private func markSummonUnknown(reason: String? = nil) {
|
||||
DroneDockDiagnostics.event("summon.outcome_unknown", taskID: createdTask?.taskID)
|
||||
summonOutcomeUnknown = true
|
||||
shootErrorCode = nil
|
||||
shootErrorMessage = (reason.map { "\($0)。" } ?? "") + "召唤结果待核对,请先核对任务状态,勿重复提交"
|
||||
recordPreparationEvent("summon.outcome_unknown")
|
||||
}
|
||||
|
||||
private func markTaskCreationUnknown(reason: String? = nil) {
|
||||
DroneDockDiagnostics.event("taskcreation.outcome_unknown", taskID: createdTask?.taskID)
|
||||
taskCreationOutcomeUnknown = true
|
||||
taskCreationErrorMessage = (reason.map { "\($0)。" } ?? "") + "创建结果待核对,请先查看订单任务,勿重复提交"
|
||||
recordPreparationEvent("taskcreation.outcome_unknown")
|
||||
}
|
||||
|
||||
private static func message(for error: Error) -> String? {
|
||||
|
||||
Reference in New Issue
Block a user