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

@ -0,0 +1,16 @@
[Unit]
Description=GPS Monitoring Service
After=network.target
[Service]
Type=simple
WorkingDirectory={{ gps_service_directory }}
ExecStart={{ gps_service_directory }}/venv/bin/python3 {{ gps_service_directory }}/app.py
Restart=always
User=root
Group=root
Environment="PATH={{ gps_service_directory }}/venv/bin"
Environment="VIRTUAL_ENV={{ gps_service_directory }}/venv"
[Install]
WantedBy=multi-user.target

54
archive/python_gps/app.py Normal file
View File

@ -0,0 +1,54 @@
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)