110 lines
2.4 KiB
PHP
110 lines
2.4 KiB
PHP
<?php
|
|
logger(DEBUG,"Including mqtt_functions.php");
|
|
|
|
function messageReceived($message)
|
|
{
|
|
global $topics, $logFh, $devices, $included;
|
|
$topic = explode ("/", $message->topic);
|
|
$callback = $topics[$topic[0]]->callback;
|
|
//logger(DEBUG, "===== topic " . print_r($topic, true));
|
|
$callback($topic, $message);
|
|
}
|
|
|
|
// payload is an array :
|
|
// $key is property and $value is value of the property
|
|
|
|
function publish($topic, $payload, $commande="set", $eventKey)
|
|
{
|
|
global $client, $mids, $logFh;
|
|
//print_r($payload);
|
|
$string = $topic . "/" . $commande;
|
|
//$mid = $client->publish($string, json_encode($payload) , 2); //TODO activer
|
|
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 sprintf(_("I got code %d and message : '%s'"), $r, $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;
|
|
//print_r($mids);
|
|
$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]);
|
|
//print_r($mids);
|
|
}else
|
|
{
|
|
//echo "setting mids" . EOL;
|
|
$mids[$mid] = true;
|
|
}
|
|
}
|
|
?>
|