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
@@ -9,7 +9,7 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
private let regularSize = CGSize(width: 402, height: 220)
private let phases: [DroneDockTaskDisplayPhase] = [
.loading, .navigating, .ready, .shooting, .postShootUploading, .postShootUploadFailed, .paused, .returning,
.completed, .failed, .blocked, .unknown, .readError
.returned, .completed, .failed, .blocked, .unknown, .readError
]
/// 紧凑屏逐一显示全部业务阶段,控件始终留在固定中间区域内。
@@ -36,7 +36,7 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
let note = try XCTUnwrap(find(in: status, suffix: "failureNote") as? UILabel)
XCTAssertFalse(isHidden(note))
XCTAssertEqual(note.text, "无人机离线,暂不可召唤")
} else if [.shooting, .postShootUploading, .completed].contains(phase) {
} else if [.shooting, .postShootUploading, .completed, .paused, .returning, .returned].contains(phase) {
XCTAssertTrue(isHidden(device))
} else {
XCTAssertFalse(isHidden(device))
@@ -147,16 +147,18 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
var actionCalls = 0
status.onPrimaryAction = { actionCalls += 1 }
apply(presentation(.ready, countdownSeconds: 12), to: status)
let countdown = try XCTUnwrap(find(in: status, suffix: "countdown") as? UILabel)
XCTAssertFalse(isHidden(countdown))
let start = try XCTUnwrap(find(in: status, suffix: "primary") as? UIButton)
XCTAssertEqual(start.title(for: .normal), "开始拍摄(12s)")
XCTAssertNil(find(in: status, suffix: "countdown"))
apply(presentation(.shooting), to: status)
let indicator = try XCTUnwrap(find(in: status, suffix: "shootingIndicator") as? UIButton)
XCTAssertFalse(isHidden(indicator))
XCTAssertTrue(isHidden(countdown))
status.updateCountdown(seconds: 5)
layoutHierarchy(status)
XCTAssertTrue(isHidden(countdown))
XCTAssertNil(countdown.text)
XCTAssertNil(find(in: status, suffix: "countdown"))
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden")
XCTAssertEqual(status.diagnosticFields["action_title"] as? String, "拍摄中")
XCTAssertFalse(start.currentTitle?.contains("s)") == true)
indicator.sendActions(for: .touchUpInside)
XCTAssertEqual(actionCalls, 0)
apply(presentation(.readError), to: status)
@@ -184,15 +186,14 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
for longCopy in [false, true] {
let status = makeStatus(size: size)
apply(presentation(.ready, longCopy: longCopy, countdownSeconds: 30), to: status)
let countdown = try XCTUnwrap(find(in: status, suffix: "countdown") as? UILabel)
let primary = try XCTUnwrap(find(in: status, suffix: "primary") as? UIButton)
let title = try XCTUnwrap(find(in: status, suffix: "statusTitle") as? UILabel)
let initialTitle = title.text
let initialButtonTitle = primary.title(for: .normal)
let initialFrame = primary.convert(primary.bounds, to: status)
var actionCalls = 0
status.onPrimaryAction = { actionCalls += 1 }
XCTAssertEqual(countdown.text, "自动开拍倒计时:30 秒")
XCTAssertFalse(isHidden(countdown))
XCTAssertNil(find(in: status, suffix: "countdown"))
try assertFullButtonTitle("开始拍摄(30s)", button: primary)
XCTAssertFalse(primary.isEnabled)
try assertReadableContainedContent(in: status)
if !longCopy { capture(status, name: "到达倒计时30秒-\(Int(size.width))x\(Int(size.height))") }
@@ -200,10 +201,10 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
for seconds in [29, 12, 1, 0, -1] {
status.updateCountdown(seconds: seconds)
layoutHierarchy(status)
XCTAssertEqual(countdown.text, "自动开拍倒计时:\(max(0, seconds)) 秒")
XCTAssertFalse(isHidden(countdown))
XCTAssertNil(find(in: status, suffix: "countdown"))
XCTAssertTrue(find(in: status, suffix: "primary") === primary)
XCTAssertEqual(primary.title(for: .normal), initialButtonTitle)
try assertFullButtonTitle("开始拍摄(\(max(0, seconds))s)", button: primary)
assertEqual(primary.convert(primary.bounds, to: status), initialFrame, message: "每秒文字刷新保持完整点击区域")
XCTAssertEqual(title.text, initialTitle)
XCTAssertFalse(primary.isEnabled, "倒数到零也不能放开开拍动作")
XCTAssertEqual(actionCalls, 0, "数字更新不能触发开拍或其他主动作")
@@ -213,8 +214,10 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
if !longCopy { capture(status, name: "到达倒计时0秒-仍待接口更新-\(Int(size.width))x\(Int(size.height))") }
status.updateCountdown(seconds: nil)
layoutHierarchy(status)
XCTAssertTrue(isHidden(countdown))
XCTAssertNil(countdown.text)
XCTAssertNil(find(in: status, suffix: "countdown"))
try assertFullButtonTitle("开始拍摄", button: primary)
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden")
assertEqual(primary.convert(primary.bounds, to: status), initialFrame, message: "清除计时不改变按钮布局")
XCTAssertEqual(actionCalls, 0)
try assertReadableContainedContent(in: status)
}
@@ -225,20 +228,31 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
func testCountdownOnlyAppearsForReadyAndClearsAfterPhaseChange() throws {
let status = makeStatus(size: compactSize)
apply(presentation(.ready, countdownSeconds: 30), to: status)
let countdown = try XCTUnwrap(find(in: status, suffix: "countdown") as? UILabel)
XCTAssertFalse(isHidden(countdown))
XCTAssertEqual(status.diagnosticFields["action_title"] as? String, "开始拍摄(30s)")
XCTAssertNil(find(in: status, suffix: "countdown"))
for phase in phases where phase != .ready {
apply(presentation(phase, countdownSeconds: 30), to: status)
XCTAssertTrue(isHidden(countdown), "\(phase)")
XCTAssertNil(countdown.text)
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden", "\(phase)")
XCTAssertFalse((status.diagnosticFields["action_title"] as? String)?.contains("s)") == true)
let actionTitle = status.diagnosticFields["action_title"] as? String
status.updateCountdown(seconds: 12)
layoutHierarchy(status)
XCTAssertTrue(isHidden(countdown), "迟到计时不得覆盖 \(phase)")
XCTAssertNil(countdown.text)
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden", "迟到计时不得覆盖 \(phase)")
XCTAssertEqual(status.diagnosticFields["action_title"] as? String, actionTitle)
XCTAssertNil(find(in: status, suffix: "countdown"))
}
apply(presentation(.ready), to: status)
XCTAssertTrue(isHidden(countdown), "未提供倒计时时保留既有界面")
XCTAssertNil(countdown.text)
XCTAssertEqual(status.diagnosticFields["action_title"] as? String, "开始拍摄")
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden")
XCTAssertNil(find(in: status, suffix: "countdown"))
let refresh = DroneDockTaskPresentation(phase: .ready, title: "无人机已到达", message: "核对状态",
deviceStatus: "", action: .refresh, actionTitle: "刷新任务状态", isActionEnabled: true, countdownSeconds: 30)
apply(refresh, to: status)
status.updateCountdown(seconds: 12)
layoutHierarchy(status)
XCTAssertEqual(status.diagnosticFields["action_title"] as? String, "刷新任务状态")
XCTAssertEqual(status.diagnosticFields["countdown"] as? String, "hidden", "ready 的其他动作不能带开拍倒计时")
}
/// 完成态的说明与标题左对齐,素材按钮全宽;刷新或阶段变化不能绕过操作门禁。
@@ -447,8 +461,9 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
case .shooting: (title, action, actionTitle, enabled) = ("无人机正在拍摄中", .none, "拍摄中", false)
case .postShootUploading: (title, action, actionTitle, enabled) = ("拍摄已完成", .materials, "查看素材", true)
case .postShootUploadFailed: (title, action, actionTitle, enabled) = ("素材上传失败", .materials, "查看素材", true)
case .paused: (title, action, actionTitle, enabled) = ("拍摄已暂停", .resume, "继续拍摄", false)
case .returning: (title, action, actionTitle, enabled) = ("无人机正在返航", .refresh, "刷新任务状态", true)
case .paused: (title, action, actionTitle, enabled) = ("无人机已暂停", .resume, "继续任务", true)
case .returning: (title, action, actionTitle, enabled) = ("无人机正在返航", .none, "拍摄已中断", false)
case .returned: (title, action, actionTitle, enabled) = ("本次拍摄已中断", .retry, "重新召唤无人机", true)
case .completed: (title, action, actionTitle, enabled) = ("本次拍摄已完成", .materials, "查看素材", true)
case .failed: (title, action, actionTitle, enabled) = ("任务执行失败", .retry, "重新召唤无人机", false)
case .blocked: (title, action, actionTitle, enabled) = ("当前无法继续拍摄", .refresh, "刷新任务状态", true)
@@ -521,6 +536,17 @@ final class DroneDockTaskFixedLayoutTests: XCTestCase {
file: file, line: line)
}
/// 同时核对模型标题和 UILabel 的实际内容、文字宽度,避免秒数被尾部裁切。
private func assertFullButtonTitle(_ expected: String, button: UIButton,
file: StaticString = #filePath, line: UInt = #line) throws {
XCTAssertEqual(button.title(for: .normal), expected, file: file, line: line)
let label = try XCTUnwrap(button.titleLabel, file: file, line: line)
XCTAssertEqual(label.text, expected, file: file, line: line)
let size = (expected as NSString).size(withAttributes: [.font: label.font as Any])
XCTAssertGreaterThanOrEqual(label.bounds.width + 1, ceil(size.width), "按钮标题不能裁切秒数", file: file, line: line)
XCTAssertGreaterThanOrEqual(button.bounds.height, 44, file: file, line: line)
}
private func assertEqual(_ lhs: CGRect, _ rhs: CGRect, message: String,
file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(lhs.origin.x, rhs.origin.x, accuracy: 0.5, message, file: file, line: line)