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
Show file tree
Hide file tree
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
Next Next commit
Use pm25 in conversion formula
  • Loading branch information
amaury1093 committed Aug 9, 2018
commit 20b6d2e7284364385223aca061cdfb2d565167db
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
6 changes: 4 additions & 2 deletions App/Screens/Screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,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
28 changes: 27 additions & 1 deletion App/utils/dataSources/windWaqi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,41 @@

import axios from 'axios';

/**
* Fetch the PM2.5 level from https://wind.waqi.info.
* @return {number} - The PM2.5 level.
*/
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 } };
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;