<?php
include 'config.php';

header('Content-Type: application/json');

$data = [];

$tvs = mysqli_query($conn, "SELECT * FROM tvs");

exec("adb devices", $output);

while($tv = mysqli_fetch_assoc($tvs)) {

    $ip = $tv['ip_address'];
    $online = false;

    foreach($output as $line){
        if(strpos($line, $ip) !== false){
            $online = true;
            break;
        }
    }

    $data[] = [
        "id" => $tv['id'],
        "adb" => $online ? "online" : "offline",
        "status" => $tv['status']
    ];
}

echo json_encode($data);
