1
0

a lot of bugs corrected\nrdc_wc_eclairage

This commit is contained in:
2022-03-13 22:33:26 +01:00
parent 9dde1c2d53
commit c8772aeda0
24 changed files with 534 additions and 191 deletions

View File

@ -20,6 +20,7 @@ $mids = array(); // list of message IDs
$devices = array(); // array of device objetcs
$indexDevices = array(); // index of devices by ieee_address
$indexFriendlyNames = array(); // index of devices by freindly name
$indexTypes = array(); // index of devices by type
$hooksList = array(); // list of hooks to be included
$hooks = array(); // array of hooks
$notificationMethods = array(); // array of notification methods objects
@ -75,7 +76,7 @@ function notify($message)
function logger($level, $log, $pos = "", $notif = true)
{
global $logFh, $logLevel, $notificationLevel, $logLevels;
$logString = date("c") . ' ' . $logLevels[$level] . " " ;
$logString = $logLevels[$level] . " " ;
if ($pos !== false)
{
$logString .= $pos;
@ -83,8 +84,8 @@ function logger($level, $log, $pos = "", $notif = true)
$logString .= " - " . $log;
if ($level & $logLevel)
{
fwrite($logFh, $logString . EOL);
print ($logString . EOL);
fwrite($logFh, date("c") . ' ' . $logString . EOL);
print ("MOHA-" . $logString . EOL);
}
$test = $level & $notificationLevel;
if (($test != 0) and ($notif === true))
@ -135,9 +136,13 @@ function endMoha()
$x = 0;
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
if (storeDB($devices, $dataPath . "moha.db") === false)
if (storeDB($devices, $dataPath . "devices.db") === false)
{
logger(ERROR, _("Can not store db" ), __FILE__ . ":" . __LINE__);
logger(ERROR, _("Can not store devices db" ), __FILE__ . ":" . __LINE__);
}
if (storeDB($topics, $dataPath . "topics.db") === false)
{
logger(ERROR, _("Can not store topics db" ), __FILE__ . ":" . __LINE__);
}
if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true));
if ($connected)
@ -173,11 +178,6 @@ if (is_readable($configDir . "/devices_constants.php"))
include $configDir . "/devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL;
logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false);
}elseif(is_readable("config/devices_constants.php"))
{
include "config/devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL;
logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false);
}else
{
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false);
@ -194,17 +194,6 @@ require "events.php";
require "db_functions.php";
require "webserver/webserver.php";
logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "moha.db", __FILE__ . ":" . __LINE__);
if (loadDB($devices, $dataPath . "moha.db") === false)
{
logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__);
}else
{
mkIndexes($devices);
}
// topics definition
listHooks("./topics_callbacks", $hooksList);
if (!empty($hooksList))
@ -216,9 +205,42 @@ if (!empty($hooksList))
}
}
// making the list of hooks to include
listHooks("./hooks", $hooksList);
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
{
foreach ($hooksList as $hookFile) // loop to include hooks in hookslist
{
logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false);
include_once $hookFile;
}
if ($logLevel & DEBUG)
{
file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list
}
$included = true;
}
logger(DEBUG, _("Loading stored topics datas from ") . $dataPath . "topics.db", __FILE__ . ":" . __LINE__);
if (file_exists($dataPath . "topics.db"))
{
if (($topics = loadDB($dataPath . "topics.db")) === false)
{
logger(ERROR, _("Can not load topics db"), __FILE__ . ":" . __LINE__);
}
}
logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "devices.db", __FILE__ . ":" . __LINE__);
if (file_exists($dataPath . "devices.db"))
{
if (($devices = loadDB($dataPath . "devices.db")) === false)
{
logger(ERROR, _("Can not load devices db"), __FILE__ . ":" . __LINE__);
}else
{
mkIndexes();
}
}
// Program start
logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false);
@ -247,7 +269,7 @@ $client->onPublish('publishResponse');
$client->connect($mqttServerIp, 1883, 5);
logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false);
foreach($topics as $name => $topic)
foreach($topics as $name => &$topic)
{
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
@ -261,23 +283,24 @@ $oneshot = false;
while (true)
{
$client->loop(); // mqtt server loop()
if (! $included) // hooks not already included
/*if (! $included) // hooks not already included
{
logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false);
logger(DEBUG, _("Loading hooks list"), __FILE__ . ":" . __LINE__, false);
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
{
foreach ($hooksList as $hookFile) // loop to include hooks in hookslist
{
logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false);
include $hookFile;
include_once $hookFile;
}
if ($logLevel & DEBUG)
{
file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list
}
file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list
$included = true;
}
}else
{
{*/
if ($oneshot === false) // execute while the first loop :WARNING hooks can to be not initialized
{
@ -285,15 +308,16 @@ while (true)
$oneshot = true;
}
$nn = 0;
if($hooksInitialized == 0) // all hooks are not initialized
{
$i = 1;
foreach($hooks as $hookName => $hook)
foreach($hooks as $hookName => &$hook)
{
if ($hook->initialized === false)
if ($hook->initialized === false and $hook->active === true)
{
logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
$i &= $hook->installHooks();
$i &= $hook->installHooks($indexDevices);
}
}
$hooksInitialized = $i;
@ -301,7 +325,6 @@ while (true)
{
logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__);
$flagHooks = true;
}else // executed when hooks initialization finished but database init not finished
{
//logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__);
@ -309,7 +332,7 @@ while (true)
}
checkEvents();
askWebServer($read);
}
//}
}
endMoha();