-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(iotevents): support timer actions #19949
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import { Construct } from 'constructs'; | ||
|
||
/** | ||
* The action to delete an existing timer. | ||
*/ | ||
export class ClearTimerAction implements iotevents.IAction { | ||
/** | ||
* @param timerName the name of the timer | ||
*/ | ||
constructor(private readonly timerName: string) {} | ||
|
||
bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig { | ||
return { | ||
configuration: { | ||
clearTimer: { | ||
timerName: this.timerName, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './clear-timer-action'; | ||
export * from './set-variable-action'; | ||
export * from './lambda-invoke-action'; | ||
export * from './reset-timer-action'; | ||
export * from './set-timer-action'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import { Construct } from 'constructs'; | ||
|
||
/** | ||
* The action to reset an existing timer. | ||
*/ | ||
export class ResetTimerAction implements iotevents.IAction { | ||
/** | ||
* @param timerName the name of the timer | ||
*/ | ||
constructor(private readonly timerName: string) {} | ||
|
||
bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig { | ||
return { | ||
configuration: { | ||
resetTimer: { | ||
timerName: this.timerName, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||||||
import * as cdk from '@aws-cdk/core'; | ||||||
import { Construct } from 'constructs'; | ||||||
|
||||||
/** | ||||||
* Configuration properties of an action to set a timer. | ||||||
*/ | ||||||
export interface SetTimerActionProps { | ||||||
/** | ||||||
* The duration of the timer, in seconds. One of `duration` or `durationExpression` is required. | ||||||
* | ||||||
* The range of the duration is 60-31622400 seconds. | ||||||
* The evaluated result of the duration expression is rounded down to the nearest whole number. | ||||||
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds. | ||||||
* | ||||||
* @default - none, required if no `durationExpression` is defined. | ||||||
*/ | ||||||
readonly duration?: cdk.Duration; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of mutually exclusive props. I think this should go in the direction of one required prop that gets translated properly into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! 🤩 I will implement as it. Thank you! |
||||||
|
||||||
/** | ||||||
* The duration of the timer, in seconds. One of `duration` or `durationExpression` is required. | ||||||
* | ||||||
* You can use a string expression that includes numbers, variables ($variable.<variable-name>), | ||||||
* and input values ($input.<input-name>.<path-to-datum>) as the duration. | ||||||
* | ||||||
* The range of the duration is 60-31622400 seconds. | ||||||
* The evaluated result of the duration expression is rounded down to the nearest whole number. | ||||||
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds. | ||||||
* | ||||||
* @default - none, required if no `duration` is defined. | ||||||
*/ | ||||||
readonly durationExpression?: iotevents.Expression; | ||||||
} | ||||||
|
||||||
/** | ||||||
* The action to create a timer with duration in seconds. | ||||||
*/ | ||||||
export class SetTimerAction implements iotevents.IAction { | ||||||
private readonly durationExpression: string | undefined; | ||||||
|
||||||
/** | ||||||
* @param timerName the name of the timer | ||||||
* @param props the properties to set duration | ||||||
*/ | ||||||
constructor(private readonly timerName: string, props: SetTimerActionProps) { | ||||||
if (!props.duration && !props.durationExpression) { | ||||||
throw new Error('Either duration or durationExpression must be specified'); | ||||||
} | ||||||
if (props.duration && props.durationExpression) { | ||||||
throw new Error('duration and durationExpression cannot be specified at the same time'); | ||||||
} | ||||||
if (props.duration) { | ||||||
const seconds = props.duration.toSeconds(); | ||||||
if (seconds < 60) { | ||||||
throw new Error(`duration cannot be less than 60 seconds, got: ${props.duration.toString()}`); | ||||||
} | ||||||
if (seconds > 31622400) { | ||||||
throw new Error(`duration cannot be greater than 31622400 seconds, got: ${props.duration.toString()}`); | ||||||
} | ||||||
this.durationExpression = seconds.toString(); | ||||||
} | ||||||
if (props.durationExpression) { | ||||||
this.durationExpression = props.durationExpression.evaluate(); | ||||||
} | ||||||
} | ||||||
|
||||||
bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be better off as it is I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm annoyed with that but it's not in scope here to ask you to fix an old contract I'm mad at. Maybe I'll get neurotic about it and do some excessive refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've understood that refactoring is necessary even in light of the current implementations. |
||||||
return { | ||||||
configuration: { | ||||||
setTimer: { | ||||||
timerName: this.timerName, | ||||||
durationExpression: this.durationExpression, | ||||||
}, | ||||||
}, | ||||||
}; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Template } from '@aws-cdk/assertions'; | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as actions from '../../lib'; | ||
|
||
let stack: cdk.Stack; | ||
let input: iotevents.IInput; | ||
beforeEach(() => { | ||
stack = new cdk.Stack(); | ||
input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input'); | ||
}); | ||
|
||
test('Default property', () => { | ||
// WHEN | ||
new iotevents.DetectorModel(stack, 'MyDetectorModel', { | ||
initialState: new iotevents.State({ | ||
stateName: 'test-state', | ||
onEnter: [{ | ||
eventName: 'test-eventName', | ||
condition: iotevents.Expression.currentInput(input), | ||
actions: [ | ||
new actions.ClearTimerAction('MyTimer'), | ||
], | ||
}], | ||
}), | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { | ||
DetectorModelDefinition: { | ||
States: [{ | ||
OnEnter: { | ||
Events: [{ | ||
Actions: [{ | ||
ClearTimer: { | ||
TimerName: 'MyTimer', | ||
}, | ||
}], | ||
}], | ||
}, | ||
}], | ||
}, | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Stack verification steps: | ||
* * put a message | ||
* * aws iotevents-data batch-put-message --region=us-east-1 --messages=messageId=(date | md5),inputName=test_input,payload=(echo '{"payload":{"deviceId":"000"}}' | base64) | ||
*/ | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { IntegTest } from '@aws-cdk/integ-tests'; | ||
import * as actions from '../../lib'; | ||
|
||
/** | ||
* This example will creates the detector model for Device HeartBeat Monitoring. | ||
* | ||
* @see https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-examples-dhb.html | ||
*/ | ||
class TestStack extends cdk.Stack { | ||
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
const input = new iotevents.Input(this, 'MyInput', { | ||
inputName: 'test_input', | ||
attributeJsonPaths: ['payload.deviceId'], | ||
}); | ||
|
||
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) }); | ||
|
||
new iotevents.DetectorModel(this, 'MyDetectorModel', { | ||
detectorKey: 'payload.deviceId', | ||
initialState: online, | ||
}); | ||
} | ||
} | ||
|
||
// GIVEN | ||
const app = new cdk.App(); | ||
const stack = new TestStack(app, 'iotevents-timer-actions-test-stack'); | ||
new IntegTest(app, 'TimerActions', { testCases: [stack] }); | ||
app.synth(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Template } from '@aws-cdk/assertions'; | ||
import * as iotevents from '@aws-cdk/aws-iotevents'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as actions from '../../lib'; | ||
|
||
let stack: cdk.Stack; | ||
let input: iotevents.IInput; | ||
beforeEach(() => { | ||
stack = new cdk.Stack(); | ||
input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input'); | ||
}); | ||
|
||
test('Default property', () => { | ||
// WHEN | ||
new iotevents.DetectorModel(stack, 'MyDetectorModel', { | ||
initialState: new iotevents.State({ | ||
stateName: 'test-state', | ||
onEnter: [{ | ||
eventName: 'test-eventName', | ||
condition: iotevents.Expression.currentInput(input), | ||
actions: [ | ||
new actions.ResetTimerAction('MyTimer'), | ||
], | ||
}], | ||
}), | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { | ||
DetectorModelDefinition: { | ||
States: [{ | ||
OnEnter: { | ||
Events: [{ | ||
Actions: [{ | ||
ResetTimer: { | ||
TimerName: 'MyTimer', | ||
}, | ||
}], | ||
}], | ||
}, | ||
}], | ||
}, | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent README!