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
|
|
|
|
2022-01-08 11:51:50 +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-04-07 01:44:17 +02:00
|
|
|
//logger(DEBUG, "message => " . var_export($message, true), __FILE__ . ":" . __LINE__);
|
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
|
|
|
|
2022-03-13 22:33:26 +01:00
|
|
|
function publish($topic, $payloadArray, $commande="set") //, $eventKey)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-17 21:01:11 +01:00
|
|
|
global $client, $mids, $logFh, $testMode;
|
2022-01-03 21:11:52 +01:00
|
|
|
$string = $topic . "/" . $commande;
|
2022-01-17 21:01:11 +01:00
|
|
|
if ($testMode === false)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-03-13 22:33:26 +01:00
|
|
|
if ($client !== null)
|
2022-01-17 21:01:11 +01:00
|
|
|
{
|
2022-03-13 22:33:26 +01:00
|
|
|
$mid = $client->publish($string, json_encode($payloadArray) , 2);
|
|
|
|
if (isset($mids[$mid]))
|
|
|
|
{
|
|
|
|
//echo "unsetting mids" .EOL;
|
|
|
|
unset ($mids[$mid]);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
//echo "setting mids" .EOL;
|
|
|
|
$mids[$mid] = true;
|
|
|
|
}
|
|
|
|
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payloadArray), __FILE__ . ":" . __LINE__);
|
2022-01-17 21:01:11 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-03-13 22:33:26 +01:00
|
|
|
logger(ERROR, $logFh, "MQTT client is null not publishing " . $string . " with payload => " . json_encode($payloadArray), __FILE__ . ":" . __LINE__);
|
2022-01-17 21:01:11 +01:00
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Test mode on: no publishing "), __FILE__ . ":" . __LINE__);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectResponse($r, $message)
|
|
|
|
{
|
|
|
|
global $connected;
|
2022-01-28 23:05:58 +01:00
|
|
|
//echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
switch ($r)
|
|
|
|
{
|
|
|
|
case 0:
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Successfull connection"), __FILE__ . ":" . __LINE__);
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(ERROR, _("Connection refused : unacceptable protocol version"), __FILE__ . ":" . __LINE__);
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(ERROR, _("Connection refused : identifier rejected"), __FILE__ . ":" . __LINE__);
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(ERROR, _("Connection refused (broker unavailable )"), __FILE__ . ":" . __LINE__);
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function subscribeResponse($mid, $qosCount)
|
|
|
|
{
|
|
|
|
global $topics, $mids, $nSubscribed;
|
2022-01-06 13:11:18 +01:00
|
|
|
//print_r($mids);
|
2021-12-30 16:18:32 +01:00
|
|
|
$key = $mids[$mid];
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Subscribed to ") . $key, __FILE__ . ":" . __LINE__);
|
2021-12-30 16:18:32 +01:00
|
|
|
$topics[$key]->status = true;
|
|
|
|
$nSubscribed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsubscribeResponse($mid)
|
|
|
|
{
|
2022-01-01 07:35:50 +01:00
|
|
|
global $client; //$topics, $mids, $nSubscribed;
|
|
|
|
//$key = $mids[$mid];
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Unsubscribed", __FILE__ . ":" . __LINE__)); // from ") . $topics[$key]->name);
|
2022-01-01 07:35:50 +01:00
|
|
|
//$topics[$key]->status = false;
|
|
|
|
//$nSubscribed -= 1;
|
|
|
|
$client->disconnect();
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function disconnectResponse($r)
|
|
|
|
{
|
2022-01-01 07:35:50 +01:00
|
|
|
global $connected;
|
|
|
|
if ($r != 0)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
$str = _('Badly ');
|
2021-12-30 16:18:32 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
$str = _('Cleanly ');
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(ERROR, $str . _("disconnected from server"));
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = false;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function publishResponse($mid)
|
|
|
|
{
|
|
|
|
global $mids, $events;
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(LOG_INFO, sprintf(_("Event with mid = %d published by MQTT broker"), $mid), __FILE__ . ":" . __LINE__);
|
2021-12-30 16:18:32 +01:00
|
|
|
if (isset($mids[$mid]))
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//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
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "setting mids" . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
$mids[$mid] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|