Screenshot rein und Uhrzeit aus GPS

This commit is contained in:
2026-05-25 18:13:47 +02:00
parent 11d1a16281
commit 92c51b81ce
6 changed files with 551 additions and 202 deletions
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
import datetime
import errno
import fcntl
import glob
@@ -12,6 +13,7 @@ import subprocess
import termios
import threading
import time
from zoneinfo import ZoneInfo
PORT = 8082
EXOMY_CONTAINER = 'exomy_autostart'
@@ -606,6 +608,74 @@ def cold_start_gps_receiver():
return device_path
def read_gps_diagnostics_from_ros():
command = [
'docker', 'exec', EXOMY_CONTAINER, 'bash', '-lc',
"source /opt/ros/melodic/setup.bash && "
"source /root/exomy_ws/devel/setup.bash && "
"timeout 8s rostopic echo -n 1 /gps/diagnostics_json"
]
stdout, stderr, returncode = run_command(command)
if returncode != 0 or not stdout:
raise RuntimeError('Keine GPS-Diagnosedaten verfügbar')
raw_text = stdout.strip()
lines = [line.strip() for line in raw_text.splitlines() if line.strip()]
if not lines:
raise RuntimeError('GPS-Diagnosedaten sind leer')
if lines[0].startswith('data:'):
payload_text = lines[0][5:].strip()
if payload_text.startswith("'") and payload_text.endswith("'"):
payload_text = payload_text[1:-1]
payload_text = payload_text.encode('utf-8').decode('unicode_escape')
else:
payload_text = lines[0]
try:
return json.loads(payload_text)
except json.JSONDecodeError as exc:
raise RuntimeError(f'GPS-Diagnosedaten konnten nicht gelesen werden: {exc}')
def parse_gps_datetime():
diagnostics = read_gps_diagnostics_from_ros()
utc_date = str(diagnostics.get('utc_date') or '').strip()
utc_time = str(diagnostics.get('utc_time') or '').strip()
if not utc_date or not utc_time:
raise RuntimeError('GPS liefert aktuell keine UTC-Zeit')
combined = f'{utc_date} {utc_time}'
for pattern in ('%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S'):
try:
return datetime.datetime.strptime(combined, pattern)
except ValueError:
continue
raise RuntimeError(f'GPS-Zeitformat unbekannt: {combined}')
def get_local_timezone():
try:
with open('/etc/timezone', 'r', encoding='utf-8') as handle:
timezone_name = handle.read().strip()
if timezone_name:
return ZoneInfo(timezone_name)
except (FileNotFoundError, OSError, ValueError):
pass
return datetime.datetime.now().astimezone().tzinfo or datetime.timezone.utc
def set_system_time_from_gps():
gps_dt = parse_gps_datetime()
gps_utc = gps_dt.replace(tzinfo=datetime.timezone.utc)
local_time = gps_utc.astimezone(get_local_timezone())
time_text = local_time.strftime('%Y-%m-%d %H:%M:%S')
stdout, stderr, returncode = run_command(['timedatectl', 'set-time', time_text])
if returncode != 0:
raise RuntimeError(stderr or stdout or 'Systemzeit konnte nicht gesetzt werden')
return time_text
def stop_exomy_container():
stdout, _, returncode = run_command(['docker', 'stop', '-t', '2', EXOMY_CONTAINER])
return returncode == 0, stdout
@@ -1420,6 +1490,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
'handler': restart_gps_node,
'status': 'GPS-Node wird neu gestartet'
},
'/api/set-time-from-gps': {
'handler': set_system_time_from_gps,
'status': 'Pi-Zeit wird aus GPS gesetzt'
},
'/api/restart-camera': {
'handler': restart_camera_service,
'status': 'Kameradienst wird neu gestartet'