62
© 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

Programming the Physical World with Device Shadows and Rules Engine

Embed Size (px)

Citation preview

Page 1: Programming the Physical World with Device Shadows and Rules Engine

© 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

Page 2: Programming the Physical World with Device Shadows and Rules Engine
Page 3: Programming the Physical World with Device Shadows and Rules Engine

Outline

• Shadows

• Rules

• Putting it all together

Page 4: Programming the Physical World with Device Shadows and Rules Engine

AWS IoT

Page 5: Programming the Physical World with Device Shadows and Rules Engine

AWS IoT

Page 6: Programming the Physical World with Device Shadows and Rules Engine

Shadows

Page 7: Programming the Physical World with Device Shadows and Rules Engine

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

Page 8: Programming the Physical World with Device Shadows and Rules Engine

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

Page 9: Programming the Physical World with Device Shadows and Rules Engine

Quick demo

Page 10: Programming the Physical World with Device Shadows and Rules Engine

Setting desired state (MQTT)

// Update the thermostat to 90 degrees

thingShadows.update('TemperatureControl', {

state: {

desired: {

setPoint: 90

}

}

});

Code (AWS JS Device SDK)

Page 11: Programming the Physical World with Device Shadows and Rules Engine

Setting desired state (MQTT)

PUBLISH $aws/things/TemperatureControl/shadow/update{

"state": {"desired": {

"setPoint": 90}

}}

Wire format (MQTT)

Page 12: Programming the Physical World with Device Shadows and Rules Engine

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)

Page 13: Programming the Physical World with Device Shadows and Rules Engine

Getting reported state (HTTP)

GET /things/TemperatureStatus/shadow

HTTP/1.1 200 OK

{

"state": {

"desired": {

"setPoint": 90

}

}

}

Wire format

Page 14: Programming the Physical World with Device Shadows and Rules Engine

Rules engine

Page 15: Programming the Physical World with Device Shadows and 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

Page 16: Programming the Physical World with Device Shadows and Rules Engine

But how?

instancedatabase

?

Page 17: Programming the Physical World with Device Shadows and Rules Engine

But how?

Highly available?

Scalable?

Easy to operate?

Easy to change?

Easy to implement?instance

database

Page 18: Programming the Physical World with Device Shadows and Rules Engine

Rules Engine

Page 19: Programming the Physical World with Device Shadows and Rules Engine

AWS IoT rules engine

predict,

republish

Amazon Machine

Learning

index

archive,

analyze

Amazon

DynamoDB

AWS

Lambda

Amazon

Redshift

process

Page 20: Programming the Physical World with Device Shadows and Rules Engine

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’)

Page 21: Programming the Physical World with Device Shadows and Rules Engine

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

Page 22: Programming the Physical World with Device Shadows and Rules Engine

Quick example

SELECT reading, timestamp() as timeFROM example-topic/inputWHERE reading > 0

Republish to example-topic/output

Page 23: Programming the Physical World with Device Shadows and Rules Engine

Quick example

Publish a message

Receive the re-published,

transformed message

Page 24: Programming the Physical World with Device Shadows and Rules Engine

new: Elasticsearch Integration

Page 25: Programming the Physical World with Device Shadows and Rules Engine

new: Predict Function

Page 26: Programming the Physical World with Device Shadows and Rules Engine

Putting it all together

Page 27: Programming the Physical World with Device Shadows and Rules Engine
Page 28: Programming the Physical World with Device Shadows and Rules Engine

AWS IoT Shape Up! architecture

Amazon

Cognito

Amazon

S3Amazon

DynamoDB

IoT

ruleIoT

policy

IoT

topic

AWS

Lambda

IoT

shadow

Page 29: Programming the Physical World with Device Shadows and Rules Engine

Amazon

DynamoDB

IoT

rule

IoT

topic

Amazon

Cognito

Amazon

S3AWS

Lambda

IoT

policy

IoT

shadow

AWS IoT Shape Up! architecture

Sign-in and

registration

Page 30: Programming the Physical World with Device Shadows and Rules Engine

IoT

policy

Amazon

Cognito

Amazon

S3

AWS IoT Shape Up! architecture

Amazon

DynamoDB

IoT

rule

IoT

topic

AWS

Lambda

IoT

shadow

Match making,

gameplay

Page 31: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! shadows

• Game

• Configuration, winner

• Grid

• Current shape in each cell

• Player

• Name, currently assigned game, grid

Page 32: Programming the Physical World with Device Shadows and Rules Engine

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

Page 33: Programming the Physical World with Device Shadows and Rules Engine

ShapeUp! deep-dive

• Matchmaking

• Gameplay

• Finding the winner

Page 34: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

Display PlayersCONNECT

Page 35: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

lifecycle

event

Display PlayersCONNECT

$aws/events/presence/connected/${COGNITO_ID}

Page 36: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

IoT

rule lifecycle

event

Display PlayersCONNECT

player

connected

topic

$aws/events/presence/connected/${COGNITO_ID}

shape-up/display/connected/${COGNITO_ID}

Page 37: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

shape-up/lobby

Display PlayersSUBSCRIBE

Page 38: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

shape-up/lobby

Display Players

PUBLISH

{"GameId": "abc123"

}

Page 39: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

shape-up/join/${CognitoId}

Display PlayersPUBLISH

{"GameId": "abc123"

}

Page 40: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

shape-up/join/${CognitoId}

Display PlayersPUBLISH

IoT

ruleAmazon

DynamoDBAWS

Lambda

{"GameId": "abc123"

}

Page 41: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

Display Players

Amazon

DynamoDBAWS

Lambda

Page 42: Programming the Physical World with Device Shadows and Rules Engine

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

Page 43: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! matchmaking

Display Players

grid 1 shadow

grid 2 shadow

SUBSCRIBE

Page 44: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! gameplay

Display Players

shape-up/move/${CognitoId}

PUBLISH{

"Shape": 2,"Row": 1,"Col": 3

}

Page 45: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! gameplay

Display Players

player C shadow

shape-up/move/${CognitoId}

PUBLISH

IoT

rule

AWS

Lambda

{"Shape": 2

}

{"Row": 1,"Col": 3

}

Page 46: Programming the Physical World with Device Shadows and Rules Engine

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

Page 47: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! gameplay

Display Players

AWS

Lambda

grid 1 shadow

{"state": {

"reported": {"Grid": {

"1_3": 2}

}}

}

(Incremental update)

Page 48: Programming the Physical World with Device Shadows and Rules Engine

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)

Page 49: Programming the Physical World with Device Shadows and Rules Engine

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)

Page 50: Programming the Physical World with Device Shadows and Rules Engine

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;

Page 51: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! finding the winner

Display Players

shape-up/move/${CognitoId}

PUBLISH{

"Shape": }

Page 52: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! finding the winner

Display Players

"reported": {"Grid": {

"1_3": "2_1": "3_2": ...

}}

$aws/.../documents

(Complete state)

Page 53: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! finding the winner

Display Players

"reported": {"Grid": {

"1_3": "2_1": "3_2": ...

}}

$aws/.../documents

(Complete state)

IoT

rule

AWS

Lambda

Page 54: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! finding the winner

Display Players

(Complete state)

game 123 shadow

IoT

rule

AWS

Lambda

"reported": {"Winner": null

},"version": 1

GET

Page 55: Programming the Physical World with Device Shadows and Rules Engine

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

Page 56: Programming the Physical World with Device Shadows and Rules Engine

Shape Up! finding the winner

Display Players

game 123 shadow

Page 57: Programming the Physical World with Device Shadows and Rules Engine

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

Page 58: Programming the Physical World with Device Shadows and Rules Engine

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

Page 59: Programming the Physical World with Device Shadows and Rules Engine

Takeaways: Lifecycle

• Trigger actions when devices connect or disconnect

Page 60: Programming the Physical World with Device Shadows and Rules Engine

© 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

Page 61: Programming the Physical World with Device Shadows and Rules Engine

Thank You!

Page 62: Programming the Physical World with Device Shadows and Rules Engine

Don’t Forget Evaluations!