add extra files for status exchange

master
Dirk Heilig 2024-10-08 14:04:12 +02:00
parent e3039a4944
commit a74c91faef
2 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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();