feat(drone-dock): 接入图传与重召唤的本地模拟流程
新增 live 接口、状态展示和本地视频预览;支持同任务重新召唤、单次回查和防重,保留禁飞拦截。同步测试、模拟证据、后端接口确认文档及景区空境库范围约定。 验证:168 个相关用例最终均通过;最后复跑 38 项零失败。真实设备指令未发送。
This commit is contained in:
@@ -9,7 +9,10 @@ enum DroneDockStartShootSimulation {
|
||||
static func makeController(arguments: [String]) -> UIViewController? {
|
||||
let startsAtPreparation = arguments.contains("--dock-preparation-mock")
|
||||
let startsAtCommands = arguments.contains("--dock-command-mock")
|
||||
guard startsAtPreparation || startsAtCommands || arguments.contains("--dock-start-shoot-mock") else { return nil }
|
||||
let startsAtResummon = arguments.contains("--dock-resummon-mock")
|
||||
let showsLive = arguments.contains("--dock-live-mock")
|
||||
guard startsAtPreparation || startsAtCommands || startsAtResummon || showsLive
|
||||
|| arguments.contains("--dock-start-shoot-mock") else { return nil }
|
||||
let resultArgument = arguments.first { $0.hasPrefix("--dock-start-shoot-result=") }
|
||||
let result = resultArgument.flatMap {
|
||||
DroneDockStartShootMockResult(rawValue: String($0.dropFirst("--dock-start-shoot-result=".count)))
|
||||
@@ -18,8 +21,16 @@ enum DroneDockStartShootSimulation {
|
||||
let commandResult = commandResultArgument.flatMap {
|
||||
DroneDockStartShootMockResult(rawValue: String($0.dropFirst("--dock-command-result=".count)))
|
||||
} ?? .success
|
||||
let resummonResult = arguments.first { $0.hasPrefix("--dock-resummon-result=") }.flatMap {
|
||||
DroneDockStartShootMockResult(rawValue: String($0.dropFirst("--dock-resummon-result=".count)))
|
||||
} ?? .success
|
||||
let liveResult = arguments.first { $0.hasPrefix("--dock-live-result=") }.map {
|
||||
String($0.dropFirst("--dock-live-result=".count))
|
||||
} ?? "success"
|
||||
return makeFlow(result: result, startsAtPreparation: startsAtPreparation,
|
||||
commandResult: commandResult, startsAtCommands: startsAtCommands)
|
||||
commandResult: commandResult, startsAtCommands: startsAtCommands,
|
||||
startsAtResummon: startsAtResummon, resummonResult: resummonResult,
|
||||
showsLive: showsLive, liveResult: liveResult)
|
||||
}
|
||||
|
||||
/// 从正式准备页进入全新的纯内存模拟,不携带真实订单、账号或设备身份。
|
||||
@@ -30,9 +41,13 @@ enum DroneDockStartShootSimulation {
|
||||
private static func makeFlow(result: DroneDockStartShootMockResult,
|
||||
startsAtPreparation: Bool,
|
||||
commandResult: DroneDockStartShootMockResult = .success,
|
||||
startsAtCommands: Bool = false) -> UIViewController? {
|
||||
startsAtCommands: Bool = false, startsAtResummon: Bool = false,
|
||||
resummonResult: DroneDockStartShootMockResult = .success,
|
||||
showsLive: Bool = false, liveResult: String = "success") -> UIViewController? {
|
||||
let session = DroneDockStartShootMockSession(result: result, requiresPreparation: startsAtPreparation,
|
||||
commandResult: commandResult, hasStarted: startsAtCommands && !startsAtPreparation)
|
||||
commandResult: commandResult, hasStarted: startsAtCommands && !startsAtPreparation,
|
||||
hasFailed: startsAtResummon && !startsAtPreparation, resummonResult: resummonResult,
|
||||
liveResult: liveResult)
|
||||
let environment = APIEnvironment(baseURL: URL(string: "https://mock.invalid")!,
|
||||
webSocketURL: URL(string: "wss://mock.invalid/unused")!)
|
||||
let client = APIClient(environment: environment, session: session)
|
||||
@@ -45,8 +60,14 @@ enum DroneDockStartShootSimulation {
|
||||
let model = DroneDockShootingTaskViewModel(context: taskContext, taskID: taskID, order: taskOrder,
|
||||
service: DroneDockShootingAPI(client: client),
|
||||
startService: DroneDockStartShootAPI(client: client),
|
||||
commandService: DroneDockCommandAPI(client: client), currentContext: { context })
|
||||
return DroneDockShootingTaskViewController(viewModel: model,
|
||||
commandService: DroneDockCommandAPI(client: client),
|
||||
resummonService: startsAtResummon ? DroneDockShootAPI(client: client) : nil,
|
||||
currentContext: { context })
|
||||
let live = showsLive ? DroneDockLiveViewModel(context: taskContext, taskID: taskID,
|
||||
service: DroneDockLiveAPI(client: client),
|
||||
localPreviewURL: liveResult == "success" ? Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4") : nil,
|
||||
currentContext: { context }) : nil
|
||||
return DroneDockShootingTaskViewController(viewModel: model, liveViewModel: live,
|
||||
materialsControllerFactory: { _, _ in
|
||||
let unavailable = UIViewController()
|
||||
unavailable.title = "本地模拟不连接素材库"
|
||||
@@ -79,6 +100,8 @@ private enum DroneDockStartShootMockResult: String {
|
||||
private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
private let result: DroneDockStartShootMockResult
|
||||
private let commandResult: DroneDockStartShootMockResult
|
||||
private let resummonResult: DroneDockStartShootMockResult
|
||||
private let liveResult: String
|
||||
private let lock = NSLock()
|
||||
private var hasCreated: Bool
|
||||
private var hasSummoned: Bool
|
||||
@@ -86,14 +109,19 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
private var isPaused = false
|
||||
private var isReturning = false
|
||||
private var wasInterrupted = false
|
||||
private var hasFailed: Bool
|
||||
private var exchanges: [Exchange] = []
|
||||
|
||||
/// 固定当前实例结果,不能用参数切换到真实传输。
|
||||
init(result: DroneDockStartShootMockResult, requiresPreparation: Bool,
|
||||
commandResult: DroneDockStartShootMockResult, hasStarted: Bool) {
|
||||
commandResult: DroneDockStartShootMockResult, hasStarted: Bool,
|
||||
hasFailed: Bool, resummonResult: DroneDockStartShootMockResult, liveResult: String) {
|
||||
self.result = result
|
||||
self.commandResult = commandResult
|
||||
self.hasStarted = hasStarted
|
||||
self.hasFailed = hasFailed
|
||||
self.resummonResult = resummonResult
|
||||
self.liveResult = liveResult
|
||||
hasCreated = !requiresPreparation
|
||||
hasSummoned = !requiresPreparation
|
||||
}
|
||||
@@ -188,8 +216,22 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
let object = try JSONSerialization.jsonObject(with: body) as? [String: Any],
|
||||
Set(object.keys) == ["id", "restart"],
|
||||
let values = try? JSONDecoder().decode(ShootBody.self, from: body),
|
||||
values.id == 901, !values.restart else { throw URLError(.badURL) }
|
||||
guard hasCreated, !hasSummoned else {
|
||||
values.id == 901 else { throw URLError(.badURL) }
|
||||
if values.restart {
|
||||
guard hasCreated, hasSummoned, hasFailed else {
|
||||
return try envelope([:], code: 100099, message: "本地模拟:当前任务不允许重新召唤")
|
||||
}
|
||||
switch resummonResult {
|
||||
case .businessFailure:
|
||||
return try envelope([:], code: 100099, message: "本地模拟:设备暂不可用,重新召唤被拒绝")
|
||||
case .timeout:
|
||||
throw URLError(.timedOut)
|
||||
case .success:
|
||||
hasFailed = false
|
||||
hasStarted = false
|
||||
isPaused = false
|
||||
}
|
||||
} else if !hasCreated || hasSummoned {
|
||||
return try envelope([:], code: 100099, message: "本地模拟:任务尚未创建或已召唤")
|
||||
}
|
||||
hasSummoned = true
|
||||
@@ -207,6 +249,16 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
guard hasSummoned else { return try envelope([:], code: 100099, message: "本地模拟:请先完成模拟召唤") }
|
||||
return try envelope(shootingData())
|
||||
}
|
||||
if request.httpMethod == "GET", url.path == "/api/app/airport/live" {
|
||||
guard queryMatches(url, required: ["id": "901"]), hasSummoned else { throw URLError(.badURL) }
|
||||
if liveResult == "timeout" { throw URLError(.timedOut) }
|
||||
if liveResult == "business-failure" {
|
||||
return try envelope([:], code: 100099, message: "本地模拟:图传连接不可用")
|
||||
}
|
||||
// 文档未提供播放协议;这里只模拟已知字段,本地视频通过独立依赖注入,绝不伪装成后端字段。
|
||||
return try envelope(["order_flight_task_id": 901, "airport": [:], "drone": [:],
|
||||
"task": taskData(), "airport_sn": "LOCAL-MOCK-DOCK-201", "metrics": simulatedMetrics])
|
||||
}
|
||||
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],
|
||||
@@ -243,7 +295,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
throw URLError(.unsupportedURL)
|
||||
}
|
||||
guard hasSummoned else { return try envelope([:], code: 100099, message: "本地模拟:请先完成模拟召唤") }
|
||||
guard !isPaused, !wasInterrupted else {
|
||||
guard !isPaused, !wasInterrupted, !hasFailed else {
|
||||
return try envelope([:], code: 100099, message: "本地模拟:任务暂停或已中断,不可开始拍摄")
|
||||
}
|
||||
guard !hasStarted else {
|
||||
@@ -282,9 +334,11 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
}
|
||||
|
||||
private func shootingData() -> [String: Any] {
|
||||
let canStart = !hasStarted && !isPaused && !wasInterrupted
|
||||
let canStart = !hasStarted && !isPaused && !wasInterrupted && !hasFailed
|
||||
let phaseTitle: String
|
||||
if wasInterrupted {
|
||||
if hasFailed {
|
||||
phaseTitle = "本地模拟:航线执行失败,可验证重新召唤流程"
|
||||
} else if wasInterrupted {
|
||||
phaseTitle = isReturning ? "本地模拟:立即返航中断任务;无人机返航中" : "本地模拟:返航已取消,任务仍已中断"
|
||||
} else if isPaused {
|
||||
phaseTitle = "拍摄已暂停(模拟)"
|
||||
@@ -322,7 +376,13 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
private func taskData() -> [String: Any] {
|
||||
var task: [String: Any] = ["id": 901, "order_number": "LOCAL-MOCK-ORDER-901", "scenic_id": 128,
|
||||
"status": 20, "status_label": "执行中(模拟)", "wayline_status": 20, "wayline_status_label": "执行中(模拟)"]
|
||||
if wasInterrupted {
|
||||
if hasFailed {
|
||||
task["status"] = 40
|
||||
task["status_label"] = "任务失败(模拟)"
|
||||
task["wayline_status"] = 91
|
||||
task["wayline_status_label"] = "执行失败(模拟)"
|
||||
task["result_message"] = "本地模拟:航线执行失败,无人机空闲待命。"
|
||||
} else if wasInterrupted {
|
||||
task["status"] = 40
|
||||
task["status_label"] = "任务已中断(模拟)"
|
||||
task["wayline_status"] = 40
|
||||
@@ -335,13 +395,14 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
}
|
||||
|
||||
private var availableCommands: [DroneDockSafetyCommand] {
|
||||
guard hasSummoned else { return [] }
|
||||
guard hasSummoned, !hasFailed else { return [] }
|
||||
if isReturning { return [.cancelReturn] }
|
||||
if wasInterrupted { return [] }
|
||||
return [isPaused ? .resume : .pause, .returnHome]
|
||||
}
|
||||
|
||||
private var simulatedPhase: String {
|
||||
if hasFailed { return "failed" }
|
||||
if isReturning { return "returning" }
|
||||
if wasInterrupted { return "failed" }
|
||||
if isPaused { return "paused" }
|
||||
@@ -380,7 +441,7 @@ private final class DroneDockStartShootMockSession: LocalSimulationURLSession {
|
||||
}
|
||||
}
|
||||
|
||||
/// 召唤只接受同一模拟任务且禁止重启,不产生真实设备操作。
|
||||
/// 首次召唤或失败重召唤只接受同一模拟任务,不产生真实设备操作。
|
||||
private struct ShootBody: Decodable {
|
||||
let id: Int
|
||||
let restart: Bool
|
||||
|
||||
Reference in New Issue
Block a user