From a74c91faeff8ce233d4c4ef32b014fe90195e3b0 Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Tue, 8 Oct 2024 14:04:12 +0200 Subject: [PATCH] add extra files for status exchange --- generator/config.yaml | 8 ++++++++ generator/generator.php | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/generator/config.yaml b/generator/config.yaml index 4775ad8..9b4bfda 100644 --- a/generator/config.yaml +++ b/generator/config.yaml @@ -16,3 +16,11 @@ variables: werkstatt_temp: topic: c3re/huette/klima/werkstatttemp cast: float + +extra_files: + prusaxl_online.json: + topic: dirk/prusa-xl/online + prusaxl_status.json: + topic: dirk/prusa-xl/status + prusaxl_percentage.json: + topic: dirk/prusa-xl/percentage \ No newline at end of file diff --git a/generator/generator.php b/generator/generator.php index e67f6d1..f518a52 100755 --- a/generator/generator.php +++ b/generator/generator.php @@ -14,7 +14,16 @@ $c = new \Mosquitto\Client(); if (isset($conf["mqtt"]["user"])) { $c->setCredentials($conf["mqtt"]["user"], $conf["mqtt"]["password"]); } -$c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) { +$c->onMessage(function (\Mosquitto\Message $message) use ( + &$data, + $conf, + &$extraDataMapper +) { + if (isset($extraDataMapper[$message->topic])) { + $file = $extraDataMapper[$message->topic]; + file_put_contents("public/$file", $message->payload); + } + if (!isset($data[$message->topic])) { return; } @@ -74,6 +83,7 @@ $c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) { }); $c->connect($conf["mqtt"]["host"]); $data = []; +$extraDataMapper = []; foreach ($conf["variables"] as $name => $variable) { $data[$variable["topic"]] = [ "value" => null, @@ -83,5 +93,10 @@ foreach ($conf["variables"] as $name => $variable) { ]; $c->subscribe($variable["topic"], 2); } +foreach ($conf["extra_files"] as $fileName => $node) { + $topic=$node["topic"]; + $c->subscribe($topic, 2); + $extraDataMapper[$topic] = $fileName; +} $c->loopForever();