- DuooomiBleConfig.owner → brand(语义:设备品牌名),HTTP header x-owner 不变 - bind() 前置 verifyBrand:缺 deviceInfo 先 getDeviceInfo,与 config.brand 严格匹配,不一致主动断连并抛 .brandMismatch - README 抹除软绑/对账/UpgradeRecord 等内部实现细节,仅保留厂商面向的公开 API(功能代码完全保留) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
152 lines
7.0 KiB
Swift
152 lines
7.0 KiB
Swift
import Foundation
|
||
|
||
// MARK: - Binding (BLE 协议层硬绑/硬解 + 解绑端的软解;OTA 检查/对账/软绑搬到 +Firmware)
|
||
|
||
extension DuooomiBleSDK {
|
||
|
||
/// 硬绑:BLE 协议层把当前用户绑定到设备(命令 0x0F),完成后同步拉一次版本号。
|
||
///
|
||
/// **作用域**:只做 BLE 通道的绑定 + `getDeviceVersion`。
|
||
/// 软绑 / 升级对账上报 / 拉最新固件 → 全部移到 `fetchLatestFirmware(sn:currentVersion:userId:)` 内部串。
|
||
///
|
||
/// 完成后 SDK 状态变化:
|
||
/// - `sdk.sn` 写入设备序列号
|
||
/// - `sdk.version` 写入当前固件版本(getVersion 失败时保持空,但 bind 不算失败)
|
||
/// - `sdk.isActivated = true`,delegate `didChangeActivation(true)` 回调
|
||
///
|
||
/// - Parameters:
|
||
/// - userId: 当前用户 ID(写入 BLE BIND payload 的 `bindUserId` 字段,不依赖登录态 token)
|
||
/// - completion: 回调 `Result<BindingResponse, Error>`
|
||
/// - 成功:`BindingResponse(success: 1, sn, contents)`
|
||
/// - 失败:`.notConnected` / `.alreadyBoundByOtherUser`(success≠1) / `.timeout(.bindDevice)`
|
||
/// - Note: `bind` / `unbind` / `setPlayMode` 共享 `0x0F` / `0x12` opId 通道,**不能并发**。
|
||
public func bind(userId: String, completion: @escaping (Result<BindingResponse, Error>) -> Void) {
|
||
guard ensureConnected(completion: completion) else { return }
|
||
verifyBrand { [weak self] result in
|
||
guard let self = self else { return }
|
||
switch result {
|
||
case .failure(let err):
|
||
completion(.failure(err))
|
||
case .success:
|
||
self.performHardBind(userId: userId, completion: completion)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 校验设备 brand 与 SDK 配置 brand 是否一致;缺失则先拉一次 deviceInfo。
|
||
/// 不一致时主动断连并回调 `.brandMismatch`。
|
||
private func verifyBrand(completion: @escaping (Result<Void, Error>) -> Void) {
|
||
if let cached = deviceInfo {
|
||
checkBrand(actual: cached.brand, completion: completion)
|
||
return
|
||
}
|
||
getDeviceInfo { [weak self] result in
|
||
guard let self = self else { return }
|
||
switch result {
|
||
case .failure(let err):
|
||
completion(.failure(err))
|
||
case .success(let info):
|
||
self.checkBrand(actual: info.brand, completion: completion)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func checkBrand(actual: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||
let expected = config.brand
|
||
if actual == expected {
|
||
completion(.success(()))
|
||
return
|
||
}
|
||
BleLog.w("Brand mismatch: expected=\(expected), actual=\(actual); disconnecting", "Command")
|
||
disconnect { _ in
|
||
completion(.failure(DuooomiBleError.brandMismatch(expected: expected, actual: actual)))
|
||
}
|
||
}
|
||
|
||
private func performHardBind(userId: String, completion: @escaping (Result<BindingResponse, Error>) -> Void) {
|
||
BleLog.d("Sending bind (userId=\(userId))", "Command")
|
||
|
||
sendAsyncAndWait(commandType: .bindDevice, completion: { [weak self] result in
|
||
guard let self = self else { return }
|
||
switch result {
|
||
case .failure(let error):
|
||
completion(.failure(error))
|
||
case .success(let data):
|
||
do {
|
||
let resp = try self.decodeResponse(BindingResponse.self, from: data)
|
||
self.isActivated = resp.success == 1
|
||
if resp.success != 1 {
|
||
BleLog.w("Hard bind failed: success=0", "Command")
|
||
completion(.failure(DuooomiBleError.alreadyBoundByOtherUser))
|
||
return
|
||
}
|
||
self.sn = resp.sn
|
||
BleLog.i("Hard bind success: sn=\(resp.sn)", "Command")
|
||
// 同步拉版本,让后续 fetchLatestFirmware 的对账上报有 currentVersion 可用
|
||
self.getVersion { _ in
|
||
completion(.success(resp))
|
||
}
|
||
} catch {
|
||
completion(.failure(error))
|
||
}
|
||
}
|
||
}, send: { done in
|
||
self.deviceInfoService.bindDevice(userId: userId, completion: done)
|
||
})
|
||
}
|
||
|
||
/// 解绑:硬解绑(BLE 0x12)→ 软解绑(shipment-sn HTTP)。SDK 内部完成全套。
|
||
///
|
||
/// 软解失败仅打 warn 不阻断硬解结果回传——对端云记录如果残留,由后续 bind 自然修正。
|
||
///
|
||
/// 完成后 SDK 状态变化:
|
||
/// - `sdk.sn` 清空
|
||
/// - `sdk.isActivated = false`,delegate `didChangeActivation(false)` 回调
|
||
///
|
||
/// - Parameters:
|
||
/// - userId: 当前用户 ID(写入 BLE UNBIND payload + 软解请求体的 `bindUserId`)
|
||
/// - completion: 回调 `Result<UnbindResponse, Error>`
|
||
/// - 成功:`UnbindResponse(success: 1)`(软解失败也算成功)
|
||
/// - 失败:`.notConnected` / `.unbindFailed`(BLE 层 success≠1) / `.timeout(.unbindDevice)`
|
||
/// - Note: `bind` / `unbind` / `setPlayMode` 共享 `0x0F` / `0x12` opId 通道,**不能并发**。
|
||
public func unbind(userId: String, completion: @escaping (Result<UnbindResponse, Error>) -> Void) {
|
||
guard ensureConnected(completion: completion) else { return }
|
||
let cachedSn = self.sn
|
||
BleLog.d("Sending unbind (userId=\(userId))", "Command")
|
||
|
||
sendAsyncAndWait(commandType: .unbindDevice, completion: { [weak self] result in
|
||
guard let self = self else { return }
|
||
switch result {
|
||
case .failure(let error):
|
||
completion(.failure(error))
|
||
case .success(let data):
|
||
do {
|
||
let resp = try self.decodeResponse(UnbindResponse.self, from: data)
|
||
if resp.success == 1 {
|
||
self.isActivated = false
|
||
self.sn = ""
|
||
BleLog.i("Hard unbind success", "Command")
|
||
if !cachedSn.isEmpty {
|
||
self.shipmentService.unbind(sn: cachedSn, bindUserId: userId) { softResult in
|
||
if case .failure(let err) = softResult {
|
||
BleLog.w("Soft unbind failed (ignored): \(err.localizedDescription)", "Command")
|
||
}
|
||
completion(.success(resp))
|
||
}
|
||
} else {
|
||
completion(.success(resp))
|
||
}
|
||
} else {
|
||
BleLog.w("Unbind failed", "Command")
|
||
completion(.failure(DuooomiBleError.unbindFailed))
|
||
}
|
||
} catch {
|
||
completion(.failure(error))
|
||
}
|
||
}
|
||
}, send: { done in
|
||
self.deviceInfoService.unbindDevice(userId: userId, completion: done)
|
||
})
|
||
}
|
||
}
|