import Foundation public struct VersionInfo: Equatable { public let version: String /// 命令类型标识,设备可能返回 Int (如 7) 或 String (如 "0x07") public let type: String /// 设备运行日志,BLE 响应中 `log` 字段对应的任意 JSON 对象;缺失/非对象时为 nil。 /// SDK 不解析其内部结构,原样透传给 `update-metadata` 接口。 public let log: [String: Any]? public init(version: String, type: String, log: [String: Any]? = nil) { self.version = version self.type = type self.log = log } public static func == (lhs: VersionInfo, rhs: VersionInfo) -> Bool { guard lhs.version == rhs.version, lhs.type == rhs.type else { return false } return NSDictionary(dictionary: lhs.log ?? [:]).isEqual(to: rhs.log ?? [:]) } } extension VersionInfo { /// 从设备 BLE 响应字节流(已剥离 0 字节)解析。 /// type 字段兼容 Int / String 两种返回形态;log 字段缺失/非对象时返回 nil。 static func parse(from data: Data) throws -> VersionInfo { let cleaned = data.filter { $0 != 0 } let object = try JSONSerialization.jsonObject(with: cleaned) guard let json = object as? [String: Any] else { throw DuooomiBleError.transferFailed("VersionInfo: top-level not an object") } let version = (json["version"] as? String) ?? "" let type: String if let s = json["type"] as? String { type = s } else if let n = json["type"] as? NSNumber { type = "\(n.intValue)" } else { type = "" } let log = json["log"] as? [String: Any] return VersionInfo(version: version, type: type, log: log) } }