diff -r ea24b4ce02b1 -r 24749c296a50 www/dbdevices.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/dbdevices.php Tue Apr 30 17:26:41 2024 +0200 @@ -0,0 +1,112 @@ + 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)); +} + + +$response = array( + 'error' => false, + 'msg' => 'Ok', +); + + +if (isset($_POST['update'])) { + + $cmd = "DEVICE PUT " . $_POST['uuid'] . "\r\n"; + $cmd .= "TYPE," . $_POST['type'] . "\r\n"; + $cmd .= "DIRECTION," . $_POST['direction'] . "\r\n"; + $cmd .= "VALUE," . $_POST['value'] . "\r\n"; // Only for outputs + $cmd .= "OFFSET," . $_POST['offset'] . "\r\n"; // Only analog + $cmd .= "PRESENT," . $_POST['present'] . "\r\n"; + $cmd .= "ADDRESS," . $_POST['address'] . "\r\n"; // Not for auto detected + $cmd .= "SUBDEVICE," . $_POST['subdevice'] . "\r\n"; // Not for auto detected + $cmd .= "GPIOPIN," . $_POST['gpiopin'] . "\r\n"; + $cmd .= "DESCRIPTION," . $_POST['description'] . "\r\n"; + $cmd .= "COMMENT," . $_POST['comment'] . "\r\n"; + $cmd .= "."; + $answer = send_cmd($cmd); + $arr = explode("\r\n", $answer); + if (! startsWith($arr[0], "219")) { + $response['error'] = true; + $response['msg'] = $arr[0]; + } + exit(json_encode($response)); + +} else if (isset($_POST['add'])) { + + $answer = send_cmd("DEVICE ADD " . $_POST['type']); + $arr = explode("\r\n", $answer); + if (! startsWith($arr[0], "211")) { + $response['error'] = true; + $response['msg'] = $arr[0]; + } + exit(json_encode($response)); + +} else if (isset($_POST['del'])) { + + $answer = send_cmd("DEVICE DEL " . $_POST['uuid']); + $arr = explode("\r\n", $answer); + if (! startsWith($arr[0], "211")) { + $response['error'] = true; + $response['msg'] = $arr[0]; + } + exit(json_encode($response)); + +} else { + + $answer = send_cmd("DEVICE JSON"); + header("Content-type: application/json"); + + $arr = explode("\r\n", $answer); + if (startsWith($arr[0], "212")) { + echo $arr[1]; + } else { + echo '{}'; + } +} + +?>