add extra files for status exchange
parent
e3039a4944
commit
a74c91faef
|
@ -16,3 +16,11 @@ variables:
|
||||||
werkstatt_temp:
|
werkstatt_temp:
|
||||||
topic: c3re/huette/klima/werkstatttemp
|
topic: c3re/huette/klima/werkstatttemp
|
||||||
cast: float
|
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
|
|
@ -14,7 +14,16 @@ $c = new \Mosquitto\Client();
|
||||||
if (isset($conf["mqtt"]["user"])) {
|
if (isset($conf["mqtt"]["user"])) {
|
||||||
$c->setCredentials($conf["mqtt"]["user"], $conf["mqtt"]["password"]);
|
$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])) {
|
if (!isset($data[$message->topic])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -74,6 +83,7 @@ $c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) {
|
||||||
});
|
});
|
||||||
$c->connect($conf["mqtt"]["host"]);
|
$c->connect($conf["mqtt"]["host"]);
|
||||||
$data = [];
|
$data = [];
|
||||||
|
$extraDataMapper = [];
|
||||||
foreach ($conf["variables"] as $name => $variable) {
|
foreach ($conf["variables"] as $name => $variable) {
|
||||||
$data[$variable["topic"]] = [
|
$data[$variable["topic"]] = [
|
||||||
"value" => null,
|
"value" => null,
|
||||||
|
@ -83,5 +93,10 @@ foreach ($conf["variables"] as $name => $variable) {
|
||||||
];
|
];
|
||||||
$c->subscribe($variable["topic"], 2);
|
$c->subscribe($variable["topic"], 2);
|
||||||
}
|
}
|
||||||
|
foreach ($conf["extra_files"] as $fileName => $node) {
|
||||||
|
$topic=$node["topic"];
|
||||||
|
$c->subscribe($topic, 2);
|
||||||
|
$extraDataMapper[$topic] = $fileName;
|
||||||
|
}
|
||||||
$c->loopForever();
|
$c->loopForever();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue