";
$button_recent = true;
$button_result = runAPI($_GET['action']);
$debug_string = $debug_string."Button Result: ".$button_result."
";
sleep(1);
}
function runAPI($submitted_status) {
if(!isset($debug_string)){
$debug_string = "";
}
$debug_string = $debug_string."runAPI called, ".$submitted_status."
";
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."
";
// Use file_get_contents or cURL to fetch the API data
$response = file_get_contents($apiUrl);
if ($response === FALSE) {
$debug_string = $debug_string."Response ERROR!!
";
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']."
";
// Assuming the API returns a single word result (like "success", "failure", etc.)
return isset($data['Status']) ? $data['Status'] : 'unknown';
}
function getGPS(){
if (!file_exists("{{ gps_service_directory }}/gps_data")) {
return "No GPS data available.";
}
try {
$LAT = shell_exec("cat {{ gps_service_directory }}/gps_data | grep lat | cut -d ':' -f 2 | awk '{printf(\"%.5f\n\", $1)}')");
$LON = shell_exec("cat {{ gps_service_directory }}/gps_data | grep lon | cut -d ':' -f 2 | awk '{printf(\"%.5f\n\", $1)}')");
$SPEED = floatval(trim(shell_exec("cat {{ gps_service_directory }}/gps_data | grep speed | cut -d ':' -f 2)"))) * 0.62;
} catch (Exception $e) {
return "Failed to parse gps_data: ".$e;
}
if (is_null($LAT)) {
return "No GPS data available.";
}
if (is_null($LON)) {
return "No GPS data available.";
}
return $LAT.", ".$LON.", ".$SPEED."mph";
}
// Check if the button was clicked via AJAX request
// if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// $button_recent = true;
// $status_at_submit = $_POST['status'];
// echo "Set when clicked, second in code - ".$status_at_submit."
";
// // Get the API result
// $apiResult = runAPI($status_at_submit);
// // Return the result as a JSON response
// echo json_encode(['status' => $apiResult]);
// exit;
// }
// URL of the external API
$apiUrl = "http://172.17.0.1:5000/status";
// Use file_get_contents to fetch data from the API
$response = file_get_contents($apiUrl);
// 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;
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
}
?>
";
echo "Current Date:
".date("F j, Y, g:i:s a")."
";
echo "Current GPS Data:
".getGPS();
?>