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-04-23 02:00:52 +02:00
|
|
|
global $topics, $devices, $included, $logFh, $indexFriendlyNames, $devicesRequest;
|
2022-07-10 19:33:19 +02:00
|
|
|
|
|
|
|
$topics[$topic[0]]->lastSeen = time();
|
2022-01-08 11:51:50 +01:00
|
|
|
if ($topic[1] == "bridge")
|
|
|
|
{
|
|
|
|
switch ($topic[2])
|
|
|
|
{
|
|
|
|
case "info":
|
2022-04-07 01:44:17 +02:00
|
|
|
$topics[$topic[0]]->info = json_decode($message->payload, true);
|
2022-01-08 11:51:50 +01:00
|
|
|
break;
|
|
|
|
case "devices":
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__);
|
2022-04-07 01:44:17 +02:00
|
|
|
$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));
|
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-04-23 02:00:52 +02:00
|
|
|
$devicesRequest = false;
|
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-04-07 01:44:17 +02:00
|
|
|
$topics[$topic[0]]->groups = json_decode($message->payload, true);
|
2022-01-08 11:51:50 +01:00
|
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
|
|
|
|
break;
|
|
|
|
case "extensions":
|
2022-04-07 01:44:17 +02:00
|
|
|
$topics[$topic[0]]->extensions = json_decode($message->payload, true);
|
2022-01-08 11:51:50 +01:00
|
|
|
break;
|
|
|
|
case "config":
|
2022-04-07 01:44:17 +02:00
|
|
|
$topics[$topic[0]]->config = json_decode($message->payload, true);
|
2022-01-08 11:51:50 +01:00
|
|
|
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);
|
2022-04-07 01:44:17 +02:00
|
|
|
if (($payloadArray = json_decode($message->payload, true)) === null)
|
|
|
|
{
|
|
|
|
$payloadArray = array($fnTreeEnd => $message->payload);
|
|
|
|
}
|
2022-02-23 10:23:16 +01:00
|
|
|
break;
|
|
|
|
default:
|
2022-04-07 01:44:17 +02:00
|
|
|
$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-04-07 01:44:17 +02:00
|
|
|
$flag = 0;
|
|
|
|
$alert = 0;
|
|
|
|
while (!array_key_exists($fn, $indexFriendlyNames))
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-04-23 02:00:52 +02:00
|
|
|
logger(ERROR, $fn . _(" device does not exists in IndexFriendlyNames: ") . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
|
2022-04-07 01:44:17 +02:00
|
|
|
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");
|
|
|
|
}
|
2022-04-07 01:44:17 +02:00
|
|
|
$flag += 1;
|
2022-04-23 02:00:52 +02:00
|
|
|
}elseif($flag++ >= 5)
|
2022-04-07 01:44:17 +02:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
//}
|
2022-04-07 01:44:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//logger(DEBUG, "PayloadArray = " . print_r($payloadArray,true), __FILE__ . ":" . __LINE__);
|
2022-04-23 02:00:52 +02:00
|
|
|
if ($flag <= 4)
|
2022-02-24 21:46:10 +01:00
|
|
|
{
|
2022-04-07 01:44:17 +02:00
|
|
|
changeDevice($topicName, $fn, $indexFriendlyNames[$fn], $payloadArray);
|
2022-04-23 02:00:52 +02:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
logger(ERROR, $fn . _(" not changed"), __FILE__ . ":" . __LINE__);
|
2022-01-08 11:51:50 +01:00
|
|
|
}
|
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 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-07-10 19:33:19 +02:00
|
|
|
$topics["zigbee2mqtt"]->timeOut = 3;
|
|
|
|
$topics["zigbee2mqtt"]->lastSeen = time();
|
2022-01-08 11:51:50 +01:00
|
|
|
?>
|