feat: 完善机巢任务实时状态、双路图传与继续查看入口

This commit is contained in:
2026-11-03 13:37:06 +08:00
parent 5d48e37cd7
commit 0ed0077ec4
28 changed files with 1326 additions and 213 deletions
@@ -21,6 +21,33 @@ struct DroneDockVolcLiveConfiguration: Equatable, Sendable, CustomDebugStringCon
guard let stream = response.play, stream.available == true else {
throw DroneDockLiveConnectionError("图传尚未提供可用的视频流")
}
return try parse(stream, response: response, now: now)
}
/// 解析两路参数,机巢排在首位;一路缺失或参数无效不影响另一路播放。
static func parseSources(_ response: DroneDockLiveResponse, now: Date) throws -> [Self] {
guard response.playable == true else { return [try parse(response, now: now)] }
var values: [Self] = []
var lastError: Error?
for role in ["airport", "drone"] {
let explicit = role == "airport" ? response.airportStream : response.droneStream
let stream = explicit?.available != nil ? explicit : (response.play?.role == role ? response.play : nil)
guard let stream, stream.available == true else { continue }
do {
guard stream.role == role else { throw DroneDockLiveConnectionError("图传来源与设备角色不一致") }
values.append(try parse(stream, response: response, now: now))
} catch { lastError = error }
}
guard !values.isEmpty else { throw lastError ?? DroneDockLiveConnectionError("图传尚未提供可用的视频流") }
if values.count == 2 {
guard values[0].appID == values[1].appID, values[0].roomID != values[1].roomID else {
throw DroneDockLiveConnectionError("机巢与无人机图传需使用同一应用下的不同房间,请联系后台确认")
}
}
return values
}
private static func parse(_ stream: DroneDockLiveStream, response: DroneDockLiveResponse, now: Date) throws -> Self {
guard stream.urlType == "volc" else {
throw DroneDockLiveConnectionError("暂不支持当前图传类型,请联系后台确认使用火山图传")
}
@@ -79,6 +106,25 @@ struct DroneDockLiveConnectionError: Error, LocalizedError, Equatable, Sendable
/// 播放器明确回调;只有首帧到达才算已显示真实画面。
enum DroneDockLivePlaybackEvent: Equatable, Sendable {
case firstFrame
/// 实际有画面的来源切换;nil 表示两路当前均无视频,继续等待推流。
case sourceChanged(String?)
case failed(String)
case expired
}
/// 双路选流只使用视频就绪/中断事件;HTTP 的 available 不代表首帧已经到达。
struct DroneDockLiveSourceSelection {
private(set) var readyRoles: Set<String> = []
/// 无人机有视频时优先;无人机停流立即退回仍有画面的机巢。
var selectedRole: String? {
if readyRoles.contains("drone") { return "drone" }
if readyRoles.contains("airport") { return "airport" }
return nil
}
/// 重复回调幂等,未知来源不会参与展示。
mutating func setReady(_ ready: Bool, role: String) {
guard ["airport", "drone"].contains(role) else { return }
if ready { readyRoles.insert(role) } else { readyRoles.remove(role) }
}
}