Archived
1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__pws2mqtt/utils.php

84 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2022-01-10 10:42:32 +01:00
<?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');
?>