<?php
// trigger_led.php
header('Content-Type: application/json');

$drink = $_GET['drink'] ?? null;
if (!$drink) {
    echo json_encode(['status' => 'error', 'message' => 'No drink specified']);
    exit;
}

$config = require 'config_dispenser.php';
$drink_config = $config['drinks'][$drink] ?? null;

if ($drink_config) {
    $device_ip = $config['device_ip'];
    $led_id = $drink_config['id'];
    $duration = $drink_config['led_time'];

    $target_url = "http://$device_ip:8088/led?id=$led_id&czas=$duration";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_exec($ch);
    curl_close($ch);

    echo json_encode(['status' => 'success']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid drink']);
}
?>