diff --git a/moha.php b/moha.php index 39f50c9..c693c06 100644 --- a/moha.php +++ b/moha.php @@ -156,6 +156,8 @@ $client = new Mosquitto\Client(); $client->onConnect('connectResponse'); $client->onDisconnect('disconnectResponse'); $client->onSubscribe('subscribeResponse'); +$client->onUnsubscribe('unsubscribeResponse'); + $client->onMessage('message'); $client->onLog('logger'); $client->onPublish('publishResponse'); @@ -250,25 +252,30 @@ function loadHooks($dir, &$hookList) function endMoha() { - global $topics, $nSubscribed ,$client; + global $topics, $nSubscribed ,$client, $logFh, $connected; $x = 0; - foreach($topics as $topic => $object) + /*foreach($topics as $topic => $object) { if ($object->status) - { - $mid = $client->unsubscribe($topic); - $mids[$mid] = $topic; + {*/ + $mid = $client->unsubscribe("#"); + /*$mids[$mid] = $topic; } - } + }*/ - while ($nSubscribed != 0) + while ($connected) { //echo $nSubscribed;0x00124b0022ebac5c - if ( $x++ > 60) exit (0); + if ( $x++ > 60) + { + $client->disconnect(); + fclose($logFh); + exit (1); + } $client->loop(); } - $client->disconnect(); fclose($logFh); + exit(0); } diff --git a/mqtt_functions.php b/mqtt_functions.php index 7365f7a..328a499 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -88,19 +88,19 @@ function connectResponse($r, $message) { case 0: echo _("Successfull connection") . EOL; - return true; + $connected = true; break; case 1: echo _("Connection refused : unacceptable protocol version") . EOL; - return false; + $connected = true; break; case 2: echo _("Connection refused : identifier rejected") . EOL; - return false; + $connected = true; break; case 3: echo _("Connection refused (broker unavailable )") . EOL; - return false; + $connected = true; break; } } @@ -116,16 +116,18 @@ function subscribeResponse($mid, $qosCount) function unsubscribeResponse($mid) { - global $topics, $mids, $nSubscribed; - $key = $mids[$mid]; - echo _("Unsubscribed from ") . $topics[$key]->name . EOL; - $topics[$key]->status = false; - $nSubscribed -= 1; + 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) { - if (! $r) + global $connected; + if ($r != 0) { echo _('Badly '); }else @@ -133,6 +135,7 @@ function disconnectResponse($r) echo _('Cleanly '); } echo _("disconnected from server") . EOL; + $connected = false; } function publishResponse($mid)