feat: 完善机巢任务安全控制与状态诊断

同步新版飞控与 WebSocket 回执合同,完善独立指令等待、暂停继续和返航状态;增加真机诊断并优化倒计时与素材库展示。
This commit is contained in:
2026-11-04 17:34:28 +08:00
parent 1c766ff12f
commit e3979e9e32
44 changed files with 4575 additions and 581 deletions
@@ -106,10 +106,13 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
private var hasCreated: Bool
private var hasSummoned: Bool
private var hasStarted: Bool
private var lastCommandProgress: [String: Any]?
private var isPaused = false
private var isReturning = false
private var wasInterrupted = false
private var hasFailed: Bool
private var executionBatch = 9_001
private var stateVersion = 1
private var exchanges: [Exchange] = []
/// 固定当前实例结果,不能用参数切换到真实传输。
@@ -191,7 +194,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
}
if request.httpMethod == "GET", url.path == "/api/app/airport/plane-status" {
guard queryMatches(url, required: ["scenic_id": "128", "live_battery": "1"]) else { throw URLError(.badURL) }
let plane: [String: Any] = ["id": 201, "name": "本地模拟无人机", "sn_number": "LOCAL-MOCK-PLANE-201",
let plane: [String: Any] = ["id": 201, "name": "本地模拟无人机", "sn_number": "LOCAL-MOCK-DOCK-201",
"online_status": 1, "work_status": 1, "work_status_label": "空闲(模拟)",
"status_text": "本地模拟 · 电量 95%", "battery_percent": 95,
"can_fly": true, "is_receive_order": 1]
@@ -230,11 +233,15 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
hasFailed = false
hasStarted = false
isPaused = false
isReturning = false
wasInterrupted = false
executionBatch += 1
}
} else if !hasCreated || hasSummoned {
return try envelope([:], code: 100099, message: "本地模拟:任务尚未创建或已召唤")
}
hasSummoned = true
stateVersion += 1
return try envelope(["order_flight_task": taskData(), "order_flight_task_id": 901,
"plane_id": 201, "airport_sn": "LOCAL-MOCK-DOCK-201",
"task_uuid": "LOCAL-MOCK-TASK-UUID-901",
@@ -262,7 +269,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
if request.httpMethod == "POST", url.path == "/api/app/airport/command" {
guard url.query == nil, let body = request.httpBody,
let object = try JSONSerialization.jsonObject(with: body) as? [String: Any],
Set(object.keys) == ["id", "command"],
Set(object.keys) == ["id", "command", "client_request_id"],
let values = try? JSONDecoder().decode(CommandBody.self, from: body),
values.id == 901, let command = DroneDockSafetyCommand(rawValue: values.command) else {
throw URLError(.badURL)
@@ -282,7 +289,17 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
case .returnHome: isReturning = true; wasInterrupted = true; isPaused = false
case .cancelReturn: isReturning = false; isPaused = true
}
return try envelope(["command": command.rawValue, "order_flight_task_id": 901,
stateVersion += 1
let commandID = "LOCAL-MOCK-COMMAND-\(stateVersion)"
lastCommandProgress = ["command_id": commandID, "client_request_id": values.clientRequestID,
"command": command.rawValue, "status": "succeeded"]
return try envelope(["accepted": true, "device_confirmed": true,
"command_id": commandID, "client_request_id": values.clientRequestID,
"pending_command": lastCommandProgress ?? [:],
"acceptance": command == .returnHome ? "accepted" : "device_confirmed",
"command": command.rawValue, "order_flight_task_id": 901,
"execution_batch": executionBatch, "state_version": stateVersion,
"flight_control": flightControlData(),
"airport_sn": "LOCAL-MOCK-DOCK-201", "dji_status": simulatedPhase,
"metrics": simulatedMetrics])
}
@@ -308,6 +325,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
throw URLError(.timedOut)
case .success:
hasStarted = true
stateVersion += 1
return try envelope([
"task_uuid": "LOCAL-MOCK-TASK-UUID-901", "order_flight_task_id": 901,
"order_flight_task": taskData(), "plane_id": 201, "airport_sn": "LOCAL-MOCK-DOCK-201",
@@ -355,13 +373,15 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
}
let deviceLabel = isReturning ? "返航中" : isPaused ? "悬停中" : hasStarted ? "拍摄中" : "空闲"
return ["order_number": "LOCAL-MOCK-ORDER-901", "order_flight_task": taskData(), "task": taskData(),
"execution_batch": executionBatch, "state_version": stateVersion, "snapshot_kind": "full",
"flight_control": flightControlData(),
"scenic": ["id": 128, "name": "本地模拟景区"],
"scenic_spot": ["id": 301, "name": "模拟点位"],
"air_line": ["id": 401, "name": "模拟航线", "scenic_spot_id": 301],
"header": ["order_number": "LOCAL-MOCK-ORDER-901", "title": "模拟点位 · 模拟航线",
"tourist_label": "本地模拟,无设备指令", "package_name": "模拟套餐 · 含5次拍摄"],
"current_plane": ["id": 201, "sn_number": "LOCAL-MOCK-PLANE-201", "online_status": 1,
"work_status": hasStarted ? 2 : 1, "work_status_label": deviceLabel,
"current_plane": ["id": 201, "sn_number": "LOCAL-MOCK-DOCK-201", "online_status": 1,
"work_status": hasFailed ? 1 : 2, "work_status_label": deviceLabel,
"status_text": "本地模拟 · \(deviceLabel) · 电量95%"],
"metrics": simulatedMetrics,
"plane_status": ["scenic_id": 128, "connected": true, "poll_hint_ms": 5000],
@@ -369,12 +389,14 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
"message": hasStarted ? "本地模拟:开拍已受理" : "本地模拟:允许验证开拍请求",
"primary_action": canStart ? "start_shoot" : simulatedPhase,
"primary_action_label": hasStarted ? "拍摄中" : "开始拍摄"],
"restart_gate": ["can_restart": hasFailed, "code": hasFailed ? "ok" : "running"],
"ui": ["phase": simulatedPhase, "title": phaseTitle, "commands": commands,
"message": wasInterrupted ? phaseTitle : "本地模拟状态回查,未向设备发送指令。"]]
}
private func taskData() -> [String: Any] {
var task: [String: Any] = ["id": 901, "order_number": "LOCAL-MOCK-ORDER-901", "scenic_id": 128,
"wayline_task_id": executionBatch, "plane_id": 201, "airport_sn": "LOCAL-MOCK-DOCK-201",
"status": 20, "status_label": "执行中(模拟)", "wayline_status": 20, "wayline_status_label": "执行中(模拟)"]
if hasFailed {
task["status"] = 40
@@ -385,7 +407,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
} else if wasInterrupted {
task["status"] = 40
task["status_label"] = "任务已中断(模拟)"
task["wayline_status"] = 40
task["wayline_status"] = 91
task["wayline_status_label"] = "已中断(模拟)"
task["result_code"] = "return_home"
task["result_message"] = isReturning ? "本地模拟:立即返航中断任务;无人机返航中。"
@@ -396,19 +418,34 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
private var availableCommands: [DroneDockSafetyCommand] {
guard hasSummoned, !hasFailed else { return [] }
if isReturning { return [.cancelReturn] }
if isReturning { return [isPaused ? .resume : .pause] }
if wasInterrupted { return [] }
return [isPaused ? .resume : .pause, .returnHome]
}
private var simulatedPhase: String {
if hasFailed { return "failed" }
if isReturning { return "returning" }
if isPaused { return "manual_paused" }
if isReturning { return "returning_home" }
if wasInterrupted { return "failed" }
if isPaused { return "paused" }
return hasStarted ? "shooting" : "arrived_pending_start"
}
/// 同一模拟批次的执行状态和权限;回巢仅在显式失败起始场景中提供 inside 证据。
private func flightControlData() -> [String: Any] {
let state = hasFailed ? "returned" : isPaused ? "paused" : isReturning ? "returning" : "executing"
let pauseReason: Any = isPaused ? "manual" : (hasStarted ? NSNull() : "arrival_wait" as Any)
return ["state": state, "execution_batch": executionBatch,
"is_airborne": !hasFailed, "pause_reason": pauseReason,
"pending_command": lastCommandProgress ?? [:],
"resume_phase": isReturning ? "returning" : hasStarted ? "shooting" : "arrived_pending_start",
"return_reason": wasInterrupted ? "manual" : NSNull(),
"drone_in_dock": hasFailed ? "inside" : "outside",
"can_pause": availableCommands.contains(.pause),
"can_resume": availableCommands.contains(.resume),
"can_return_home": availableCommands.contains(.returnHome)]
}
private var simulatedMetrics: [[String: String]] {
[["key": "battery", "value": "95%"], ["key": "env_temp", "value": "20.3℃"],
["key": "wind_speed", "value": "0m/s"], ["key": "rainfall", "value": "无雨"],
@@ -456,6 +493,8 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
private struct CommandBody: Decodable {
let id: Int
let command: String
let clientRequestID: String
private enum CodingKeys: String, CodingKey { case id, command, clientRequestID = "client_request_id" }
}
}
#endif