| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- <?php
- declare(strict_types=1);
- require_once dirname(__DIR__) . '/src/bootstrap.php';
- header('Content-Type: text/html; charset=UTF-8');
- $auth = app_admin_auth();
- $auth->start();
- $configRepository = app_config_repository();
- $message = null;
- $messageType = 'success';
- $rawConfigText = null;
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- if ($action === 'login') {
- $success = $auth->login(
- trim((string) ($_POST['username'] ?? '')),
- (string) ($_POST['password'] ?? '')
- );
- if ($success) {
- app_redirect(app_url('/admin/'));
- }
- $message = 'Login fehlgeschlagen. Bitte Zugangsdaten prüfen.';
- $messageType = 'error';
- }
- if ($action === 'save_config' && $auth->isAuthenticated()) {
- try {
- $config = $configRepository->getConfig();
- $newPassword = trim((string) ($_POST['admin_password'] ?? ''));
- $config['app'] = [
- 'name' => trim((string) ($_POST['app_name'] ?? 'Getränkeautomat Monitor')),
- 'timezone' => trim((string) ($_POST['timezone'] ?? 'Europe/Berlin')),
- 'dashboard_refresh_seconds' => max(5, (int) ($_POST['dashboard_refresh_seconds'] ?? 15)),
- 'default_from_email' => trim((string) ($_POST['default_from_email'] ?? 'monitor@example.local')),
- 'base_path' => app_normalize_base_path((string) ($_POST['base_path'] ?? '')),
- ];
- $config['api'] = [
- 'bearer_token' => trim((string) ($_POST['bearer_token'] ?? 'change-me-token')),
- ];
- $config['admin'] = [
- 'username' => trim((string) ($_POST['admin_username'] ?? 'admin')),
- 'password_hash' => $newPassword !== ''
- ? password_hash($newPassword, PASSWORD_BCRYPT)
- : (string) ($config['admin']['password_hash'] ?? ''),
- ];
- $config['alerts'] = [
- 'webhooks' => normalizeWebhooks($_POST['webhooks'] ?? []),
- 'emails' => normalizeEmails($_POST['emails'] ?? []),
- ];
- $config['machines'] = normalizeMachines($_POST['machines'] ?? []);
- $configRepository->saveConfig($config);
- $message = 'Konfiguration gespeichert.';
- $messageType = 'success';
- } catch (Throwable $exception) {
- $message = 'Konfiguration konnte nicht gespeichert werden: ' . $exception->getMessage();
- $messageType = 'error';
- }
- }
- if ($action === 'save_raw_config' && $auth->isAuthenticated()) {
- $rawConfigText = (string) ($_POST['raw_config'] ?? '');
- try {
- $decoded = json_decode($rawConfigText, true, 512, JSON_THROW_ON_ERROR);
- if (!is_array($decoded) || array_is_list($decoded)) {
- throw new JsonException('Die JSON-Config muss ein JSON-Objekt sein.');
- }
- $configRepository->saveConfig($decoded);
- $message = 'JSON-Config gespeichert.';
- $messageType = 'success';
- $rawConfigText = encodeConfigForEditor($decoded);
- } catch (Throwable $exception) {
- $message = 'JSON-Config ist ungültig: ' . $exception->getMessage();
- $messageType = 'error';
- }
- }
- }
- $config = $configRepository->getConfig();
- $rawConfigText ??= encodeConfigForEditor($config);
- if (!$auth->isAuthenticated()) {
- renderLogin($message, $messageType);
- exit;
- }
- renderAdmin($config, $rawConfigText, $message, $messageType);
- function renderLogin(?string $message, string $messageType): void
- {
- ?>
- <!DOCTYPE html>
- <html lang="de">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Adminpanel Login</title>
- <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
- </head>
- <body>
- <main class="auth-page">
- <section class="auth-card">
- <p class="eyebrow">Adminbereich</p>
- <h1>Konfiguration sichern</h1>
- <p>Logge dich mit den statischen Zugangsdaten aus der JSON-Config ein.</p>
- <?php if ($message !== null): ?>
- <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
- <?php endif; ?>
- <form method="post">
- <input type="hidden" name="action" value="login">
- <label>
- Benutzername
- <input type="text" name="username" required>
- </label>
- <label>
- Passwort
- <input type="password" name="password" required>
- </label>
- <button class="button button--primary" type="submit">Einloggen</button>
- <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Zurück zum Dashboard</a>
- </form>
- </section>
- </main>
- </body>
- </html>
- <?php
- }
- function renderAdmin(array $config, string $rawConfigText, ?string $message, string $messageType): void
- {
- ?>
- <!DOCTYPE html>
- <html lang="de">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Adminpanel</title>
- <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
- </head>
- <body>
- <main class="admin-page">
- <section class="admin-shell">
- <div class="admin-header">
- <div>
- <p class="eyebrow">Adminpanel</p>
- <h1>Konfiguration der Automaten</h1>
- <p>Hier werden API-Token, Zugangsdaten, Fächer und Alarmwege direkt in der JSON-Config gepflegt.</p>
- </div>
- <div class="inline-actions">
- <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Dashboard</a>
- <a class="button button--secondary" href="<?= htmlspecialchars(app_url('/admin/logout.php'), ENT_QUOTES) ?>">Logout</a>
- </div>
- </div>
- <?php if ($message !== null): ?>
- <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
- <?php endif; ?>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>JSON-Config direkt bearbeiten</h2>
- <p>Die komplette Konfiguration kann hier als Klartext angezeigt und direkt als JSON gespeichert werden.</p>
- </div>
- </div>
- <form method="post" class="admin-form">
- <input type="hidden" name="action" value="save_raw_config">
- <label>
- Konfiguration als JSON
- <textarea class="config-textarea" name="raw_config" spellcheck="false"><?= htmlspecialchars($rawConfigText, ENT_QUOTES) ?></textarea>
- </label>
- <div class="section-actions">
- <button class="button button--primary" type="submit">JSON speichern</button>
- </div>
- </form>
- </section>
- <form method="post" class="admin-form" id="admin-form">
- <input type="hidden" name="action" value="save_config">
- <section class="form-section">
- <h2>Allgemein</h2>
- <div class="field-grid">
- <label>
- App-Name
- <input type="text" name="app_name" value="<?= htmlspecialchars((string) ($config['app']['name'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Zeitzone
- <input type="text" name="timezone" value="<?= htmlspecialchars((string) ($config['app']['timezone'] ?? 'Europe/Berlin'), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Auto-Refresh Sekunden
- <input type="number" min="5" name="dashboard_refresh_seconds" value="<?= (int) ($config['app']['dashboard_refresh_seconds'] ?? 15) ?>" required>
- </label>
- <label>
- Absender für Email
- <input type="email" name="default_from_email" value="<?= htmlspecialchars((string) ($config['app']['default_from_email'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Basis-Pfad
- <input type="text" name="base_path" value="<?= htmlspecialchars((string) ($config['app']['base_path'] ?? ''), ENT_QUOTES) ?>" placeholder="/auswertung">
- </label>
- </div>
- <p class="field-help">Leer lassen für den Domain-Root. Für Unterordner z. B. <code>/auswertung</code> eintragen.</p>
- </section>
- <section class="admin-grid">
- <section class="form-section">
- <h2>API</h2>
- <label>
- Bearer-Token
- <input type="text" name="bearer_token" value="<?= htmlspecialchars((string) ($config['api']['bearer_token'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <p class="field-help">ESP32-Clients senden dieses Token im Header <code>Authorization: Bearer ...</code>.</p>
- </section>
- <section class="form-section">
- <h2>Adminzugang</h2>
- <div class="field-grid">
- <label>
- Benutzername
- <input type="text" name="admin_username" value="<?= htmlspecialchars((string) ($config['admin']['username'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Neues Passwort
- <input type="password" name="admin_password" placeholder="Leer lassen, um es nicht zu ändern">
- </label>
- </div>
- </section>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Webhooks</h2>
- <p>Mehrere Ziele sind möglich. Header können als JSON hinterlegt werden.</p>
- <p>Webhooks sind Alert-Ziele, wenn eine Alarmstufe bei einem Fach erreicht wurde</p>
- </div>
- <button class="button button--secondary" type="button" data-add="webhook">Webhook hinzufügen</button>
- </div>
- <div class="repeat-stack" id="webhook-stack">
- <?php foreach (($config['alerts']['webhooks'] ?? []) as $index => $webhook): ?>
- <?= renderWebhookRow((int) $index, $webhook) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Email-Empfänger</h2>
- <p>Emails werden über die Serverkonfiguration von <code>mail()</code> versendet.</p>
- </div>
- <button class="button button--secondary" type="button" data-add="email">Email-Empfänger hinzufügen</button>
- </div>
- <div class="repeat-stack" id="email-stack">
- <?php foreach (($config['alerts']['emails'] ?? []) as $index => $email): ?>
- <?= renderEmailRow((int) $index, $email) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Automaten und Fächer</h2>
- <p>Jeder Automat enthält beliebig viele Fächer, die jeweils genau einem Sensor zugeordnet sind.</p>
- </div>
- <button class="button button--secondary" type="button" data-add="machine">Automat hinzufügen</button>
- </div>
- <div class="repeat-stack" id="machine-stack">
- <?php foreach (($config['machines'] ?? []) as $machineIndex => $machine): ?>
- <?= renderMachineRow((int) $machineIndex, $machine) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <div class="section-actions">
- <button class="button button--primary" type="submit">Konfiguration speichern</button>
- </div>
- </form>
- </section>
- </main>
- <template id="tpl-webhook"><?= renderWebhookRow('__INDEX__', []) ?></template>
- <template id="tpl-email"><?= renderEmailRow('__INDEX__', []) ?></template>
- <template id="tpl-machine"><?= renderMachineRow('__MACHINE__', []) ?></template>
- <template id="tpl-slot"><?= renderSlotRow('__MACHINE__', '__SLOT__', []) ?></template>
- <script>
- const webhookStack = document.getElementById('webhook-stack');
- const emailStack = document.getElementById('email-stack');
- const machineStack = document.getElementById('machine-stack');
- const renderTemplate = (id, replacements) => {
- let html = document.getElementById(id).innerHTML;
- Object.entries(replacements).forEach(([needle, value]) => {
- html = html.replaceAll(needle, value);
- });
- return html;
- };
- const countChildren = (node, selector) => node.querySelectorAll(selector).length;
- const toggleTelegramFields = (row) => {
- const typeSelect = row.querySelector('select[name*="[type]"]');
- const telegramFields = row.querySelector('.telegram-fields');
- if (typeSelect && telegramFields) {
- telegramFields.style.display = typeSelect.value === 'telegram' ? '' : 'none';
- }
- };
- document.addEventListener('click', (event) => {
- const addType = event.target.getAttribute('data-add');
- const removeType = event.target.getAttribute('data-remove');
- if (addType === 'webhook') {
- const index = countChildren(webhookStack, '[data-webhook-row]');
- webhookStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-webhook', { '__INDEX__': index }));
- }
- if (addType === 'email') {
- const index = countChildren(emailStack, '[data-email-row]');
- emailStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-email', { '__INDEX__': index }));
- }
- if (addType === 'machine') {
- const index = countChildren(machineStack, '[data-machine-row]');
- machineStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-machine', { '__MACHINE__': index, '__SLOT__': 0 }));
- }
- if (addType === 'slot') {
- const machineIndex = event.target.getAttribute('data-machine-index');
- const container = document.querySelector(`[data-slot-stack="${machineIndex}"]`);
- const slotIndex = countChildren(container, '[data-slot-row]');
- container.insertAdjacentHTML('beforeend', renderTemplate('tpl-slot', {
- '__MACHINE__': machineIndex,
- '__SLOT__': slotIndex
- }));
- }
- if (removeType) {
- const row = event.target.closest(`[data-${removeType}-row]`);
- if (row) {
- row.remove();
- }
- }
- });
- document.addEventListener('change', (event) => {
- if (event.target.matches('select[name*="[type]"]')) {
- const row = event.target.closest('[data-webhook-row]');
- if (row) {
- toggleTelegramFields(row);
- }
- }
- });
- // Initialize existing webhook rows
- document.querySelectorAll('[data-webhook-row]').forEach(toggleTelegramFields);
- </script>
- </body>
- </html>
- <?php
- }
- function renderWebhookRow(int|string $index, array $webhook): string
- {
- $headers = $webhook['headers'] ?? [];
- $headerJson = json_encode($headers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- $type = $webhook['type'] ?? 'generic';
- $botToken = $webhook['bot_token'] ?? '';
- $chatId = $webhook['chat_id'] ?? '';
- ob_start();
- ?>
- <div class="repeat-card" data-webhook-row>
- <div class="panel__header">
- <h4>Webhook</h4>
- <button class="button button--danger" type="button" data-remove="webhook">Entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- Label
- <input type="text" name="webhooks[<?= $index ?>][label]" value="<?= htmlspecialchars((string) ($webhook['label'] ?? ''), ENT_QUOTES) ?>" placeholder="z.B. lager-webhook">
- </label>
- <label>
- Name
- <input type="text" name="webhooks[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($webhook['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Typ
- <select name="webhooks[<?= $index ?>][type]">
- <option value="generic" <?= $type === 'generic' ? 'selected' : '' ?>>Generic</option>
- <option value="telegram" <?= $type === 'telegram' ? 'selected' : '' ?>>Telegram</option>
- </select>
- </label>
- <label>
- URL
- <input type="url" name="webhooks[<?= $index ?>][url]" value="<?= htmlspecialchars((string) ($webhook['url'] ?? ''), ENT_QUOTES) ?>" placeholder="https://example.com/webhook">
- </label>
- <label>
- Aktiv
- <select name="webhooks[<?= $index ?>][enabled]">
- <option value="1" <?= !empty($webhook['enabled']) ? 'selected' : '' ?>>Ja</option>
- <option value="0" <?= empty($webhook['enabled']) ? 'selected' : '' ?>>Nein</option>
- </select>
- </label>
- </div>
-
- <div class="telegram-fields" style="<?= $type === 'telegram' ? '' : 'display:none;' ?>">
- <div class="field-grid">
- <label>
- Bot Token (nur für Telegram)
- <input type="text" name="webhooks[<?= $index ?>][bot_token]" value="<?= htmlspecialchars((string) $botToken, ENT_QUOTES) ?>" placeholder="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11">
- </label>
- <label>
- Chat ID (nur für Telegram)
- <input type="text" name="webhooks[<?= $index ?>][chat_id]" value="<?= htmlspecialchars((string) $chatId, ENT_QUOTES) ?>" placeholder="-100123456789">
- </label>
- </div>
- </div>
-
- <label>
- Header als JSON-Objekt (für Generic)
- <textarea name="webhooks[<?= $index ?>][headers_json]"><?= htmlspecialchars((string) $headerJson, ENT_QUOTES) ?></textarea>
- </label>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderEmailRow(int|string $index, array $email): string
- {
- ob_start();
- ?>
- <div class="repeat-card" data-email-row>
- <div class="panel__header">
- <h4>Email-Empfänger</h4>
- <button class="button button--danger" type="button" data-remove="email">Entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- Label
- <input type="text" name="emails[<?= $index ?>][label]" value="<?= htmlspecialchars((string) ($email['label'] ?? ''), ENT_QUOTES) ?>" placeholder="z.B. lager-team">
- </label>
- <label>
- Name
- <input type="text" name="emails[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($email['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Adresse
- <input type="email" name="emails[<?= $index ?>][address]" value="<?= htmlspecialchars((string) ($email['address'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Aktiv
- <select name="emails[<?= $index ?>][enabled]">
- <option value="1" <?= !empty($email['enabled']) ? 'selected' : '' ?>>Ja</option>
- <option value="0" <?= empty($email['enabled']) ? 'selected' : '' ?>>Nein</option>
- </select>
- </label>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderMachineRow(int|string $machineIndex, array $machine): string
- {
- ob_start();
- ?>
- <div class="repeat-card" data-machine-row>
- <div class="panel__header">
- <div>
- <h4>Automat</h4>
- <p>Fächer können direkt darunter verwaltet werden.</p>
- </div>
- <div class="inline-actions">
- <button class="button button--secondary" type="button" data-add="slot" data-machine-index="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">Fach hinzufügen</button>
- <button class="button button--danger" type="button" data-remove="machine">Automat entfernen</button>
- </div>
- </div>
- <div class="field-grid">
- <label>
- ID
- <input type="text" name="machines[<?= $machineIndex ?>][id]" value="<?= htmlspecialchars((string) ($machine['id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Name
- <input type="text" name="machines[<?= $machineIndex ?>][name]" value="<?= htmlspecialchars((string) ($machine['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Standort
- <input type="text" name="machines[<?= $machineIndex ?>][location]" value="<?= htmlspecialchars((string) ($machine['location'] ?? ''), ENT_QUOTES) ?>">
- </label>
- </div>
- <div class="repeat-stack" data-slot-stack="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">
- <?php foreach (($machine['slots'] ?? []) as $slotIndex => $slot): ?>
- <?= renderSlotRow($machineIndex, (int) $slotIndex, $slot) ?>
- <?php endforeach; ?>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderSlotRow(int|string $machineIndex, int|string $slotIndex, array $slot): string
- {
- ob_start();
- ?>
- <div class="slot-editor" data-slot-row>
- <div class="panel__header">
- <h5>Fach</h5>
- <button class="button button--danger" type="button" data-remove="slot">Fach entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- Sensor-ID
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][sensor_id]" value="<?= htmlspecialchars((string) ($slot['sensor_id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Label
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][label]" value="<?= htmlspecialchars((string) ($slot['label'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Produktname
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][product_name]" value="<?= htmlspecialchars((string) ($slot['product_name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Voll-Distanz in mm
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][full_distance_mm]" value="<?= htmlspecialchars((string) ($slot['full_distance_mm'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Leer-Distanz in mm
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][empty_distance_mm]" value="<?= htmlspecialchars((string) ($slot['empty_distance_mm'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Distanz pro Flasche
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][distance_per_unit]" value="<?= htmlspecialchars((string) ($slot['distance_per_unit'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Alarm unter Bestand(Anzahl)
- <input type="number" step="1" min="0" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][alert_below_units]" value="<?= htmlspecialchars((string) ($slot['alert_below_units'] ?? ''), ENT_QUOTES) ?>">
- </label>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function normalizeWebhooks(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $label = trim((string) ($row['label'] ?? ''));
- $url = trim((string) ($row['url'] ?? ''));
- if ($label === '' && $url === '') {
- continue;
- }
- $headers = json_decode((string) ($row['headers_json'] ?? '{}'), true);
- $type = trim((string) ($row['type'] ?? 'generic'));
-
- $item = [
- 'label' => $label,
- 'name' => trim((string) ($row['name'] ?? '')),
- 'type' => $type,
- 'url' => $url,
- 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
- 'headers' => is_array($headers) ? $headers : [],
- ];
-
- // Add Telegram-specific fields
- if ($type === 'telegram') {
- $item['bot_token'] = trim((string) ($row['bot_token'] ?? ''));
- $item['chat_id'] = trim((string) ($row['chat_id'] ?? ''));
- }
-
- $items[] = $item;
- }
- return array_values($items);
- }
- function normalizeEmails(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $label = trim((string) ($row['label'] ?? ''));
- $address = trim((string) ($row['address'] ?? ''));
- if ($label === '' && $address === '') {
- continue;
- }
- $items[] = [
- 'label' => $label,
- 'name' => trim((string) ($row['name'] ?? '')),
- 'address' => $address,
- 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
- ];
- }
- return array_values($items);
- }
- function normalizeMachines(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $id = trim((string) ($row['id'] ?? ''));
- $name = trim((string) ($row['name'] ?? ''));
- if ($id === '' && $name === '') {
- continue;
- }
- $slots = [];
- foreach (($row['slots'] ?? []) as $slot) {
- $sensorId = trim((string) ($slot['sensor_id'] ?? ''));
- if ($sensorId === '') {
- continue;
- }
- $slots[] = [
- 'sensor_id' => $sensorId,
- 'label' => trim((string) ($slot['label'] ?? $sensorId)),
- 'product_name' => trim((string) ($slot['product_name'] ?? '')),
- 'full_distance_mm' => (float) ($slot['full_distance_mm'] ?? 0),
- 'empty_distance_mm' => (float) ($slot['empty_distance_mm'] ?? 0),
- 'distance_per_unit' => max(0.01, (float) ($slot['distance_per_unit'] ?? 1)),
- 'alert_below_units' => max(0, (int) ($slot['alert_below_units'] ?? 0)),
- ];
- }
- $items[] = [
- 'id' => $id,
- 'name' => $name,
- 'location' => trim((string) ($row['location'] ?? '')),
- 'slots' => $slots,
- ];
- }
- return array_values($items);
- }
- function parseIdList(string $value): array
- {
- $parts = array_map('trim', explode(',', $value));
- return array_values(array_filter($parts, static fn (string $item): bool => $item !== ''));
- }
- function encodeConfigForEditor(array $config): string
- {
- $json = json_encode(
- $config,
- JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
- );
- return $json === false ? '{}' : $json;
- }
|