diff --git a/flows/pomp/index.php b/flows/pomp/index.php index 75e8836..4de7ddb 100644 --- a/flows/pomp/index.php +++ b/flows/pomp/index.php @@ -1,181 +1,130 @@ $_POST['assetId'] ?? '', - 'assetName' => $_POST['assetName'] ?? '', - 'param1' => $_POST['param1'] ?? '', - 'param2' => $_POST['param2'] ?? '' + 'assetId' => $_POST['assetId'] ?? '', + 'assetName' => $_POST['assetName'] ?? '', + 'param1' => $_POST['param1'] ?? '', + 'param2' => $_POST['param2'] ?? '' ]; -$deployResponse = ''; -$newFileName = ''; -$envUrl = ''; +// Node-RED omgevingen +$envFile = __DIR__ . '/../../settings/environments.json'; +$environments = getNodeRedEnvironments($envFile); +$selectedEnv = $_POST['environment'] ?? ''; -// Alert tonen als template versie hoger is dan laatste deploy +// Alert template-versie $newVersionAlert = checkNewTemplateVersionAlert($templatePath, $deployDir); -// Deploy knop verwerken -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy']) && $selectedEnv) { - $templateVersion = getVersionFromTemplate($templatePath); - - $placeholders = [ - 'id' => $parameters['assetId'], - 'name' => $parameters['assetName'], - 'version' => $templateVersion, - 'param1' => $parameters['param1'], - 'param2' => $parameters['param2'] - ]; - - $flowJson = renderTemplate($templatePath, $placeholders); - $newFileName = generateNextDeployFileName($templateVersion); - file_put_contents($deployDir.'/'.$newFileName, $flowJson); - - // Zoek de URL van de geselecteerde omgeving - foreach ($environments as $env) { - if (($env['ip'] ?? '') === $selectedEnv) { - $envUrl = $env['url'] ?? ''; - break; - } - } - - if ($envUrl) { - $deployResponse = deployFlowToUrl($deployDir.'/'.$newFileName, $envUrl); - } else { - $deployResponse = 'Geselecteerde omgeving niet gevonden of URL ontbreekt!'; - } -} - -// Overzicht van deploys met versies +// Overzicht deploys $deploys = getDeploysWithVersions($deployDir); -/** - * Deploy naar specifieke Node-RED URL - */ -function deployFlowToUrl(string $flowPath, string $url): string { - if (!file_exists($flowPath)) return "Flowbestand niet gevonden: $flowPath"; +$deployResponse = ''; +$newFileName = ''; +$envUrl = ''; +$successMessage = ''; - $flowJson = file_get_contents($flowPath); +// Deploy verwerken +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy'])) { + $deployResult = deployFlow($parameters, $templatePath, $deployDir, $environments, $selectedEnv); - $ch = curl_init(rtrim($url, '/') . '/flows'); // Zorg dat er geen dubbele slashes komen - curl_setopt_array($ch, [ - CURLOPT_POST => true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => ['Content-Type: application/json'], - CURLOPT_POSTFIELDS => $flowJson, - ]); - $response = curl_exec($ch); - $error = curl_error($ch); - curl_close($ch); - - return $error ?: $response; + if ($deployResult['success']) { + $deployResponse = $deployResult['response']; + $newFileName = $deployResult['file']; + $envUrl = $deployResult['envUrl']; + $successMessage = renderAlert($deployResult['message'], 'success'); + } else { + $successMessage = renderAlert($deployResult['message'], 'danger'); + } } ?>
-
-

Pomp flow

-
+
+

Pomp flow

+
- - - - + + + ".htmlspecialchars($deployResponse).""; ?> - - - $newFileName gedeployed naar $envUrl!", 'success') ?> -
- +
+
+
Parameters voor deploy
+
+
+ + +
- -
-
-
Parameters voor deploy
+
+ + +
- -
- - +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
-
- - -
- -
- - -
- -
- - -
- -
- -
- - -
-
- -
-
-
Overzicht van deploys
- -
- - - - - - - - - - - - - - - - - -
BestandVersieBekijk
- Bekijk -
-
- -

Geen deploys gevonden.

- -
-
diff --git a/flows/pomp/template.json.tpl b/flows/pomp/template.json.tpl index 598cab1..9df5d61 100644 --- a/flows/pomp/template.json.tpl +++ b/flows/pomp/template.json.tpl @@ -1,59 +1,60 @@ [ - { - "id": "inject1", - "type": "inject", - "name": "Start – {{name}} v{{version}}", - "props": [ { "p": "payload" } ], - "repeat": "", - "crontab": "", - "once": false, - "onceDelay": 0.1, - "topic": "", - "payload": "{{id}}", - "payloadType": "str", - "wires": [["function1"]], - "version": 9 - }, - { - "id": "function1", - "type": "function", - "name": "Verwerk asset data v{{version}}", - "func": "msg.payload = { \"asset_id\": \"{{id}}\", \"asset_name\": \"{{name}}\", \"version\": {{version}}, \"timestamp\": Date.now() }; return msg;", - "outputs": 1, - "wires": [["change1","debug1","http1"]] - }, - { - "id": "change1", - "type": "change", - "name": "Voeg status toe v{{version}}", - "rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ], - "wires": [["debug1"]] - }, - { - "id": "http1", - "type": "http request", - "name": "Stuur naar API v{{version}}", - "method": "POST", - "ret": "txt", - "url": "http://localhost:3000/api/asset", - "tls": "", - "persist": false, - "proxy": "", - "authType": "", - "wires": [["debug1"]] - }, - { - "id": "debug1", - "type": "debug", - "name": "Output debug v{{version}}", - "active": true, - "tosidebar": true, - "console": false, - "tostatus": false, - "complete": "payload", - "targetType": "msg", - "statusVal": "", - "statusType": "auto", - "wires": [] - } +{ +"id": "inject1", +"type": "inject", +"name": "Start – {{name}} v{{version}}", +"props": [ { "p": "payload" } ], +"repeat": "", +"crontab": "", +"once": false, +"onceDelay": 0.1, +"topic": "", +"payload": "{{id}}", +"payloadType": "str", +"wires": [["function1"]], +"version": 1, +"url": "http://{{ip}}:3000/api/asset" +}, +{ +"id": "function1", +"type": "function", +"name": "Verwerk asset data v{{version}}", +"func": "msg.payload = { \"asset_id\": \"{{id}}\", \"asset_name\": \"{{name}}\", \"version\": {{version}}, \"timestamp\": Date.now() }; return msg;", +"outputs": 1, +"wires": [["change1","debug1","http1"]] +}, +{ +"id": "change1", +"type": "change", +"name": "Voeg status toe v{{version}}", +"rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ], +"wires": [["debug1"]] +}, +{ +"id": "http1", +"type": "http request", +"name": "Stuur naar API v{{version}}", +"method": "POST", +"ret": "txt", +"url": "http://{{ip}}:3000/api/asset", +"tls": "", +"persist": false, +"proxy": "", +"authType": "", +"wires": [["debug1"]] +}, +{ +"id": "debug1", +"type": "debug", +"name": "Output debug v{{version}}", +"active": true, +"tosidebar": true, +"console": false, +"tostatus": false, +"complete": "payload", +"targetType": "msg", +"statusVal": "", +"statusType": "auto", +"wires": [] +} ] diff --git a/flows/pomp/version0_202510050852.json b/flows/pomp/version0_202510050852.json deleted file mode 100644 index 06d7405..0000000 Binary files a/flows/pomp/version0_202510050852.json and /dev/null differ diff --git a/flows/pomp/version8_202510050842.json b/flows/pomp/version8_202510050842.json deleted file mode 100644 index 3e73f21..0000000 --- a/flows/pomp/version8_202510050842.json +++ /dev/null @@ -1,59 +0,0 @@ -[ - { - "id": "inject1", - "type": "inject", - "name": "Start – 1234 v8", - "props": [ { "p": "payload" } ], - "repeat": "", - "crontab": "", - "once": false, - "onceDelay": 0.1, - "topic": "", - "payload": "123", - "payloadType": "str", - "wires": [["function1"]], - "version": 8 - }, - { - "id": "function1", - "type": "function", - "name": "Verwerk asset data v8", - "func": "msg.payload = { \"asset_id\": \"123\", \"asset_name\": \"1234\", \"version\": 8, \"timestamp\": Date.now() }; return msg;", - "outputs": 1, - "wires": [["change1","debug1","http1"]] - }, - { - "id": "change1", - "type": "change", - "name": "Voeg status toe v8", - "rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ], - "wires": [["debug1"]] - }, - { - "id": "http1", - "type": "http request", - "name": "Stuur naar API v8", - "method": "POST", - "ret": "txt", - "url": "http://localhost:3000/api/asset", - "tls": "", - "persist": false, - "proxy": "", - "authType": "", - "wires": [["debug1"]] - }, - { - "id": "debug1", - "type": "debug", - "name": "Output debug v8", - "active": true, - "tosidebar": true, - "console": false, - "tostatus": false, - "complete": "payload", - "targetType": "msg", - "statusVal": "", - "statusType": "auto", - "wires": [] - } -] diff --git a/include/include_functions.php b/include/include_functions.php index c37c96a..5fd83f1 100644 --- a/include/include_functions.php +++ b/include/include_functions.php @@ -1,30 +1,38 @@ true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => ['Content-Type: application/json'], - CURLOPT_POSTFIELDS => $flowJson, - ]); - $response = curl_exec($ch); - $error = curl_error($ch); - curl_close($ch); - - return $error ?: $response; +/** + * Verkrijg een overzicht van alle Node-RED omgevingen + * + * @param string $envFile Pad naar JSON-bestand met omgevingen + * @return array + */ +function getNodeRedEnvironments(string $envFile): array { + $json = @file_get_contents($envFile); + if (!$json) return []; + $data = json_decode($json, true); + return is_array($data) ? $data : []; } -// Haal alle versie-bestanden op in een map +/** + * Vind alle versioned JSON-bestanden in een directory + * + * @param string $dir + * @return array + */ function findVersionedJsonFilesInDir(string $dir): array { $matches = []; foreach (scandir($dir) as $file) { @@ -32,35 +40,56 @@ function findVersionedJsonFilesInDir(string $dir): array { $matches[] = ['name' => $file]; } } - usort($matches, fn($a,$b) => strcmp($a['name'],$b['name'])); + usort($matches, fn($a, $b) => strcmp($a['name'], $b['name'])); return $matches; } -// Vind het laatst gedeployde bestand +/** + * Vind het laatst gedeployde bestand + * + * @param string $dir + * @return string|null + */ function findLatestDeployFile(string $dir): ?string { $files = findVersionedJsonFilesInDir($dir); return !empty($files) ? end($files)['name'] : null; } -// Haal versie uit een JSON-bestand +/** + * Haal versie uit een JSON-bestand + * + * @param string $filePath + * @return 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; } -// Haal versie uit een template +/** + * Haal versie uit een template + * + * @param string $templatePath + * @return 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; } -// Controleer of de template een hogere versie heeft dan de laatste deploy +/** + * Controleer of de template een hogere versie heeft dan de laatste deploy + * + * @param string $templatePath + * @param string $deployDir + * @return string|null + */ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string { - $templateVersion = getVersionFromTemplate($templatePath); + $templateVersion = getVersionFromTemplate($templatePath); $latestDeployFile = findLatestDeployFile($deployDir); - $latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0; + $latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0; if ($templateVersion > $latestVersion) { return "
@@ -71,12 +100,23 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): return null; } -// Genereer de bestandsnaam voor een nieuwe deploy +/** + * Genereer de bestandsnaam voor een nieuwe deploy + * + * @param int $version + * @return string + */ function generateNextDeployFileName(int $version): string { return "version{$version}_".date('YmdHi').".json"; } -// Template renderen met placeholders +/** + * Render een template met placeholders + * + * @param string $templatePath + * @param array $placeholders + * @return string + */ function renderTemplate(string $templatePath, array $placeholders): string { $content = file_get_contents($templatePath); foreach ($placeholders as $key => $value) { @@ -85,26 +125,133 @@ function renderTemplate(string $templatePath, array $placeholders): string { return $content; } -// Render een standaard alert +/** + * Render een standaard alert + * + * @param string $message + * @param string $type + * @return string + */ function renderAlert(string $message, string $type = 'info'): string { return "
$message
"; } -// Verkrijg een overzicht van alle deploys inclusief versies +/** + * Verkrijg een overzicht van alle deploys inclusief versies + * + * @param string $dir + * @return array + */ function getDeploysWithVersions(string $dir): array { $files = findVersionedJsonFilesInDir($dir); return array_map(function($f) use ($dir) { return [ - 'name' => $f['name'], + 'name' => $f['name'], 'version' => getVersionFromJson($dir.'/'.$f['name']) ]; }, array_reverse($files)); } -function getNodeRedEnvironments(string $envFile = '/environments.json'): array { - $json = @file_get_contents($envFile); // @ om warnings te onderdrukken - if (!$json) return []; - $data = json_decode($json, true); - return is_array($data) ? $data : []; +// ------------------------------------------------------ +// *** DEPLOY FUNCTIES *** +// ------------------------------------------------------ + +/** + * Deploy een JSON-flowbestand naar een Node-RED URL via POST + * + * @param string $flowPath Pad naar flow JSON + * @param string $url Node-RED URL + * @return string HTTP status code of foutmelding + */ +function deployFlowToUrl(string $flowPath, string $url): string { + if (!file_exists($flowPath)) { + return ''; + } + + $flowJson = file_get_contents($flowPath); + + $ch = curl_init(rtrim($url, '/') . '/flows'); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => ['Content-Type: application/json'], + CURLOPT_POSTFIELDS => $flowJson, + ]); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + return $httpCode; } +/** + * Deploy een flow naar een geselecteerde Node-RED omgeving + * + * @param array $parameters Asset parameters + * @param string $templatePath Pad naar template JSON + * @param string $deployDir Deploy directory + * @param array $environments Beschikbare Node-RED omgevingen + * @param string $selectedEnv Geselecteerde omgeving (IP) + * @return array Resultaat van de deploy + */ +function deployFlow(array $parameters, string $templatePath, string $deployDir, array $environments, string $selectedEnv): array { + $result = [ + 'success' => false, + 'message' => '', + 'file' => null, + 'envUrl' => null, + 'response' => null + ]; + + if (empty($selectedEnv)) { + $result['message'] = 'Geen omgeving geselecteerd!'; + return $result; + } + + // Template-versie ophalen + $templateVersion = getVersionFromTemplate($templatePath); + + // URL van geselecteerde omgeving + $envUrl = ''; + foreach ($environments as $env) { + if (($env['ip'] ?? '') === $selectedEnv) { + $envUrl = $env['ip']; + break; + } + } + + if (!$envUrl) { + $result['message'] = 'Omgeving niet gevonden of URL ontbreekt!'; + return $result; + } + + // Placeholders invullen + $placeholders = [ + 'id' => $parameters['assetId'] ?? '', + 'name' => $parameters['assetName'] ?? '', + 'version' => $templateVersion, + 'param1' => $parameters['param1'] ?? '', + 'param2' => $parameters['param2'] ?? '', + 'ip' => $envUrl + ]; + + // Template renderen en deploy-bestand schrijven + $flowJson = renderTemplate($templatePath, $placeholders); + $newFileName = generateNextDeployFileName($templateVersion); + $deployFile = $deployDir . '/' . $newFileName; + file_put_contents($deployFile, $flowJson); + + // Deploy naar Node-RED uitvoeren + $deployResponse = deployFlowToUrl($deployFile, $envUrl); + + $result['success'] = true; + $result['message'] = "Nieuwe versie $newFileName gedeployed naar $envUrl!"; + $result['file'] = $newFileName; + $result['envUrl'] = $envUrl; + $result['response'] = $deployResponse; + + return $result; +} diff --git a/include/include_header.php b/include/include_header.php index 2ec8d52..3c9868e 100644 --- a/include/include_header.php +++ b/include/include_header.php @@ -1,5 +1,5 @@ @@ -34,7 +34,7 @@