1
0

moving some scripts, making unsubsribe, disconnect and ending clean

This commit is contained in:
daniel Tartavel 2022-01-01 07:35:50 +01:00
parent cac1c638b9
commit f2b24bc0fe
2 changed files with 29 additions and 19 deletions

View File

@ -156,6 +156,8 @@ $client = new Mosquitto\Client();
$client->onConnect('connectResponse'); $client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse'); $client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse'); $client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$client->onMessage('message'); $client->onMessage('message');
$client->onLog('logger'); $client->onLog('logger');
$client->onPublish('publishResponse'); $client->onPublish('publishResponse');
@ -250,25 +252,30 @@ function loadHooks($dir, &$hookList)
function endMoha() function endMoha()
{ {
global $topics, $nSubscribed ,$client; global $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0; $x = 0;
foreach($topics as $topic => $object) /*foreach($topics as $topic => $object)
{ {
if ($object->status) if ($object->status)
{ {*/
$mid = $client->unsubscribe($topic); $mid = $client->unsubscribe("#");
$mids[$mid] = $topic; /*$mids[$mid] = $topic;
}
} }
}*/
while ($nSubscribed != 0) while ($connected)
{ {
//echo $nSubscribed;0x00124b0022ebac5c //echo $nSubscribed;0x00124b0022ebac5c
if ( $x++ > 60) exit (0); if ( $x++ > 60)
$client->loop(); {
}
$client->disconnect(); $client->disconnect();
fclose($logFh); fclose($logFh);
exit (1);
}
$client->loop();
}
fclose($logFh);
exit(0);
} }

View File

@ -88,19 +88,19 @@ function connectResponse($r, $message)
{ {
case 0: case 0:
echo _("Successfull connection") . EOL; echo _("Successfull connection") . EOL;
return true; $connected = true;
break; break;
case 1: case 1:
echo _("Connection refused : unacceptable protocol version") . EOL; echo _("Connection refused : unacceptable protocol version") . EOL;
return false; $connected = true;
break; break;
case 2: case 2:
echo _("Connection refused : identifier rejected") . EOL; echo _("Connection refused : identifier rejected") . EOL;
return false; $connected = true;
break; break;
case 3: case 3:
echo _("Connection refused (broker unavailable )") . EOL; echo _("Connection refused (broker unavailable )") . EOL;
return false; $connected = true;
break; break;
} }
} }
@ -116,16 +116,18 @@ function subscribeResponse($mid, $qosCount)
function unsubscribeResponse($mid) function unsubscribeResponse($mid)
{ {
global $topics, $mids, $nSubscribed; global $client; //$topics, $mids, $nSubscribed;
$key = $mids[$mid]; //$key = $mids[$mid];
echo _("Unsubscribed from ") . $topics[$key]->name . EOL; echo _("Unsubscribed"); // from ") . $topics[$key]->name . EOL;
$topics[$key]->status = false; //$topics[$key]->status = false;
$nSubscribed -= 1; //$nSubscribed -= 1;
$client->disconnect();
} }
function disconnectResponse($r) function disconnectResponse($r)
{ {
if (! $r) global $connected;
if ($r != 0)
{ {
echo _('Badly '); echo _('Badly ');
}else }else
@ -133,6 +135,7 @@ function disconnectResponse($r)
echo _('Cleanly '); echo _('Cleanly ');
} }
echo _("disconnected from server") . EOL; echo _("disconnected from server") . EOL;
$connected = false;
} }
function publishResponse($mid) function publishResponse($mid)