-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-example.js
More file actions
72 lines (52 loc) · 2.12 KB
/
simple-example.js
File metadata and controls
72 lines (52 loc) · 2.12 KB
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
// *********************************************************
// RTView - Simple Example
// Configure the application to make use of the rtview-utils package
// and reference it using the variable name 'rtview'
var rtview = require('rtview-utils')();
// Configure package to send data to an RTView DataServer running on 'localhost:3270'
// and exposing an http servlet named 'rtvpost' (the default out-of-the-box dataserver)
//var url = 'http://localhost:3275';
var url = 'http://localhost:3270/rtvpost';
rtview.set_targeturl(url);
// Send data when number of buffered rows reaches 10
var size = 10; // default is 50
rtview.set_batchsize(size);
// Send data at an interval of five seconds
var interval = 5000; // default is 2000 ms
rtview.set_interval(interval);
// Create a cache named SensorData with the specified properties and structure
var sensorCacheName = "SensorData";
var sensorProperties = {
"indexColumnNames" : "ID",
"historyColumnNames" : "temperature;humidity",
"condenseRowsGroupBy": "temperature:average;humidity:average"
};
var sensorMetadata = [
{ "ID" : "string" },
{ "temperature" : "double" },
{ "humidity" : "double" },
{ "temp_unit" : "string"} // a column that is neither index nor history
];
rtview.create_datacache(sensorCacheName, sensorProperties, sensorMetadata);
var numberOfSensors = 5;
var temperatureSeed = 71;
var humiditySeed = 53;
processData();
async function processData() {
while (true) {
for (var i = 0; i < numberOfSensors; i++) {
var data = {};
data.ID = 'Sensor-' + (i+1).toString();
data.temperature = parseFloat(temperatureSeed + (Math.random() * 0.3)).toFixed(2);
data.humidity = parseFloat(humiditySeed + (Math.random() * 0.4)).toFixed(2);
data.temp_unit = '\xB0C'; // '°C'
console.log('\n... sending data: ' + JSON.stringify(data));
rtview.send_datatable(sensorCacheName, data);
}
console.log(); // Separate the output per interval
await sleep(10000); // create data points every 10 seconds
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}