feat(drone-dock): 接入火山实时图传并保留禁飞保护
This commit is contained in:
@@ -1,32 +1,42 @@
|
||||
import Foundation
|
||||
|
||||
/// 任务图传区域的单次请求与生命周期;正式入口不注入操作服务,模拟来源单独注入。
|
||||
/// 任务图传的单次请求与生命周期;正式图传手动开启,模拟来源单独注入。
|
||||
final class DroneDockLiveViewModel {
|
||||
/// 当前图传所属的账号、景区与门店范围。
|
||||
let context: DroneDockHomeContext
|
||||
/// 固定订单飞行任务身份。
|
||||
let taskID: Int
|
||||
/// 最近一次通过身份核对的图传回执,不用作实时播放配置。
|
||||
/// 最近一次通过身份核对的回执;播放配置还需通过独立的协议校验。
|
||||
private(set) var response: DroneDockLiveResponse?
|
||||
/// 图传区域当前可展示状态。
|
||||
private(set) var state: DroneDockLiveState
|
||||
/// 请求期间阻止重复获取,不安排定时轮询。
|
||||
private(set) var isLoading = false
|
||||
/// 仅显式正式入口允许解析设备 RTC;本地模拟即使收到参数也不连接设备。
|
||||
let allowsDevicePlayback: Bool
|
||||
/// 正式图传必须手动开启;演示入口可自动载入本地样片。
|
||||
var loadsAutomatically: Bool { !allowsDevicePlayback }
|
||||
private(set) var rtcConfiguration: DroneDockVolcLiveConfiguration?
|
||||
private(set) var playbackSessionID: UUID?
|
||||
|
||||
private let service: (any DroneDockLiveServing)?
|
||||
private let localPreviewURL: URL?
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var generation = 0
|
||||
private var identityInvalidated = false
|
||||
private let now: @Sendable () -> Date
|
||||
|
||||
/// 本地素材仅供明确的模拟入口注入,后端 JSON 不能生成此 URL。
|
||||
init(context: DroneDockHomeContext, taskID: Int, service: (any DroneDockLiveServing)? = nil,
|
||||
localPreviewURL: URL? = nil,
|
||||
localPreviewURL: URL? = nil, allowsDevicePlayback: Bool = false,
|
||||
now: @escaping @Sendable () -> Date = { Date() },
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.context = context
|
||||
self.taskID = taskID
|
||||
self.service = service
|
||||
self.localPreviewURL = localPreviewURL
|
||||
self.allowsDevicePlayback = allowsDevicePlayback
|
||||
self.now = now
|
||||
self.currentContext = currentContext
|
||||
state = service == nil ? .restricted : .idle
|
||||
}
|
||||
@@ -49,6 +59,8 @@ final class DroneDockLiveViewModel {
|
||||
case .unavailable: return "图传暂不可用"
|
||||
case .failed: return "图传加载失败"
|
||||
case .localPreview: return "本地模拟画面"
|
||||
case .connecting: return "正在连接图传"
|
||||
case .playing: return "实时图传"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +68,12 @@ final class DroneDockLiveViewModel {
|
||||
var message: String {
|
||||
switch state {
|
||||
case .restricted: return "禁飞调试期间不启动设备图传"
|
||||
case .idle: return "按当前任务获取图传信息"
|
||||
case .idle: return allowsDevicePlayback ? "点击开启当前任务的实时图传" : "按当前任务获取图传信息"
|
||||
case .loading: return "请稍候"
|
||||
case .unavailable(let reason), .failed(let reason): return reason
|
||||
case .localPreview: return "仅播放本地样片,未连接无人机"
|
||||
case .connecting: return "正在等待视频画面"
|
||||
case .playing: return rtcConfiguration?.role == "airport" ? "机巢摄像头" : "无人机摄像头"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,10 +83,12 @@ final class DroneDockLiveViewModel {
|
||||
if isLoading { return "正在加载…" }
|
||||
switch state {
|
||||
case .restricted: return nil
|
||||
case .idle: return localPreviewURL == nil ? "获取图传信息" : "查看模拟图传"
|
||||
case .idle: return allowsDevicePlayback ? "开启实时图传" : (localPreviewURL == nil ? "获取图传信息" : "查看模拟图传")
|
||||
case .loading: return "正在加载…"
|
||||
case .failed, .unavailable: return "重试"
|
||||
case .localPreview: return "重新加载模拟图传"
|
||||
case .connecting: return "重新连接"
|
||||
case .playing: return "刷新图传"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +100,7 @@ final class DroneDockLiveViewModel {
|
||||
let requestGeneration = generation
|
||||
guard accepts(requestGeneration) else { return }
|
||||
isLoading = true
|
||||
if playbackURL == nil { state = .loading }
|
||||
if playbackURL == nil && rtcConfiguration == nil { state = .loading }
|
||||
onRequestStarted?()
|
||||
defer {
|
||||
if generation == requestGeneration {
|
||||
@@ -100,6 +116,18 @@ final class DroneDockLiveViewModel {
|
||||
throw APIError.decodeFailed("图传回执与当前拍摄任务不一致")
|
||||
}
|
||||
response = value
|
||||
if allowsDevicePlayback {
|
||||
rtcConfiguration = nil
|
||||
playbackSessionID = nil
|
||||
do {
|
||||
rtcConfiguration = try DroneDockVolcLiveConfiguration.parse(value, now: now())
|
||||
playbackSessionID = UUID()
|
||||
state = .connecting
|
||||
} catch {
|
||||
state = .unavailable(error.localizedDescription)
|
||||
}
|
||||
return
|
||||
}
|
||||
guard let airportSN = DroneDockHomeMetric.usableText(value.airportSN), !airportSN.isEmpty else {
|
||||
state = .unavailable("当前任务尚未返回已锁定的机场信息")
|
||||
return
|
||||
@@ -118,6 +146,8 @@ final class DroneDockLiveViewModel {
|
||||
state = .idle
|
||||
} catch {
|
||||
guard accepts(requestGeneration) else { return }
|
||||
rtcConfiguration = nil
|
||||
playbackSessionID = nil
|
||||
state = .failed(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
@@ -128,9 +158,34 @@ final class DroneDockLiveViewModel {
|
||||
generation &+= 1
|
||||
isLoading = false
|
||||
response = nil
|
||||
rtcConfiguration = nil
|
||||
playbackSessionID = nil
|
||||
state = service == nil ? .restricted : .idle
|
||||
}
|
||||
|
||||
/// 只接受当前播放器会话的事件;首帧之前不显示已连接,旧回调不能恢复画面。
|
||||
@MainActor
|
||||
func receivePlaybackEvent(_ event: DroneDockLivePlaybackEvent, sessionID: UUID) {
|
||||
guard sessionID == playbackSessionID, rtcConfiguration != nil else { return }
|
||||
guard context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
|
||||
invalidate()
|
||||
identityInvalidated = true
|
||||
state = .failed("账号或景区已变化,请返回工作台重新进入")
|
||||
return
|
||||
}
|
||||
switch event {
|
||||
case .firstFrame: state = .playing
|
||||
case .failed(let message):
|
||||
rtcConfiguration = nil
|
||||
playbackSessionID = nil
|
||||
state = .failed(message)
|
||||
case .expired:
|
||||
rtcConfiguration = nil
|
||||
playbackSessionID = nil
|
||||
state = .unavailable("图传凭证即将或已经过期,请重新获取")
|
||||
}
|
||||
}
|
||||
|
||||
/// 只接受当前页面与当前身份下的结果,身份变化后不允许重新请求旧任务。
|
||||
@MainActor
|
||||
private func accepts(_ requestGeneration: Int) -> Bool {
|
||||
|
||||
Reference in New Issue
Block a user