that_one_nigga_you_k

front end pra várias placas

Jun 26th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pt-br">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Painel de Controle dos ESP32</title>
  7.     <style>
  8.         body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f0f2f5; margin: 0; padding: 2rem; }
  9.         h1 { color: #1c1e21; text-align: center; }
  10.         #device-container { display: flex; flex-wrap: wrap; gap: 2rem; justify-content: center; }
  11.         .device-card { background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); padding: 1.5rem; width: 340px; text-align: center; }
  12.         .device-card h3 { margin-top: 0; font-size: 1.1rem; color: #050505; word-wrap: break-word; }
  13.         .device-card img { width: 320px; height: 240px; border-radius: 8px; background-color: #e4e6eb; object-fit: cover; }
  14.         .device-card button { margin-top: 1rem; padding: 0.75rem 1.5rem; font-size: 1rem; font-weight: 600; border-radius: 8px; border: none; cursor: pointer; color: white; transition: background-color 0.3s; width: 100%; }
  15.         .flash-on { background-color: #f5b400; }
  16.         .flash-off { background-color: #38a169; }
  17.         #loading-message { text-align: center; font-size: 1.2rem; color: #65676b; }
  18.     </style>
  19. </head>
  20. <body>
  21.  
  22.     <h1>Painel de Controle dos ESP32</h1>
  23.     <div id="device-container">
  24.         <p id="loading-message">Carregando dispositivos...</p>
  25.     </div>
  26.  
  27.     <script>
  28.         const deviceContainer = document.getElementById('device-container');
  29.         const loadingMessage = document.getElementById('loading-message');
  30.         const managedDevices = {};
  31.  
  32.         async function fetchDevices() {
  33.             try {
  34.                 // Confirma que a busca é no endpoint correto '/devices'
  35.                 const response = await fetch('/devices');
  36.                 if (!response.ok) {
  37.                     loadingMessage.textContent = "Erro ao buscar dispositivos do servidor.";
  38.                     return;
  39.                 }
  40.                
  41.                 const deviceIds = await response.json();
  42.  
  43.                 if (deviceIds.length > 0) {
  44.                     loadingMessage.style.display = 'none';
  45.                 } else {
  46.                     loadingMessage.textContent = "Nenhuma placa registrada. Ligue um ESP32 e aguarde.";
  47.                 }
  48.  
  49.                 for (const id of deviceIds) {
  50.                     if (!managedDevices[id]) {
  51.                         createDeviceCard(id);
  52.                         managedDevices[id] = {
  53.                             imageInterval: setInterval(() => refreshImage(id), 5000)
  54.                         };
  55.                         refreshImage(id);
  56.                         updateFlashButton(id);
  57.                     }
  58.                 }
  59.  
  60.             } catch (error) {
  61.                 console.error("Erro ao buscar dispositivos:", error);
  62.                 loadingMessage.textContent = "Erro de conexão ao servidor.";
  63.             }
  64.         }
  65.  
  66.         function createDeviceCard(id) {
  67.             const card = document.createElement('div');
  68.             card.className = 'device-card';
  69.             card.id = `device-${id}`;
  70.  
  71.             card.innerHTML = `
  72.                 <h3>Dispositivo ID: ${id}</h3>
  73.                 <img id="img-${id}" alt="Aguardando foto..." src="">
  74.                 <button id="btn-${id}">Carregando...</button>
  75.             `;
  76.            
  77.             deviceContainer.appendChild(card);
  78.  
  79.             const flashButton = document.getElementById(`btn-${id}`);
  80.             flashButton.addEventListener('click', () => toggleFlash(id));
  81.         }
  82.  
  83.         function refreshImage(id) {
  84.             const imgElement = document.getElementById(`img-${id}`);
  85.             if (imgElement) {
  86.                 imgElement.src = `/latest-photo/${id}?t=${new Date().getTime()}`;
  87.             }
  88.         }
  89.  
  90.         async function updateFlashButton(id) {
  91.             const button = document.getElementById(`btn-${id}`);
  92.             if (!button) return;
  93.            
  94.             try {
  95.                 const response = await fetch(`/command/${id}`);
  96.                 const state = await response.text();
  97.                
  98.                 if (state === 'FLASH_ON') {
  99.                     button.textContent = 'Desligar Flash';
  100.                     button.className = 'flash-on';
  101.                 } else {
  102.                     button.textContent = 'Ligar Flash';
  103.                     button.className = 'flash-off';
  104.                 }
  105.             } catch (e) {
  106.                 button.textContent = 'Erro de estado';
  107.             }
  108.         }
  109.  
  110.         async function toggleFlash(id) {
  111.             const button = document.getElementById(`btn-${id}`);
  112.             const isCurrentlyOn = button.classList.contains('flash-on');
  113.             const newCommand = isCurrentlyOn ? 'FLASH_OFF' : 'FLASH_ON';
  114.  
  115.             try {
  116.                 await fetch(`/command/${id}`, {
  117.                     method: 'POST',
  118.                     headers: { 'Content-Type': 'application/json' },
  119.                     body: JSON.stringify({ command: newCommand })
  120.                 });
  121.                 updateFlashButton(id);
  122.             } catch (e) {
  123.                 console.error(`Erro ao enviar comando para ${id}:`, e);
  124.             }
  125.         }
  126.  
  127.         // Inicia a aplicação
  128.         fetchDevices();
  129.         setInterval(fetchDevices, 10000);
  130.     </script>
  131.  
  132. </body>
  133. </html>
  134.  
Add Comment
Please, Sign In to add comment