Skip to content

Instantly share code, notes, and snippets.

@jezinka

jezinka/app.js Secret

Created February 10, 2019 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jezinka/04196a710f7067d9fee1077682ad548a to your computer and use it in GitHub Desktop.
Save jezinka/04196a710f7067d9fee1077682ad548a to your computer and use it in GitHub Desktop.
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