Real-Time IoT Device Monitoring with Kinesis Data Analytics
Real-Time IoT Device Monitoring with Kinesis Data Analytics

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Appendix A: Code Components

The Real-Time IoT Device Monitoring with Kinesis Data Analytics solution uses four main code components to process and display metrics on the real-time dashboard. The Amazon Kinesis Data Analytics application (KinesisAnalyticsApp) runs SQL queries against the in-application streams and emits the results. An AWS Lambda function (UpdateDDBLambda) sends processed data to an Amazon DynamoDB table to be stored. A JavaScript file (dash.js) populates the chart with the results of the queries, and an HTML file (index.html) renders the chart on the dashboard in real-time.

SQL Query

This SQL query calculates the maximum data point (temperature) per connected device in one-minute intervals. The result is stored in an output in-application stream (FAN_OUT_STREAM) with the name of the metric (PerDeviceMaxTemp) and the corresponding values.

CREATE OR REPLACE PUMP per_device_max_pump AS INSERT INTO FAN_OUT_STREAM SELECT STREAM STEP(source_sql_stream_001."COL_time" BY INTERVAL '1' MINUTE) AS eventTimeStamp, 'PerDeviceMaxTemp', "device", 0, 'Maximum', MAX("temp") AS max_value FROM source_sql_stream_001 GROUP BY "device", STEP(source_sql_stream_001.rowtime BY INTERVAL '1' MINUTE), STEP(source_sql_stream_001."COL_time" BY INTERVAL '1' MINUTE);

Lambda

The UpdateDDBLambda function sends the processed PerDeviceMaxTemp data from the Kinesis data analytics application in real-time to a DynamoDB table to be stored.

type_operator_map = { 'ConnectedDevicesCount' : max, 'PerDeviceMaxTemp' : max, 'PerDeviceMinTemp': min, 'PerDeviceAvgTemp': avg, 'DeviceTempAnomalyScore': max, 'AvgTempValue': avg, 'MinTempValue': min, 'MaxTempValue': max, 'MaxDisconnTime': max, 'MinDisconnTime': min, 'AvgDisconnTime': avg, 'MaxConnTime': max, 'MinConnTime': min, 'AvgConnTime': avg }

JavaScript

The JavaScript (in the dash.js file) populates the chart with the maximum data point (temperature) per connected device.

var PerDeviceMaxTempParams = retrieveParams("PerDeviceMaxTemp", maxTempPerDeviceQueryTime); docClient.query(PerDeviceMaxTempParams, function(err, data) { if (err) console.log(err); else { maxTempPerDeviceQueryTime = updateHorizontalBarChart(data, 20, maxTempPerDeviceChart, maxTempPerDeviceQueryTime, splitFunc); } });

HTML Element

The HTML element (in the index.html file) renders the maximum temperature per device chart with the results of the SQL query.

<div class="row aws-mb-l">
 <div class="col-md-5 col-md-offset-1 col-xs-12">
 <div class="x_title"> <h3>Min Temperature per Device</h3>
 </div> <div class="x_content"> <canvas id="minTempCanvas"></canvas> </div> </div> <div class="col-md-5 col-xs-12"> <div class="x_title"> <h3>Max Temperature per Device</h3> </div> <div class="x_content"> <canvas id="maxTempCanvas"></canvas>
 </div> </div> </div>