攝影機上線後的特徵

   在 iOS 範例應用程式中導入   

裝置啟用後可使用的特徵如下:

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)
}