Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AQI->PM25 fix #39

Merged
merged 4 commits into from
Aug 9, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use pm25 level instead of AQI (fix #36)
  • Loading branch information
amaury1093 committed Aug 9, 2018
commit c8a1c115bebfba0dedf426e3fea37c9f8d1a4fd2
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;