攝影機上線後的特徵

裝置委派後可使用的特徵包括:

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