Implemented custom activity, to not always rely on timeout
This commit is contained in:
parent
92b2cdf9c9
commit
d8c9e3d388
1 changed files with 31 additions and 20 deletions
51
index.php
51
index.php
|
@ -3,56 +3,67 @@
|
||||||
// Config
|
// Config
|
||||||
$SECRET_KEY = "ENTER_SECRET_HERE";
|
$SECRET_KEY = "ENTER_SECRET_HERE";
|
||||||
$FILENAME = "last_activity.txt";
|
$FILENAME = "last_activity.txt";
|
||||||
|
$SEPARATOR = " ";
|
||||||
$IDLE_DURATION_THRESHOLD = 60 * 30; // In seconds
|
$IDLE_DURATION_THRESHOLD = 60 * 30; // In seconds
|
||||||
$SLEEP_DURATION_THRESHOLD = 60 * 60 * 4; // In seconds
|
$SLEEP_DURATION_THRESHOLD = 60 * 60 * 3; // In seconds
|
||||||
$EXPECTED_SLEEP_DURATION = 60 * 60 * 9.5; // In seconds
|
$EXPECTED_SLEEP_DURATION = 60 * 60 * 9.5; // In seconds
|
||||||
|
|
||||||
|
|
||||||
function storeTime($time)
|
function storeActivity($time, $activity = "")
|
||||||
{
|
{
|
||||||
global $FILENAME;
|
global $FILENAME, $SEPARATOR;
|
||||||
$file = fopen($FILENAME, "w");
|
$file = fopen($FILENAME, "w");
|
||||||
fwrite($file, $time);
|
fwrite($file, $time . $SEPARATOR . $activity);
|
||||||
fclose($file);
|
fclose($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTime()
|
function getActivity()
|
||||||
{
|
{
|
||||||
global $FILENAME;
|
global $FILENAME, $SEPARATOR;
|
||||||
$file = fopen($FILENAME, "r");
|
$file = fopen($FILENAME, "r");
|
||||||
$time = fread($file, filesize($FILENAME));
|
$line = fgets($file);
|
||||||
fclose($file);
|
fclose($file);
|
||||||
return (int)$time;
|
$parts = explode($SEPARATOR, $line, 2);
|
||||||
|
return array(
|
||||||
|
"time" => (int)$parts[0],
|
||||||
|
"activity" => $parts[1]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Is secret given?
|
// Is secret given?
|
||||||
if (isset($_GET['secret']) && $_GET['secret'] == $SECRET_KEY) {
|
if (isset($_GET['secret']) && $_GET['secret'] == $SECRET_KEY) {
|
||||||
|
$activity = isset($_GET['activity']) ? $_GET['activity'] : "";
|
||||||
|
|
||||||
// Update the file
|
// Update the file
|
||||||
storeTime(time());
|
storeActivity(time(), $activity);
|
||||||
echo "Last activity udpated";
|
echo "Last activity updated";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current status requested
|
// Current status requested
|
||||||
$last_activity = getTime();
|
$last_activity = getActivity();
|
||||||
$response = array(
|
$response = array(
|
||||||
"last_activity" => $last_activity
|
"last_activity" => $last_activity['time'],
|
||||||
|
"status" => $last_activity['activity'],
|
||||||
);
|
);
|
||||||
|
|
||||||
// Estimate current activity
|
// Estimate current activity
|
||||||
$now = time();
|
$now = time();
|
||||||
$diff = $now - $last_activity; // DIFFERENCE IN SECONDS
|
$diff = $now - $last_activity["time"]; // DIFFERENCE IN SECONDS
|
||||||
if ($diff >= $SLEEP_DURATION_THRESHOLD) {
|
|
||||||
$response['status'] = "Sleeping";
|
if ($response['status'] == "") {
|
||||||
} else if ($diff >= $IDLE_DURATION_THRESHOLD) {
|
if ($diff >= $SLEEP_DURATION_THRESHOLD) {
|
||||||
$response['status'] = "Idle";
|
$response['status'] = "Sleeping";
|
||||||
} else {
|
} else if ($diff >= $IDLE_DURATION_THRESHOLD) {
|
||||||
$response['status'] = "Active";
|
$response['status'] = "Awake";
|
||||||
|
} else {
|
||||||
|
$response['status'] = "Busy";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Estimated time until wake up
|
// Estimated time until wake up
|
||||||
if ($response['status'] == "Sleeping") {
|
if (strtolower($response['status']) == "sleeping") {
|
||||||
$response['time_until_wake'] = $EXPECTED_SLEEP_DURATION - $diff;
|
$response['time_until_wake'] = $EXPECTED_SLEEP_DURATION - $diff;
|
||||||
} else {
|
} else {
|
||||||
$response['time_until_wake'] = 0;
|
$response['time_until_wake'] = 0;
|
||||||
|
|
Loading…
Reference in a new issue