Last active
March 11, 2016 03:20
-
-
Save bencentra/f44e056b35e539e98555 to your computer and use it in GitHub Desktop.
Send email alerts to drink admins if a drink machine's temperature drops too low
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Script for alerting drink admins if a machine is gonna freeze. | |
# | |
# Requires a WebDrink API key: https://webdrink.csh.rit.edu/#/settings | |
# | |
# Should be run as a cron job on san.csh.rit.edu: | |
# */10 * * * * php /users/u18/bencentra/scripts/drink-temp-alert.php | |
$apiKey = "API_KEY_HERE"; | |
$threshold = 32; | |
$machines = array( | |
"Little Drink" => 1, | |
"Big Drink" => 2 | |
// "Snack" => 3 | |
); | |
$recipients = array( | |
"[email protected]" | |
); | |
function getMachineTemp($machineId) { | |
$url = "https://webdrink.csh.rit.edu/api/index.php?request=temps/machines"; | |
$url .= "&api_key=$apiKey&limit=1&machine_id=$machineId"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($output, true); | |
return $result["data"][0][1]; | |
} | |
function sendEmail($to, $machine, $temperature) { | |
$subject = "$machine Temperature Alert"; | |
$message = "Head's up, $machine is currently $temperature degrees."; | |
$headers = "From: [email protected]"; | |
mail($to, $subject, $message, $headers); | |
} | |
foreach($machines as $machine => $id) { | |
$file = __DIR__ . "/$machine.txt"; | |
$temperature = getMachineTemp($id); | |
if ($temperature < $threshold) { | |
if (!file_exists($file)) { | |
foreach($recipients as $recipient) { | |
sendEmail($recipient, $machine, $temperature); | |
} | |
file_put_contents($file, $temperature); | |
} | |
} | |
else { | |
if (file_exists($file)) { | |
unlink($file); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment