- 增加了wrapper处理原生app的交互

This commit is contained in:
Yudi Xiao
2026-04-08 17:04:29 +08:00
parent 90dcd9605c
commit f9dfde0ace
19 changed files with 1501 additions and 32 deletions

View File

@@ -0,0 +1,180 @@
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]) ?? []
}
}