";
$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
try {
$response = file_get_contents($apiUrl);
} catch (Exception $e) {
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
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';
}
// | 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
}
?>
";
echo "Current Date:
".date("F j, Y, g:i:s a")."
";
echo "Current GPS Data:
".getGPS();
?>