From 17b514505b1d44ff5f0d93fb1609f2289da459e0 Mon Sep 17 00:00:00 2001 From: pimmoerman Date: Thu, 9 Oct 2025 16:20:58 +0200 Subject: [PATCH] nieuwe versie 0.1 --- flows/pomp/index.php | 37 -------------- flows/pomp/template.json.tpl | 2 +- include/include_functions.php | 94 +++++++++++++++++++++++++++++------ settings/index.php | 2 + 4 files changed, 81 insertions(+), 54 deletions(-) diff --git a/flows/pomp/index.php b/flows/pomp/index.php index 4de7ddb..45903c3 100644 --- a/flows/pomp/index.php +++ b/flows/pomp/index.php @@ -5,43 +5,6 @@ $deployDir = __DIR__; include __DIR__ . '/../../include/include_header.php'; include __DIR__ . '/../../include/include_functions.php'; -// --- Parameters ophalen of defaults --- -$parameters = [ - 'assetId' => $_POST['assetId'] ?? '', - 'assetName' => $_POST['assetName'] ?? '', - 'param1' => $_POST['param1'] ?? '', - 'param2' => $_POST['param2'] ?? '' -]; - -// Node-RED omgevingen -$envFile = __DIR__ . '/../../settings/environments.json'; -$environments = getNodeRedEnvironments($envFile); -$selectedEnv = $_POST['environment'] ?? ''; - -// Alert template-versie -$newVersionAlert = checkNewTemplateVersionAlert($templatePath, $deployDir); - -// Overzicht deploys -$deploys = getDeploysWithVersions($deployDir); - -$deployResponse = ''; -$newFileName = ''; -$envUrl = ''; -$successMessage = ''; - -// Deploy verwerken -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy'])) { - $deployResult = deployFlow($parameters, $templatePath, $deployDir, $environments, $selectedEnv); - - if ($deployResult['success']) { - $deployResponse = $deployResult['response']; - $newFileName = $deployResult['file']; - $envUrl = $deployResult['envUrl']; - $successMessage = renderAlert($deployResult['message'], 'success'); - } else { - $successMessage = renderAlert($deployResult['message'], 'danger'); - } -} ?>
diff --git a/flows/pomp/template.json.tpl b/flows/pomp/template.json.tpl index 9df5d61..491821a 100644 --- a/flows/pomp/template.json.tpl +++ b/flows/pomp/template.json.tpl @@ -12,7 +12,7 @@ "payload": "{{id}}", "payloadType": "str", "wires": [["function1"]], -"version": 1, +"version": 3, "url": "http://{{ip}}:3000/api/asset" }, { diff --git a/include/include_functions.php b/include/include_functions.php index 5fd83f1..5f3804e 100644 --- a/include/include_functions.php +++ b/include/include_functions.php @@ -14,15 +14,60 @@ // *** DEFAULT SETTINGS FLOW *** // ------------------------------------------------------ +// --- Parameters ophalen of defaults --- +$parameters = [ + 'assetId' => $_POST['assetId'] ?? '', + 'assetName' => $_POST['assetName'] ?? '', + 'param1' => $_POST['param1'] ?? '', + 'param2' => $_POST['param2'] ?? '' +]; + +// Node-RED omgevingen +$envFile = __DIR__ . '/../settings/environments.json'; +$environments = getNodeRedEnvironments($envFile); +$selectedEnv = $_POST['environment'] ?? ''; + +// Alert template-versie +$newVersionAlert = checkNewTemplateVersionAlert($templatePath, $deployDir); + +// Overzicht deploys +$deploys = getDeploysWithVersions($deployDir); + +// Init variabelen +$deployResponse = ''; +$newFileName = ''; +$envUrl = ''; +$successMessage = ''; + +// Deploy verwerken +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy'])) { + $deployResult = deployFlow($parameters, $templatePath, $deployDir, $environments, $selectedEnv); + + if ($deployResult['success']) { + $deployResponse = $deployResult['response']; + $newFileName = $deployResult['file']; + $envUrl = $deployResult['envUrl']; + $successMessage = renderAlert($deployResult['message'], 'success'); + } else { + $successMessage = renderAlert($deployResult['message'], 'danger'); + } +} + +// ------------------------------------------------------ +// *** HELPERS *** +// ------------------------------------------------------ + /** * Verkrijg een overzicht van alle Node-RED omgevingen * * @param string $envFile Pad naar JSON-bestand met omgevingen * @return array */ -function getNodeRedEnvironments(string $envFile): array { +function getNodeRedEnvironments(string $envFile): array +{ $json = @file_get_contents($envFile); if (!$json) return []; + $data = json_decode($json, true); return is_array($data) ? $data : []; } @@ -33,13 +78,15 @@ function getNodeRedEnvironments(string $envFile): array { * @param string $dir * @return array */ -function findVersionedJsonFilesInDir(string $dir): array { +function findVersionedJsonFilesInDir(string $dir): array +{ $matches = []; foreach (scandir($dir) as $file) { if (preg_match('/^version\d+_\d{12}\.json$/', $file)) { $matches[] = ['name' => $file]; } } + usort($matches, fn($a, $b) => strcmp($a['name'], $b['name'])); return $matches; } @@ -50,7 +97,8 @@ function findVersionedJsonFilesInDir(string $dir): array { * @param string $dir * @return string|null */ -function findLatestDeployFile(string $dir): ?string { +function findLatestDeployFile(string $dir): ?string +{ $files = findVersionedJsonFilesInDir($dir); return !empty($files) ? end($files)['name'] : null; } @@ -61,8 +109,10 @@ function findLatestDeployFile(string $dir): ?string { * @param string $filePath * @return int */ -function getVersionFromJson(string $filePath): int { +function getVersionFromJson(string $filePath): int +{ if (!file_exists($filePath)) return 0; + $json = json_decode(file_get_contents($filePath), true); return $json[0]['version'] ?? 0; } @@ -73,8 +123,10 @@ function getVersionFromJson(string $filePath): int { * @param string $templatePath * @return int */ -function getVersionFromTemplate(string $templatePath): int { +function getVersionFromTemplate(string $templatePath): int +{ if (!file_exists($templatePath)) return 0; + preg_match('/"version"\s*:\s*(\d+)/', file_get_contents($templatePath), $m); return $m[1] ?? 0; } @@ -86,10 +138,11 @@ function getVersionFromTemplate(string $templatePath): int { * @param string $deployDir * @return string|null */ -function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string { +function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string +{ $templateVersion = getVersionFromTemplate($templatePath); $latestDeployFile = findLatestDeployFile($deployDir); - $latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0; + $latestVersion = $latestDeployFile ? getVersionFromJson($deployDir . '/' . $latestDeployFile) : 0; if ($templateVersion > $latestVersion) { return "
@@ -97,6 +150,7 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): (Template v$templateVersion > Laatste deploy v$latestVersion)
"; } + return null; } @@ -106,8 +160,9 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): * @param int $version * @return string */ -function generateNextDeployFileName(int $version): string { - return "version{$version}_".date('YmdHi').".json"; +function generateNextDeployFileName(int $version): string +{ + return "version{$version}_" . date('YmdHi') . ".json"; } /** @@ -117,11 +172,13 @@ function generateNextDeployFileName(int $version): string { * @param array $placeholders * @return string */ -function renderTemplate(string $templatePath, array $placeholders): string { +function renderTemplate(string $templatePath, array $placeholders): string +{ $content = file_get_contents($templatePath); foreach ($placeholders as $key => $value) { $content = str_replace("{{{$key}}}", $value, $content); } + return $content; } @@ -132,7 +189,8 @@ function renderTemplate(string $templatePath, array $placeholders): string { * @param string $type * @return string */ -function renderAlert(string $message, string $type = 'info'): string { +function renderAlert(string $message, string $type = 'info'): string +{ return "
$message
"; } @@ -142,12 +200,14 @@ function renderAlert(string $message, string $type = 'info'): string { * @param string $dir * @return array */ -function getDeploysWithVersions(string $dir): array { +function getDeploysWithVersions(string $dir): array +{ $files = findVersionedJsonFilesInDir($dir); - return array_map(function($f) use ($dir) { + + return array_map(function ($f) use ($dir) { return [ 'name' => $f['name'], - 'version' => getVersionFromJson($dir.'/'.$f['name']) + 'version' => getVersionFromJson($dir . '/' . $f['name']) ]; }, array_reverse($files)); } @@ -163,7 +223,8 @@ function getDeploysWithVersions(string $dir): array { * @param string $url Node-RED URL * @return string HTTP status code of foutmelding */ -function deployFlowToUrl(string $flowPath, string $url): string { +function deployFlowToUrl(string $flowPath, string $url): string +{ if (!file_exists($flowPath)) { return '