監控 Android 裝置的狀態

在 Android 的 Google Home API 中,您可以使用 Kotlin 中的流程,觀察 Google Home 中的狀態變更。您可以使用從 HomeObjectsFlow 介面繼承的任何 API,觀察 Home API 中結構體、房間、裝置中繼資料和裝置狀態的變更。方法是從資料流收集資料。

當集合中的任何項目新增、刪除或修改時,系統會傳回集合的最新快照。

開發人員可以將此快照與舊版副本進行比較,推斷出具體變更。Home API 中的每個父項物件類型提供的 id 欄位可用於此用途。

如何使用流程

以下是從 Home API 中的流程中收集資料的基本範例。在下列範例中,必須先建立 Home 的例項,才能存取主畫面的集合:

val context = LocalContext.current
val homeManager = Home.getClient(context)

追蹤結構體的變更

下列結構變更會觸發這項收集作業:

homeManager.structures().map { it.firstOrNull() }.collect {
  println("Structure ${it.id} updated to ${it}")
}

val structure =  homeManager.structures().list().firstOrNull()!!

追蹤特定裝置的變更

裝置發生下列變更時,系統就會觸發這項收集作業:

structure
  .devices()
  .map { devices -> device.filter { it.name == "Bedroom Lamp" }.single() }
  .collect {
    println("The bedroom lamp has changed!")
  }

val device = structure.devices().list().firstOrNull { it.name == "Bedroom Lamp" }!!

追蹤裝置上特定特徵的變更

使用裝置和 Google Home API 支援的任何特徵。如需完整清單,請參閱 Trait

device.trait(OnOff).collect {
  if (it != null) {
    println("Got new state update! ${it.onOff}")
  }
}

追蹤裝置上特定類型的變更

下列裝置類型變更會觸發這項收集作業:

使用 Google Home API 支援的任何 Matter 裝置類型。如需完整清單,請參閱 DeviceType

device.type(DimmableLightDevice).collect { type ->
    println("Got new state update! ${type.trait(LevelControl)?.currentLevel}")
}

追蹤結構體中房間的變更

以下對聊天室的變更會觸發這項收集作業:

如要追蹤裝置何時加入或離開房間,請使用 devices() 流程

structure.rooms().collect {
  println("Got a new updated set of rooms!")

  for (room in it) {
    println("Got room $room")
  }
}

訂閱事件

在 Google Home API 中,事件可用於偵測裝置狀態的變化。

如要訂閱特定裝置上的事件,請呼叫其中一個 HomeDeviceevents 函式,每個函式都會傳回 Flow<Event>

  1. events(event: EventFactory<T>) 會傳回特定類型事件的流程:

    val eventFlow = homeDevice.type(DoorLockDevice).first().events(DoorLock.DoorLockAlarmEvent)
    
  2. events(trait: TraitFactory<T>) 會傳回特徵傳送的所有事件流程:

    val eventflow = homeDevice.type(DoorLockDevice).first().events(DoorLock)
    
  3. events() 會傳回可用於物件的事件流程:

    val eventflow = homeDevice.type(DoorLockDevice).first().events()
    

如要使用資料流中的事件,請使用 flow.collect() 函式:

eventflow.collect { event ->
  if (event != null) {
    logger.atInfo().log("Received event %s", event)
    // do something
  }
}

訂閱實體關係事件

您可以監聽事件,這些事件會在每次新增、移除或更新實體 (例如結構體、房間、裝置或自動化動作) 時觸發。這些事件是 HomeObjectChangeEvent 的例項,並附帶已變更的實體 ID。

如要取得可產生所需事件的串流,請在對應的 Flow 方法產生的 HomeObjectsFlow<T> 上呼叫 stream() 方法:

表格:事件串流
實體 介面 流程方法
Structure HasStructures structures()
Room HasRooms rooms()
HomeDevice HasHomeDevices devices()
Automation HasAutomations automations()

舉例來說,您可以透過以下方式處理裝置變更:

val devicesStream = home.devices().stream()

// Collect and react to device changes.
devicesStream.collect { deviceEvent ->
  when (deviceEvent) {
    is HomeObjectAddedEvent ->
      println("New device now available with id: ${deviceEvent.id}")
    is HomeObjectUpdatedEvent -> println("Device ${deviceEvent.id} has been updated")
    is HomeObjectRemovedEvent -> println("Device is removed from your account")
    else -> {}
  }
}