satellitenhimmel gefixt

This commit is contained in:
Eskimue
2026-04-05 15:11:37 +02:00
parent 5dfc805915
commit 11bd9ac840
2 changed files with 213 additions and 27 deletions
+70 -27
View File
@@ -39,6 +39,54 @@ function h(?string $value): string
return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}
function getPythonCandidates(): array
{
if (PHP_OS_FAMILY === 'Windows') {
return ['python', 'py'];
}
return ['/usr/bin/python3', '/usr/bin/python', 'python3', 'python'];
}
function runPythonCommand(string $scriptPath, array $args): array
{
putenv('PYTHONUTF8=1');
putenv('PYTHONIOENCODING=utf-8');
$escapedArguments = array_map('escapeshellarg', array_merge([$scriptPath], $args));
$lastError = 'Python konnte nicht gestartet werden.';
foreach (getPythonCandidates() as $pythonBinary) {
$command = $pythonBinary . ' ' . implode(' ', $escapedArguments) . ' 2>&1';
$output = [];
$resultCode = 0;
exec($command, $output, $resultCode);
$joined = trim(implode("\n", $output));
$decoded = json_decode($joined, true);
if (is_array($decoded)) {
return [
'ok' => (bool) ($decoded['ok'] ?? false),
'data' => $decoded,
'raw' => $joined,
'command' => $command,
];
}
if ($joined !== '') {
$lastError = $joined;
} elseif ($resultCode !== 0) {
$lastError = 'Fehlercode ' . $resultCode . ' bei ' . $pythonBinary;
}
}
return [
'ok' => false,
'error' => $lastError,
];
}
// ── Satelliten aus DB laden ──────────────────────────────────────────────────
function tleChecksum(string $line): int
@@ -291,34 +339,29 @@ if (!empty($satellitePayload) && !empty($defaultLocation['latitude'])) {
$windowStart = $dayStart->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ATOM);
$windowEnd = $dayEnd->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ATOM);
$scriptPath = realpath(__DIR__ . '/py/api.py');
$satellitesJson = json_encode($satellitePayload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$args = [
'/usr/bin/python',
escapeshellarg($scriptPath),
escapeshellarg('satellite_passes'),
escapeshellarg((string)$lat),
escapeshellarg((string)$lon),
escapeshellarg((string)$elev),
escapeshellarg($tz),
escapeshellarg($windowStart),
escapeshellarg($windowEnd),
escapeshellarg($satellitesJson),
];
$command = implode(' ', $args) . ' 2>&1';
$output = [];
$code = 0;
exec($command, $output, $code);
$joined = implode('', $output);
$decoded = json_decode($joined, true);
if (is_array($decoded) && ($decoded['ok'] ?? false)) {
$pythonResult = $decoded;
$scriptPath = realpath(__DIR__ . '/py/api.py');
if ($scriptPath === false) {
$pythonError = 'Das Python-Skript für den Satellitenhimmel wurde nicht gefunden.';
} else {
$pythonError = $joined ?: 'Leere Antwort von Python.';
$satellitesJson = json_encode($satellitePayload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$satellitesPayload = base64_encode((string)$satellitesJson);
$pythonCall = runPythonCommand($scriptPath, [
'satellite_passes',
(string)$lat,
(string)$lon,
(string)$elev,
$tz,
$windowStart,
$windowEnd,
$satellitesPayload,
]);
if ($pythonCall['ok'] ?? false) {
$pythonResult = $pythonCall['data'];
} else {
$pythonError = $pythonCall['raw'] ?? $pythonCall['error'] ?? 'Leere Antwort von Python.';
}
}
}
?>