1
0
moha/topics_callbacks/zigbee2mqtt.php

93 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2022-01-08 23:45:38 +01:00
if (!array_key_exists("zigbee2mqtt", $topics)) $topics["zigbee2mqtt"] = new topic;
if (!array_key_exists("zigbee2mqtt", $devices)) $devices["zigbee2mqtt"] = array();
2022-02-23 10:23:16 +01:00
function zigbee2mqttCallback($topic, $message)
{
global $topics, $devices, $included, $logFh, $indexFriendlyNames;
2022-02-23 10:23:16 +01:00
if ($topic[1] == "bridge")
{
switch ($topic[2])
{
case "info":
$topics[$topic[0]]->info = json_decode($message->payload);
break;
case "devices":
2022-01-28 23:05:58 +01:00
logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__);
$topics[$topic[0]]->devices = json_decode($message->payload);
2022-02-23 10:23:16 +01:00
//fwrite($logFh, var_export($topics[$topic[0]]->devices, true));
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
2022-02-07 16:58:42 +01:00
getDevicesValues($topic[0]);
break;
case "groups":
2022-01-28 23:05:58 +01:00
logger(DEBUG,_("Inserting zigbee groups in DB"), __FILE__ . ":" . __LINE__);
$topics[$topic[0]]->groups = json_decode($message->payload);
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
break;
case "extensions":
$topics[$topic[0]]->extensions = json_decode($message->payload);
break;
case "config":
$topics[$topic[0]]->config = json_decode($message->payload);
break;
case "logging":
//TODO
break;
case "state":
$topics[$topic[0]]->state = $message->payload;
break;
default:
break;
};
2022-03-04 22:30:16 +01:00
}elseif (($topic[array_key_last($topic)]) != "get" and $included)
{
$topic = explode ("/", $message->topic, 2); // get topic name
2022-02-23 10:23:16 +01:00
$topicName = $topic[0];
$fn = $topic[1];
$device = new device;
$fnTree = explode ("/" , $topic[1]); // get friendlyname
2022-02-23 10:23:16 +01:00
echo $topicName . " => " . $fn . EOL;
logger(DEBUG, _("end of fnTree = ") . end($fnTree), __FILE__ . ":" . __LINE__);
$fnTreeEnd = end($fnTree);
switch($fnTreeEnd)
{
/*case "l1":
2022-02-23 10:23:16 +01:00
case "l2":
$fnTreeEnd = "state_" . $fnTreeEnd;*/
case "set":
2022-02-23 10:23:16 +01:00
case "mode":
case "contact":
case "availability":
array_pop ($fnTree);
$fn = implode("/", $fnTree);
$fn = implode("/", $fnTree);
$payloadArray = array($fnTreeEnd => $message->payload);
//TODO payload can be a json object
break;
default:
$payloadArray = json_decode($message->payload,true);
}
2022-02-23 10:23:16 +01:00
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
if (!array_key_exists($fn, $indexFriendlyNames[$topicName]))
{
mkDevicesDB($topicName, $message->payload);
2022-02-23 10:23:16 +01:00
logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
}else
{
logger(DEBUG, "PayloadArray = " . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
changeDevice($topicName, $fn, $indexFriendlyNames[$topicName][$fn], $payloadArray);
}
2022-02-23 10:23:16 +01:00
}else
{
2022-03-04 22:30:16 +01:00
logger(DEBUG, _("Zigbee2mqtt doing get or set !?"));
}
2022-01-08 23:45:38 +01:00
};
$topics["zigbee2mqtt"]->callback = "zigbee2mqttCallback";
2022-02-23 10:23:16 +01:00
?>