181 lines
5.6 KiB
Swift
181 lines
5.6 KiB
Swift
import Foundation
|
||
|
||
/// 扫描阶段发现的设备摘要信息。
|
||
public struct DuooomiDiscoveredDevice {
|
||
/// 设备唯一标识。
|
||
public let id: String
|
||
/// 设备名称,可能为空。
|
||
public let name: String?
|
||
/// 信号强度,可能为空。
|
||
public let rssi: Int?
|
||
|
||
init(dictionary: [String: Any]) throws {
|
||
self.id = try DuooomiBleValueDecoder.requiredString(dictionary, key: "id")
|
||
self.name = DuooomiBleValueDecoder.optionalString(dictionary, key: "name")
|
||
self.rssi = DuooomiBleValueDecoder.optionalInt(dictionary, key: "rssi")
|
||
}
|
||
}
|
||
|
||
/// 设备连接结果摘要。
|
||
public struct DuooomiConnectedDevice {
|
||
/// 设备唯一标识。
|
||
public let id: String
|
||
/// 设备名称,可能为空。
|
||
public let name: String?
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.id = try DuooomiBleValueDecoder.requiredString(dictionary, key: "id")
|
||
self.name = DuooomiBleValueDecoder.optionalString(dictionary, key: "name")
|
||
}
|
||
}
|
||
|
||
/// 设备详细信息。
|
||
public struct DuooomiDeviceInfo {
|
||
/// 总存储空间,字符串表示以避免跨桥数值精度问题。
|
||
public let allspace: String
|
||
/// 可用存储空间,字符串表示以避免跨桥数值精度问题。
|
||
public let freespace: String
|
||
/// 设备名称。
|
||
public let name: String
|
||
/// 设备分辨率,例如 `360x360`。
|
||
public let size: String
|
||
/// 品牌名。
|
||
public let brand: String
|
||
/// 电量百分比,范围 `0...100`。
|
||
public let powerlevel: Int
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.allspace = try DuooomiBleValueDecoder.requiredString(dictionary, key: "allspace")
|
||
self.freespace = try DuooomiBleValueDecoder.requiredString(dictionary, key: "freespace")
|
||
self.name = try DuooomiBleValueDecoder.requiredString(dictionary, key: "name")
|
||
self.size = try DuooomiBleValueDecoder.requiredString(dictionary, key: "size")
|
||
self.brand = try DuooomiBleValueDecoder.requiredString(dictionary, key: "brand")
|
||
self.powerlevel = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "powerlevel")
|
||
}
|
||
}
|
||
|
||
/// 设备版本信息。
|
||
public struct DuooomiVersionInfo {
|
||
/// 固件版本号。
|
||
public let version: String
|
||
/// 协议返回的版本类型字段。
|
||
public let type: String
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.version = try DuooomiBleValueDecoder.requiredString(dictionary, key: "version")
|
||
self.type = try DuooomiBleValueDecoder.requiredString(dictionary, key: "type")
|
||
}
|
||
}
|
||
|
||
/// 绑定结果信息。
|
||
public struct DuooomiBindingResult {
|
||
/// 协议类型。
|
||
public let type: Int
|
||
/// 设备 SN。
|
||
public let sn: String
|
||
/// 绑定结果,`1` 表示成功。
|
||
public let success: Int
|
||
/// 已存在内容列表。
|
||
public let contents: [String]
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.type = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "type")
|
||
self.sn = try DuooomiBleValueDecoder.requiredString(dictionary, key: "sn")
|
||
self.success = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "success")
|
||
self.contents = DuooomiBleValueDecoder.stringArray(dictionary, key: "contents")
|
||
}
|
||
}
|
||
|
||
/// 解绑结果信息。
|
||
public struct DuooomiUnbindResult {
|
||
/// 解绑结果,`1` 表示成功。
|
||
public let success: Int
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.success = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "success")
|
||
}
|
||
}
|
||
|
||
/// 删除文件结果信息。
|
||
public struct DuooomiDeleteFileResult {
|
||
/// 协议类型。
|
||
public let type: Int
|
||
/// 删除结果,`0` 成功,`1` 失败,`2` 文件不存在。
|
||
public let success: Int
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.type = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "type")
|
||
self.success = try DuooomiBleValueDecoder.requiredInt(dictionary, key: "success")
|
||
}
|
||
}
|
||
|
||
/// 通用布尔完成结果。
|
||
public struct DuooomiCommandResult {
|
||
/// 是否执行成功。
|
||
public let success: Bool
|
||
|
||
init(dictionary: [String: Any?]) throws {
|
||
self.success = try DuooomiBleValueDecoder.requiredBool(dictionary, key: "success")
|
||
}
|
||
}
|
||
|
||
enum DuooomiBleValueDecoder {
|
||
static func requiredString(_ dictionary: [String: Any?], key: String) throws -> String {
|
||
if let value = optionalString(dictionary, key: key) {
|
||
return value
|
||
}
|
||
throw DuooomiBleManagerError(message: "Missing string value for key: \(key)")
|
||
}
|
||
|
||
static func optionalString(_ dictionary: [String: Any?], key: String) -> String? {
|
||
if let value = dictionary[key] as? String {
|
||
return value
|
||
}
|
||
|
||
if let value = dictionary[key] as? NSNumber {
|
||
return value.stringValue
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
static func requiredInt(_ dictionary: [String: Any?], key: String) throws -> Int {
|
||
if let value = optionalInt(dictionary, key: key) {
|
||
return value
|
||
}
|
||
throw DuooomiBleManagerError(message: "Missing integer value for key: \(key)")
|
||
}
|
||
|
||
static func optionalInt(_ dictionary: [String: Any?], key: String) -> Int? {
|
||
if let value = dictionary[key] as? Int {
|
||
return value
|
||
}
|
||
|
||
if let value = dictionary[key] as? NSNumber {
|
||
return value.intValue
|
||
}
|
||
|
||
if let value = dictionary[key] as? String {
|
||
return Int(value)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
static func requiredBool(_ dictionary: [String: Any?], key: String) throws -> Bool {
|
||
if let value = dictionary[key] as? Bool {
|
||
return value
|
||
}
|
||
|
||
if let value = dictionary[key] as? NSNumber {
|
||
return value.boolValue
|
||
}
|
||
|
||
throw DuooomiBleManagerError(message: "Missing boolean value for key: \(key)")
|
||
}
|
||
|
||
static func stringArray(_ dictionary: [String: Any?], key: String) -> [String] {
|
||
(dictionary[key] as? [String]) ?? []
|
||
}
|
||
}
|