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
@@ -10,13 +10,24 @@ final class DroneDockLiveViewModel {
private(set) var response: DroneDockLiveResponse?
/// 图传区域当前可展示状态。
private(set) var state: DroneDockLiveState
/// 请求期间阻止重复获取,不安排定时轮询。
/// 请求期间阻止重复获取,缺少流参数或凭证临期时由页面安排重查。
private(set) var isLoading = false
/// 仅显式正式入口允许解析设备 RTC;本地模拟即使收到参数也不连接设备。
let allowsDevicePlayback: Bool
/// 每次进入页面或回前台仅从空闲态自动加载,状态刷新不重复启动图传。
var loadsAutomatically: Bool { canLoad && state == .idle }
private(set) var rtcConfiguration: DroneDockVolcLiveConfiguration?
/// 同一会话的机巢与无人机配置,可增补或续期而不重启正在播放的一路。
private(set) var rtcConfigurations: [DroneDockVolcLiveConfiguration] = []
private(set) var activeRole: String?
/// 只在缺一路参数或凭证临期时请求 live;普通状态刷新不触发推流请求。
var nextParameterRefreshDelay: TimeInterval? {
guard allowsDevicePlayback, !identityInvalidated, !rtcConfigurations.isEmpty else { return nil }
let expiryDelay = rtcConfigurations.map { $0.expiresAt.timeIntervalSince(now()) - 30 }.min() ?? 30
let discoveryDelay = rtcConfigurations.count < 2 ? Double(min(max(response?.refreshHintMS ?? 30_000, 5_000), 60_000)) / 1000 : .infinity
return max(5, min(expiryDelay, discoveryDelay))
}
private(set) var playbackSessionID: UUID?
private let service: (any DroneDockLiveServing)?
@@ -73,7 +84,7 @@ final class DroneDockLiveViewModel {
case .unavailable(let reason), .failed(let reason): return reason
case .localPreview: return "仅播放本地样片,未连接无人机"
case .connecting: return "正在等待视频画面"
case .playing: return rtcConfiguration?.role == "airport" ? "机巢摄像头" : "无人机摄像头"
case .playing: return (activeRole ?? rtcConfiguration?.role) == "airport" ? "机巢摄像头" : "无人机摄像头"
}
}
@@ -94,7 +105,7 @@ final class DroneDockLiveViewModel {
/// 发起一次图传请求,拒绝重复点击、迟到响应、身份不符及远程模拟视频来源。
@MainActor
func refresh(onRequestStarted: (@MainActor () -> Void)? = nil) async {
func refresh(keepingPlayback: Bool = false, onRequestStarted: (@MainActor () -> Void)? = nil) async {
guard canLoad, let service else { return }
generation &+= 1
let requestGeneration = generation
@@ -113,20 +124,31 @@ final class DroneDockLiveViewModel {
guard accepts(requestGeneration) else { return }
guard value.orderFlightTaskID == taskID, value.task?.id == nil || value.task?.id == taskID,
value.task?.scenicID == nil || value.task?.scenicID == context.scenicID else {
clearPlayback()
throw APIError.decodeFailed("图传回执与当前拍摄任务不一致")
}
let previousResponse = response
let sameDevice = previousResponse?.planeID == value.planeID && previousResponse?.airportSN == value.airportSN
if previousResponse != nil && !sameDevice { clearPlayback() }
response = value
if allowsDevicePlayback {
rtcConfiguration = nil
playbackSessionID = nil
do {
rtcConfiguration = try DroneDockVolcLiveConfiguration.parse(value, now: now())
playbackSessionID = UUID()
state = .connecting
DroneDockDiagnostics.event("live.parameters_valid", taskID: taskID, fields: ["provider": "volc", "playback_session": playbackSessionID?.uuidString ?? ""])
let configurations = try DroneDockVolcLiveConfiguration.parseSources(value, now: now())
let canKeepSession = keepingPlayback && playbackSessionID != nil
&& sameDevice
&& rtcConfigurations.first?.appID == configurations.first?.appID
rtcConfigurations = configurations
rtcConfiguration = configurations.first
if !canKeepSession {
activeRole = nil
playbackSessionID = UUID()
state = .connecting
}
} catch {
state = .unavailable(error.localizedDescription)
DroneDockDiagnostics.event("live.parameters_rejected", taskID: taskID, fields: ["reason": error.localizedDescription])
if !keepingPlayback || rtcConfigurations.isEmpty {
clearPlayback()
state = .unavailable(error.localizedDescription)
}
}
return
}
@@ -145,12 +167,16 @@ final class DroneDockLiveViewModel {
}
} catch is CancellationError {
guard generation == requestGeneration else { return }
state = .idle
if rtcConfigurations.isEmpty { state = .idle }
} catch {
guard accepts(requestGeneration) else { return }
rtcConfiguration = nil
playbackSessionID = nil
state = .failed(error.localizedDescription)
let accessDenied: Bool
if case APIError.serverCode(let code, _) = error { accessDenied = code == 100098 || code == 100099 }
else { accessDenied = false }
if !keepingPlayback || rtcConfigurations.isEmpty || accessDenied {
clearPlayback()
state = .failed(error.localizedDescription)
}
}
}
@@ -161,8 +187,7 @@ final class DroneDockLiveViewModel {
generation &+= 1
isLoading = false
response = nil
rtcConfiguration = nil
playbackSessionID = nil
clearPlayback()
state = service == nil ? .restricted : .idle
}
@@ -179,18 +204,28 @@ final class DroneDockLiveViewModel {
DroneDockDiagnostics.event("live.playback_event", taskID: taskID,
fields: ["playback_session": sessionID.uuidString, "result": String(describing: event)])
switch event {
case .firstFrame: state = .playing
case .firstFrame:
activeRole = rtcConfiguration?.role
state = .playing
case .sourceChanged(let role):
activeRole = role
state = role == nil ? .connecting : .playing
case .failed(let message):
rtcConfiguration = nil
playbackSessionID = nil
clearPlayback()
state = .failed(message)
case .expired:
rtcConfiguration = nil
playbackSessionID = nil
clearPlayback()
state = .unavailable("图传凭证即将或已经过期,请重新获取")
}
}
private func clearPlayback() {
rtcConfiguration = nil
rtcConfigurations = []
activeRole = nil
playbackSessionID = nil
}
/// 只接受当前页面与当前身份下的结果,身份变化后不允许重新请求旧任务。
@MainActor
private func accepts(_ requestGeneration: Int) -> Bool {