From 853407a0cb430e4e5a525d5b23552703f00d37f9 Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 23 Sep 2024 15:06:15 +0200 Subject: [PATCH 1/2] admin: add capture test for gphoto2 Change-Id: I6f88022ca76469817f3b0d91882660a9cfc3e8b9 --- admin/capturetest/index.php | 75 +++++++++++++++++++++ src/PhotoboothCaptureTest.php | 119 ++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 admin/capturetest/index.php create mode 100644 src/PhotoboothCaptureTest.php diff --git a/admin/capturetest/index.php b/admin/capturetest/index.php new file mode 100644 index 000000000..c3b1b2ac5 --- /dev/null +++ b/admin/capturetest/index.php @@ -0,0 +1,75 @@ +getTitle(); +include PathUtility::getAbsolutePath('admin/components/head.admin.php'); +include PathUtility::getAbsolutePath('admin/helper/index.php'); + +?> +
+
+ +
+
+ + + +

+ translate('test_capture') ?> +

+
+captureCmds as $index => $command) { + // Set filename for each test command + $test->fileName = sprintf('test-%d.jpg', $index + 1); + $test->tmpFile = $test->tmpFolder . DIRECTORY_SEPARATOR . $test->fileName; + + $test->addLog('debug', 'Executing Command #' . ($index + 1), ['command' => $command]); + + // Execute the command + $test->executeCmd($command); + + foreach ($test->logData as $log) { + $level = htmlspecialchars($log['level']); + $message = htmlspecialchars($log['message']); + $context = htmlspecialchars(json_encode($log['context'], JSON_PRETTY_PRINT)); + + echo '
+
' . strtoupper($level) . '
+
' . $message . '
+
' . $context . '
+
'; + } + $test->logData = []; +} + +?> +
+ +
+
+ +tmpFolder = FolderEnum::TEMP->absolute(); + } + + public function addLog(string $level, string $message, array $context = []): void + { + $this->logData[] = [ + 'level' => $level, + 'message' => $message, + 'context' => $context + ]; + } + + /** + * Function to iterate through capture commands and execute them. + */ + public function executeCaptureTests(): void + { + foreach ($this->captureCmds as $index => $command) { + // Set filename for each test command + $this->fileName = sprintf('test-%d.jpg', $index + 1); + $this->tmpFile = $this->tmpFolder . DIRECTORY_SEPARATOR . $this->fileName; + + $this->addLog('debug', 'Executing Command #' . ($index + 1), ['command' => $command]); + + // Execute the command + $this->executeCmd($command); + } + } + + /** + * Function to execute a single command. + * + * @param string $command The command to execute + */ + public function executeCmd(string $command): void + { + // Change directory if using gphoto command + if (substr($command, 0, strlen('gphoto')) === 'gphoto') { + chdir(dirname($this->tmpFile)); + } + + // Prepare the command and redirect stderr to stdout + $cmd = sprintf($command, $this->tmpFile); + $cmd .= ' 2>&1'; + $start_time = hrtime(true); + exec($cmd, $output, $returnValue); + + // Handle command errors + if ($returnValue) { + $this->addLog('error', 'Command failed', [ + 'command' => $command, + 'output' => $output, + 'returnValue' => $returnValue + ]); + return; + } else { + $this->addLog('info', 'Command executed successfully', [ + 'command' => $command, + 'output' => $output + ]); + } + + // Wait for the file to be created, if necessary + $i = 0; + $processingTime = 300; // 30 seconds (300 * 100ms) + while ($i < $processingTime) { + if (file_exists($this->tmpFile)) { + break; + } else { + $i++; + usleep(100000); // Wait 100ms + } + } + + // If the file does not exist, print the error and proceed + if (!file_exists($this->tmpFile)) { + $this->addLog('error', 'File was not created', [ + 'command' => $command, + 'output' => $output, + 'returnValue' => $returnValue + ]); + } + $end_time = hrtime(true); + $execution_time = $end_time - $start_time; + $execution_time_in_seconds = $execution_time / 1e9; + $this->addLog('info', 'Execution time', [ + 'output' => $execution_time_in_seconds . ' seconds' + ]); + } +} From a8a6cd48d18d084549b315db7567bb49430c38c6 Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 23 Sep 2024 23:25:48 +0200 Subject: [PATCH 2/2] prettier Change-Id: I55b867a9e6de42fced72b48cfdb30c7b9c0bd4d8 --- admin/capturetest/index.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/admin/capturetest/index.php b/admin/capturetest/index.php index c3b1b2ac5..de778db79 100644 --- a/admin/capturetest/index.php +++ b/admin/capturetest/index.php @@ -23,9 +23,18 @@ include PathUtility::getAbsolutePath('admin/helper/index.php'); ?> +