diff --git a/readme.md b/readme.md index c9596c5..360b071 100644 --- a/readme.md +++ b/readme.md @@ -26,8 +26,7 @@ In case the sensor is a PM sensor the subsequent structure is returned: type: 'PM' location: { longitude: 9.228, - latitude: 48.804, - altitude: 234.1 + latitude: 48.804 }, PM10: 6.4, PM2_5: 5.9, @@ -43,11 +42,22 @@ In case the sensor is a temperature (celsius) sensor the subsequent structure is type: 'temperature', location: { longitude: 9.228, - latitude: 48.804, - altitude: 234.1 + latitude: 48.804 }, temperature: 1.9, - humidity: 85.7 } + humidity: 85.7, timestamp: '2018-02-04 14:38:08' } ``` + +There is also a method returning a 24h average value. The output format remains the same as above. + +``` +var sensor = require("air-sensor"); + +var sensorId = 9322; +sensor.lookup24hAvg( sensorId ).then( + data => console.log( data ) +); +``` + diff --git a/src/index.js b/src/index.js index 1184c76..0102120 100644 --- a/src/index.js +++ b/src/index.js @@ -4,58 +4,62 @@ const _ = require("lodash"); "use strict"; -const fetchSensorData = (sensorID) => { +const order = (data) => _.orderBy(data, "timestamp", "asc"); - const order = (data) => _.orderBy(data, "timestamp", "asc"); +const filter = (sensorID) => (data) => _.filter(data, (item) => item.sensor.id === sensorID); - const last = (data, expr) => _.last(jpath({json: data, path: expr})); +const extract = (data, expr) => _.last(jpath({json: data, path: expr})); - const mapData = function (data) { +const mapData = (sensorID) => (data) => { - if (data.length === 0) { - return {}; - } else { + if (data.length === 0) { + return {}; + } else { + + const timestamp = extract(data, "$..timestamp"); + const pm10 = extract(data, "$..sensordatavalues[?(@.value_type=='P1')]/.value"); + const pm2_5 = extract(data, "$..sensordatavalues[?(@.value_type=='P2')]/.value"); + const temperature = extract(data, "$..sensordatavalues[?(@.value_type=='temperature')]/.value"); + const humidity = extract(data, "$..sensordatavalues[?(@.value_type=='humidity')]/.value"); + + const answer = { + id: sensorID, + location: { + longitude: parseFloat(extract(data, "$..location.longitude")), + latitude: parseFloat(extract(data, "$..location.latitude")) + }, + timestamp: timestamp + }; + + if (pm10 !== undefined) { + answer.type = "PM"; + answer.PM10 = parseFloat(pm10); + answer.PM2_5 = parseFloat(pm2_5); - const timestamp = last(data, "$..timestamp"); - const pm10 = last(data, "$..sensordatavalues[?(@.value_type=='P1')]/.value"); - const pm2_5 = last(data, "$..sensordatavalues[?(@.value_type=='P2')]/.value"); - const temperature = last(data, "$..sensordatavalues[?(@.value_type=='temperature')]/.value"); - const humidity = last(data, "$..sensordatavalues[?(@.value_type=='humidity')]/.value"); - - const answer = { - id: sensorID, - location: { - longitude: parseFloat(last(data, "$..location.longitude")), - latitude: parseFloat(last(data, "$..location.latitude")), - altitude: parseFloat(last(data, "$..location.altitude")), - country: last(data, "$..location.country") - }, - timestamp: timestamp - }; - - if (pm10 !== undefined) { - answer.type = "PM"; - answer.PM10 = parseFloat(pm10); - answer.PM2_5 = parseFloat(pm2_5); - - } else if (temperature !== undefined) { - answer.type = "temperature"; - answer.temperature = parseFloat(temperature); - answer.humidity = parseFloat(humidity); - } else { - answer.type = "void"; - } - - return answer; + } else if (temperature !== undefined) { + answer.type = "temperature"; + answer.temperature = parseFloat(temperature); + answer.humidity = parseFloat(humidity); + } else { + answer.type = "void"; } - }; - return fetch("http://api.luftdaten.info/v1/sensor/" + sensorID + "/") - .then((res) => res.json()) - .then(order) - .then(mapData); + return answer; + } }; +const fetchSensorData = (sensorID) => fetch("http://api.luftdaten.info/v1/sensor/" + sensorID + "/") + .then((res) => res.json()) + .then(order) + .then(mapData(sensorID)); + +const fetchSensorDataAvg = (sensorID) => fetch("http://api.luftdaten.info/static/v2/data.24h.json ") + .then((res) => res.json()) + .then(filter(sensorID)) + .then(order) + .then(mapData(sensorID)); + module.exports = { - lookup: fetchSensorData + lookup: fetchSensorData, + lookup24hAvg: fetchSensorDataAvg };