在 Android 设备上控制设备

检查某个特征是否支持某个命令

还可以检查特征命令的支持情况。您还可以使用特征级 supports 函数来检查特定设备是否支持某个命令。

例如,如需检查设备是否支持“开/关”特征的 toggle 命令,请执行以下操作:

// Check if the OnOff trait supports the toggle command.
if (onOffTrait.supports(OnOff.Command.Toggle)) {
  println("onOffTrait supports toggle command")
} else {
  println("onOffTrait does not support stateful toggle command")
}

向设备发送命令

发送命令类似于从特征读取状态属性。如需开启或关闭设备,请使用 OnOff 特征的 Toggle 命令,该命令在 Google Home 生态系统数据模型中定义为 toggle()。如果此方法为 true,则将其更改为 false;如果此方法为 false,则将其更改为 trueonOff

// Calling a command on a trait.
try {
  onOffTrait.toggle()
} catch (e: HomeException) {
  // Code for handling the exception
}

所有特征命令都是 suspend 函数,只有在 API 返回响应(例如确认设备状态已更改)时才会完成。如果检测到执行流程存在问题,命令可能会返回异常。作为开发者,您应使用 try-catch 块来正确处理这些异常,并在错误可操作的情况下向用户显示详细信息。未处理的异常会停止应用运行时,并可能导致应用崩溃。

或者,使用 off()on() 命令明确设置状态:

onOffTrait.off()
onOffTrait.on()

发送更改状态的命令后,一旦完成,您就可以按照读取设备状态中所述读取状态,以便在应用中处理该状态。或者,您也可以使用观测状态中所述的 flow,这是首选方法。

发送带参数的命令

某些命令可能会使用参数,例如 OnOffLevelControl 特征中的参数:

offWithEffect

// Turn off the light using the DyingLight effect.
onOffTrait.offWithEffect(
  effectIdentifier = OnOffTrait.EffectIdentifierEnum.DyingLight,
  effectVariant = 0u,
)

moveToLevel

// Change the brightness of the light to 50%
levelControlTrait.moveToLevel(
  level = 127u.toUByte(),
  transitionTime = null,
  optionsMask = LevelControlTrait.OptionsBitmap(),
  optionsOverride = LevelControlTrait.OptionsBitmap(),
)

有些命令具有可选实参,这些实参位于必需实参之后。

例如,FanControl 特征的 step 命令有两个可选实参:

val fanControlTraitFlow: Flow<FanControl?> =
  device.type(FanDevice).map { it.standardTraits.fanControl }.distinctUntilChanged()

val fanControl = fanControlTraitFlow.firstOrNull()

// Calling a command with optional parameters not set.
fanControl?.step(direction = FanControlTrait.StepDirectionEnum.Increase)

// Calling a command with optional parameters.
fanControl?.step(direction = FanControlTrait.StepDirectionEnum.Increase) { wrap = true }

检查某个特征是否支持某个属性

某些设备可能支持 Matter 特征,但不支持特定属性。例如,映射到 MatterCloud-to-cloud 设备可能不支持所有 Matter 属性。为了处理此类情况,请使用特性级 supports 函数和特性的 Attribute 枚举来检查特定设备是否支持该属性。

例如,如需检查设备是否支持 On/Off 特征的 onOff 属性,请执行以下操作:

// Check if the OnOff trait supports the onOff attribute.
if (onOffTrait.supports(OnOff.Attribute.onOff)) {
  println("onOffTrait supports onOff state")
} else {
  println("onOffTrait is for a command only device!")
}

某些属性在 Matter 规范或 Cloud-to-cloud smart home 架构中可为 null。对于这些属性,您可以结合使用 supportsisNullable 来确定属性返回的 null 是因为设备未报告相应值,还是属性的值实际上为 null

// Check if a nullable attribute is set or is not supported.
if (onOffTrait.supports(OnOff.Attribute.startUpOnOff)) {
  // The device supports startupOnOff, it is safe to expect this value in the trait.
  if (OnOff.Attribute.startUpOnOff.isNullable && onOffTrait.startUpOnOff == null) {
    // This value is nullable and set to null. Check the specification as to
    // what null in this case means
    println("onOffTrait supports startUpOnOff and it is null")
  } else {
    // This value is nullable and set to a value.
    println("onOffTrait supports startUpOnOff and it is set to ${onOffTrait.startUpOnOff}")
  }
} else {
  println("onOffTrait does not support startUpOnOff!")
}

更新特征属性

如果您想更改给定属性的值,但没有任何特征命令可以实现此目的,则该属性可能支持显式设置其值。

能否更改属性的值取决于以下两个因素:

  • 属性是否可写入?
  • 属性的值是否会因发送特征命令而发生变化?

如需了解此信息,请参阅有关特质及其属性的参考文档。

因此,决定属性值可能如何更改的属性组合如下:

  • 只读,不受其他命令的影响。这意味着相应属性的值不会发生变化。例如,Switch 特征的 currentPosition 属性

  • 只读,受其他命令影响。这意味着,属性值的唯一更改方式是发送命令。例如,LevelControl Matter 特征的 currentLevel 属性是只读的,但其值可以通过 moveToLevel 等命令进行更改。

  • 可写入,且不受其他命令的影响。这意味着,您可以使用该特征的 update 函数直接更改属性的值,但没有任何命令会影响该属性的值。例如,DoorLock 特征的 WrongCodeEntryLimit 属性

  • 可写入,并受其他命令的影响。这意味着,您可以使用该特征的 update 函数直接更改属性的值,并且属性的值可能会因发送命令而发生变化。例如,FanControlTraitspeedSetting 属性可以直接写入,但也可以使用 step 命令进行更改。

使用更新函数更改属性值的示例

此示例展示了如何显式设置 DoorLockTrait.WrongCodeEntryLimit 属性的值。

如需设置属性值,请调用特性的 update 函数,并向其传递一个用于设置新值的 mutator 函数。 最好先验证该特征是否支持某个属性

例如:

    val doorLockDevice = home.devices().list().first { device -> device.has(DoorLock) }

    val traitFlow: Flow<DoorLock?> =
      doorLockDevice.type(DoorLockDevice).map { it.standardTraits.doorLock }.distinctUntilChanged()

    val doorLockTrait: DoorLock = traitFlow.first()!!

    if (doorLockTrait.supports(DoorLock.Attribute.wrongCodeEntryLimit)) {
      val unused = doorLockTrait.update { setWrongCodeEntryLimit(3u) }
    }

一次发送多条指令

借助批处理 API,客户端可以在单个载荷中发送多个 Home API 设备命令。这些命令会批量处理到单个载荷中并并行执行,类似于使用并行节点构建 Home API 自动化(例如在日出前打开百叶窗)的方式。不过,与 Automation API 相比,Batching API 支持更复杂、更精细的行为,例如能够根据任何条件在运行时动态选择设备。

一个批处理中的命令可以定位多个结构中的多个房间内的多个设备上的多个特征。

批量发送命令可让设备同时执行操作,而如果通过单独的请求按顺序发送命令,则无法实现这一点。通过批处理命令实现的行为可让开发者将一组设备的状态设置为与预定的汇总状态相匹配。

使用 Batching API

通过 Batching API 调用命令涉及三个基本步骤:

  1. 调用 Home.sendBatchedCommands() 方法。
  2. sendBatchedCommands() 代码块的正文中,指定要包含在批处理中的命令。
  3. 检查已发送命令的结果,看看它们是成功还是失败。

调用 sendBatchedCommands() 方法

调用 Home.sendBatchedCommands() 方法。在后台,此方法会在特殊的批处理上下文中设置 lambda 表达式。

home.sendBatchedCommands() {

指定批处理命令

sendBatchedCommands() 代码块的正文中,填充 可批处理的命令。可批处理的命令是现有设备 API 命令的“影子”版本,可在批处理上下文中使用,并且在命名时添加了后缀 Batchable。例如,LevelControl 特征的 moveToLevel() 命令有一个名为 moveToLevelBatchable() 的对应命令。

示例:

  val response1 = add(command1)

  val response2 = add(command2)

当所有命令都已添加到批处理上下文且执行已离开该上下文时,系统会自动发送批处理。

响应会捕获到 DeferredResponse<T> 对象中。

DeferredResponse<T> 实例可以收集到任何类型的对象中,例如 Collection 或您定义的数据类。无论您选择哪种类型的对象来组装响应,sendBatchedCommands() 都会返回该类型的对象。例如,批处理上下文可以在 Pair 中返回两个 DeferredResponse 实例:

  val (response1, response2) = homeClient.sendBatchedComamnds {
    val response1 = add(someCommandBatched(...))
    val response2 = add(someOtherCommandBatched(...))
    Pair(response1, response2)
  }

或者,批处理上下文可以在自定义数据类中返回 DeferredResponse 实例:

  // Custom data class
  data class SpecialResponseHolder(
    val response1: DeferredResponse<String>,
    val response2: DeferredResponse<Int>,
    val other: OtherResponses
  )
  data class OtherResponses(...)

检查每个回答

sendBatchedCommands() 代码块之外,检查响应以确定相应命令是成功还是失败。为此,请调用 DeferredResponse.getOrThrow(),该方法会执行以下操作之一: - 返回已执行命令的结果 - 或者,如果批处理范围尚未完成或命令未成功执行,则抛出错误。

您应该只检查 sendBatchedCommands() lambda 范围之外的结果。

示例

假设您想构建一个应用,该应用使用 Batching API 设置“晚安”场景,以便在所有家庭成员入睡后将家中的所有设备配置为夜间模式。此应用应关闭灯并锁定前门和后门。

以下是完成此任务的一种方法:

val lightDevices: List<OnOffLightDevice>
val doorlockDevices: List<DoorLockDevice>

// Send all the commands
val responses: List<DeferredResponse<Unit>> = home.sendBatchedCommands {
  // For each light device, send a Batchable command to turn it on
  val lightResponses: List<DeferredResponse<Unit>> = lightDevices.map { lightDevice ->
    add(lightDevice.standardTraits.onOff.onBatchable())
  }

  // For each doorlock device, send a Batchable command to lock it
  val doorLockResponse: List<DeferredResponse<Unit>> = doorlockDevices.map { doorlockDevice ->
    add(doorlockDevice.standardTraits.doorLock.lockDoorBatchable())
  }

  lightResponses + doorLockResponses
}

// Check that all responses were successful
for (response in responses) {
  response.getOrThrow()
}