Skip to content

Commit

Permalink
Merge pull request #39 from amaurymartiny/am-aqi-fix
Browse files Browse the repository at this point in the history
AQI->PM25 fix
  • Loading branch information
amaury1093 committed Aug 9, 2018
2 parents b70fa8a + e669676 commit 694fab9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 30 deletions.
7 changes: 4 additions & 3 deletions App/Screens/Home/Cigarettes/Cigarettes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';

import Cigarette from './Cigarette';
import pm25ToCigarette from '../../../utils/pm25ToCigarettes';
import pm25ToCigarettes from '../../../utils/pm25ToCigarettes';

export default class Cigarettes extends Component {
getSize = cigarettes => {
Expand All @@ -16,9 +16,10 @@ export default class Cigarettes extends Component {
};

render () {
const { api, style } = this.props;
const cigarettes = Math.min(pm25ToCigarette(api), 63); // We don't show more than 63
const { pm25, style } = this.props;
const cigarettes = Math.min(pm25ToCigarettes(pm25), 63); // We don't show more than 63
// const cigarettes = 0.9; // Can change values here for testing

const count = Math.floor(cigarettes);
const decimal = cigarettes - count;

Expand Down
21 changes: 14 additions & 7 deletions App/Screens/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ export default class Home extends Component {
title:
'Did you know that you may be smoking up to 20 cigarettes per day, just for living in a big city?',
message: `Shoot! I 'smoked' ${pm25ToCigarettes(
this.props.screenProps.api
this.props.screenProps.api.pm25
)} cigarettes today by breathing urban air. And you? Find out here: shootismoke.github.io`
});

render () {
const {
screenProps: { api }
screenProps: {
api: { pm25 }
}
} = this.props;
return (
<ScrollView bounces={false} contentContainerStyle={styles.container}>
<View style={styles.content}>
<Cigarettes api={api} />
<Cigarettes pm25={pm25} />
<View style={styles.main}>{this.renderText()}</View>
<TouchableOpacity
onPress={this.handleShare}
Expand All @@ -74,9 +76,11 @@ export default class Home extends Component {

renderShit = () => {
const {
screenProps: { api }
screenProps: {
api: { pm25 }
}
} = this.props;
const cigarettes = pm25ToCigarettes(api);
const cigarettes = pm25ToCigarettes(pm25);

if (cigarettes <= 1) return 'Oh';
if (cigarettes < 5) return 'Sh*t';
Expand All @@ -86,9 +90,12 @@ export default class Home extends Component {

renderText = () => {
const {
screenProps: { api }
screenProps: {
api: { pm25 }
}
} = this.props;
const cigarettes = pm25ToCigarettes(api);
// Round to 1 decimal
const cigarettes = Math.round(pm25ToCigarettes(pm25) * 10) / 10;

return (
<Text style={styles.shit}>
Expand Down
11 changes: 8 additions & 3 deletions App/Screens/Screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export default class Screens extends Component {

const api = await retry(
async (_, attempt) => {
const result = await sources[attempt % 2](currentLocation || coords);
// Attempt starts at 1
const result = await sources[(attempt - 1) % 2](
currentLocation || coords
);
return result;
},
{ retries: 3 } // 2 attemps per source
Expand All @@ -110,8 +113,10 @@ export default class Screens extends Component {
}

getVideoStyle = () => {
const { api } = this.state;
const cigarettes = pm25ToCigarettes(api);
const {
api: { pm25 }
} = this.state;
const cigarettes = pm25ToCigarettes(pm25);

if (cigarettes <= 1) return { opacity: 0.2 };
if (cigarettes < 5) return { opacity: 0.5 };
Expand Down
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;
32 changes: 31 additions & 1 deletion App/utils/dataSources/windWaqi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,45 @@

import axios from 'axios';

/**
* Fetch the PM2.5 level from https://wind.waqi.info.
*/
const windWaqi = async ({ latitude, longitude }) => {
const { data: response } = await axios.get(
`https://wind.waqi.info/mapq/nearest?geo=1/${latitude}/${longitude}`,
{ timeout: 6000 }
);

// Example response:
// Object {
// "d": Array [
// Object {
// "d": 0.8,
// "geo": Array [
// 52.489451,
// 13.430844,
// ],
// "key": "_c08tyk3Mq9R3Si3KyczT90stzT68LScnT9cvMa84Na-4pCjx8PxUAA",
// "nlo": "Neukölln-Nansenstraße, Berlin",
// "nna": "",
// "pol": "pm25",
// "t": 1533819600,
// "u": "Germany/Berlin/Neukölln-Nansenstraße",
// "v": "75",
// "x": "10032",
// },
// ],
// "g": null,
// }

if (response.d && response.d.length) {
const data = response.d[0];
return { aqi: data.v, city: { geo: data.geo, name: data.nlo } };

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

return { pm25: data.v, city: { geo: data.geo, name: data.nlo } };
} else {
throw new Error(response);
}
Expand Down
12 changes: 4 additions & 8 deletions App/utils/pm25ToCigarettes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
// SPDX-License-Identifier: GPL-3.0

/**
* Convert pm25 level to number of cigarettes
* Convert pm25 level to number of cigarettes. 1 cigarette is equivalent of a
* PM2.5 level of 22ug/m3.
*
* @see http:https://berkeleyearth.org/air-pollution-and-cigarette-equivalence/
* @param {Float} api - The api object returned by the WAQI api.
*/
const pm25ToCigarettes = api => {
const pm25 = api.aqi;
// pm25 = 22 is equivalent to 1 cigarette per day
const cigarettes = pm25 / 22;
// Format to 1 decimal
return Math.round(cigarettes * 10) / 10;
};
const pm25ToCigarettes = pm25 => pm25 / 22;

export default pm25ToCigarettes;

0 comments on commit 694fab9

Please sign in to comment.