Skip to content

Instantly share code, notes, and snippets.

@jezinka

jezinka/app.js Secret

Created Feb 10, 2019
Embed
What would you like to do?
const https = require("https");
const http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'application/json'});
const req = https.request('https://xxxxxxxxxxxxx.com/data.json', (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
const data = JSON.parse(chunk);
const sensors = [];
for (const id in data.readings) {
const sensor = {};
const sourceSensor = data.sensors[id];
sensor.label = sourceSensor.label;
const readings = data.readings[id];
for (const k of ["hum_temp", "stamp", "hum_hum", "lux", "bar_pres_rel", "vbat", "vreg", "id"]) {
if (k === 'stamp') {
sensor[k] = readings[k] * 1000
} else {
sensor[k] = readings[k]
}
}
sensors.push(sensor);
}
response.end(JSON.stringify(sensors));
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.end();
}).listen(8081);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment