www/api/commands/enqueue.php
2026-05-11 23:31:48 +01:00

37 lines
1002 B
PHP

<?php
require __DIR__ . '/../db.php';
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['uid'], $input['cmd'])) {
http_response_code(400);
echo json_encode(['ok' => false, 'err' => 'invalid_input']);
exit;
}
$uid = $input['uid'];
$cmd = $input['cmd'];
$params = isset($input['params']) ? json_encode($input['params']) : '{}';
// Confirmar se o device existe
$stmt = $pdo->prepare("SELECT id FROM devices WHERE uid=? LIMIT 1");
$stmt->execute([$uid]);
$dev = $stmt->fetchColumn();
if (!$dev) {
http_response_code(404);
echo json_encode(['ok' => false, 'err' => 'device_not_found']);
exit;
}
// Inserir comando
$stmt = $pdo->prepare("
INSERT INTO commands (device_id, cmd, params, status, queued_at)
VALUES (?, ?, ?, 'pending', NOW())
");
$stmt->execute([$dev, $cmd, $params]);
$id = $pdo->lastInsertId();
echo json_encode(['ok' => true, 'id' => (int)$id], JSON_UNESCAPED_SLASHES);