相机启用后的特征

设备完成调试后可以使用的特征包括:

OtaSoftwareUpdateRequestorTrait

此特征提供了一个接口,用于监控摄像头设备上的 OTA 软件更新。完成调试后,摄像头会启动更新并向应用报告其状态。应用收到软件更新完成的状态后,摄像头即可开始直播。

以下示例代码和说明演示了如何使用该特征。

device.types.subscribe(OtaRequestorDeviceType.self)
  .receive(on: DispatchQueue.main)
  .compactMap { $0.traits[Matter.OtaSoftwareUpdateRequestorTrait.self] }
  .removeDuplicates()
  .timeout(.seconds(30), scheduler: DispatchQueue.main)
  .sink { [weak self] completion in
    guard let self else { return }
    Logger.debug("OTA trait subscription completed unexpectedly: \(completion).")
    if case .otaDownload = self.step {
      Logger.debug("Advancing to settings step due to OTA trait subscription completion")
      self.step = .settings
    }
  } receiveValue: { [weak self] otaTrait in
    Logger.debug(
      "OTA update is in progress, state: \(otaTrait.attributes.updateState), progress: \(otaTrait.attributes.updateStateProgress ?? 0)"
    )

    self.step = .otaDownload(
      state: otaTrait.attributes.updateState ?? .querying,
      progress: Double(otaTrait.attributes.updateStateProgress ?? 0) / 100.0)
    }
  }
  .store(in: &cancellables)
  • 订阅设备特征:它会订阅设备的特征,专门查找 OtaSoftwareUpdateRequestorTrait 以侦听 OTA 更新信息。

  • 设置超时时间:设置 30 秒的超时时间。如果未在相应时间内收到来自特征的更新,订阅就会结束。

  • 处理 OTA 状态更新 (receiveValue):当 OtaSoftwareUpdateRequestorTrait 提供新状态时,系统会执行此代码块。它会记录当前状态和 OTA 更新的进度。然后,它会更新应用的内部状态 (step) 以反映此新信息。

  • 处理完成 (sink):此代码块在订阅结束时执行,并记录完成情况。如果进程处于下载状态,应用状态会前进到下一步 (.settings)。

  • 管理订阅生命周期:.store(in: &cancellables) 行可确保在拥有订阅的对象被取消分配时,订阅得到妥善管理和取消。

如需了解详情,请参阅 OtaSoftwareUpdateRequestorTrait 特征参考文档

ConfigurationDoneTrait

此特征不是摄像头激活所必需的,但它允许应用通过将 AppConfigurationComplete 属性设置为 true 来跟踪设备何时完全设置完毕并可供使用。此特征应在 OtaSoftwareUpdateRequestorTrait 之后使用。

let configDoneTrait = try await device.types.get(OtaRequestorDeviceType.self)?.traits[Google.ConfigurationDoneTrait.self]
_ = try await configDoneTrait?.update {
  $0.setAppConfigurationComplete(true)
}