设备调试完成后,可以使用以下 trait:
OtaSoftwareUpdateRequestor trait
此 trait 提供了一个接口,用于监控摄像头设备上的 OTA 软件更新。调试完成后,摄像头会启动更新并向应用报告其状态。应用收到软件更新完成的状态后,摄像头即可开始直播。
以下示例代码和说明演示了如何使用该 trait。
withTimeout(OTA_TIMEOUT_MS) {
device.type(OtaRequestorDevice)
.mapNotNull { it?.trait(OtaSoftwareUpdateRequestor) }
.distinctUntilChanged()
.transformWhile { trait ->
emit(trait)
!isTerminalState(trait.updateState)
}
.collect { trait ->
updateState = trait.updateState
updateStateProgress = trait.updateStateProgress
}
}
超时:
withTimeout会强制规定监控流程的时长上限 (OTA_TIMEOUT_MS)。如果更新未在此时间内完成,则会超时。观察 trait 变化:
.distinctUntilChanged()运算符可确保链中的后续代码仅在 trait 的状态实际发生变化时做出反应,这比持续轮询更高效。处理直至完成:只要 OTA
updateState不是最终状态(例如成功或失败),.transformWhile运算符就会继续处理更新。收集状态:每次状态发生变化时,系统都会执行
.collect代码块,以使用 trait 中的最新值更新updateState和updateStateProgress变量。
如需了解详情,请参阅 OtaSoftwareUpdateRequestor 特性参考文档。
ConfigurationDone 特征
此 trait 不是激活摄像头所必需的,但它允许应用通过将 AppConfigurationComplete 属性设置为 true 来跟踪设备何时完全设置好并可供使用。此 trait 应在 OtaSoftwareUpdateRequestor trait 之后使用。
device.trait(ConfigurationDone).first().update { setAppConfigurationComplete(true) }