90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
|
|
$topics["zigbee2mqtt"] = new topic;
|
|
$devices["zigbee2mqtt"] = array();
|
|
|
|
$topics["zigbee2mqtt"]->callback = function($topic, $message)
|
|
{
|
|
global $topics, $devices, $included, $logFh;
|
|
|
|
if ($topic[1] == "bridge")
|
|
{
|
|
switch ($topic[2])
|
|
{
|
|
case "info":
|
|
$topics[$topic[0]]->info = json_decode($message->payload);
|
|
break;
|
|
case "devices":
|
|
logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__);
|
|
$topics[$topic[0]]->devices = json_decode($message->payload);
|
|
//fwrite($logFh, var_export($topics[$topic[0]]->devices, true));
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
|
|
getDevicesValues($topic[0]);
|
|
break;
|
|
case "groups":
|
|
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;
|
|
};
|
|
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set" and $included)
|
|
{
|
|
$topic = explode ("/", $message->topic, 2); // get topic name
|
|
$topicName = $topic[0];
|
|
$fn = $topic[1];
|
|
$device = null;
|
|
$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 "mode":
|
|
case "contact":
|
|
case "availability":
|
|
array_pop ($fnTree);
|
|
$fn = implode("/", $fnTree);
|
|
$fn = implode("/", $fnTree);
|
|
$payloadArray = array($fnTreeEnd => $message->payload);
|
|
//TODO payl
|
|
$payloadArray = array($fnTreeEnd => $message->payload);
|
|
//TODO payload can be a json object
|
|
break;
|
|
default:
|
|
$payloadArray = json_decode($message->payload,true);
|
|
}
|
|
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
|
|
if ((getDeviceByFriendlyname($device, $topicName, $fn, $payloadArray, true)) === false)
|
|
{
|
|
logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
|
|
}
|
|
print_r($device);
|
|
changeDevice($topicName, $fn, $device["device"], $payloadArray);
|
|
}else
|
|
{
|
|
logger(DEBUG, _("Zigbee2mqtt doing nothing !?"));
|
|
}
|
|
|
|
};
|
|
|
|
|
|
?>
|