* limitations under the License.
*/
-var https = require("https");
-var timers = require("timers");
-var fs = require("fs");
+var https = require('https');
+var timers = require('timers');
+var fs = require('fs');
var sendTemp = 0;
var fileName = 'BatchData.txt';
function sendData(temperature, timeMillis) {
-
- var data = JSON.stringify({ data: { temp: temperature, onFire: false },
- sdid: '170e5221612b4bc38dce53fd4395174a',
- ts: timeMillis,
- type: 'message' });
+ var data = JSON.stringify(
+ {
+ data: {
+ temp: temperature,
+ onFire: false,
+ },
+ sdid: '170e5221612b4bc38dce53fd4395174a',
+ ts: timeMillis,
+ type: 'message',
+ });
var options = {
- "method": "POST",
- "hostname": "api.artik.cloud",
- "path": "/v1.1/messages",
- "headers": {
- "content-type": "application/json",
- "content-length": data.length,
- "authorization": "Bearer 1718113118564ad495ad03f04116f379"
- }
+ 'method': 'POST',
+ 'hostname': 'api.artik.cloud',
+ 'path': '/v1.1/messages',
+ 'headers': {
+ 'content-type': 'application/json',
+ 'content-length': data.length,
+ 'authorization': 'Bearer 1718113118564ad495ad03f04116f379',
+ },
};
- var req = https.request(options, function (res) {
+ var req = https.request(options, function(res) {
var resBody = '';
- res.on("data", function (chunk) {
+ res.on('data', function(chunk) {
resBody += chunk.toString();
});
- res.on("end", function () {
- console.log('Request Ended. Server sent - '+ resBody);
+ res.on('end', function() {
+ console.log('Request Ended. Server sent - ' + resBody);
});
});
function writeData() {
sendTemp = sendTemp + 10;
var timeStampMillis = Math.floor(new Date().valueOf());
- var data = new Buffer(sendTemp+','+timeStampMillis+'\n')
- console.log('Writing to file : '+ data);
+ var data = new Buffer(sendTemp + ',' + timeStampMillis + '\n');
+ console.log('Writing to file : ' + data);
var fd = fs.openSync(fileName, 'a');
fs.writeSync(fd, data, 0, data.length);
}
function readData() {
- console.log('Reading data from file.')
+ console.log('Reading data from file.');
var fileContents = fs.readFileSync(fileName);
fs.unlinkSync(fileName);
var array = fileContents.toString().split('\n');
- for (i in array) {
+ for (var i = 0; i < array.length; i++) {
var parts = array[i].split(',');
var temp = parts[0];
var timestamp = parts[1];
- if(temp) {
- console.log('Currently sending temperature to cloud - '+temp
- +' and timestamp as - '+ timestamp);
+ if (temp) {
+ console.log('Currently sending temperature to cloud - ' + temp
+ + ' and timestamp as - ' + timestamp);
sendData(temp, timestamp);
}
}
* limitations under the License.
*/
-var https = require("https");
-var timers = require("timers");
+var https = require('https');
+var timers = require('timers');
function sendData(temperature) {
-
- var data = JSON.stringify({ data: { temp: temperature, onFire: false },
- sdid: '170e5221612b4bc38dce53fd4395174a',
- type: 'message' });
+ var data = JSON.stringify(
+ {
+ data: {
+ temp: temperature,
+ onFire: false,
+ },
+ sdid: '170e5221612b4bc38dce53fd4395174a',
+ type: 'message',
+ });
var options = {
- "method": "POST",
- "hostname": "api.artik.cloud",
- "path": "/v1.1/messages",
- "headers": {
- "content-type": "application/json",
- "content-length": data.length,
- "authorization": "Bearer 1718113118564ad495ad03f04116f379"
- }
+ 'method': 'POST',
+ 'hostname': 'api.artik.cloud',
+ 'path': '/v1.1/messages',
+ 'headers': {
+ 'content-type': 'application/json',
+ 'content-length': data.length,
+ 'authorization': 'Bearer 1718113118564ad495ad03f04116f379',
+ },
};
- var req = https.request(options, function (res) {
+ var req = https.request(options, function(res) {
var resBody = '';
- res.on("data", function (chunk) {
+ res.on('data', function(chunk) {
console.log('Received response from server');
resBody += chunk.toString();
console.log(resBody);
});
- res.on("end", function () {
+ res.on('end', function() {
console.log('Request Ended. Final data sent by the server - ');
console.log(resBody);
console.log('Waiting for next Interval\n');
function generateData() {
sendtemp = sendtemp + 10;
- console.log('Sending Temperature to Artik Cloud : '+ sendtemp);
+ console.log('Sending Temperature to Artik Cloud : ' + sendtemp);
sendData(sendtemp);
}