Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Last active September 6, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsnell/512fe8a52afa8863c4354520d3c09c8c to your computer and use it in GitHub Desktop.
Save jasonsnell/512fe8a52afa8863c4354520d3c09c8c to your computer and use it in GitHub Desktop.
Monochrome Air Quality BitBar plugin
#!/usr/bin/env php
<?php
error_reporting( 0 );
// find a PurpleAir sensor ID on the PurpleAir map and put that ID in $show
$show = '6732';
$url = ( 'https://www.purpleair.com/json?show=' . $show );
// Get the sensor data via JSON
$json = @file_get_contents( $url );
$array = json_decode( $json, true );
for ( $i = 0; $i < sizeof( $array['results'] ); $i++ ) {
$array['results'][ $i ]['Stats'] = json_decode( $array['results'][ $i ]['Stats'], true );
}
$pm25a = $array['results'][0]['Stats']['v1'];
$pm25b = $array['results'][1]['Stats']['v1'];
$pm25livea = $array['results'][0]['Stats']['v'];
$pm25liveb = $array['results'][1]['Stats']['v'];
$location = $array['results'][0]['Label'];
$lat = $array['results'][1]['Lat'];
$lon = $array['results'][1]['Lon'];
$aqia = aqiFromPM( $pm25a );
$aqib = aqiFromPM( $pm25b );
$aqilivea = aqiFromPM( $pm25livea );
$aqiliveb = aqiFromPM( $pm25liveb );
settype( $aqia, 'integer' );
settype( $aqib, 'integer' );
settype( $aqilivea, 'integer' );
settype( $aqiliveb, 'integer' );
$AQI = round( ( $aqia + $aqib ) / 2 );
$AQIlive = round( ( $aqilivea + $aqiliveb ) / 2 );
echo ( $output . getAQIDescription( $AQI ) . ' (' . $AQI . getAQItrend( $AQI, $AQIlive ) . ')|color=' . getAQIcolor( $AQI ) . '
---
' . $location . '|href=https://www.purpleair.com/map?opt=1/i/mAQI/a10/cC0&select=' . $show . '#11/' . $lat . '/' . $lon );
// Function to get AQI number from PPM reading
function aqiFromPM( $pm ) {
if ( is_nan( $pm ) ) {
return '-';
}
if ( isset( $pm ) == false ) {
return 'Error: No value';
}
if ( $pm < 0.0 ) {
return $pm;
}
if ( $pm > 1000.0 ) {
return '-';
}
if ( $pm > 350.5 ) {
return calcAQI( $pm, 500.0, 401.0, 500.0, 350.5 );
} elseif ( $pm > 250.5 ) {
return calcAQI( $pm, 400.0, 301.0, 350.4, 250.5 );
} elseif ( $pm > 150.5 ) {
return calcAQI( $pm, 300.0, 201.0, 250.4, 150.5 );
} elseif ( $pm > 55.5 ) {
return calcAQI( $pm, 200.0, 151.0, 150.4, 55.5 );
} elseif ( $pm > 35.5 ) {
return calcAQI( $pm, 150.0, 101.0, 55.4, 35.5 );
} elseif ( $pm > 12.1 ) {
return calcAQI( $pm, 100.0, 51.0, 35.4, 12.1 );
} elseif ( $pm >= 0.0 ) {
return calcAQI( $pm, 50.0, 0.0, 12.0, 0.0 );
} else {
return '-';
}
}
// Function that actually calculates the AQI number
function calcAQI( $Cp, $Ih, $Il, $BPh, $BPl ) {
$a = ( $Ih - $Il );
$b = ( $BPh - $BPl );
$c = ( $Cp - $BPl );
return round( ( $a / $b ) * $c + $Il );
}
// Function that gets the AQI's description
function getAQIDescription( $aqinum ) {
if ( $aqinum >= 401 ) {
return 'Hazardous';
} elseif ( $aqinum >= 301 ) {
return 'Hazardous';
} elseif ( $aqinum >= 201 ) {
return 'Very Unhealthy';
} elseif ( $aqinum >= 151 ) {
return 'Unhealthy';
} elseif ( $aqinum >= 101 ) {
return 'Unhealthy for Sensitive Groups';
} elseif ( $aqinum >= 51 ) {
return 'Moderate';
} elseif ( $aqinum >= 0 ) {
return 'Good';
} else {
return 'Unknown';
}
}
// Function that gets the AQI's color code
function getAQIColor( $aqinum ) {
$darkmode = shell_exec('osascript <<EOF
tell application "System Events"
tell appearance preferences
set theMode to dark mode
end tell
end tell
return theMode
EOF
');
$darkmodestate = filter_var($darkmode, FILTER_VALIDATE_BOOLEAN);
if ( $darkmodestate ) {
// we are in dark mode
return '#ffffff';
} else {
// we are in light mode
return '#000000';
}
}
// Function that checks for the AQI trend
function getAQItrend( $average, $live ) {
if ( ( $average - $live ) > 9 ) {
return '↓';
} elseif ( ( $average - $live ) < -9 ) {
return '↑';
} else {
return '';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment