234 lines
6.6 KiB
PHP
234 lines
6.6 KiB
PHP
<?php
|
|
date_default_timezone_set('America/Los_Angeles');
|
|
# set default values
|
|
$status = "Unchecked";
|
|
$message = "No attempt made yet.";
|
|
$button_text = "Pending";
|
|
$button_active = false;
|
|
$button_recent = false;
|
|
$button_action = "";
|
|
$button_result = "";
|
|
$http_host = $_SERVER['HTTP_HOST'];
|
|
$debug_string = "";
|
|
|
|
if (isset($_GET['action'])) {
|
|
$debug_string = $debug_string."GET called, ".$_GET['action']."<br>";
|
|
$button_recent = true;
|
|
$button_result = runAPI($_GET['action']);
|
|
$debug_string = $debug_string."Button Result: ".$button_result."<br>";
|
|
sleep(1);
|
|
}
|
|
|
|
function runAPI($submitted_status) {
|
|
if(!isset($debug_string)){
|
|
$debug_string = "";
|
|
}
|
|
$debug_string = $debug_string."runAPI called, ".$submitted_status."<br>";
|
|
switch ($submitted_status) {
|
|
case "stop":
|
|
$apiUrl = "http://172.17.0.1:5000/stop";
|
|
break;
|
|
case "start":
|
|
$apiUrl = "http://172.17.0.1:5000/start";
|
|
break;
|
|
default:
|
|
$apiUrl = "http://172.17.0.1:5000/status";
|
|
break;
|
|
}
|
|
// API URL
|
|
$debug_string = $debug_string."After switch, apiUrl is ".$apiUrl."<br>";
|
|
// Use file_get_contents or cURL to fetch the API data
|
|
try {
|
|
$response = file_get_contents($apiUrl);
|
|
} catch (Exception $e) {
|
|
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
|
|
}
|
|
if ($response === FALSE) {
|
|
$debug_string = $debug_string."Response ERROR!!<br>";
|
|
return "error"; // API error
|
|
}
|
|
|
|
// Decode the JSON response (assuming the API returns JSON)
|
|
$data = json_decode($response, true);
|
|
$debug_string = $debug_string."Data from API call: ".$data['Message']."<br>";
|
|
// Assuming the API returns a single word result (like "success", "failure", etc.)
|
|
return isset($data['Status']) ? $data['Status'] : 'unknown';
|
|
}
|
|
|
|
// | awk '{printf(\"%.5f\n\", $1)}'
|
|
// | awk '{printf(\"%.5f\n\", $1)}'
|
|
|
|
|
|
function getGPS(){
|
|
// check the API
|
|
$gps_data = file_get_contents("http://172.17.0.1:5000/return_gps");
|
|
try {
|
|
$gps_data = json_decode($gps_data, true);
|
|
|
|
} catch (Exception $e){
|
|
$gps_data = '{c}';
|
|
}
|
|
|
|
//set the vars
|
|
$LAT = $gps_data['lat'];
|
|
$LON = $gps_data['lon'];
|
|
$SPEED = $gps_data['speed'];
|
|
|
|
if (is_null($LAT) || $LAT == 0) {
|
|
return "No GPS data available - null LAT";
|
|
}
|
|
if (is_null($LON) || $LON == 0) {
|
|
return "No GPS data available - null LON";
|
|
}
|
|
|
|
return $LAT.", ".$LON.", ".$SPEED."mph";
|
|
}
|
|
|
|
// URL of the external API
|
|
$apiUrl = "http://172.17.0.1:5000/status";
|
|
|
|
// Use file_get_contents to fetch data from the API
|
|
try {
|
|
$response = file_get_contents($apiUrl);
|
|
} catch (Exception $e) {
|
|
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
|
|
}
|
|
|
|
// Check if the request was successful
|
|
if ($response === FALSE) {
|
|
$data = "Failed to fetch data.";
|
|
}
|
|
else {
|
|
// Decode the JSON response (assuming the API returns JSON)
|
|
$data = json_decode($response, true);
|
|
// If you want to display specific data, adjust this part
|
|
// For example, if the API returns a 'message' key, display it
|
|
if (isset($data['Message'])) {
|
|
$message = $data['Message'];
|
|
} else {
|
|
$message = "No message found in the response.";
|
|
}
|
|
if (isset($data['Status'])) {
|
|
$status = $data['Status'];
|
|
} else {
|
|
$status = "None";
|
|
}
|
|
}
|
|
|
|
switch ($status) {
|
|
case "active":
|
|
$button_text = "Stop Service";
|
|
$button_active = true;
|
|
break;
|
|
case "inactive":
|
|
$button_text = "Start Service";
|
|
$button_active = true;
|
|
break;
|
|
case "failed":
|
|
$button_text = "Start Service - Warning";
|
|
$button_active = true;
|
|
break;
|
|
case "deactivating":
|
|
$button_text = "Service Stopping...";
|
|
$button_active = false;
|
|
break;
|
|
case "unknowable":
|
|
$button_text = "Service Manager Down...";
|
|
$button_active = false;
|
|
break;
|
|
default:
|
|
$button_text = "Error, No Status";
|
|
$button_active = false;
|
|
$status="unknown";
|
|
break;
|
|
}
|
|
|
|
if($button_active && !$button_recent){
|
|
$action = "";
|
|
if($status == "active"){
|
|
$action = "stop";
|
|
}
|
|
else if($status == "inactive"){
|
|
$action = "start";
|
|
}
|
|
else if($status == "failed"){
|
|
$action = "start";
|
|
}
|
|
$button_action = ' onclick="location.href=\'/?action='.$action.'\'" ';
|
|
}
|
|
|
|
if($button_recent){
|
|
/*
|
|
$delay = 5; // Number of seconds before redirection
|
|
echo "You will be redirected in $delay seconds.";
|
|
header("Refresh: $delay; url=https://example.com");
|
|
exit();
|
|
*/
|
|
header("Location: http://".$http_host);
|
|
exit(); // Always include exit() after headers are sent
|
|
}
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>API Data Display</title>
|
|
<link rel="stylesheet" href="styles.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="container">
|
|
|
|
<div class="button-container">
|
|
<button <?php echo $button_action; ?> class="<?php echo $status; ?>" id="phpButton"<?php if(!$button_active || $button_recent) {echo " disabled";} ?>>
|
|
<?php echo $button_text; ?>
|
|
</button>
|
|
<p>
|
|
</div>
|
|
<p>
|
|
<div class="api-data">
|
|
<!-- PHP will inject the data here -->
|
|
<?php
|
|
echo "Full Message:<br>".htmlspecialchars($message)."<p>";
|
|
echo "Current Date:<br>".date("F j, Y, g:i:s a")."<p>";
|
|
echo "Current GPS Data:<br>".getGPS();
|
|
?>
|
|
</div>
|
|
|
|
</div>
|
|
<script>
|
|
|
|
// When the button is clicked, send an AJAX request to fetch new data
|
|
document.getElementById('phpButton').addEventListener('click', function() {
|
|
// Disable button to avoid multiple clicks
|
|
this.disabled = true;
|
|
this.innerText = 'Loading...';
|
|
|
|
// Make an AJAX request
|
|
fetch('index.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
'status': '<?php echo $status; ?>'
|
|
},
|
|
body: 'action=fetch_data' // Trigger the PHP function via POST
|
|
})
|
|
});
|
|
</script>
|
|
|
|
<script>
|
|
// Automatically refresh the page every second
|
|
setTimeout(function() {
|
|
location.reload();
|
|
}, <?php if($button_recent){ echo "5000"; } else{ echo "1000"; } ?>); // Refresh interval (1000 ms = 1 second)
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|
|
|