debugging
This commit is contained in:
107
moha.php
107
moha.php
@ -17,9 +17,6 @@ declare(ticks = 1);
|
||||
$listProperties = array("powerSource" => "batterie");
|
||||
$listPropertiesKeys = array("property");
|
||||
|
||||
|
||||
require "class/main.php";
|
||||
|
||||
//global variables
|
||||
$topics = array(); // list of topics
|
||||
$mids = array(); // list of message IDs
|
||||
@ -30,7 +27,7 @@ $hooks = array(); // array of hooks
|
||||
$notificationMethods = array(); // array of notification methods objects
|
||||
$events = array(); // list of event objects
|
||||
$changed = array(); // list of changed devices
|
||||
$dbInit = false; // flag to indicate that desvices db is initialized
|
||||
$dbInit = 0; // flag to indicate that devices db is initializedl
|
||||
$connected = false; // connected to MQTT server
|
||||
$included = false; // flag indicate scripts are loaded
|
||||
$nSubscribed = 0; // Number of topics subsribed
|
||||
@ -38,9 +35,27 @@ $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/)
|
||||
$properties2log = array("action", "state", "contact", "temperature", "state_l1", "state_l2", "humidity", "current_heating_setpoint", "position", "pressure", "occupancy", "tamper", "illuminance_lux","illuminance", "requested_brightness_level", );
|
||||
$hooksInitialized = 0; // are all hooks initialized ? false/true
|
||||
require $configDir . "/properties2log.php";
|
||||
|
||||
logger(DEBUG, _("lauching init function"), false);
|
||||
if (!init()) exit(1);
|
||||
|
||||
// gettext
|
||||
bindtextdomain("moha", "./locale");
|
||||
textdomain("moha");
|
||||
|
||||
function notify($message)
|
||||
{
|
||||
global $notificationMethods;
|
||||
$result = false;
|
||||
foreach($notificationMethods as $value)
|
||||
{
|
||||
//$result = $result | $value->send($message);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function logger($level, $log, $notif = true)
|
||||
{
|
||||
global $logFh, $logLevel, $notificationLevel;
|
||||
@ -61,22 +76,6 @@ function logger($level, $log, $notif = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
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);
|
||||
|
||||
|
||||
logger(DEBUG, _('assigning variable : $client'), false);
|
||||
$client = new Mosquitto\Client();
|
||||
@ -93,14 +92,20 @@ $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 "mqtt_functions.php";
|
||||
require "events.php";
|
||||
require "db.php";
|
||||
require "db_functions.php";
|
||||
|
||||
logger(DEBUG, _("Loading stored devices datas"));
|
||||
loadDB($devices, "moha.db");
|
||||
|
||||
|
||||
// topics definition
|
||||
loadHooks("./topics_callbacks", $hooksList);
|
||||
listHooks("./topics_callbacks", $hooksList);
|
||||
if (!empty($hooksList))
|
||||
{
|
||||
foreach ($hooksList as $callback)
|
||||
@ -114,14 +119,17 @@ 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";
|
||||
include $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), false);
|
||||
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);
|
||||
|
||||
logger(DEBUG, _("Program start"), false);
|
||||
// Program start
|
||||
$client = new Mosquitto\Client();
|
||||
@ -155,11 +163,11 @@ $oneshot = false;
|
||||
while (true)
|
||||
{
|
||||
$client->loop();
|
||||
if ($dbInit == 2 and ! $included)
|
||||
if (! $included)
|
||||
{
|
||||
logger(DEBUG, _("Making hooks list"), false);
|
||||
getDevicesValues();
|
||||
loadHooks("./hooks", $hooksList);
|
||||
|
||||
if (!empty($hooksList))
|
||||
{
|
||||
foreach ($hooksList as $hook)
|
||||
@ -167,23 +175,38 @@ while (true)
|
||||
logger(INFO, _("Including ") . $hook, false);
|
||||
include $hook;
|
||||
}
|
||||
file_put_contents("moha.devices", print_r($devices, true));
|
||||
$included = true;
|
||||
}
|
||||
}elseif($dbInit == 2 and $included)
|
||||
|
||||
}elseif($included)
|
||||
{
|
||||
if ($oneshot === false) // execute once initialization finished
|
||||
{
|
||||
logger(DEBUG, _("Oneshot part of loop"), false);
|
||||
$oneshot = true;
|
||||
/*foreach($topics as $name => $topic)
|
||||
{
|
||||
//echo $name;
|
||||
|
||||
$topic->mid = $client->subscribe($name . "/#", 2);
|
||||
$mids[$topic->mid] = $name;
|
||||
$topic->status = false;
|
||||
}*/
|
||||
}
|
||||
checkEvents();
|
||||
if($hooksInitialized == 0)
|
||||
{
|
||||
$i = 1;
|
||||
foreach($hooks as $hookName => $hook)
|
||||
{
|
||||
if ($hook->initialized === false)
|
||||
{
|
||||
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;
|
||||
}else
|
||||
{
|
||||
logger(DEBUG,_("All hooks initialized"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -193,7 +216,6 @@ function init()
|
||||
{
|
||||
global $logFile, $logFh, $client;
|
||||
date_default_timezone_set('Europe/Paris');
|
||||
|
||||
if (! $logFh = fopen($logFile, "w") )
|
||||
{
|
||||
echo _("error opening log file");
|
||||
@ -202,9 +224,8 @@ function init()
|
||||
return true;
|
||||
}
|
||||
|
||||
function loadHooks($dir, &$hookList)
|
||||
function listHooks($dir, &$hookList)
|
||||
{
|
||||
global $included;
|
||||
$files = scandir($dir);
|
||||
//print_r($files);
|
||||
|
||||
@ -217,7 +238,7 @@ function loadHooks($dir, &$hookList)
|
||||
if (is_dir($dir . "/" . $file))
|
||||
{
|
||||
//echo "directory : " . $dir . '/' . $file . EOL;
|
||||
loadHooks($dir . '/' . $file, $hookList);
|
||||
listHooks($dir . '/' . $file, $hookList);
|
||||
}elseif (strpos($file, ".php", -4) !== false)
|
||||
{
|
||||
//echo "file : " . $dir . "/" . $file . EOL;
|
||||
@ -229,13 +250,14 @@ function loadHooks($dir, &$hookList)
|
||||
}
|
||||
}
|
||||
//print_r($hookList);
|
||||
$included = true;
|
||||
}
|
||||
|
||||
function endMoha()
|
||||
{
|
||||
global $topics, $nSubscribed ,$client, $logFh, $connected;
|
||||
global $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
|
||||
$x = 0;
|
||||
storeDB($devices, "moha.db");
|
||||
file_put_contents("moha.devices", print_r($devices, true));
|
||||
if ($connected)
|
||||
{
|
||||
$mid = $client->unsubscribe("#");
|
||||
@ -253,6 +275,7 @@ function endMoha()
|
||||
}
|
||||
}
|
||||
fclose($logFh);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user