相机启用后的特征

   在 iOS 版示例应用中实现   

设备调试完成后,可以使用以下 trait:

OtaSoftwareUpdateRequestorTrait

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

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

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)
  • 订阅设备 trait:它会订阅设备的 trait,特别是查找 OtaSoftwareUpdateRequestorTrait 以监听 OTA 更新信息。

  • 设置超时:设置 30 秒的超时。如果在此期间未收到来自特征的更新,则订阅结束。

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

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

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

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

ConfigurationDoneTrait

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

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