1
0
moha/mqtt_functions.php

110 lines
2.4 KiB
PHP
Raw Normal View History

2021-12-30 16:18:32 +01:00
<?php
2022-01-17 00:18:50 +01:00
logger(DEBUG,"Including mqtt_functions.php");
2021-12-30 16:18:32 +01:00
function messageReceived($message)
2021-12-30 16:18:32 +01:00
{
2022-01-06 13:03:26 +01:00
global $topics, $logFh, $devices, $included;
2021-12-30 16:18:32 +01:00
$topic = explode ("/", $message->topic);
2022-01-08 23:45:38 +01:00
$callback = $topics[$topic[0]]->callback;
2022-01-17 00:18:50 +01:00
//logger(DEBUG, "===== topic " . print_r($topic, true));
2022-01-08 23:45:38 +01:00
$callback($topic, $message);
2021-12-30 16:18:32 +01:00
}
// payload is an array :
2022-01-17 00:18:50 +01:00
// $key is property and $value is value of the property
2021-12-30 16:18:32 +01:00
function publish($topic, $payload, $commande="set", $eventKey)
2021-12-30 16:18:32 +01:00
{
global $client, $mids, $logFh;
//print_r($payload);
$string = $topic . "/" . $commande;
2022-01-17 00:18:50 +01:00
//$mid = $client->publish($string, json_encode($payload) , 2); //TODO activer
if (isset($mids[$mid]))
2021-12-30 16:18:32 +01:00
{
//echo "unsetting mids" .EOL;
2021-12-30 16:18:32 +01:00
unset ($mids[$mid]);
}else
{
//echo "setting mids" .EOL;
2021-12-30 16:18:32 +01:00
$mids[$mid] = true;
}
//echo $string . " =>>>>>> " . json_encode($payload) . EOL;
2021-12-30 16:18:32 +01:00
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;
2021-12-30 16:18:32 +01:00
switch ($r)
{
case 0:
echo _("Successfull connection") . EOL;
$connected = true;
2021-12-30 16:18:32 +01:00
break;
case 1:
echo _("Connection refused : unacceptable protocol version") . EOL;
$connected = true;
2021-12-30 16:18:32 +01:00
break;
case 2:
echo _("Connection refused : identifier rejected") . EOL;
$connected = true;
2021-12-30 16:18:32 +01:00
break;
case 3:
echo _("Connection refused (broker unavailable )") . EOL;
$connected = true;
2021-12-30 16:18:32 +01:00
break;
}
}
function subscribeResponse($mid, $qosCount)
{
global $topics, $mids, $nSubscribed;
//print_r($mids);
2021-12-30 16:18:32 +01:00
$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();
2021-12-30 16:18:32 +01:00
}
function disconnectResponse($r)
{
global $connected;
if ($r != 0)
2021-12-30 16:18:32 +01:00
{
echo _('Badly ');
}else
{
echo _('Cleanly ');
}
echo _("disconnected from server") . EOL;
$connected = false;
2021-12-30 16:18:32 +01:00
}
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;
2021-12-30 16:18:32 +01:00
unset ($mids[$mid]);
2022-01-06 13:03:26 +01:00
//print_r($mids);
2021-12-30 16:18:32 +01:00
}else
{
//echo "setting mids" . EOL;
2021-12-30 16:18:32 +01:00
$mids[$mid] = true;
}
}
?>