first successful test

This commit is contained in:
2025-08-24 19:49:45 -07:00
parent 96906161fd
commit cc24088bbe
9 changed files with 388 additions and 110 deletions

View File

@ -9,21 +9,54 @@ $button_recent = false;
$button_action = "";
$button_result = "";
$http_host = $_SERVER['HTTP_HOST'];
$debug_string = "";
$capture_duration = "3";
$elapsed_time = "0";
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);
// Service Status API Checker
$status_API_URL = "http://172.17.0.1:5000/status";
// Use file_get_contents to fetch data from the API
try {
$status_API_response = file_get_contents($status_API_URL);
} catch (Exception $e) {
$status_API_response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($status_API_response === FALSE) {
$data = "Failed to fetch data.";
}
function runAPI($submitted_status) {
if(!isset($debug_string)){
$debug_string = "";
else {
// Decode the JSON response (assuming the API returns JSON)
$data = json_decode($status_API_response, true);
if (isset($data['Message'])) {
$message = $data['Message'];
} else {
$message = "No message found in the response.";
}
$debug_string = $debug_string."runAPI called, ".$submitted_status."<br>";
if (isset($data['Status'])) {
$status = $data['Status'];
} else {
$status = "None";
}
if (isset($data['Elapsed_Time'])) {
$elapsed_time = $data['Elapsed_Time'];
} else {
$elapsed_time = "-1";
}
}
if (isset($_GET['new_duration'])){
$button_recent = true;
set_duration($_GET['new_duration']);
}
if (isset($_GET['action'])) {
$button_recent = true;
$button_result = service_manager($_GET['action']);
}
function service_manager($submitted_status) {
switch ($submitted_status) {
case "stop":
$apiUrl = "http://172.17.0.1:5000/stop";
@ -35,8 +68,6 @@ function runAPI($submitted_status) {
$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);
@ -44,77 +75,15 @@ function runAPI($submitted_status) {
$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";
@ -168,6 +137,133 @@ if($button_recent){
exit(); // Always include exit() after headers are sent
}
function duration_button_handler($button_ID, $function) {
// Duration Status API Checker
$duration_API_URL = "http://172.17.0.1:5000/check_duration";
// Use file_get_contents to fetch data from the API
try {
$duration_API_URL = file_get_contents($duration_API_URL);
} catch (Exception $e) {
$duration_API_URL = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($duration_API_URL === FALSE) {
$raw_duration = "Failed to fetch data.";
}
else {
// Decode the JSON response (assuming the API returns JSON)
$raw_duration = json_decode($duration_API_URL, true);
if (isset($raw_duration['duration'])) {
$capture_duration = $raw_duration['duration'];
} else {
$capture_duration = "3";
}
}
// Service Status API Checker
$status_API_URL = "http://172.17.0.1:5000/status";
// Use file_get_contents to fetch data from the API
try {
$status_API_response = file_get_contents($status_API_URL);
} catch (Exception $e) {
$status_API_response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($status_API_response === FALSE) {
$data = "Failed to fetch data.";
}
else {
// Decode the JSON response (assuming the API returns JSON)
$data = json_decode($status_API_response, true);
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";
}
}
# function 0 - return class string
# function 1 - return active string
switch ($function) {
case "0":
# these classes are weird because reasons
# specifically because the classes are named
# based on service status
# to decode this, inactive is green
# and deactivating is blue
# so all the selectable buttons are blue and the active one is green
if($button_ID == $capture_duration){
return "inactive";
}
else {
return "selected";
}
break;
case "1":
# this disables clicking the active button
if($status == "active") {
return " disabled";
}
else if($button_ID == $capture_duration){
return " disabled";
}
else {
return "";
}
break;
default:
break;
}
}
# this will be an integer from 0-3 for
# 30min, 1hr, 2hr, and 6rh respectively
# info = {
# "duration": new_duration
# }
function set_duration($duration) {
switch ($duration) {
case "0":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=0";
break;
case "1":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=1";
break;
case "2":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=2";
break;
case "3":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=3";
break;
default:
$apiUrl = "http://172.17.0.1:5000/status";
break;
}
try {
$response = file_get_contents($apiUrl);
} catch (Exception $e) {
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
if ($response === FALSE) {
return "error"; // API error
}
}
?>
<!DOCTYPE html>
@ -185,22 +281,53 @@ if($button_recent){
<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";} ?>>
<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="button-container">
<table>
<tr>
<td>
<button onclick="location.href='/?new_duration=3'" class="<?php echo duration_button_handler(3, 0); ?>" id="6_hour_button" <?php echo duration_button_handler(3, 1); ?>>6 Hours</button>
</td>
<td>
<button onclick="location.href='/?new_duration=2'" class="<?php echo duration_button_handler(2, 0); ?>" id="2_hour_button" <?php echo duration_button_handler(2, 1); ?>>2 Hours</button>
</td>
</tr>
<tr>
<td>
<button onclick="location.href='/?new_duration=1'" class="<?php echo duration_button_handler(1, 0); ?>" id="1_hour_button" <?php echo duration_button_handler(1, 1); ?>>1 Hour</button>
</td>
<td>
<button onclick="location.href='/?new_duration=0'" class="<?php echo duration_button_handler(0, 0); ?>" id="30_min_button" <?php echo duration_button_handler(0, 1); ?>>30 min</button>
</td>
</tr>
</table>
</div>
<p>
<div class="api-data">
<!-- PHP will inject the data here -->
<?php
echo "Full Message:<br>".htmlspecialchars($message)."<p>";
if ($status == "active"){
echo "Time Elapsed:<br>".htmlspecialchars($elapsed_time)."s<p>";
}
echo "Current Date:<br>".date("F j, Y, g:i:s a")."<p>";
echo "Current GPS Data:<br>".getGPS();
?>
</div>
<p>
</div>
<script>
// When the button is clicked, send an AJAX request to fetch new data
@ -220,13 +347,14 @@ if($button_recent){
})
});
</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>