2022-01-08 11:51:50 +01:00
|
|
|
<?php
|
2022-01-08 23:45:38 +01:00
|
|
|
|
2022-03-13 22:33:26 +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
|
|
|
|
2022-03-13 22:33:26 +01:00
|
|
|
function zigbee2mqttCallback($topic, $message)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-02-24 21:46:10 +01:00
|
|
|
global $topics, $devices, $included, $logFh, $indexFriendlyNames;
|
2022-02-23 10:23:16 +01:00
|
|
|
|
2022-01-08 11:51:50 +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__);
|
2022-01-08 11:51:50 +01:00
|
|
|
$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));
|
2022-01-08 11:51:50 +01:00
|
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
|
2022-02-07 16:58:42 +01:00
|
|
|
getDevicesValues($topic[0]);
|
2022-01-08 11:51:50 +01:00
|
|
|
break;
|
|
|
|
case "groups":
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG,_("Inserting zigbee groups in DB"), __FILE__ . ":" . __LINE__);
|
2022-01-08 11:51:50 +01:00
|
|
|
$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)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
|
|
|
$topic = explode ("/", $message->topic, 2); // get topic name
|
2022-02-23 10:23:16 +01:00
|
|
|
$topicName = $topic[0];
|
|
|
|
$fn = $topic[1];
|
2022-02-24 21:46:10 +01:00
|
|
|
$device = new device;
|
2022-01-08 11:51:50 +01:00
|
|
|
$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)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
/*case "l1":
|
2022-02-23 10:23:16 +01:00
|
|
|
case "l2":
|
2022-03-28 00:40:34 +02:00
|
|
|
$fnTreeEnd = "state_" . $fnTreeEnd;*/
|
2022-03-06 01:30:33 +01:00
|
|
|
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-01-08 11:51:50 +01:00
|
|
|
}
|
2022-02-23 10:23:16 +01:00
|
|
|
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
|
2022-02-24 21:46:10 +01:00
|
|
|
if (!array_key_exists($fn, $indexFriendlyNames[$topicName]))
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-03-15 22:49:13 +01:00
|
|
|
mkDevicesDB($topicName, $message->payload);
|
2022-02-23 10:23:16 +01:00
|
|
|
logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
|
2022-02-24 21:46:10 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(DEBUG, "PayloadArray = " . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
|
2022-02-24 21:46:10 +01:00
|
|
|
changeDevice($topicName, $fn, $indexFriendlyNames[$topicName][$fn], $payloadArray);
|
2022-01-08 11:51:50 +01:00
|
|
|
}
|
2022-02-24 21:46:10 +01:00
|
|
|
|
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 11:51:50 +01:00
|
|
|
}
|
2022-01-08 23:45:38 +01:00
|
|
|
};
|
|
|
|
|
2022-03-13 22:33:26 +01:00
|
|
|
$topics["zigbee2mqtt"]->callback = "zigbee2mqttCallback";
|
2022-02-23 10:23:16 +01:00
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
?>
|