177 lines
4.2 KiB
PHP
177 lines
4.2 KiB
PHP
<?php
|
|
|
|
function message($message)
|
|
{
|
|
global $topics, $logFh, $devices;
|
|
$topic = explode ("/", $message->topic);
|
|
if ($topic[1] == "bridge")
|
|
{
|
|
switch ($topic[2])
|
|
{
|
|
case "info":
|
|
$topics[$topic[0]]->info = json_decode($message->payload);
|
|
break;
|
|
case "devices":
|
|
$topics[$topic[0]]->devices = json_decode($message->payload);
|
|
fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
|
|
break;
|
|
case "groups":
|
|
$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")
|
|
{
|
|
|
|
$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);
|
|
print_r($payloadArray);
|
|
}else
|
|
{
|
|
$payloadArray = json_decode($message->payload);
|
|
}
|
|
$device = & $devices[$topic[0]];
|
|
foreach($fnTree as $fn)
|
|
{
|
|
//print_r($device) ;
|
|
if (!isset($device[$fn]))
|
|
{
|
|
logger(LOG_INFO, $logFh, "init of " . $fn .EOL);
|
|
$device[$fn] = array();
|
|
$device[$fn]["device"] = new device;
|
|
}
|
|
$device = & $device[$fn];
|
|
}
|
|
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
|
|
//fwrite($logFh, print_r($msg, true));
|
|
}
|
|
}
|
|
|
|
// payload is an array :
|
|
// key is parameter => value is value of parameter
|
|
|
|
function publish($device, $payload, $commande="set", $eventKey)
|
|
{
|
|
global $client, $mids, $logFh;
|
|
//print_r($payload);
|
|
$string = $device->topic . "/" . $device->friendlyName . "/" . $commande;
|
|
$mid = $client->publish($string, json_encode($payload) , 2);
|
|
if (isset($mids[$mid]))
|
|
{
|
|
//echo "unsetting mids" .EOL;
|
|
unset ($mids[$mid]);
|
|
}else
|
|
{
|
|
//echo "setting mids" .EOL;
|
|
$mids[$mid] = true;
|
|
}
|
|
//echo $string . " =>>>>>> " . json_encode($payload) . EOL;
|
|
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
|
|
}
|
|
|
|
function connectResponse($r, $message)
|
|
{
|
|
global $connected;
|
|
echo _("I got code ") . $r . _(" and message : '") . $message . "'" . EOL;
|
|
switch ($r)
|
|
{
|
|
case 0:
|
|
echo _("Successfull connection") . EOL;
|
|
$connected = true;
|
|
break;
|
|
case 1:
|
|
echo _("Connection refused : unacceptable protocol version") . EOL;
|
|
$connected = true;
|
|
break;
|
|
case 2:
|
|
echo _("Connection refused : identifier rejected") . EOL;
|
|
$connected = true;
|
|
break;
|
|
case 3:
|
|
echo _("Connection refused (broker unavailable )") . EOL;
|
|
$connected = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
function subscribeResponse($mid, $qosCount)
|
|
{
|
|
global $topics, $mids, $nSubscribed;
|
|
$key = $mids[$mid];
|
|
echo _("Subscribed to ") . $key . EOL;
|
|
$topics[$key]->status = true;
|
|
$nSubscribed += 1;
|
|
}
|
|
|
|
function unsubscribeResponse($mid)
|
|
{
|
|
global $client; //$topics, $mids, $nSubscribed;
|
|
//$key = $mids[$mid];
|
|
echo _("Unsubscribed"); // from ") . $topics[$key]->name . EOL;
|
|
//$topics[$key]->status = false;
|
|
//$nSubscribed -= 1;
|
|
$client->disconnect();
|
|
}
|
|
|
|
function disconnectResponse($r)
|
|
{
|
|
global $connected;
|
|
if ($r != 0)
|
|
{
|
|
echo _('Badly ');
|
|
}else
|
|
{
|
|
echo _('Cleanly ');
|
|
}
|
|
echo _("disconnected from server") . EOL;
|
|
$connected = false;
|
|
}
|
|
|
|
function publishResponse($mid)
|
|
{
|
|
global $mids, $events;
|
|
logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker");
|
|
if (isset($mids[$mid]))
|
|
{
|
|
//echo "unsetting mids" . EOL;
|
|
unset ($mids[$mid]);
|
|
}else
|
|
{
|
|
//echo "setting mids" . EOL;
|
|
$mids[$mid] = true;
|
|
}
|
|
}
|
|
|
|
function logger($level, $log)
|
|
{
|
|
global $logFh, $logLevel;
|
|
if ( $level >= $logLevel)
|
|
{
|
|
fwrite($logFh, "$level : $log" . EOL);
|
|
print ("$level : $log" . EOL);
|
|
}
|
|
}
|
|
|
|
?>
|