diff --git a/admin/capturetest/index.php b/admin/capturetest/index.php new file mode 100644 index 000000000..de778db79 --- /dev/null +++ b/admin/capturetest/index.php @@ -0,0 +1,86 @@ +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 . '
+
+
'; + } + echo '
'; + $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' + ]); + } +}