1
0
moha/topics_callbacks/zigbee2mqtt.php

125 lines
3.8 KiB
PHP
Raw Permalink 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)
{
2022-04-23 02:00:52 +02:00
global $topics, $devices, $included, $logFh, $indexFriendlyNames, $devicesRequest;
2022-09-05 13:47:41 +02:00
logger(DEBUG, _("CallBack Zigbee2mqtt"), __FILE__ . ":" . __LINE__);
$topics[$topic[0]]->lastSeen = time();
if ($topic[1] == "bridge")
{
switch ($topic[2])
{
case "info":
$topics[$topic[0]]->info = json_decode($message->payload, true);
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, true);
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]);
2022-04-23 02:00:52 +02:00
$devicesRequest = false;
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, true);
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
break;
case "extensions":
$topics[$topic[0]]->extensions = json_decode($message->payload, true);
break;
case "config":
$topics[$topic[0]]->config = json_decode($message->payload, true);
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);
if (($payloadArray = json_decode($message->payload, true)) === null)
{
$payloadArray = array($fnTreeEnd => $message->payload);
}
2022-02-23 10:23:16 +01:00
break;
default:
$payloadArray = json_decode($message->payload, true);
}
2022-02-23 10:23:16 +01:00
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
$flag = 0;
$alert = 0;
while (!array_key_exists($fn, $indexFriendlyNames))
{
2022-04-23 02:00:52 +02:00
logger(ERROR, $fn . _(" device does not exists in IndexFriendlyNames: ") . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
if ($flag == 0)
{
logger(INFO, "Reindexing all indexes", __FILE__ . ":" . __LINE__);
mkIndexes();
$flag += 1;
}elseif($flag == 1)
{
logger(INFO, "getting device list from zigbee2mqtt", __FILE__ . ":" . __LINE__);
2022-04-23 02:00:52 +02:00
if ($devicesRequest === false)
{
publish("zigbee2mqtt", array("bridge/devices" => ""), "get");
}
$flag += 1;
2022-04-23 02:00:52 +02:00
}elseif($flag++ >= 5)
{
$flage = 0;
2022-04-23 02:00:52 +02:00
//if ($alert++ > 4)
//{
logger(ERROR, _(" stopping searching for ") . $fn . " payloadArray =>" . print_r($payloadArray, true), __FILE__ . ":" . __LINE__);
break;
//}
}
}
//logger(DEBUG, "PayloadArray = " . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
2022-04-23 02:00:52 +02:00
if ($flag <= 4)
{
changeDevice($topicName, $fn, $indexFriendlyNames[$fn], $payloadArray);
2022-04-23 02:00:52 +02:00
}else
{
logger(ERROR, $fn . _(" not changed"), __FILE__ . ":" . __LINE__);
}
2022-02-23 10:23:16 +01:00
}else
{
2022-04-23 02:00:52 +02:00
logger(DEBUG, _("Zigbee2mqtt doing get or hooks not included !?"), __FILE__ . ":" . __LINE__);
}
2022-01-08 23:45:38 +01:00
};
$topics["zigbee2mqtt"]->callback = "zigbee2mqttCallback";
$topics["zigbee2mqtt"]->timeOut = 3;
$topics["zigbee2mqtt"]->lastSeen = time();
?>