setUsername($cfg['username']) ->setPassword($cfg['password']) ->setUseTls(true) ->setTlsCertificateAuthorityFile('/etc/mosquitto/certs/chain.pem') ->setConnectTimeout(3); $client = new MqttClient( $cfg['host'], $cfg['port'], 'web-devices-' . getmypid() ); $client->connect($settings, true); $client->publish($topic, json_encode($payload), 1); $client->disconnect(); } catch (Throwable $e) { error_log("MQTT publish error: " . $e->getMessage()); } } // ===================================================== // APROVAR — desbloqueia e manda REJOIN via MQTT // ===================================================== if (isset($_GET['approve'])) { $uid = $_GET['approve']; $pdo->prepare(" UPDATE contadores SET blocked=0 WHERE uid=? ")->execute([$uid]); // REJOIN para o ESP fazer auth imediatamente mqtt_publish($mqttCfg, "esp/$uid/cmd", ['cmd' => 'REJOIN']); header("Location: devices.php"); exit; } // ===================================================== // REJEITAR / REMOVER // ===================================================== if (isset($_GET['reject'])) { $uid = $_GET['reject']; $pdo->prepare(" DELETE FROM contadores WHERE uid=? ")->execute([$uid]); header("Location: devices.php"); exit; } // ===================================================== // BLOQUEAR / DESBLOQUEAR // ===================================================== if (isset($_GET['toggle'])) { $uid = $_GET['toggle']; $pdo->prepare(" UPDATE contadores SET blocked = 1 - blocked WHERE uid=? ")->execute([$uid]); header("Location: devices.php"); exit; } // ===================================================== // DESPROMOVER — volta a pendente // ===================================================== if (isset($_GET['demote'])) { $uid = $_GET['demote']; $pdo->prepare(" UPDATE contadores SET blocked=1, auth_ok=0 WHERE uid=? ")->execute([$uid]); header("Location: devices.php"); exit; } // ===================================================== // QUERY // ===================================================== $pending = $pdo->query(" SELECT uid, note, last_seen, blocked, auth_ok, TIMESTAMPDIFF(SECOND, last_seen, NOW()) AS ago FROM contadores WHERE blocked=1 AND auth_ok=0 ORDER BY last_seen DESC ")->fetchAll(); $approved = $pdo->query(" SELECT uid, note, last_seen, online, blocked, auth_ok, current_broker, COALESCE(brightness, 128) AS brightness, TIMESTAMPDIFF(SECOND, last_seen, NOW()) AS ago FROM contadores WHERE blocked=0 OR auth_ok=1 ORDER BY online DESC, last_seen DESC ")->fetchAll(); ?>
| UID | NOTA | ÚLTIMO HELLO | AÇÕES |
|---|---|---|---|
| = htmlspecialchars($r['uid']) ?> | = htmlspecialchars($r['note'] ?? '') ?> | = $lastSeen ?> |
| UID | NOTA | ESTADO | BROKER | ÚLTIMO | BRILHO | AÇÕES |
|---|---|---|---|---|---|---|
| = htmlspecialchars($r['uid']) ?> | = htmlspecialchars($r['note'] ?? '') ?> | = $state ?> | = $broker ?> | = $lastSeen ?> |
>
= $bri ?>
|