1
0

some debugging and separating callbacks of the main program

This commit is contained in:
2022-01-08 11:51:50 +01:00
parent e702d333c8
commit e7618f9d86
6 changed files with 161 additions and 56 deletions

100
moha.php
View File

@ -7,6 +7,7 @@ define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
define("MANUAL", 1);
define( "DEBUG", 16); // => 16
declare(ticks = 1);
@ -34,21 +35,50 @@ $logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl
$configDir = "./config"; // default config dir (production value is /etc/moha/)
// topics definition
$topics["zigbee2mqtt"] = new topic;
function logger($level, $log, $notif = true)
{
global $logFh, $logLevel, $notificationLevel;
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
//echo $log .EOL;
if ($level & $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
print ("$level : $log" . EOL);
}
$test = $level & $notificationLevel;
//echo "notif =>" .$notif . EOL;
if (($test != 0) and ($notif === true))
{
if(notify("Moha\n" . $log) === false)
{
logger(INFO, _("Notification not sent"), false);
}
}
}
// topics definition
logger(DEBUG, _("Require topics definition -> zigbee3mqtt"), false);
$topics["zigbee2mqtt"] = new topic;
require "topics_callbacks/zigbee2mqtt.php";
logger(DEBUG, _("Require topics definition -> pws2mqtt"), false);
$topics["pws2mqtt"] = new topic;
require "topics_callbacks/pws2mqtt.php";
logger(DEBUG, _("gettext init"), false);
// gettext
bindtextdomain("moha", "./locale");
textdomain("moha");
logger(DEBUG, _("lauching init function"), false);
if (!init()) exit(1);
logger(DEBUG, _('assigning variable : $client'), false);
$client = new Mosquitto\Client();
// log levels
define( "DEBUG", $client::LOG_DEBUG); // => 16
//define( "DEBUG", 16); // => 16
define( "INFO", $client::LOG_INFO); // => 1
define( "NOTICE", $client::LOG_NOTICE); // => 2
define( "WARNING", $client::LOG_WARNING); // => 4
@ -58,60 +88,69 @@ define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
$logLevel = ALL;
$notificationLevel = WARNING | ERROR;
logger(DEBUG, _("requiring php modules"), false);
require "utils.php";
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
//require "class/availability.php";
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
//include predefined file witch define constants for devices
if (is_readable($configDir . "/" . "devices_constants.php"))
{
$hooksList[] = $configDir . "/" . "devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL;
logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir));
logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir), false);
}else
{
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir));
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false);
}
logger(DEBUG, _("Program start"), false);
// Program start
$client = new Mosquitto\Client();
// defining callback functions
$client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$client->onMessage('message');
$client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
//signal handling
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
// Program start
$client->connect("192.168.1.253", 1883, 5);
/*while(!$connected)
{
sleep (1);
}*/
logger(DEBUG, _("Subsribing to bridge"), false);
foreach($topics as $name => $topic)
{
//echo $name;
$topic->mid = $client->subscribe($name . "/bridge/#", 2);
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
logger(DEBUG, _("Starting loop"), false);
$oneshot = false;
while (true)
{
$client->loop();
if ($dbInit == 2 and ! $included)
{
logger(DEBUG, _("Making hooks list"), false);
getDevicesValues();
loadHooks("./hooks", $hooksList);
if (!empty($hooksList))
{
foreach ($hooksList as $hook)
{
logger(INFO, _("Including ") . $hook . EOL);
logger(INFO, _("Including ") . $hook, false);
include $hook;
}
}
@ -119,14 +158,16 @@ while (true)
{
if ($oneshot === false) // execute once initialization finished
{
logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true;
foreach($topics as $name => $topic)
/*foreach($topics as $name => $topic)
{
//echo $name;
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
}*/
}
checkEvents();
}
@ -179,18 +220,21 @@ function endMoha()
{
global $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0;
$mid = $client->unsubscribe("#");
while ($connected)
if ($connected)
{
$mid = $client->unsubscribe("#");
$client->disconnect();
//echo $nSubscribed;0x00124b0022ebac5c
if ( $x++ > 60)
while ($connected)
{
$client->disconnect();
fclose($logFh);
exit (1);
if ( $x++ <= 60)
{
fclose($logFh);
exit (1);
}
$client->loop();
}
$client->loop();
}
fclose($logFh);
exit(0);