对 AWS 的动作: 检测器模型
本单元中更高层次的构件的API是实验性的并正在积极开发中. 在今后任何版本中,它们都须作非背向相容的修改或去除。 这些是 不适用 语义版本 模型和断开的更改 在发行笔记中宣布. 这意味着虽然你可能使用它们,但可能需要更新 当更新到此软件包的更新版本时,您的源代码.
该库包含集成类,用于指定探测器模型在下列情况下的状态事件动作: @aws-cdk/aws-iotevents-alpha。 。 。 .
这些班级的情况应传至 State 定义 @aws-cdk/aws-iotevents-alpha
您可以定义使用定时器或设置变量的内置动作,或者将数据发送到其他 AWS 资源.
此库包含集成类,用于使用计时器或设置变量,或将数据发送到其他AWS资源. AWS IOT事件在检测出指定的事件或过渡事件时,可以触发动作.
目前得到支持的有:
- 使用计时器
- 设定用于检测的变量实例
- 启动 Lambda 函数
使用计时器
下面的代码片段创建一个动作,以秒为持续时间创建定时器.
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';
import * as actions from '@aws-cdk/aws-iotevents-actions-alpha';
declare const input: iotevents.IInput;
const state = new iotevents.State({
stateName: 'MyState',
onEnter: [{
eventName: 'test-event',
condition: iotevents.Expression.currentInput(input),
actions: [
new actions.SetTimerAction('MyTimer', {
duration: cdk.Duration.seconds(60),
}),
],
}],
});
确定持续时间 IOT 事件表达式数字 :
new actions.SetTimerAction('MyTimer', {
durationExpression: iotevents.Expression.inputAttribute(input, 'payload.durationSeconds'),
})
而计时器可以被重置并清除. 下面是一般的例子 设备心Beat 探测器模型 :
const online = new iotevents.State({
stateName: 'Online',
onEnter: [{
eventName: 'enter-event',
condition: iotevents.Expression.currentInput(input),
actions: [
new actions.SetTimerAction('MyTimer', {
duration: cdk.Duration.seconds(60),
}),
],
}],
onInput: [{
eventName: 'input-event',
condition: iotevents.Expression.currentInput(input),
actions: [
new actions.ResetTimerAction('MyTimer'),
],
}],
onExit: [{
eventName: 'exit-event',
actions: [
new actions.ClearTimerAction('MyTimer'),
],
}],
});
const offline = new iotevents.State({ stateName: 'Offline' });
online.transitionTo(offline, { when: iotevents.Expression.timeout('MyTimer') });
offline.transitionTo(online, { when: iotevents.Expression.currentInput(input) });
设定用于检测的变量实例
下面的代码片段创建一个动作, 设置变量到检测器实例 当它被触发.
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';
import * as actions from '@aws-cdk/aws-iotevents-actions-alpha';
declare const input: iotevents.IInput;
const state = new iotevents.State({
stateName: 'MyState',
onEnter: [{
eventName: 'test-event',
condition: iotevents.Expression.currentInput(input),
actions: [
new actions.SetVariableAction(
'MyVariable',
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
),
],
}],
});
启动 Lambda 函数
下面的代码片断创建了引用 Lambda 函数的动作 当它被触发.
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';
import * as actions from '@aws-cdk/aws-iotevents-actions-alpha';
import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const input: iotevents.IInput;
declare const func: lambda.IFunction;
const state = new iotevents.State({
stateName: 'MyState',
onEnter: [{
eventName: 'test-event',
condition: iotevents.Expression.currentInput(input),
actions: [new actions.LambdaInvokeAction(func)],
}],
});