1
0
moha/topics_callbacks/zigbee2mqtt.php

76 lines
2.3 KiB
PHP

<?php
$callback = function($topic, $message)
{
global $topics, $logFh, $devices, $included;
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
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL;
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
if ($fnTree[array_key_last($fnTree)] == "availability")
{
unset ($fnTree[array_key_last($fnTree)]);
$payloadArray = array("availability" => $message->payload);
//TODO payload can be a json object
}else
{
$payloadArray = json_decode($message->payload);
}
$device = & $devices[$topic[0]];
foreach($fnTree as $fn)
{
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ...
{
logger(LOG_WARNING, $logFh, _("init of ") . $fn, __FILE__ . ":" . __LINE__);
$device[$fn] = array();
$device[$fn]["device"] = new device;
addDevice($device[$fn], $fn, $payloadArray);
}
$device = & $device[$fn];
}
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//fwrite($logFh, print_r($msg, true));
}
};
$topics["zigbee2mqtt"] = new topic;
$topics["zigbee2mqtt"]->callback = & $callback;
?>