Skip to content

Instantly share code, notes, and snippets.

@bencentra
Last active March 11, 2016 03:20
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 bencentra/f44e056b35e539e98555 to your computer and use it in GitHub Desktop.
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
<?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(
"blcentra@gmail.com"
);
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: bencentra@csh.rit.edu";
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