feat: 完善机巢任务实时状态、双路图传与继续查看入口
This commit is contained in:
@@ -213,7 +213,11 @@ struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
|
||||
let materialStatus: Int?
|
||||
/// 已有成片的有效任务数。
|
||||
let filmCount: Int?
|
||||
/// 后端是否允许新增待飞任务。
|
||||
/// orders 选定的执行中订单飞行任务 ID;详情和旧摘要未提供时为空。
|
||||
let activeTaskID: Int?
|
||||
/// orders 返回的执行中状态,当前合同有任务时为 20,不作为任务页阶段。
|
||||
let activeTaskStatus: Int?
|
||||
/// 后端是否允许新增待飞任务;有执行中任务时优先查看该任务。
|
||||
let canCreateTask: Bool?
|
||||
/// 后端是否允许上传成片。
|
||||
let canUploadFilm: Bool?
|
||||
@@ -230,6 +234,7 @@ struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
|
||||
case touristPhoneMask = "user_phone", packageName = "project_name"
|
||||
case shotTotal = "shoot_count", shotUsed = "shot_count", shotRemain = "remaining_count"
|
||||
case shootStatus = "shoot_status", orderStatus = "order_status", materialStatus = "material_status"
|
||||
case activeTaskID = "active_task_id", activeTaskStatus = "active_task_status"
|
||||
case filmCount = "film_count", canCreateTask = "can_create_task", canUploadFilm = "can_upload_film"
|
||||
case canConfirm = "can_confirm", confirmStatus = "confirm_status", confirmedAt = "confirmed_at"
|
||||
}
|
||||
@@ -263,6 +268,11 @@ struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
|
||||
shootStatus = try container.decodeIfPresent(String.self, forKey: .shootStatus)
|
||||
orderStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .orderStatus)?.value
|
||||
materialStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .materialStatus)?.value
|
||||
activeTaskID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .activeTaskID)?.value
|
||||
activeTaskStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .activeTaskStatus)?.value
|
||||
if let activeTaskID, activeTaskID <= 0 {
|
||||
throw DecodingError.dataCorruptedError(forKey: .activeTaskID, in: container, debugDescription: "进行中任务 ID 必须为正整数")
|
||||
}
|
||||
canCreateTask = try container.decodeIfPresent(Bool.self, forKey: .canCreateTask)
|
||||
canUploadFilm = try container.decodeIfPresent(Bool.self, forKey: .canUploadFilm)
|
||||
canConfirm = try container.decodeIfPresent(Bool.self, forKey: .canConfirm)
|
||||
@@ -312,8 +322,9 @@ struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
|
||||
/// 邀请游客确认须拍满套餐次数,且后端明确允许发起确认。
|
||||
var canInviteVisitorConfirmation: Bool { hasCompletedShootingQuota && canConfirm == true }
|
||||
|
||||
/// 依据后端权限提供入口标识;邀请确认额外要求成功拍满套餐次数。
|
||||
/// 后端选定的执行中任务优先于新增拍摄;无任务时沿用交付和次数规则。
|
||||
var primaryAction: String? {
|
||||
if activeTaskID != nil { return "view_task" }
|
||||
if canCreateTask == true { return "start_shoot" }
|
||||
if canInviteVisitorConfirmation { return "confirm_delivery" }
|
||||
if canUploadFilm == true { return "view_materials" }
|
||||
@@ -323,6 +334,7 @@ struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
|
||||
/// 与后端权限对应的按钮文案。
|
||||
var actionTitle: String? {
|
||||
switch primaryAction {
|
||||
case "view_task": return "继续查看任务"
|
||||
case "start_shoot": return "开始拍摄"
|
||||
case "confirm_delivery": return "邀请确认"
|
||||
case "view_materials": return "查看素材"
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,11 @@ struct DroneDockLiveResponse: Decodable, Equatable, Sendable {
|
||||
let playable: Bool?
|
||||
let unavailableMessage: String?
|
||||
let play: DroneDockLiveStream?
|
||||
/// 两路独立接收参数;是否真正有画面由 RTC 视频事件判断。
|
||||
let airportStream: DroneDockLiveStream?
|
||||
let droneStream: DroneDockLiveStream?
|
||||
/// 后端建议的缺流重查间隔,缺失时按 30 秒处理。
|
||||
let refreshHintMS: Int?
|
||||
/// 随接口返回的展示读数,不触发额外实时采集。
|
||||
let metrics: [DroneDockHomeMetric]
|
||||
/// 脱敏回执,不包含 RTC 连接串或 Token。
|
||||
@@ -24,6 +29,7 @@ struct DroneDockLiveResponse: Decodable, Equatable, Sendable {
|
||||
/// live 文档中的已知字段映射。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case orderFlightTaskID = "order_flight_task_id", airport, drone, task
|
||||
case protocolInfo = "protocol"
|
||||
case airportSN = "airport_sn", metrics
|
||||
case planeID = "plane_id", playable, play, unavailableMessage = "unavailable_message"
|
||||
}
|
||||
@@ -40,6 +46,13 @@ struct DroneDockLiveResponse: Decodable, Equatable, Sendable {
|
||||
playable = try container.decodeIfPresent(Bool.self, forKey: .playable)
|
||||
unavailableMessage = try container.decodeIfPresent(String.self, forKey: .unavailableMessage)
|
||||
play = try container.decodeIfPresent(DroneDockLiveStream.self, forKey: .play)
|
||||
airportStream = try? container.decodeIfPresent(DroneDockLiveStream.self, forKey: .airport)
|
||||
droneStream = try? container.decodeIfPresent(DroneDockLiveStream.self, forKey: .drone)
|
||||
let protocolInfo = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .protocolInfo)
|
||||
if case .object(let fields) = protocolInfo, case .number(let milliseconds) = fields["refresh_hint_ms"],
|
||||
milliseconds.isFinite, milliseconds > 0, milliseconds < 3_600_000 {
|
||||
refreshHintMS = NSDecimalNumber(decimal: milliseconds).intValue
|
||||
} else { refreshHintMS = nil }
|
||||
metrics = try container.decodeIfPresent([DroneDockHomeMetric].self, forKey: .metrics) ?? []
|
||||
let raw = try DroneDockHomeJSONValue(from: decoder)
|
||||
let encoder = JSONEncoder()
|
||||
|
||||
@@ -7,22 +7,23 @@ enum DroneDockSnapshotAcceptance {
|
||||
|
||||
/// 先解析业务码再解析 data,兼容订阅失败时 data=[]。
|
||||
enum DroneDockShootingSocketMessage {
|
||||
case opened
|
||||
case subscribed(taskID: Int, hasSnapshot: Bool)
|
||||
case snapshot(DroneDockShootingResponse)
|
||||
case rejected(code: Int, message: String)
|
||||
case unrelated
|
||||
|
||||
/// WebSocket 成功码为 0,不使用 HTTP 的 100000 Envelope。
|
||||
/// 按新版协议解析 100000 成功包,欢迎帧仅用于触发 310,不代表订阅成功。
|
||||
static func decode(_ data: Data) throws -> Self {
|
||||
// 服务端建连先发 Opened。它不代表 310 订阅成功,也不能取消订阅超时或 HTTP 兜底。
|
||||
if String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) == "Opened" {
|
||||
return .unrelated
|
||||
return .opened
|
||||
}
|
||||
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
||||
if json as? String == "Opened" { return .unrelated }
|
||||
if json as? String == "Opened" { return .opened }
|
||||
guard let root = json as? [String: Any],
|
||||
let code = root["code"] as? Int else { throw APIError.invalidResponse }
|
||||
if code != 0 {
|
||||
if code != 100000 {
|
||||
return .rejected(code: code, message: root["msg"] as? String ?? "任务订阅失败")
|
||||
}
|
||||
guard let body = root["data"] as? [String: Any] else { throw APIError.invalidResponse }
|
||||
|
||||
@@ -10,6 +10,8 @@ enum DroneDockTaskDisplayPhase: String, Equatable, Sendable {
|
||||
case ready
|
||||
/// 服务端明确正在拍摄。
|
||||
case shooting
|
||||
/// 录制已结束,素材仍在下载、上传或渲染,尚未就绪。
|
||||
case postShootUploading
|
||||
/// 服务端明确已暂停。
|
||||
case paused
|
||||
/// 服务端明确正在返航。
|
||||
|
||||
Reference in New Issue
Block a user