-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicas.js
161 lines (136 loc) · 4.64 KB
/
practicas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
AWS.config.update({
region: "us-east-1",
// The endpoint should point to the local or remote computer where DynamoDB (downloadable) is running.
endpoint: 'https://dynamodb.us-east-1.amazonaws.com ',
/*
accessKeyId and secretAccessKey defaults can be used while using the downloadable version of DynamoDB.
For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
*/
accessKeyId: "XXXXXXXXXXXXXXXXXXXXXXX",
secretAccessKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
});
var dynamodb = new AWS.DynamoDB();
var docClient = new AWS.DynamoDB.DocumentClient();
var datumVal = new Date() - 86400000;
function scanTime() {
document.getElementById('textarea').innerHTML = "";
document.getElementById('textarea').innerHTML += "Scanning for the sensor data during the last 24 hours." + "\n";
var params = {
TableName: "dashboard_table",
KeyConditionExpression: "#id = :iotTopic AND #ts >= :datum",
ExpressionAttributeNames: {
"#id": "id",
"#ts": "timestamp"
},
ExpressionAttributeValues: {
":iotTopic": "thing01/data",
":datum": datumVal
}
};
docClient.query(params, onScan);
function onScan(err, data) {
if (err) {
document.getElementById('textarea').innerHTML += "Unable to scan the table: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
// Print all the data sensor
document.getElementById('textarea').innerHTML += "Scan succeeded: " + "\n";
data.Items.forEach(function(sensor) {
document.getElementById('textarea').innerHTML += "\n" + sensor.payload.humidity + ": " + sensor.payload.temperature + ": " + sensor.payload.timestamp;
});
/*
// Continue scanning if we have more sensor data (per scan 1mb limitation)
document.getElementById('textarea').innerHTML += "Scanning for more..." + "\n";
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
*/
}
}
}
var params = {
TableName: "dashboard_table",
KeyConditionExpression: "#id = :iotTopic AND #ts >= :datum",
ExpressionAttributeNames: {
"#id": "id",
"#ts": "timestamp"
},
ExpressionAttributeValues: {
":iotTopic": "thing01/data",
":datum": datumVal
}
};
var tctx = $("#temperaturegraph").get(0).getContext("2d");
var hctx = $("#humiditygraph").get(0).getContext("2d");
var options = {
responsive: true,
showLines: true,
scales: {
xAxes: [{
display: true
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
};
var tinit = {
labels: [],
datasets: [
{
label: "Temperature °C",
backgroundColor: 'rgba(204,229,255,0.5)',
borderColor: "#3e95cd",
data: []
}
]
};
var hinit = {
labels: [],
datasets: [
{
label: "Humidity %",
backgroundColor: 'rgba(229,204,255,0.5)',
borderColor: "#3e95cd",
data: []
}
]
};
var temperaturegraph = new Chart.Line(tctx, {data: tinit, options: options});
var humiditygraph = new Chart.Line(hctx, {data: hinit, options: options});
$(function() {
getData();
$.ajaxSetup({ cache: false });
setInterval(getData, 500);
});
function getData() {
docClient.query(params, function(err, data) {
if (err) {
console.log(err);
return null;
} else {
var temperatureValues = [];
var humidityValues = [];
//var timeValues = [];
var labelValues = [];
var temperatureRead = 0.0;
var humidityRead = 0.0;
//timeRead = "";
for (var i in data['Items']) {
temperatureRead = parseFloat(data['Items'][i]['payload']['temperature']);
humidityRead = parseFloat(data['Items'][i]['payload']['humidity']);
//timeRead = new Date(data['Items'][i]['timestamp']);
timeRead = data['Items'][i]['payload']['timestamp'];
temperatureValues.push(temperatureRead);
humidityValues.push(humidityRead);
labelValues.push(timeRead);
}
}
temperaturegraph.data.labels = labelValues;
temperaturegraph.data.datasets[0].data = temperatureValues;
humiditygraph.data.labels = labelValues;
humiditygraph.data.datasets[0].data = humidityValues;
temperaturegraph.update();
humiditygraph.update();
});
}