debugging
This commit is contained in:
45
moha.php
45
moha.php
@ -56,10 +56,15 @@ function notify($message)
|
||||
return $result;
|
||||
}
|
||||
|
||||
function logger($level, $log, $notif = true)
|
||||
function logger($level, $log, $pos = false, $notif = true)
|
||||
{
|
||||
global $logFh, $logLevel, $notificationLevel, $logLevels;
|
||||
$logString = date("c") . ' ' . $logLevels[$level] . " " . __FILE__ . ":" . __LINE__ . " : " . $log;
|
||||
$logString = date("c") . ' ' . $logLevels[$level] . " " ;
|
||||
if ($pos !== false)
|
||||
{
|
||||
$logString .= $pos;
|
||||
}
|
||||
$logString .= " - " . $log;
|
||||
if ($level & $logLevel)
|
||||
{
|
||||
fwrite($logFh, $logString . EOL);
|
||||
@ -70,7 +75,7 @@ function logger($level, $log, $notif = true)
|
||||
{
|
||||
if(notify("Moha\n" . $logString) === false)
|
||||
{
|
||||
logger(INFO, _("Notification not sent"), false);
|
||||
logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -112,6 +117,8 @@ function endMoha()
|
||||
{
|
||||
global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
|
||||
$x = 0;
|
||||
|
||||
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
|
||||
storeDB($devices, "/usr/share/moha/moha.db");
|
||||
if($testMode) file_put_contents("moha.devices", print_r($devices, true));
|
||||
if ($connected)
|
||||
@ -137,8 +144,8 @@ function connect2mqttServer()
|
||||
global $client;
|
||||
$client->onConnect('connectResponse');
|
||||
}
|
||||
|
||||
logger(DEBUG, _("requiring php modules"), false);
|
||||
logger(WARNING, _("starting moha"), __FILE__ . ":" . __LINE__);
|
||||
logger(DEBUG, _("requiring php modules"), __FILE__ . ":" . __LINE__);
|
||||
require "class/main.php";
|
||||
require "class/db.php";
|
||||
require "class/hook_class.php";
|
||||
@ -152,7 +159,7 @@ require "webserver.php";
|
||||
//logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
|
||||
//$client = new Mosquitto\Client();
|
||||
|
||||
logger(DEBUG, _("Loading stored devices datas"));
|
||||
logger(DEBUG, _("Loading stored devices datas"), __FILE__ . ":" . __LINE__);
|
||||
loadDB($devices, "moha.db");
|
||||
|
||||
|
||||
@ -162,28 +169,28 @@ if (!empty($hooksList))
|
||||
{
|
||||
foreach ($hooksList as $callback)
|
||||
{
|
||||
logger(INFO, _("Including ") . $callback, false);
|
||||
logger(INFO, _("Including ") . $callback, __FILE__ . ":" . __LINE__);
|
||||
include $callback;
|
||||
}
|
||||
}
|
||||
|
||||
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
|
||||
logger(DEBUG, _("requiring config files -> devices_constants.php"), __FILE__ . ":" . __LINE__);
|
||||
//include predefined file witch define constants for devices
|
||||
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), false);
|
||||
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), false);
|
||||
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false);
|
||||
}
|
||||
|
||||
// making the list of hooks to include
|
||||
listHooks("./hooks", $hooksList);
|
||||
|
||||
// Program start
|
||||
logger(DEBUG, _("Program start"), false);
|
||||
logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false);
|
||||
$client = new Mosquitto\Client();
|
||||
|
||||
// defining callback functions
|
||||
@ -193,7 +200,7 @@ try
|
||||
connect2mqttServer();
|
||||
}catch ( Mosquitto\Exception $err)
|
||||
{
|
||||
logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), false);
|
||||
logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), __FILE__ . ":" . __LINE__, false);
|
||||
sleep (10);
|
||||
exit(1);
|
||||
}
|
||||
@ -208,7 +215,7 @@ $client->onPublish('publishResponse');
|
||||
// connectong to mqtt server
|
||||
$client->connect($mqttServerIp, 1883, 5);
|
||||
|
||||
logger(INFO, _("Subscribing to bridge"), false);
|
||||
logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false);
|
||||
foreach($topics as $name => $topic)
|
||||
{
|
||||
$topic->mid = $client->subscribe($name . "/#", 2);
|
||||
@ -217,20 +224,20 @@ foreach($topics as $name => $topic)
|
||||
}
|
||||
|
||||
// starting main loop
|
||||
logger(INFO, _("Starting loop"), false);
|
||||
logger(INFO, _("Starting loop"), __FILE__ . ":" . __LINE__, false);
|
||||
$oneshot = false;
|
||||
while (true)
|
||||
{
|
||||
$client->loop(); // mqtt server loop()
|
||||
if (! $included) // hooks not already included
|
||||
{
|
||||
logger(DEBUG, _("Making hooks list"), false);
|
||||
logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false);
|
||||
getDevicesValues(); // TODO get the values of devices
|
||||
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
|
||||
{
|
||||
foreach ($hooksList as $hook) // loop to include hooks in hookslist
|
||||
{
|
||||
logger(INFO, _("Including ") . $hook, false);
|
||||
logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false);
|
||||
include $hook;
|
||||
}
|
||||
file_put_contents("/usr/share/moha/moha.devices", print_r($devices, true)); // debugging : save device list
|
||||
@ -242,7 +249,7 @@ while (true)
|
||||
|
||||
if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
|
||||
{
|
||||
logger(DEBUG, _("Oneshot part of loop"), false);
|
||||
logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false);
|
||||
$oneshot = true;
|
||||
}
|
||||
if($hooksInitialized == 0) // all hooks are not initialized
|
||||
@ -252,14 +259,14 @@ while (true)
|
||||
{
|
||||
if ($hook->initialized === false)
|
||||
{
|
||||
logger(WARNING, _("Hook not completely initialized :") . $hookName);
|
||||
logger(WARNING, _("Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
|
||||
$i &= $hook->installHooks();
|
||||
}
|
||||
}
|
||||
$hooksInitialized = $i;
|
||||
}else
|
||||
{
|
||||
logger(DEBUG,_("All hooks initialized"));
|
||||
logger(DEBUG,_("All hooks initialized"), __FILE__ . ":" . __LINE__);
|
||||
}
|
||||
checkEvents();
|
||||
askWebServer($read);
|
||||
|
Reference in New Issue
Block a user