Skip to content

Commit

Permalink
feat(Added 1h average lookup, changed the order of latitude and longi…
Browse files Browse the repository at this point in the history
…tude of fetchSensorDataOfArea f
  • Loading branch information
aschuma committed Feb 7, 2018
1 parent 41cf176 commit 8592c83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ In addition its also possible to fetch all current sensor data of an area. This

```
var sensor = require("air-sensor");
var latitude = 49.1355;
var longitude = 9.228;
var distance = 1.1;
var longitude = 49.1355;
var latitude = 9.228;
var distance = 0.5;
sensor.lookupArea(longitude,latitude,distance).
sensor.lookupArea(latitude,longitude,distance).then(
data => console.log( data )
);
```
31 changes: 20 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const filter = (sensorID) => (data) => _.filter(data, (item) => item.sensor.id =

const extract = (data, expr) => _.last(jpath({json: data, path: expr}));

const mapData = (sensorID) => (data) => {
const mapData = (data) => {

if (data.length === 0) {
return {};
Expand All @@ -27,10 +27,10 @@ const mapData = (sensorID) => (data) => {
const humidity = extract(data, "$..sensordatavalues[?(@.value_type=='humidity')]/.value");

const answer = {
id: sensorID || id,
id: id,
location: {
longitude: parseFloat(extract(data, "$..location.longitude")),
latitude: parseFloat(extract(data, "$..location.latitude"))
latitude: parseFloat(extract(data, "$..location.latitude")),
longitude: parseFloat(extract(data, "$..location.longitude"))
},
timestamp: timestamp
};
Expand All @@ -55,24 +55,33 @@ const mapData = (sensorID) => (data) => {
const fetchSensorData = (sensorID) => fetch("http://api.luftdaten.info/v1/sensor/" + sensorID + "/")
.then((res) => res.json())
.then(order)
.then(mapData(sensorID));
.then(_.last)
.then(mapData);

const fetchSensorDataAvg = (sensorID) => fetch("http://api.luftdaten.info/static/v2/data.24h.json ")
const fetchSensorData1hAvg = (sensorID) => fetch("http://api.luftdaten.info/static/v2/data.1h.json ")
.then((res) => res.json())
.then(filter(sensorID))
.then(order)
.then(mapData(sensorID));
.then(_.last)
.then(mapData);

const fetchSensorDataOfArea = (longitude, latitude, distance) => fetch("http://api.luftdaten.info/v1/filter/area=" + longitude + "," + latitude + "," + distance)
const fetchSensorData24hAvg = (sensorID) => fetch("http://api.luftdaten.info/static/v2/data.24h.json ")
.then((res) => res.json())
.then(filter(sensorID))
.then(order)
.then(_.last)
.then(mapData);

const fetchSensorDataOfArea = (latitude, longitude, distance) => fetch("http://api.luftdaten.info/v1/filter/area=" + latitude + "," + longitude + "," + distance)
.then((res) => res.json())
.then(order)
.then(reverse)
.then((data) => _.uniqBy(data, (it) => it.sensor.id))
.then((data) => _.map(data, (value) => [value]))
.then((data) => _.map(data, mapData()));
.then((data) => _.map(data, mapData));

module.exports = {
lookup: fetchSensorData,
lookupArea: fetchSensorDataOfArea,
lookup24hAvg: fetchSensorDataAvg
lookup1hAvg: fetchSensorData1hAvg,
lookup24hAvg: fetchSensorData24hAvg
};

0 comments on commit 8592c83

Please sign in to comment.