code indentation and readablity
This commit is contained in:
parent
a17fc4cebc
commit
487c533750
|
|
@ -13,6 +13,7 @@ namespace Salman\Mqtt\MqttClass;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* phpMQTT */
|
/* phpMQTT */
|
||||||
|
|
||||||
class MqttService
|
class MqttService
|
||||||
{
|
{
|
||||||
private $socket; /* holds the socket */
|
private $socket; /* holds the socket */
|
||||||
|
|
@ -33,6 +34,7 @@ class MqttService
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
$this->broker($address, $port, $clientid, $cafile);
|
$this->broker($address, $port, $clientid, $cafile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sets the broker details */
|
/* sets the broker details */
|
||||||
function broker($address, $port, $clientid, $cafile = NULL){
|
function broker($address, $port, $clientid, $cafile = NULL){
|
||||||
$this->address = $address;
|
$this->address = $address;
|
||||||
|
|
@ -40,12 +42,14 @@ class MqttService
|
||||||
$this->clientid = $clientid;
|
$this->clientid = $clientid;
|
||||||
$this->cafile = $cafile;
|
$this->cafile = $cafile;
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect_auto($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
function connect_auto($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
||||||
while($this->connect($clean, $will, $username, $password)==false){
|
while($this->connect($clean, $will, $username, $password)==false){
|
||||||
sleep(10);
|
sleep(10);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* connects to the broker
|
/* connects to the broker
|
||||||
inputs: $clean: should the client send a clean session flag */
|
inputs: $clean: should the client send a clean session flag */
|
||||||
function connect($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
function connect($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
||||||
|
|
@ -118,6 +122,7 @@ class MqttService
|
||||||
$this->timesinceping = time();
|
$this->timesinceping = time();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read: reads in so many bytes */
|
/* read: reads in so many bytes */
|
||||||
function read($int = 8192, $nb = false){
|
function read($int = 8192, $nb = false){
|
||||||
// print_r(socket_get_status($this->socket));
|
// print_r(socket_get_status($this->socket));
|
||||||
|
|
@ -140,6 +145,7 @@ class MqttService
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* subscribe: subscribes to topics */
|
/* subscribe: subscribes to topics */
|
||||||
function subscribe($topics, $qos = 0){
|
function subscribe($topics, $qos = 0){
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
@ -165,6 +171,7 @@ class MqttService
|
||||||
$bytes = ord(substr($string,1,1));
|
$bytes = ord(substr($string,1,1));
|
||||||
$string = $this->read($bytes);
|
$string = $this->read($bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ping: sends a keep alive ping */
|
/* ping: sends a keep alive ping */
|
||||||
function ping(){
|
function ping(){
|
||||||
$head = " ";
|
$head = " ";
|
||||||
|
|
@ -173,6 +180,7 @@ class MqttService
|
||||||
fwrite($this->socket, $head, 2);
|
fwrite($this->socket, $head, 2);
|
||||||
if($this->debug) echo "ping sent\n";
|
if($this->debug) echo "ping sent\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disconnect: sends a proper disconect cmd */
|
/* disconnect: sends a proper disconect cmd */
|
||||||
function disconnect(){
|
function disconnect(){
|
||||||
$head = " ";
|
$head = " ";
|
||||||
|
|
@ -180,11 +188,13 @@ class MqttService
|
||||||
$head{1} = chr(0x00);
|
$head{1} = chr(0x00);
|
||||||
fwrite($this->socket, $head, 2);
|
fwrite($this->socket, $head, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* close: sends a proper disconect, then closes the socket */
|
/* close: sends a proper disconect, then closes the socket */
|
||||||
function close(){
|
function close(){
|
||||||
$this->disconnect();
|
$this->disconnect();
|
||||||
stream_socket_shutdown($this->socket, STREAM_SHUT_WR);
|
stream_socket_shutdown($this->socket, STREAM_SHUT_WR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* publish: publishes $content on a $topic */
|
/* publish: publishes $content on a $topic */
|
||||||
function publish($topic, $content, $qos = 0, $retain = 0){
|
function publish($topic, $content, $qos = 0, $retain = 0){
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
@ -207,6 +217,7 @@ class MqttService
|
||||||
fwrite($this->socket, $head, strlen($head));
|
fwrite($this->socket, $head, strlen($head));
|
||||||
fwrite($this->socket, $buffer, $i);
|
fwrite($this->socket, $buffer, $i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* message: processes a recieved topic */
|
/* message: processes a recieved topic */
|
||||||
function message($msg){
|
function message($msg){
|
||||||
$tlen = (ord($msg{0})<<8) + ord($msg{1});
|
$tlen = (ord($msg{0})<<8) + ord($msg{1});
|
||||||
|
|
@ -227,6 +238,7 @@ class MqttService
|
||||||
}
|
}
|
||||||
if($this->debug && !$found) echo "msg recieved but no match in subscriptions\n";
|
if($this->debug && !$found) echo "msg recieved but no match in subscriptions\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/* proc: the processing loop for an "allways on" client
|
/* proc: the processing loop for an "allways on" client
|
||||||
set true when you are doing other stuff in the loop good for watching something else at the same time */
|
set true when you are doing other stuff in the loop good for watching something else at the same time */
|
||||||
function proc( $loop = true){
|
function proc( $loop = true){
|
||||||
|
|
@ -291,6 +303,7 @@ class MqttService
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* getmsglength: */
|
/* getmsglength: */
|
||||||
function getmsglength(&$msg, &$i){
|
function getmsglength(&$msg, &$i){
|
||||||
$multiplier = 1;
|
$multiplier = 1;
|
||||||
|
|
@ -303,6 +316,7 @@ class MqttService
|
||||||
}while (($digit & 128) != 0);
|
}while (($digit & 128) != 0);
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setmsglength: */
|
/* setmsglength: */
|
||||||
function setmsglength($len){
|
function setmsglength($len){
|
||||||
$string = "";
|
$string = "";
|
||||||
|
|
@ -316,6 +330,7 @@ class MqttService
|
||||||
}while ( $len > 0 );
|
}while ( $len > 0 );
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* strwritestring: writes a string to a buffer */
|
/* strwritestring: writes a string to a buffer */
|
||||||
function strwritestring($str, &$i){
|
function strwritestring($str, &$i){
|
||||||
$ret = " ";
|
$ret = " ";
|
||||||
|
|
@ -328,6 +343,7 @@ class MqttService
|
||||||
$i += ($len+2);
|
$i += ($len+2);
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function printstr($string){
|
function printstr($string){
|
||||||
$strlen = strlen($string);
|
$strlen = strlen($string);
|
||||||
for($j=0;$j<$strlen;$j++){
|
for($j=0;$j<$strlen;$j++){
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue