1
0

récupération après crash

This commit is contained in:
2022-12-05 12:23:43 +01:00
parent 559039b5e3
commit a27db71327
22 changed files with 300 additions and 147 deletions

View File

@ -1,14 +1,17 @@
<?php
$title = "moha";
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
declare(ticks = 1);
$testMode = false;
$apiServerIsActive = true;
require "constants.php";
//Test mode is executing moha from its directory
//no test mode, moha is system wide
if ($testMode)
{
$logLevel = ALL; // INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
@ -28,6 +31,8 @@ if ($testMode)
}
require $configDir . "/config.php";
require $configDir . "/liste_telephones.php";
$listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("expose");
@ -42,6 +47,8 @@ $indexTypes = array();
$indexProperties = array(); // index of properties
$hooksList = array(); // list of hooks to be included
$hooks = array(); // array of hooks
$scripts = array(); // array of user's script
$propertiesLoggers = array(); // array of loggers's callbacks
$topicsCallbacks = array(); // array of topics's callbacks
$notificationMethods = array(); // array of notification methods objects
$events = array(); // list of event objects
@ -58,10 +65,9 @@ $hooksInitialized = 0; // are all hooks initialized ? false/true
$flagHooks = false;
$devicesRequest = false; // set to true when publishing device request to zigbee2mqtt
$presence = array(); // name and status of presence
$timeLoop = new datetime("now");
// initalisation
if (!init()) exit(1);
// (re)démarre le démon de présence
@ -71,6 +77,10 @@ system("sudo /usr/bin/systemctl restart presenceD.service");
bindtextdomain("moha", "./locale");
textdomain("moha");
######################################################################################################################
## Fonctions ##
######################################################################################################################
function notify($message, $device)
{
global $notificationMethods, $defaultUser;
@ -192,10 +202,9 @@ function endMoha()
logger(ERROR, _("Can not store events db" ), __FILE__ . ":" . __LINE__);
}
if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true));
// stop la surveillance
inotify_rm_watch($cwfd, $watch_descriptor);
// Destruction de l'instance inotify
fclose($cwfd);
configWatchStop();
if ($connected)
{
$mid = $client->unsubscribe("#");
@ -219,9 +228,12 @@ function connect2mqttServer()
global $client;
$client->onConnect('connectResponse');
}
######################################################################################################################
## Main program ##
######################################################################################################################
logger(ALERT, _("starting moha"), __FILE__ . ":" . __LINE__);
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"))
@ -247,9 +259,6 @@ require "config/liste_telephones.php";
require "presence.php";
require "apiserver/apiserver.php";
require "configWatch.php";
require $configDir . "/liste_telephones.php";
logger(DEBUG, _("Loading stored events datas from ") . $dataPath . "events.db", __FILE__ . ":" . __LINE__);
if (file_exists($dataPath . "events.db"))
@ -290,6 +299,17 @@ if (!empty($hooksList)) // hooks to include if hooklist is not empty
$included = true;
}
//making list of scripts to include
listHooks("./scripts", $scriptsList);
if (!empty($scriptsList))
{
foreach ($scriptsList as $script)
{
logger(INFO, _("Including ") . $script, __FILE__ . ":" . __LINE__);
include $script;
}
}
logger(DEBUG, _("Loading stored topics datas from ") . $dataPath . "topics.db", __FILE__ . ":" . __LINE__);
if (file_exists($dataPath . "topics.db"))
{
@ -366,16 +386,25 @@ configWatchInit();
while (true)
{
// test if looping is more then 5 second and send and alert
$now = new datetime("now");
$testTime = clone $now;
$testTime->modify("-5 second");
if ($timeLoop < $testTime)
{
logger(ALERT, _("MOHA Looping is > to 5 seconds"), __FILE__ . ":" . __LINE__);
}
$timeLoop = $now;
$client->loop(); // mqtt server loop()
if ($oneshot === false) // execute while the first loop :WARNING hooks can to be not initialized
if ($oneshot === false) // execute while the first loop :WARNING hooks may not be initialized
{
logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false);
pws2mqttGetList();
$oneshot = true;
}
$nn = 0;
if($hooksInitialized == 0) // all hooks are not initialized
{
$i = 1;
@ -394,16 +423,17 @@ while (true)
}
}
$hooksInitialized = $i;
}elseif($flagHooks === false)
/*}elseif($flagHooks === false)
{
logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__);
$flagHooks = true;
}else // executed when hooks initialization finished but database init not finished
$flagHooks = true;*/
}else // executed when hooks initialization finished but database init may be not finished
{
//logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__);
}
checkEvents();
checkTopicsAvailability();
if ($apiServerIsActive) apiServer($read);
// surveillance du dossier de configuration
@ -420,13 +450,11 @@ while (true)
include $configDir . "/" . $event["name"];
}else
{
logger(DEBUG, _("configWatch : mauvaise extension, n'inclus pas ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
logger(DEBUG, _("configWatch : mauvaise extension, je n'inclus pas ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
}
}
}
}
//presence();
}
endMoha();