Expressions
AWS IoT Events 提供了多种在创建和更新检测器模型时指定值的方法。您可以使用表达式来指定文字值,或者 AWS IoT Events 可以在指定特定值之前计算表达式。
Syntax
您可以在 AWS IoT Events 表达式。
Literals
-
整数
-
小数
-
String
-
布尔型
Operators
- 一元
-
-
非(布尔):
!
-
不是(逐位):
~
-
减(算术):
-
-
- String
-
-
连接:
+
两个操作数都必须是字符串。字符串文字必须以单引号(')括起来。
例如:
'my' + 'string'
->和/或'mystring'
-
- 算术
-
-
添加:
+
两个操作数必须为数字。
-
减法:
-
-
分部:
/
除法结果为舍入整数值,除非至少一个运算数(除法或分红)是小数值。
-
乘法:
*
-
- Bitwise(整数)
-
-
或:
|
例如:
13 | 5
->和/或13
-
和:
&
例如:
13 & 5
->和/或5
-
XOR(异或):
^
例如:
13 ^ 5
->和/或8
-
不是:
~
例如:
~13
->和/或-14
-
- 布尔型
-
-
小于:
<
-
小于或等于:
<=
-
等于:
==
-
不等于:
!=
-
大于或等于:
>=
-
大于:
>
-
和:
&&
-
或:
||
注意 当
||
包含未定义的数据,该子表达式被视为false
.
-
- 圆括号
-
您可以使用圆括号对表达式中的术语进行分组。
Functions
- 内置函数
-
timeout("
timer-name
")-
评估至
true
如果指定的计时器已过。替换“计时器名称
",使用您定义的计时器的名称,引号。在事件操作中,您可以定义计时器,然后启动计时器、重置计时器或清除之前定义的计时器。查看字段detectorModelDefinition.states.onInput|onEnter|onExit.events.actions.setTimer.timerName
.一个状态下设置的计时器可以在不同状态下被引用。您必须访问创建计时器的状态,才能进入引用计时器的状态。
例如,检测器模型具有两种状态:TemperatureChecked和RecordUpdated。您在TemperatureChecked状态下创建了计时器。您必须先访问TemperatureChecked状态,然后才能使用RecordUpdated状态中的计时器。
为确保准确性,应设置计时器的最短时间为60秒。
注意 timeout()
退货true
仅在实际计时器过期后第一次检查并返回false
之后。 convert(
type
,expression
)-
计算转换为指定类型的表达式的值。这些区域有:
type
值必须String
,Boolean
,或Decimal
。使用这些关键字之一或计算包含关键字的字符串的表达式。只有下列转换成功并返回有效值:-
布尔->字符串
返回字符串
"true"
或"false"
. -
十进制->字符串
-
字符串->布尔
-
字符串->小数
指定的字符串必须是小数的有效表示形式,或
convert()
失败。
如果
convert()
不返回有效值,它是的一部分的表达式也无效。这个结果相当于false
不会触发actions
或过渡到nextState
指定为表达式发生的事件的一部分。 -
isNull(
expression
)-
评估至
true
如果表达式返回null。例如,如果输入MyInput
收到消息{ "a": null }
,然后以下评估true
,但isUndefined($input.MyInput.a)
评估到false
.isNull($input.MyInput.a)
isUndefined(
expression
)-
评估至
true
如果表达式未定义。例如,如果输入MyInput
收到消息{ "a": null }
,然后以下评估false
,但isNull($input.MyInput.a)
评估到true
.isUndefined($input.MyInput.a)
triggerType("
type
")-
这些区域有:
type
价值可以是"Message"
或"Timer"
。评估至true
正在评估其出现的事件条件,因为计时器已过期,如以下示例所示。triggerType("Timer")
或收到输入消息。
triggerType("Message")
currentInput("
input
")-
评估至
true
正在评估其出现的事件条件,因为收到了指定的输入消息。例如,如果输入Command
收到消息{ "value": "Abort" }
,然后以下评估以true
.currentInput("Command")
使用此函数验证是否正在评估条件,因为已经接收到特定输入且计时器尚未过期,如下表达式所示。
currentInput("Command") && $input.Command.value == "Abort"
- 字符串匹配函数
-
startsWith(
expression1
,expression2
)-
评估至
true
所述第一字符串表达式以所述第二字符串表达式开头。例如,如果输入MyInput
收到消息{ "status": "offline"}
,然后以下评估true
.startsWith($input.MyInput.status, "off")
Both expressions must evaluate to a string value. If either expression does not evaluate to a string value, then the result of the function is undefined. No conversions are performed.
endsWith(
expression1
,expression2
)-
评估至
true
所述第一字符串表达式以所述第二字符串表达式结束。例如,如果输入MyInput
收到消息{ "status": "offline" }
,然后以下评估true
.endsWith($input.MyInput.status, "line")
Both expressions must evaluate to a string value. If either expression does not evaluate to a string value, then the result of the function is undefined. No conversions are performed.
contains(
expression1
,expression2
)-
评估至
true
所述第一字符串表达式包含所述第二字符串表达式。例如,如果输入MyInput
收到消息{ "status": "offline" }
,然后以下评估true
.contains($input.MyInput.value, "fli")
Both expressions must evaluate to a string value. If either expression does not evaluate to a string value, then the result of the function is undefined. No conversions are performed.
- BitwiseInteger操作函数
-
bitor(
expression1
,expression2
)-
计算整数表达式的比特OR(对整数的相应比特执行二进制OR运算)。例如,如果输入
MyInput
收到消息{ "value1": 13, "value2": 5 }
,然后以下评估以13
.bitor($input.MyInput.value1, $input.MyInput.value2)
Both expressions must evaluate to an integer value. If either expression does not evaluate to an integer value, then the result of the function is undefined. No conversions are performed.
bitand(
expression1
,expression2
)-
求取整数表达式的bitwiseAND(对整数的相应bits进行二进制AND运算) 例如,如果输入
MyInput
收到消息{ "value1": 13, "value2": 5 }
,然后以下评估5
.bitand($input.MyInput.value1, $input.MyInput.value2)
Both expressions must evaluate to an integer value. If either expression does not evaluate to an integer value, then the result of the function is undefined. No conversions are performed.
bitxor(
expression1
,expression2
)-
计算整数表达式的bitwiseXOR(对整数的相应bits进行二进制XOR操作) 例如,如果输入
MyInput
收到消息{ "value1": 13, "value2": 5 }
,然后以下评估8
.bitxor($input.MyInput.value1, $input.MyInput.value2)
Both expressions must evaluate to an integer value. If either expression does not evaluate to an integer value, then the result of the function is undefined. No conversions are performed.
bitnot(
expression
)-
计算整数表达式的逐位NOT(对整数的比特执行二进制NOT运算)。例如,如果输入
MyInput
收到消息{ "value": 13 }
,然后以下评估-14
.bitnot($input.MyInput.value)
Both expressions must evaluate to an integer value. If either expression does not evaluate to an integer value, then the result of the function is undefined. No conversions are performed.
References
- Inputs
-
$input.
input-name
.path-to-data
input-name
是使用 创建输入 行动。例如,如果您有一个名为
TemperatureInput
定义inputDefinition.attributes.jsonPath
条目,值可能出现在以下可用字段中。{ "temperature": 78.5, "date": 2018-10-03T16:09:09Z }
要引用的值
temperature
字段中,请使用以下命令。$input.TemperatureInput.temperature
对于值是数组的字段,您可以使用
[
。例如,给定以下值:n
]{ "temperatures": [ 78.4, 77.9, 78.8 ], "date": 2018-10-03T16:09:09Z }
价值
78.8
可以使用以下命令引用。$input.TemperatureInput.temperatures[2]
- Variables
-
$variable.
variable-name
的
是使用 创建检测器模型 行动。variable-name
例如,如果您有一个名为的变量
TechnicianID
所定义detectorDefinition.states.onInputEvents.actions.setVariable.variableName
,您可以使用以下命令引用最近提供给变量的(string)值。$variable.TechnicianID
您只能使用
setVariable
行动。您无法为表达式中的变量分配值。无法取消设置变量。例如,您不能为其分配值null
.
在引用中使用不遵循(正则表达式)模式的标识符 [a-zA-Z][a-zA-Z0-9_]*
,您必须将这些标识符包含在反选中(`
)。例如,引用名为 MyInput
有一个名为 _value
必须指定此字段为 $input.MyInput.`_value`
.
在表达式中使用引用时,请检查以下内容:
-
在将引用用作具有一个或多个运算符的操作数时,请确保您引用的所有数据类型均兼容。
例如,在以下表达式中,整数
2
是==
和&&
运算符的操作数。要确保操作数兼容,$variable.testVariable + 1
和$variable.testVariable
必须引用整数或小数。此外,整数
1
是+
运算符的操作数。因此,$variable.testVariable
必须引用整数或小数。‘$variable.testVariable + 1 == 2 && $variable.testVariable’
-
在将引用用作传递给函数的参数时,请确保该函数支持您引用的数据类型。
例如,以下
timeout("
函数要求使用双引号作为参数的字符串。如果您对time-name
")timer-name
值时,您必须引用带双引号的字符串。timeout("
timer-name
")注意 对于
convert(
函数,如果您对 使用参考type
,expression
)type
值,则引用的计算结果必须为String
、Decimal
或Boolean
。
AWS IoT Events 表达式支持整数、小数、字符串和布尔数据类型。下表提供了类型不兼容对的列表。
不兼容的类型对 |
---|
整数,字符串 |
整数,布尔 |
十进制,字符串 |
十进制,布尔 |
字符串,布尔 |
替换模板
-
'${
expression
}'的
${}
将字符串标识为内插字符串。的expression
可以是任何 AWS IoT Events 表达式。这包括运算符、函数和引用。例如,您使用了 设置变量操作 操作以定义变量。的
variableName
是SensorID
和value
是10
。您可以创建以下替换模板。替代模板 结果字符串 '${'Sensor ' + $variable.SensorID}'
"Sensor 10"
'Sensor ' + '${$variable.SensorID + 1}'
"Sensor 11"
'Sensor 10: ${$variable.SensorID == 10}'
"Sensor 10: true"
'{\"sensor\":\"${$variable.SensorID + 1}\"}'
"{\"sensor"\:\"11\"}"
'{\"sensor\":${$variable.SensorID + 1}}'
"{\"sensor\":11}"