Skip to content

Commit

Permalink
feat(Support for 24h average values.):
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Altitude value was removed from output. This value is not part of the original
average API.
  • Loading branch information
aschuma committed Feb 5, 2018
1 parent 6b08acb commit c04633e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 49 deletions.
20 changes: 15 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 )
);
```

92 changes: 48 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit c04633e

Please sign in to comment.