Skip to content

Commit

Permalink
feat(Added support for area based lookup of sensor values.):
Browse files Browse the repository at this point in the history
  • Loading branch information
aschuma committed Feb 6, 2018
1 parent c04633e commit 56d01d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ sensor.lookup24hAvg( sensorId ).then(
);
```

In addition its also possible to fetch all current sensor data of an area. This will return an array of objects having the same structure as above.

```
var sensor = require("air-sensor");
var longitude = 49.1355;
var latitude = 9.228;
var distance = 0.5;
sensor.lookupArea(longitude,latitude,distance).
data => console.log( data )
);
```
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const _ = require("lodash");

const order = (data) => _.orderBy(data, "timestamp", "asc");

const reverse = (data) => _.reverse(data);

const filter = (sensorID) => (data) => _.filter(data, (item) => item.sensor.id === sensorID);

const extract = (data, expr) => _.last(jpath({json: data, path: expr}));
Expand All @@ -16,14 +18,15 @@ const mapData = (sensorID) => (data) => {
return {};
} else {

const id = extract(data, "$..sensor.id");
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,
id: sensorID || id,
location: {
longitude: parseFloat(extract(data, "$..location.longitude")),
latitude: parseFloat(extract(data, "$..location.latitude"))
Expand Down Expand Up @@ -59,7 +62,16 @@ const fetchSensorDataAvg = (sensorID) => fetch("http://api.luftdaten.info/static
.then(order)
.then(mapData(sensorID));

const fetchSensorDataOfArea = (longitude, latitude, distance) => fetch("http://api.luftdaten.info/v1/filter/area=" + longitude + "," + latitude + "," + 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()));

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

0 comments on commit 56d01d3

Please sign in to comment.