Skip to content

Commit

Permalink
Use pm25 level instead of AQI (fix #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Aug 9, 2018
1 parent 20b6d2e commit c8a1c11
Showing 1 changed file with 71 additions and 8 deletions.
79 changes: 71 additions & 8 deletions App/utils/dataSources/aqicn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import axios from 'axios';
import { Constants } from 'expo';

/**
* Fetch the PM2.5 level from http:https://api.waqi.info.
*/
const aqicn = async ({ latitude, longitude }) => {
const { data: response } = await axios.get(
`http:https://api.waqi.info/feed/geo:${latitude};${longitude}/?token=${
Expand All @@ -12,16 +15,76 @@ const aqicn = async ({ latitude, longitude }) => {
{ timeout: 6000 }
);

if (response.status === 'ok') {
// ComponentDidCatch not working https://github.com/facebook/react-native/issues/18491
// So we handle errors manually
if (!response.data || !response.data.aqi) {
throw new Error('AQI not defined in response.');
}
return response.data;
} else {
// Example response
// Object {
// "data": Object {
// "aqi": 51,
// "attributions": Array [
// Object {
// "name": "Air Lorraine - Surveillance et étude de la qualité de l'air en Lorraine",
// "url": "http:https://air-lorraine.org/",
// },
// Object {
// "name": "European Environment Agency",
// "url": "http:https://www.eea.europa.eu/themes/air/",
// },
// ],
// "city": Object {
// "geo": Array [
// 49.39429190887402,
// 6.201473467510839,
// ],
// "name": "Garche, Thionville-Nord",
// "url": "http:https://aqicn.org/city/france/lorraine/thionville-nord/garche/",
// },
// "dominentpol": "pm25",
// "iaqi": Object {
// "no2": Object {
// "v": 3.2,
// },
// "o3": Object {
// "v": 34.6,
// },
// "p": Object {
// "v": 1012.5,
// },
// "pm10": Object {
// "v": 20,
// },
// "pm25": Object {
// "v": 51,
// },
// "so2": Object {
// "v": 1.6,
// },
// "t": Object {
// "v": 21.6,
// },
// },
// "idx": 7751,
// "time": Object {
// "s": "2018-08-09 14:00:00",
// "tz": "+01:00",
// "v": 1533823200,
// },
// },
// "status": "ok",
// }

if (response.status !== 'ok') {
throw new Error(response.data);
}

if (
!response.data ||
!response.data.iaqi ||
!response.data.iaqi.pm25 ||
response.data.iaqi.pm25.v === undefined
) {
throw new Error('PM2.5 not defined in response.');
}

return { pm25: response.data.iaqi.pm25.v, city: response.data.city };
};

export default aqicn;

0 comments on commit c8a1c11

Please sign in to comment.