Upload
amazon-web-services
View
73
Download
1
Embed Size (px)
Citation preview
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
David Yanacek, Principal Engineer, AWS IoT
October 24, 2016
Programming the Physical
World with AWS IoT Device
Shadows and Rules Engine
Outline
• Shadows
• Rules
• Putting it all together
AWS IoT
AWS IoT
Shadows
1. Device Publishes Current State
2. Persist JSON Data Store
3. App requests device’s current state
4. App requests change the state5. Device Shadow sync’s
updated state
6. Device Publishes Current State 7. Device Shadow confirms state change
AWS IoT Device Shadow Flow
AWS IoT Device Shadow - Simple Yet Powerful
{
"state" : {
“desired" : {
"lights": { "color": "RED" },
"engine" : "ON"
},
"reported" : {
"lights" : { "color": "GREEN" },
"engine" : "ON"
},
"delta" : {
"lights" : { "color": "RED" }
} },
"version" : 10
}
Device
Report its current state to one or multiple shadows
Retrieve its desired state from shadow
Mobile App
Set the desired state of a device
Get the last reported state of the device
Delete the shadow
Shadow
Shadow reports delta, desired and reported
states along with metadata and version
Quick demo
Setting desired state (MQTT)
// Update the thermostat to 90 degrees
thingShadows.update('TemperatureControl', {
state: {
desired: {
setPoint: 90
}
}
});
Code (AWS JS Device SDK)
Setting desired state (MQTT)
PUBLISH $aws/things/TemperatureControl/shadow/update{
"state": {"desired": {
"setPoint": 90}
}}
Wire format (MQTT)
Getting reported state (HTTP)
// Get the last reported internal temperature
iot.getThingShadow({
thingName: 'TemperatureStatus'
}, function(err, data) {
if (err) { console.log(err); return; }
else {
var state = JSON.parse(data.payload).state.reported;
console.log('Internal temp: ' + state.intTemp);
}
});
Code (AWS JS SDK)
Getting reported state (HTTP)
GET /things/TemperatureStatus/shadow
HTTP/1.1 200 OK
{
"state": {
"desired": {
"setPoint": 90
}
}
}
Wire format
Rules engine
Extracting the value from messages
• Filter messages with certain criteria
• Move messages to other topics
• Move messages to other systems
• Transform the payload of messages
• Predict messages based on trends
• React based on messages
But how?
instancedatabase
?
But how?
Highly available?
Scalable?
Easy to operate?
Easy to change?
Easy to implement?instance
database
Rules Engine
AWS IoT rules engine
predict,
republish
Amazon Machine
Learning
index
archive,
analyze
Amazon
DynamoDB
AWS
Lambda
Amazon
Redshift
process
AWS IoT - SQL Reference
SELECT DATA FROM TOPIC WHERE FILTER
• Like scanning a database table
• Default source is an MQTT topic
EXAMPLES:
• FROM mqtt(‘my/topic’)
• FROM mqtt(‘my/wildcard/+/topic’)
• FROM (‘my/topic’)
Rules Engine
• Familiar SQL syntax
• SELECT * FROM topic WHERE filter
• Functions
• String manipulation (regex support)
• Mathematical operations
• Context based helper functions
• Crypto support
• UUID, timestamp, rand, etc.
• Execute Simultaneous Actions
Quick example
SELECT reading, timestamp() as timeFROM example-topic/inputWHERE reading > 0
Republish to example-topic/output
Quick example
Publish a message
Receive the re-published,
transformed message
new: Elasticsearch Integration
new: Predict Function
Putting it all together
AWS IoT Shape Up! architecture
Amazon
Cognito
Amazon
S3Amazon
DynamoDB
IoT
ruleIoT
policy
IoT
topic
AWS
Lambda
IoT
shadow
Amazon
DynamoDB
IoT
rule
IoT
topic
Amazon
Cognito
Amazon
S3AWS
Lambda
IoT
policy
IoT
shadow
AWS IoT Shape Up! architecture
Sign-in and
registration
IoT
policy
Amazon
Cognito
Amazon
S3
AWS IoT Shape Up! architecture
Amazon
DynamoDB
IoT
rule
IoT
topic
AWS
Lambda
IoT
shadow
Match making,
gameplay
Shape Up! shadows
• Game
• Configuration, winner
• Grid
• Current shape in each cell
• Player
• Name, currently assigned game, grid
Shape Up! topics
• shape-up/lobby
• shape-up/join/${CognitoId}/match/${MatchId}
• shape-up/move/${CognitoId}
• $aws/things/${CognitoId}/shadow/update/documents
• $aws/things/${GridId}/shadow/update/documents
ShapeUp! deep-dive
• Matchmaking
• Gameplay
• Finding the winner
Shape Up! matchmaking
Display PlayersCONNECT
Shape Up! matchmaking
lifecycle
event
Display PlayersCONNECT
$aws/events/presence/connected/${COGNITO_ID}
Shape Up! matchmaking
IoT
rule lifecycle
event
Display PlayersCONNECT
player
connected
topic
$aws/events/presence/connected/${COGNITO_ID}
shape-up/display/connected/${COGNITO_ID}
Shape Up! matchmaking
shape-up/lobby
Display PlayersSUBSCRIBE
Shape Up! matchmaking
shape-up/lobby
Display Players
PUBLISH
{"GameId": "abc123"
}
Shape Up! matchmaking
shape-up/join/${CognitoId}
Display PlayersPUBLISH
{"GameId": "abc123"
}
Shape Up! matchmaking
shape-up/join/${CognitoId}
Display PlayersPUBLISH
IoT
ruleAmazon
DynamoDBAWS
Lambda
{"GameId": "abc123"
}
Shape Up! matchmaking
Display Players
Amazon
DynamoDBAWS
Lambda
Shape Up! matchmaking
Display Players
Amazon
DynamoDBAWS
Lambda
{"GameId": "abc123","GridId": "g1","Row": 1,“Col": 1
}
player A shadow player B shadow
player C shadow
grid 1 shadow
grid 2 shadow
Shape Up! matchmaking
Display Players
grid 1 shadow
grid 2 shadow
SUBSCRIBE
Shape Up! gameplay
Display Players
shape-up/move/${CognitoId}
PUBLISH{
"Shape": 2,"Row": 1,"Col": 3
}
Shape Up! gameplay
Display Players
player C shadow
shape-up/move/${CognitoId}
PUBLISH
IoT
rule
AWS
Lambda
{"Shape": 2
}
{"Row": 1,"Col": 3
}
Shape Up! gameplay
Display Players
player C shadow
shape-up/move/${CognitoId}
PUBLISH
IoT
rule
AWS
Lambda
{"Shape": 2
}
{"Row": 1,"Col": 3
}grid 1 shadow
IoT
rule
shape-up/display/move
Shape Up! gameplay
Display Players
AWS
Lambda
grid 1 shadow
{"state": {
"reported": {"Grid": {
"1_3": 2}
}}
}
(Incremental update)
Shape Up! gameplay
Display Players
grid 1 shadow
"reported": {"Grid": {
"1_3": 2,"2_1": 1,"3_2": 4,...
}}
$aws/things/${GridId}/shadow/update/documents
(Complete state)
Shape Up! gameplay
Display Players
grid 1 shadow
IoT
rule
shape-up/display/move
"reported": {"Grid": {
"1_3": 2,"2_1": 1,"3_2": 4,...
}}
$aws/things/${GridId}/shadow/update/documents
(Complete state)
Shape Up! gameplay
Display Players
grid 1 shadow
"reported": {"Grid": {
"1_3": 2,"2_1": 1,"3_2": 4,...
}},"version": 6
$aws/things/${GridId}/shadow/update/documents
(Complete state)
// Don’t go backwards!var oldVersion = 5;
Shape Up! finding the winner
Display Players
shape-up/move/${CognitoId}
PUBLISH{
"Shape": }
Shape Up! finding the winner
Display Players
"reported": {"Grid": {
"1_3": "2_1": "3_2": ...
}}
$aws/.../documents
(Complete state)
Shape Up! finding the winner
Display Players
"reported": {"Grid": {
"1_3": "2_1": "3_2": ...
}}
$aws/.../documents
(Complete state)
IoT
rule
AWS
Lambda
Shape Up! finding the winner
Display Players
(Complete state)
game 123 shadow
IoT
rule
AWS
Lambda
"reported": {"Winner": null
},"version": 1
GET
Shape Up! finding the winner
Display Players
(Complete state)
game 123 shadow
IoT
rule
AWS
Lambda
"reported": {"Winner": "Team1"
},"version": 2
UPDATE
IF v==1
Shape Up! finding the winner
Display Players
game 123 shadow
Takeaways: Rules
• Use rules to store huge data volumes
• Rule actions integrate with big data storage services
• Use rules as triggers to drive your application
• Transform data moving from or to devices
• Supplement data with other data, predictions
Takeaways: Shadows
• Devices report state
• Applications set desired state
• Call APIs for devices, even when they’re offline
• Sync state with PubSub
• Consume deltas or complete state
• Use version numbers
Takeaways: Lifecycle
• Trigger actions when devices connect or disconnect
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
All attendees will receive a special giveaway gift!
Please join us for the
AWS DevDay Networking Reception
5:00 - 6:30 PM
JW Grand Foyer
Thank You!
Don’t Forget Evaluations!