我已经开始使用rxbluetoothkit,对我们正在开发的外围有良好的结果。它运行良好,我能够执行我们需要的交易。
我有一个设计模式问题。我们有几个命令,其中API包含多步命令。例如,该应用程序编写命令启动代码,外围设备确认ACK,然后该应用程序写入命令,等待ACK,发出另一个命令,等待另一个ACK等。命令停止代码将来会处于某个不确定点 - 例如,当用户告诉应用程序停止时。
RX世界中是否有适当的编码成语或模式来实现这一目标?我是RX的新手,很好奇这可能是最简单,最干净的实现。
谢谢。
详细信息一如既往地取决于您非常特定的用例。我喜欢将RX视为基于流的构建块,需要连接到建模您的业务逻辑。
有一个示例可能是:
enum DeviceService: String, ServiceIdentifier {
case myService = "ffff"
var uuid: CBUUID {
return CBUUID(string: self.rawValue)
}
}
enum DeviceCharacteristic: String, CharacteristicIdentifier {
case startCharacteristic = "0001"
case stopCharacteristic = "0002"
case ackCharacteristic = "ffff"
case command1Characteristic = "0003"
case command2Characteristic = "0004"
var uuid: CBUUID {
return CBUUID(string: self.rawValue)
}
var service: ServiceIdentifier {
return DeviceService.myService
}
}
let peripheral : Peripheral? = nil
// Some internal command 1
let command1 = peripheral!.writeValue(Data(bytes: [0xff, 0xfe]),
for: DeviceCharacteristic.command1Characteristic,
type: .withResponse)
// Some internal command 2
let command2 = peripheral!.writeValue(Data(bytes: [0xdd, 0xee]),
for: DeviceCharacteristic.command2Characteristic,
type: .withResponse)
func batchCommands(commands: [Observable<Characteristic>]) -> Observable<Characteristic> {
let commandsWithAck = commands.map { command in
return command.flatMap { characteristic in
return peripheral!.monitorValueUpdate(for: DeviceCharacteristic.ackCharacteristic).take(1)
}
}
let start = peripheral!.writeValue(Data(bytes: [0x01]),
for: DeviceCharacteristic.startCharacteristic,
type: .withResponse)
let stop = peripheral!.writeValue(Data(bytes: [0x00]),
for: DeviceCharacteristic.startCharacteristic,
type: .withResponse)
return start.concat(Observable.concat(commandsWithAck)).concat(stop)
}
// Call it:
let subscription = batchCommands(commands: [command1, command2])
.subscribe(onNext: nil, onError: nil, onCompleted: nil, onDisposed: nil)
在那里,可以更改start
和stop
可观察物,以监视用户的行为并在应进行实际开始/停止操作时发射项目。