84 lines
1.4 KiB
PHP
84 lines
1.4 KiB
PHP
<?php
|
|
define( "EOL", "\n");
|
|
|
|
cli_set_process_title($topicName);
|
|
file_put_contents("/proc/".getmypid()."/comm",$topicName);
|
|
|
|
function now()
|
|
{
|
|
return new DateTime("now");
|
|
}
|
|
|
|
function stopped()
|
|
{
|
|
global $client, $logFh, $connected;
|
|
$x = 0;
|
|
$exit = 0;
|
|
if ($connected)
|
|
{
|
|
$mid = $client->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');
|
|
?>
|