Upload files to "flows/pomp"

This commit is contained in:
2025-10-05 09:12:59 +00:00
parent e7bfb663f9
commit 9e544fcdc1
4 changed files with 299 additions and 0 deletions

181
flows/pomp/index.php Normal file
View File

@@ -0,0 +1,181 @@
<?php
include '../../include/include_header.php';
include '../../include/include_functions.php';
$templatePath = __DIR__ . '/template.json.tpl';
$deployDir = __DIR__;
// --- Node-RED omgevingen ophalen ---
$envFile = __DIR__ . '/../../environments.json'; // Pas eventueel pad aan
$environments = getNodeRedEnvironments($envFile);
$selectedEnv = $_POST['environment'] ?? '';
// --- Parameters ophalen of defaults ---
$parameters = [
'assetId' => $_POST['assetId'] ?? '',
'assetName' => $_POST['assetName'] ?? '',
'param1' => $_POST['param1'] ?? '',
'param2' => $_POST['param2'] ?? ''
];
$deployResponse = '';
$newFileName = '';
$envUrl = '';
// Alert tonen als template versie hoger is dan laatste deploy
$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
$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";
$flowJson = file_get_contents($flowPath);
$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;
}
?>
<main class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 text-dark">Pomp flow</h1>
</div>
<!-- Alert nieuwe template-versie -->
<?php if ($newVersionAlert): ?>
<?= $newVersionAlert ?>
<?php endif; ?>
<!-- Deploy response -->
<?php if ($deployResponse): ?>
<?= renderAlert("Nieuwe versie <strong>$newFileName</strong> gedeployed naar <strong>$envUrl</strong>!", 'success') ?>
<pre class="bg-light border p-2 rounded small"><?= htmlspecialchars($deployResponse) ?></pre>
<?php endif; ?>
<!-- Parameters form + deploy knop -->
<div class="card shadow-sm mb-4">
<div class="card-body">
<h5 class="card-title mb-3">Parameters voor deploy</h5>
<form method="post">
<div class="mb-3">
<label for="assetId" class="form-label">Asset ID</label>
<input type="text" class="form-control" id="assetId" name="assetId" value="<?= htmlspecialchars($parameters['assetId']) ?>" required>
</div>
<div class="mb-3">
<label for="assetName" class="form-label">Asset Naam</label>
<input type="text" class="form-control" id="assetName" name="assetName" value="<?= htmlspecialchars($parameters['assetName']) ?>" required>
</div>
<div class="mb-3">
<label for="param1" class="form-label">Extra parameter 1</label>
<input type="text" class="form-control" id="param1" name="param1" value="<?= htmlspecialchars($parameters['param1']) ?>">
</div>
<div class="mb-3">
<label for="param2" class="form-label">Extra parameter 2</label>
<input type="text" class="form-control" id="param2" name="param2" value="<?= htmlspecialchars($parameters['param2']) ?>">
</div>
<div class="mb-3">
<label for="environment" class="form-label">Selecteer Node-RED omgeving</label>
<select class="form-select" id="environment" name="environment" required>
<option value="">-- Kies een omgeving --</option>
<?php if (!empty($environments)): ?>
<?php foreach($environments as $env): ?>
<option value="<?= htmlspecialchars($env['ip'] ?? '') ?>" <?= $selectedEnv === ($env['ip'] ?? '') ? 'selected' : '' ?>>
<?= htmlspecialchars($env['name'] ?? 'Onbekend') ?> (<?= htmlspecialchars($env['ip'] ?? '') ?>)
</option>
<?php endforeach; ?>
<?php else: ?>
<option value="">Geen omgevingen gevonden</option>
<?php endif; ?>
</select>
</div>
<button type="submit" name="deploy" class="btn btn-primary">Nieuwe versie deployen</button>
</form>
</div>
</div>
<!-- Overzicht deploys -->
<div class="card shadow-sm">
<div class="card-body">
<h5 class="card-title mb-3">Overzicht van deploys</h5>
<?php if (!empty($deploys)): ?>
<div class="table-responsive">
<table class="table table-hover table-bordered mb-0">
<thead class="table-light">
<tr>
<th>Bestand</th>
<th>Versie</th>
<th>Bekijk</th>
</tr>
</thead>
<tbody>
<?php foreach ($deploys as $d): ?>
<tr>
<td><?= htmlspecialchars($d['name']) ?></td>
<td><?= htmlspecialchars($d['version']) ?></td>
<td>
<a href="<?= htmlspecialchars($d['name']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary">Bekijk</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="text-muted mb-0">Geen deploys gevonden.</p>
<?php endif; ?>
</div>
</div>
</main>

View File

@@ -0,0 +1,59 @@
[
{
"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": []
}
]

Binary file not shown.

View File

@@ -0,0 +1,59 @@
[
{
"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": []
}
]