1
0

debugging hooks\ncleaning code

This commit is contained in:
2022-01-17 21:01:11 +01:00
parent 4fb5504cdc
commit 11a2253804
7 changed files with 61 additions and 91 deletions

View File

@ -1,5 +1,7 @@
<?php
$title = "moha";
$testMode = false;
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
@ -10,7 +12,14 @@ define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
define("MANUAL", 1);
// log levels
define( "DEBUG", 16); // => 16
define( "INFO", 1); // => 1
define( "NOTICE", 2); // => 2
define( "WARNING", 4); // => 4
define( "ERROR", 8); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
declare(ticks = 1);
@ -18,6 +27,8 @@ $listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("property");
//global variables
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = WARNING | ERROR;
$topics = array(); // list of topics
$mids = array(); // list of message IDs
$devices = array(); // array of device objetcs
@ -36,9 +47,8 @@ $logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl
$configDir = "./config"; // default config dir (production value is /etc/moha/)
$hooksInitialized = 0; // are all hooks initialized ? false/true
require $configDir . "/properties2log.php";
logger(DEBUG, _("lauching init function"), false);
if (!init()) exit(1);
// gettext
@ -77,29 +87,21 @@ function logger($level, $log, $notif = true)
}
}
logger(DEBUG, _('assigning variable : $client'), false);
$client = new Mosquitto\Client();
// log levels
//define( "DEBUG", 16); // => 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 = ALL;
$notificationLevel = WARNING | ERROR;
logger(DEBUG, _("requiring php modules"), false);
require "class/main.php";
require "class/db.php";
require "class/hook_class.php";
require "utils.php";
require $configDir . "/properties2log.php";
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
$client = new Mosquitto\Client();
logger(DEBUG, _("Loading stored devices datas"));
loadDB($devices, "moha.db");
@ -130,8 +132,8 @@ if (is_readable($configDir . "/" . "devices_constants.php"))
// making the list of hooks to include
listHooks("./hooks", $hooksList);
logger(DEBUG, _("Program start"), false);
// Program start
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client();
// defining callback functions
@ -143,51 +145,47 @@ $client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
// connectong to mqtt server
$client->connect("192.168.1.253", 1883, 5);
/*while(!$connected)
{
sleep (1);
}*/
logger(DEBUG, _("Subsribing to bridge"), false);
logger(INFO, _("Subscribing to bridge"), false);
foreach($topics as $name => $topic)
{
//echo $name;
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
logger(DEBUG, _("Starting loop"), false);
// starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false;
while (true)
{
$client->loop();
if (! $included)
$client->loop(); // mqtt server loop()
if (! $included) // hooks not already included
{
logger(DEBUG, _("Making hooks list"), false);
getDevicesValues();
if (!empty($hooksList))
getDevicesValues(); // TODO get the values of devices
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
{
foreach ($hooksList as $hook)
foreach ($hooksList as $hook) // loop to include hooks in hookslist
{
logger(INFO, _("Including ") . $hook, false);
include $hook;
}
file_put_contents("moha.devices", print_r($devices, true));
file_put_contents("moha.devices", print_r($devices, true)); // debugging : save device list
$included = true;
}
}elseif($included)
}else
{
if ($oneshot === false) // execute once initialization finished
if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
{
logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true;
}
checkEvents();
if($hooksInitialized == 0)
if($hooksInitialized == 0) // all hooks are not initialized
{
$i = 1;
foreach($hooks as $hookName => $hook)
@ -196,10 +194,6 @@ while (true)
{
logger(WARNING, _("Hook not completely initialized :") . $hookName);
$i &= $hook->installHooks();
echo "hook->initialized";var_dump($hook->initialized);echo EOL;
// (int)$hook->initialized;
var_dump($hook);
echo "i =======> " . $i;var_dump($i); echo EOL;
}
}
$hooksInitialized = $i;
@ -227,21 +221,15 @@ function init()
function listHooks($dir, &$hookList)
{
$files = scandir($dir);
//print_r($files);
foreach ($files as $file)
{
//echo "=====> $file" . EOL;
if ($file != "." and $file != "..")
{
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
if (is_dir($dir . "/" . $file))
{
//echo "directory : " . $dir . '/' . $file . EOL;
listHooks($dir . '/' . $file, $hookList);
}elseif (strpos($file, ".php", -4) !== false)
{
//echo "file : " . $dir . "/" . $file . EOL;
if (substr($file, -4) == ".php")
{
$hookList[] = $dir . "/" . $file;
@ -249,7 +237,6 @@ function listHooks($dir, &$hookList)
}
}
}
//print_r($hookList);
}
function endMoha()
@ -262,12 +249,10 @@ function endMoha()
{
$mid = $client->unsubscribe("#");
$client->disconnect();
//echo $nSubscribed;0x00124b0022ebac5c
while ($connected)
{
if ( $x++ <= 60)
{
fclose($logFh);
exit (1);
}
@ -275,7 +260,6 @@ function endMoha()
}
}
fclose($logFh);
exit(0);
}