persist data

master
Dirk Heilig 2024-09-23 15:08:48 +02:00
parent fc0ac95e98
commit 2a07084f9e
1 changed files with 18 additions and 2 deletions

View File

@ -24,7 +24,15 @@ $ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1) ? 1 : 0;
$mqtt = new Mosquitto\Client($mqttClientId); $mqtt = new Mosquitto\Client($mqttClientId);
$data = null;
if (is_file("/www/metrics/raw")) {
$data = json_decode(strval(file_get_contents("/www/metrics/raw")), true);
}
if (!is_array($data)) {
$data = []; $data = [];
}
$mqtt->setCredentials($mqttUser, $mqttPass); $mqtt->setCredentials($mqttUser, $mqttPass);
$usedConfig = [ $usedConfig = [
@ -355,6 +363,14 @@ function output(): void
JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE
) )
); );
file_put_contents(
"/www/metrics/raw",
json_encode(
$data,
JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE
)
);
rename("/www/metrics/new.json", "/www/metrics/metrics.json"); rename("/www/metrics/new.json", "/www/metrics/metrics.json");
rename("/www/metrics/new.prom", "/www/metrics/index.prom"); rename("/www/metrics/new.prom", "/www/metrics/index.prom");
// return $prom; // return $prom;