Android DSL 运算符参考文档

借助运算符,您可以根据特定值检查特征属性的值、将它们相互比较,以及组合 condition 节点中使用的表达式。

运算符通过以下 import 语句提供:

import com.google.home.automation.and
import com.google.home.automation.between
import com.google.home.automation.contains
import com.google.home.automation.equals
import com.google.home.automation.greaterThan
import com.google.home.automation.greaterThanOrEquals
import com.google.home.automation.lessThan
import com.google.home.automation.not
import com.google.home.automation.notEquals
import com.google.home.automation.or

比较运算符

介于下列二者之间:

当表达式 1 的值介于表达式 2 和表达式 3 的值之间(含边界值)时,计算结果为 true。表达式的排名因其数据类型而异。数字和字符串等简单数据类型的排名方式与在 Kotlin 中相同。

示例
表达式 1 表达式 2 表达式 3 结果
6 1 3 false
2 1 3 true

DSL 示例

val time = stateReader<_>(structure, Time)
condition() {
  expression = time.currentTime
     .between(
      time.sunsetTime,
      time.sunriseTime)
}

使用带有 between() 的日期

使用 between() 运算符时,您可以指定日期范围:

val exp2 =
  time.currentDate.between(
    LocalDate.of(2025, Month.OCTOBER, 1),
    LocalDate.of(2025, Month.DECEMBER, 15),
  )

不过,您不一定非要指定完整日期。自动化 DSL 还允许您以多种方式表达日期范围:

  • 仅使用年份和月份:
val exp2 =
  time.currentDate.yearMonth.between(
    YearMonth.of(2024, Month.OCTOBER),
    YearMonth.of(2026, Month.JANUARY),
  )
  • 仅使用月份和日期:
val exp2 =
  time.currentDate.monthDay.between(
    MonthDay.of(Month.OCTOBER, 1),
    MonthDay.of(Month.DECEMBER, 15),
  )
  • 仅使用日期:
val exp2 = time.currentDate.day.between(1, 15)

等于

如果表达式 1 等于表达式 2,则求值为 true

示例
表达式 1 表达式 2 结果
6 1 false
2 2 true

DSL 示例

washer.operationalState equals STOPPED

greaterThan

当表达式 1 大于表达式 2 时,计算结果为 true

示例
表达式 1 表达式 2 结果
6 1 true
1 6 false

DSL 示例

( blindsPosition.currentPositionLift greaterThan 0u )

greaterThanOrEquals

当表达式 1 大于或等于表达式 2 时,计算结果为 true

示例
表达式 1 表达式 2 结果
8 6 true
6 6 true
1 6 false

DSL 示例

( starterNode.measuredValue greaterThanOrEquals 50 )

lessThan

当表达式 1 小于表达式 2 时,求值为 true

示例
表达式 1 表达式 2 结果
6 1 false
1 6 true

DSL 示例

time.currentTime lessThan LocalTime.of(22,0,0,0)

lessThanOrEquals

当表达式 1 小于或等于表达式 2 时,计算结果为 true

示例
表达式 1 表达式 2 结果
8 6 false
6 6 true
1 6 true

DSL 示例

( starterNode.measuredValue lessThanOrEquals 25 )

notEquals

如果表达式 1 不等于表达式 2,则求值为 true

示例
表达式 1 表达式 2 结果
6 1 true
1 6 true
2 2 false

DSL 示例

occupancyStateChange.occupied notEquals 0

算术运算符

添加

加法运算符 ( +)。

DSL 示例

var totalCount = 0
...
totalCount = totalCount + 1

减去

减法运算符 ( -)。

DSL 示例

var countdown = 10
...
countdown = countdown - 1

乘号

乘法运算符 ( *)。

DSL 示例

val millis = seconds * 1000

除法运算符 ( /)。

DSL 示例

val rpm = revolutions / minutes

逻辑运算符

将两个表达式组合成一个逻辑 AND 表达式,当两个表达式均为 true 时,该表达式的计算结果为 true

示例
表达式 1 表达式 2 结果
false false false
true false false
false true false
true true true

DSL 示例

((device.occupied notEquals 0) and
   time.currentTime.between(time.sunriseTime, time.sunsetTime))

对表达式的逻辑值求反。

示例
表达式 结果
true false
false true

DSL 示例

time.currentTime not (between(time.sunriseTime, time.sunsetTime))

将两个表达式合并为一个逻辑 OR 表达式。

示例
表达式 1 表达式 2 结果
false false false
true false true
false true true

DSL 示例

(time.currentTime equals LocalTime.of(10,0,0,0)) or
  (time.currentTime equals LocalTime.of(22,0,0,0))