From 534174fe1332e05826d2d25b34db3531d73b9019 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Mon, 10 Jan 2022 10:42:32 +0100 Subject: [PATCH] initial commit --- mqtt_functions.php | 137 +++++++++++++++++++++++++++++++++++++++++++++ pws2mqtt.php | 93 ++++++++++++++++++++++++++++++ utils.php | 83 +++++++++++++++++++++++++++ 3 files changed, 313 insertions(+) create mode 100644 mqtt_functions.php create mode 100644 pws2mqtt.php create mode 100644 utils.php diff --git a/mqtt_functions.php b/mqtt_functions.php new file mode 100644 index 0000000..dba1c96 --- /dev/null +++ b/mqtt_functions.php @@ -0,0 +1,137 @@ + 16 +define( "INFO", $client::LOG_INFO); // => 1 +define( "NOTICE", $client::LOG_NOTICE); // => 2 +define( "WARNING", $client::LOG_WARNING); // => 4 +define( "ERROR", $client::LOG_ERR); // => 8 +define( "ALERT", 32); +define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT); +$logLevel = INFO | WARNING | ERROR | ALERT; +//$notificationLevel = WARNING | ERROR; // TODO send notification + +logger(DEBUG, _("defining callback functions")); + +// defining callback functions +$client->onConnect('connectResponse'); +$client->onDisconnect('disconnectResponse'); +$client->onSubscribe('subscribeResponse'); +$client->onUnsubscribe('unsubscribeResponse'); +$client->onMessage('messageReceived'); +$client->onLog('logger'); +$client->onPublish('publishResponse'); + +logger(DEBUG, _("connecting to mqtt server")); +$client->connect($mqttServerAddress, $mqttServerPort, 30); +logger(DEBUG, _("subscribing")); +$mid = $client->subscribe($topicName . "/#", 2); + +function messageReceived($message) +{ + global $topicName, $logFh; + $topic = explode ("/", $message->topic); + if($topic[array_key_last($topic)] != "get" and ($topic[array_key_last($topic)]) != "set") + { + $topic = explode ("/", $message->topic, 2); // get topic name + $fnTree = explode ("/" , $topic[1]); // get friendlyname + echo $topic[0] . " => " . $topic[1] . EOL; + logger(INFO, print_r(json_decode($message->payload), true)); + } +} + +// payload is an array : +// $key is property => $value is value of the parameter + +function publish($payload, $commande="") +{ + global $mids, $friendlyName, $topicName, $client, $logFh; + //print_r($payload); + $string = $topicName . "/" . $friendlyName; + $mid = $client->publish($string, $payload , 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 => " . $payload); +} + +function connectResponse($r, $message) +{ + global $connected; + echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL; + switch ($r) + { + case 0: + logger(INFO, _("Successfull connection")); + $connected = true; + break; + case 1: + logger(INFO, _("Connection refused : unacceptable protocol version")); + $connected = false; + break; + case 2: + logger(INFO, _("Connection refused : identifier rejected")); + $connected = false; + break; + case 3: + logger(INFO, _("Connection refused (broker unavailable )")); + $connected = false; + break; + } + return $connected; +} + +function subscribeResponse($mid, $qosCount) +{ + global $topics; + logger(INFO, _("Subscribed")); +} + +function unsubscribeResponse($mid) +{ + global $client; + logger(INFO, _("Unsubscribed")); + $client->disconnect(); +} + +function disconnectResponse($r) +{ + global $connected; + if ($r != 0) + { + $str = _('Badly '); + }else + { + $str = _('Cleanly '); + } + logger(INFO, $str . _("disconnected from server")); + $connected = false; +} + +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; + unset ($mids[$mid]); + //print_r($mids); + }else + { + //echo "setting mids" . EOL; + $mids[$mid] = true; + } +} +?> diff --git a/pws2mqtt.php b/pws2mqtt.php new file mode 100644 index 0000000..3ea937e --- /dev/null +++ b/pws2mqtt.php @@ -0,0 +1,93 @@ +loop(); + sleep(1); +} + +// Prepare payload to send to mqtt server +function mkPayload($array) +{ + global $macAddress, $type; + unset ($array[0]); + unset ($array[1]); + $payload = '{"ieeeAddress":"' . $macAddress . '","type":"' . $type . '"'; + + foreach($array as $property) + { + $r = explode("=", $property); + $payload .= ',"' . $r[0] . '":'; + if (is_numeric($r[1])) + { + $payload .= $r[1]; + }else + { + $payload .= '"' . $r[1] . '"'; + } + + + } + return $payload . "}"; +} + +function closeAll() +{ + socket_close($spawn); + socket_close($socket); +} +?> diff --git a/utils.php b/utils.php new file mode 100644 index 0000000..d4b58ba --- /dev/null +++ b/utils.php @@ -0,0 +1,83 @@ +unsubscribe("#"); + $client->disconnect(); + while ($connected) + { + if ( $x++ <= 60) + { + $exit = 1; + break; + } + $client->loop(); + } + } + closeAll(); + fclose($logFh); + exit(0); +} + + +function signalHandler($signal) +{ + global $connected, $client, $logFh; + $x = 0; + while ($connected) + { + if ( $x++ > 60) + { + $client->disconnect(); + fclose($logFh); + exit (1); + } + $client->loop(); + } + fclose($logFh); + exit(0); +} + +function logger($level, $log, $notif = false) +{ + global $logFh, $logLevel, $notificationLevel; + //echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ; + //echo $log .EOL; + var_dump($level); + var_dump($log); + if ($level & $logLevel) + { + fwrite($logFh, "$level : $log" . EOL); + print ("$level : $log" . EOL); + } +} + +date_default_timezone_set('Europe/Paris'); + +if (! $logFh = fopen("/var/log/pws2mqtt.log", "w") ) +{ + echo _("error opening log file") . EOL; +}else +{ + echo _("Log file opened") . EOL; +} + +//signal handling +pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called) +pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out +pcntl_signal(SIGINT, 'signalHandler'); +?>