1
0

some debugging and separating callbacks of the main program

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

View File

@ -14,6 +14,7 @@ class topic {
public $groups;
public $extensions;
public $config;
public $callback;
}
class device

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,19 +220,22 @@ function endMoha()
{
global $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0;
if ($connected)
{
$mid = $client->unsubscribe("#");
$client->disconnect();
//echo $nSubscribed;0x00124b0022ebac5c
while ($connected)
{
//echo $nSubscribed;0x00124b0022ebac5c
if ( $x++ > 60)
if ( $x++ <= 60)
{
$client->disconnect();
fclose($logFh);
exit (1);
}
$client->loop();
}
}
fclose($logFh);
exit(0);
}

View File

@ -1,6 +1,6 @@
<?php
function message($message)
function messageReceived($message)
{
global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic);
@ -69,7 +69,7 @@ function message($message)
}
// payload is an array :
// $key is parameter => $value is value of the parameter
// $key is property => $value is value of the property
function publish($topic, $payload, $commande="set", $eventKey)
{

View File

@ -0,0 +1,8 @@
<?php
$topics["pws2mqtt"]->callback = function()
{
global $topics, $logFh, $devices, $included;
}
?>

View File

@ -0,0 +1,68 @@
<?php
$topics["zigbee2mqtt"]->callback = function()
{
global $topics, $logFh, $devices, $included;
if ($topic[1] == "bridge")
{
switch ($topic[2])
{
case "info":
$topics[$topic[0]]->info = json_decode($message->payload);
break;
case "devices":
$topics[$topic[0]]->devices = json_decode($message->payload);
fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
break;
case "groups":
$topics[$topic[0]]->groups = json_decode($message->payload);
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
break;
case "extensions":
$topics[$topic[0]]->extensions = json_decode($message->payload);
break;
case "config":
$topics[$topic[0]]->config = json_decode($message->payload);
break;
case "logging":
//TODO
break;
case "state":
$topics[$topic[0]]->state = $message->payload;
break;
default:
break;
};
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set" and $included)
{
$topic = explode ("/", $message->topic, 2); // get topic name
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL;
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
if ($fnTree[array_key_last($fnTree)] == "availability")
{
unset ($fnTree[array_key_last($fnTree)]);
$payloadArray = array("availability" => $message->payload);
//print_r($payloadArray);
}else
{
$payloadArray = json_decode($message->payload);
}
$device = & $devices[$topic[0]];
foreach($fnTree as $fn)
{
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ...
{
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
$device[$fn] = array();
$device[$fn]["device"] = new device;
//addDevice($device[$fn], $fn, );
}
$device = & $device[$fn];
}
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//fwrite($logFh, print_r($msg, true));
}
}
?>

View File

@ -5,11 +5,6 @@ function now()
return new DateTime("now");
}
function signalHandler($signal)
{
endMoha();
}
function notify($message)
{
global $notificationMethods;
@ -21,29 +16,18 @@ function notify($message)
return $result;
}
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);
}
}
}
function mktopic($device)
{
return $device->topic . "/" . $device->friendlyName;
}
logger(DEBUG, _("signal handling"), false);
//signal handling
function signalHandler($signal)
{
endMoha();
}
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
?>