26 lines
1.0 KiB
PHP
26 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
header('Content-Type: application/json');
|
|
require __DIR__ . '/db.php';
|
|
require __DIR__ . '/mqtt.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$uid = $data['uid'] ?? null;
|
|
$url = $data['url'] ?? null;
|
|
$cmd = $data['cmd'] ?? 'OTA_CHECK';
|
|
|
|
if (!$url) { http_response_code(400); echo json_encode(['error'=>'url_required']); exit; }
|
|
if (!in_array($cmd, ['OTA_CHECK','OTA_URL'], true)) { http_response_code(400); echo json_encode(['error'=>'cmd_invalid']); exit; }
|
|
|
|
$payload = json_encode(['cmd'=>$cmd,'url'=>$url], JSON_UNESCAPED_SLASHES);
|
|
|
|
if ($uid === 'all') {
|
|
$rows = $pdo->query("SELECT uid FROM contadores WHERE blocked=0 AND online=1")->fetchAll(PDO::FETCH_ASSOC);
|
|
foreach ($rows as $r) mqtt_publish("esp/{$r['uid']}/cmd", $payload);
|
|
echo json_encode(['ok'=>1,'sent'=>count($rows)]);
|
|
} else {
|
|
if (!$uid) { http_response_code(400); echo json_encode(['error'=>'uid_required']); exit; }
|
|
mqtt_publish("esp/$uid/cmd", $payload);
|
|
echo json_encode(['ok'=>1]);
|
|
}
|