1
0

debuggage

This commit is contained in:
2022-01-23 09:46:06 +01:00
parent 10f2a1087a
commit fc4eaf2238
14 changed files with 294 additions and 157 deletions

272
moha.php
View File

@ -2,6 +2,8 @@
$title = "moha";
$testMode = true;
$webServerIsActive = true;
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
@ -14,7 +16,7 @@ $listPropertiesKeys = array("property");
//global variables
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = WARNING | ERROR;
$notificationLevel = ALERT | ERROR;
$topics = array(); // list of topics
$mids = array(); // list of message IDs
$devices = array(); // array of device objetcs
@ -32,8 +34,12 @@ $logFile = "/var/log/moha.log"; // Path of log file
$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
$hooksInitialized = 0; // are all hooks initialized ? false/true
$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode
if ($testMode)
{
$mqttServerIp = "192.168.1.253"; // IP address of mqttserver in test mode
}
if (!init()) exit(1);
@ -47,7 +53,7 @@ function notify($message)
$result = false;
foreach($notificationMethods as $value)
{
//$result = $result | $value->send($message);
$result = $result | $value->send($message);
}
return $result;
}
@ -55,8 +61,6 @@ function notify($message)
function logger($level, $log, $notif = true)
{
global $logFh, $logLevel, $notificationLevel, $logLevels;
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
//echo $log .EOL;
$logString = date("c") . ' ' . $logLevels[$level] . " : $log";
if ($level & $logLevel)
{
@ -64,7 +68,6 @@ function logger($level, $log, $notif = true)
print ($logString . EOL);
}
$test = $level & $notificationLevel;
//echo "notif =>" .$notif . EOL;
if (($test != 0) and ($notif === true))
{
if(notify("Moha\n" . $logString) === false)
@ -74,125 +77,6 @@ function logger($level, $log, $notif = true)
}
}
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");
// topics definition
listHooks("./topics_callbacks", $hooksList);
if (!empty($hooksList))
{
foreach ($hooksList as $callback)
{
logger(INFO, _("Including ") . $callback, false);
include $callback;
}
}
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
//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);
}else
{
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false);
}
// making the list of hooks to include
listHooks("./hooks", $hooksList);
// Program start
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client();
// defining callback functions
$client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
// connectong to mqtt server
$client->connect("192.168.1.253", 1883, 5);
logger(INFO, _("Subscribing to bridge"), false);
foreach($topics as $name => $topic)
{
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
// starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false;
while (true)
{
$client->loop(); // mqtt server loop()
if (! $included) // hooks not already included
{
logger(DEBUG, _("Making hooks list"), 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);
include $hook;
}
file_put_contents("moha.devices", print_r($devices, true)); // debugging : save device list
$included = true;
}
}else
{
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) // all hooks are not initialized
{
$i = 1;
foreach($hooks as $hookName => $hook)
{
if ($hook->initialized === false)
{
logger(WARNING, _("Hook not completely initialized :") . $hookName);
$i &= $hook->installHooks();
}
}
$hooksInitialized = $i;
}else
{
logger(DEBUG,_("All hooks initialized"));
}
}
}
endMoha();
function init()
{
global $logFile, $logFh, $client;
@ -230,8 +114,8 @@ function endMoha()
{
global $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0;
storeDB($devices, "moha.db");
file_put_contents("moha.devices", print_r($devices, true));
storeDB($devices, "/usr/share/moha/moha.db");
if($testMode) file_put_contents("moha.devices", print_r($devices, true));
if ($connected)
{
$mid = $client->unsubscribe("#");
@ -250,6 +134,138 @@ function endMoha()
exit(0);
}
function connect2mqttServer()
{
global $client;
$client->onConnect('connectResponse');
}
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");
// topics definition
listHooks("./topics_callbacks", $hooksList);
if (!empty($hooksList))
{
foreach ($hooksList as $callback)
{
logger(INFO, _("Including ") . $callback, false);
include $callback;
}
}
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
//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);
}else
{
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false);
}
// making the list of hooks to include
listHooks("./hooks", $hooksList);
// Program start
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client();
// defining callback functions
try
{
connect2mqttServer();
}catch ( Mosquitto\Exception $err)
{
logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), false);
sleep (10);
exit(1);
}
$client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
// connectong to mqtt server
$client->connect($mqttServerIp, 1883, 5);
logger(INFO, _("Subscribing to bridge"), false);
foreach($topics as $name => $topic)
{
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
// starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false;
while (true)
{
$client->loop(); // mqtt server loop()
if (! $included) // hooks not already included
{
logger(DEBUG, _("Making hooks list"), 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);
include $hook;
}
file_put_contents("/usr/share/moha/moha.devices", print_r($devices, true)); // debugging : save device list
$included = true;
}
}else
{
if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
{
logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true;
}
if($hooksInitialized == 0) // all hooks are not initialized
{
$i = 1;
foreach($hooks as $hookName => $hook)
{
if ($hook->initialized === false)
{
logger(WARNING, _("Hook not completely initialized :") . $hookName);
$i &= $hook->installHooks();
}
}
$hooksInitialized = $i;
}else
{
logger(DEBUG,_("All hooks initialized"));
}
checkEvents();
}
}
endMoha();
?>