1
0
moha/topics_callbacks/zigbee2mqtt.php
2022-09-05 13:47:41 +02:00

125 lines
3.8 KiB
PHP

<?php
if (!array_key_exists("zigbee2mqtt", $topics)) $topics["zigbee2mqtt"] = new topic;
if (!array_key_exists("zigbee2mqtt", $devices)) $devices["zigbee2mqtt"] = array();
function zigbee2mqttCallback($topic, $message)
{
global $topics, $devices, $included, $logFh, $indexFriendlyNames, $devicesRequest;
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":
logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__);
$topics[$topic[0]]->devices = json_decode($message->payload, true);
//fwrite($logFh, var_export($topics[$topic[0]]->devices, true));
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
getDevicesValues($topic[0]);
$devicesRequest = false;
break;
case "groups":
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;
};
}elseif (($topic[array_key_last($topic)]) != "get" and $included)
{
$topic = explode ("/", $message->topic, 2); // get topic name
$topicName = $topic[0];
$fn = $topic[1];
$device = new device;
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topicName . " => " . $fn . EOL;
logger(DEBUG, _("end of fnTree = ") . end($fnTree), __FILE__ . ":" . __LINE__);
$fnTreeEnd = end($fnTree);
switch($fnTreeEnd)
{
/*case "l1":
case "l2":
$fnTreeEnd = "state_" . $fnTreeEnd;*/
case "set":
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);
}
break;
default:
$payloadArray = json_decode($message->payload, true);
}
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
$flag = 0;
$alert = 0;
while (!array_key_exists($fn, $indexFriendlyNames))
{
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__);
if ($devicesRequest === false)
{
publish("zigbee2mqtt", array("bridge/devices" => ""), "get");
}
$flag += 1;
}elseif($flag++ >= 5)
{
$flage = 0;
//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__);
if ($flag <= 4)
{
changeDevice($topicName, $fn, $indexFriendlyNames[$fn], $payloadArray);
}else
{
logger(ERROR, $fn . _(" not changed"), __FILE__ . ":" . __LINE__);
}
}else
{
logger(DEBUG, _("Zigbee2mqtt doing get or hooks not included !?"), __FILE__ . ":" . __LINE__);
}
};
$topics["zigbee2mqtt"]->callback = "zigbee2mqttCallback";
$topics["zigbee2mqtt"]->timeOut = 3;
$topics["zigbee2mqtt"]->lastSeen = time();
?>