- mqtt_listener.php: brightness DB sync, LWT offline, remove 1883 - set_brightness.php e mqtt_publish.php adicionados - comandos.php: tabela de brilho com valor real do ESP - console.php: pausar refresh ao editar notas Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
header('Content-Type: application/json');
|
|
ini_set('display_errors', '0');
|
|
|
|
require_once __DIR__ . '/protecao.php';
|
|
|
|
if (!file_exists(__DIR__ . '/../vendor/autoload.php')) {
|
|
echo json_encode(['ok' => false, 'error' => 'vendor not found: ' . __DIR__ . '/../vendor/autoload.php']);
|
|
exit;
|
|
}
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use PhpMqtt\Client\MqttClient;
|
|
use PhpMqtt\Client\ConnectionSettings;
|
|
|
|
$uid = trim($_POST['uid'] ?? '');
|
|
$value = (int)($_POST['value'] ?? -1);
|
|
|
|
if ($uid === '' || $value < 0 || $value > 255) {
|
|
http_response_code(400);
|
|
echo json_encode(['ok' => false, 'error' => 'parametros invalidos']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$settings = (new ConnectionSettings())
|
|
->setUsername('xupa')
|
|
->setPassword('xupa')
|
|
->setUseTls(true)
|
|
->setTlsSelfSignedAllowed(true)
|
|
->setConnectTimeout(5)
|
|
->setSocketTimeout(3);
|
|
|
|
$mqtt = new MqttClient('mqtt.xupas.mywire.org', 8883, 'web-brightness-' . getmypid());
|
|
$mqtt->connect($settings, false);
|
|
|
|
$payload = json_encode(['cmd' => 'BRIGHTNESS', 'value' => $value]);
|
|
$mqtt->publish("esp/$uid/cmd", $payload, 1, false);
|
|
|
|
$mqtt->disconnect();
|
|
|
|
echo json_encode(['ok' => true, 'uid' => $uid, 'value' => $value]);
|
|
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
|
}
|