--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/getdevice.php Tue Apr 16 16:20:34 2024 +0200 @@ -0,0 +1,65 @@ +<?php + + +function open_socket() +{ + $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + + if (!($sock === false)) { + if (socket_connect($sock, "localhost", 6554)) { + socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0)); + } else { + socket_close($sock); + } + } + return $sock; +} + + +/** + * @param string $command to send to the server. + * @return string with the complete reply from the + * server. This can be a multiline reply. + */ +function send_cmd($command) +{ + $sock = open_socket(); + if ($sock == false) { + return ""; + } + socket_write($sock, $command . "\r\n", 4096); + + $answer = ""; + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + $answer .= $line; + } + socket_close($sock); + + return $answer; +} + + +function startsWith($haystack, $needle) +{ + return !strncmp($haystack, $needle, strlen($needle)); +} + + +if (isset($_GET["uuid"])) + $uuid = $_GET["uuid"]; +else + $uuid = "08eb9507-8b1a-42cc-ac66-9ffcf864dc0c"; + +$answer = send_cmd("DEVICE JSON ".$uuid); +header("Content-type: application/json"); + +$arr = explode("\r\n", $answer); +if (startsWith($arr[0], "213")) { + echo $arr[1]; +} else { + echo '{}'; +} +