Initial version
This commit is contained in:
commit
b613307a6f
1 changed files with 65 additions and 0 deletions
65
index.php
Executable file
65
index.php
Executable file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
// Config
|
||||
$SECRET_KEY = "ENTER_SECRET_HERE";
|
||||
$FILENAME = "last_activity.txt";
|
||||
$IDLE_DURATION_THRESHOLD = 60 * 30; // In seconds
|
||||
$SLEEP_DURATION_THRESHOLD = 60 * 60 * 4; // In seconds
|
||||
$EXPECTED_SLEEP_DURATION = 60 * 60 * 9.5; // In seconds
|
||||
|
||||
|
||||
function storeTime($time)
|
||||
{
|
||||
global $FILENAME;
|
||||
$file = fopen($FILENAME, "w");
|
||||
fwrite($file, $time);
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
function getTime()
|
||||
{
|
||||
global $FILENAME;
|
||||
$file = fopen($FILENAME, "r");
|
||||
$time = fread($file, filesize($FILENAME));
|
||||
fclose($file);
|
||||
return $time;
|
||||
}
|
||||
|
||||
|
||||
// Is secret given?
|
||||
if (isset($_GET['secret']) && $_GET['secret'] == $SECRET_KEY) {
|
||||
// Update the file
|
||||
storeTime(time());
|
||||
echo "Last activity udpated";
|
||||
die();
|
||||
}
|
||||
|
||||
// Current status requested
|
||||
$last_activity = getTime();
|
||||
$response = array(
|
||||
"last_activity" => date(DATE_ATOM, $last_activity)
|
||||
);
|
||||
|
||||
// Estimate current activity
|
||||
$now = time();
|
||||
$diff = $now - $last_activity; // DIFFERENCE IN SECONDS
|
||||
if ($diff >= $SLEEP_DURATION_THRESHOLD) {
|
||||
$response['status'] = "Sleeping";
|
||||
} else if ($diff >= $IDLE_DURATION_THRESHOLD) {
|
||||
$response['status'] = "Idle";
|
||||
} else {
|
||||
$response['status'] = "Active";
|
||||
}
|
||||
|
||||
// Estimated time until wake up
|
||||
if ($response['status'] == "Sleeping") {
|
||||
$response['time_until_wake'] = $EXPECTED_SLEEP_DURATION - $diff;
|
||||
} else {
|
||||
$response['time_until_wake'] = 0;
|
||||
}
|
||||
|
||||
// Respond with JSON
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($response);
|
||||
|
||||
|
Loading…
Reference in a new issue