more cleaning, adding GPS data to service control site

This commit is contained in:
2025-07-28 10:10:51 -07:00
parent c36e56f234
commit 463c5a4784
7 changed files with 69 additions and 33 deletions

View File

@ -1,54 +0,0 @@
from flask import Flask, jsonify
import gps
import threading
import time
app = Flask(__name__)
# GPS data container
current_gps_data = {
"latitude": None,
"longitude": None,
"altitude": None,
"speed": None,
"timestamp": None
}
# Global gps session object
session = gps.gps(mode=gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
def gps_monitor():
global current_gps_data
while True:
try:
# Wait for new data from the GPS device
report = session.next()
if report['class'] == 'TPV':
current_gps_data = {
"latitude": report.get('lat', None),
"longitude": report.get('lon', None),
"altitude": report.get('alt', None),
"speed": report.get('speed', None),
"timestamp": report.get('time', None)
}
except gps.GPSException as e:
print(f"GPS Error: {e}")
except KeyError as e:
print(f"Missing Key: {e}")
time.sleep(1)
@app.route('/gps', methods=['GET'])
def get_gps_data():
"""API endpoint to fetch current GPS data"""
if None in current_gps_data.values():
return jsonify({"error": "No GPS data available yet"}), 404
return jsonify(current_gps_data)
if __name__ == '__main__':
# Start GPS monitoring in a separate thread
gps_thread = threading.Thread(target=gps_monitor)
gps_thread.daemon = True # This makes the thread exit when the program does
gps_thread.start()
# Run the Flask web service
app.run(host='0.0.0.0', port=5000)

View File

@ -1,203 +0,0 @@
<?php
# 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
$response = file_get_contents($apiUrl);
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';
}
// 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."<br>";
// // 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
}
?>
<!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>Current Date:<br>".date("F j, Y, g:i:s a")."<p>";
?>
</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>