- 所有公开 API 改为 completion handler (Result<T, Error>) - 状态通知改为 DuooomiBleSDKDelegate 协议 - 移除 Sendable、@MainActor、AsyncStream、CheckedContinuation - 网络请求改为 URLSession.dataTask 回调 - BLE 通信改为 delegate + 闭包模式 - deployment target 降至 iOS 12.2 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
81 lines
2.0 KiB
Swift
81 lines
2.0 KiB
Swift
import Foundation
|
||
|
||
/// 设备命令服务:发送 JSON 命令(fire-and-forget,响应由协议层回调处理)
|
||
final class DeviceInfoService {
|
||
private let protocolService: BleProtocolService
|
||
|
||
init(protocolService: BleProtocolService) {
|
||
self.protocolService = protocolService
|
||
}
|
||
|
||
// MARK: - Commands
|
||
|
||
func getDeviceInfo() throws {
|
||
try protocolService.sendJSON(
|
||
type: .getDeviceInfo,
|
||
payload: CommandPayload(type: CommandType.getDeviceInfo.rawValue)
|
||
)
|
||
}
|
||
|
||
func getDeviceVersion() throws {
|
||
try protocolService.sendJSON(
|
||
type: .getDeviceVersion,
|
||
payload: CommandPayload(type: CommandType.getDeviceVersion.rawValue)
|
||
)
|
||
}
|
||
|
||
func bindDevice(userId: String) throws {
|
||
try protocolService.sendJSON(
|
||
type: .bindDevice,
|
||
payload: BindPayload(type: CommandType.bindDevice.rawValue, userId: userId)
|
||
)
|
||
}
|
||
|
||
func unbindDevice(userId: String) throws {
|
||
try protocolService.sendJSON(
|
||
type: .unbindDevice,
|
||
payload: BindPayload(type: CommandType.unbindDevice.rawValue, userId: userId)
|
||
)
|
||
}
|
||
|
||
func deleteFile(key: String) throws {
|
||
try protocolService.sendJSON(
|
||
type: .deleteFile,
|
||
payload: FileKeyPayload(type: CommandType.deleteFile.rawValue, key: key)
|
||
)
|
||
}
|
||
|
||
func prepareTransfer(key: String, size: Int) throws {
|
||
try protocolService.sendJSON(
|
||
type: .prepareTransfer,
|
||
payload: PrepareTransferPayload(
|
||
type: CommandType.prepareTransfer.rawValue,
|
||
key: key,
|
||
size: size
|
||
)
|
||
)
|
||
}
|
||
}
|
||
|
||
// MARK: - Command Payloads
|
||
|
||
private struct CommandPayload: Encodable {
|
||
let type: UInt8
|
||
}
|
||
|
||
private struct BindPayload: Encodable {
|
||
let type: UInt8
|
||
let userId: String
|
||
}
|
||
|
||
private struct FileKeyPayload: Encodable {
|
||
let type: UInt8
|
||
let key: String
|
||
}
|
||
|
||
private struct PrepareTransferPayload: Encodable {
|
||
let type: UInt8
|
||
let key: String
|
||
let size: Int
|
||
}
|