2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
2022-01-10 00:12:30 +01:00
|
|
|
$title = "moha";
|
|
|
|
cli_set_process_title($title);
|
|
|
|
file_put_contents("/proc/".getmypid()."/comm",$title);
|
2022-01-27 18:41:16 +01:00
|
|
|
declare(ticks = 1);
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-27 18:41:16 +01:00
|
|
|
$testMode = true;
|
|
|
|
$webServerIsActive = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-27 18:41:16 +01:00
|
|
|
require "constants.php";
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
$listProperties = array("powerSource" => "batterie");
|
2022-02-12 15:23:58 +01:00
|
|
|
$listPropertiesKeys = array("expose");
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
//global variables
|
2022-02-23 10:23:16 +01:00
|
|
|
|
2022-01-23 09:46:06 +01:00
|
|
|
$notificationLevel = ALERT | ERROR;
|
2021-12-30 16:18:32 +01:00
|
|
|
$topics = array(); // list of topics
|
|
|
|
$mids = array(); // list of message IDs
|
|
|
|
$devices = array(); // array of device objetcs
|
2022-02-12 15:23:58 +01:00
|
|
|
$indexDevices = array(); // index of devices by ieee_address
|
|
|
|
$indexFriendlyNames = array(); // index of devices by freindly name
|
2022-01-05 00:01:41 +01:00
|
|
|
$hooksList = array(); // list of hooks to be included
|
2022-01-01 06:26:02 +01:00
|
|
|
$hooks = array(); // array of hooks
|
|
|
|
$notificationMethods = array(); // array of notification methods objects
|
|
|
|
$events = array(); // list of event objects
|
2022-01-30 00:21:50 +01:00
|
|
|
$monitored = array(); // list of device to watch
|
2022-01-01 06:26:02 +01:00
|
|
|
$changed = array(); // list of changed devices
|
2022-01-17 00:18:50 +01:00
|
|
|
$dbInit = 0; // flag to indicate that devices db is initializedl
|
2021-12-30 16:18:32 +01:00
|
|
|
$connected = false; // connected to MQTT server
|
|
|
|
$included = false; // flag indicate scripts are loaded
|
2022-01-02 13:17:51 +01:00
|
|
|
$nSubscribed = 0; // Number of topics subsribed
|
2022-01-01 06:26:02 +01:00
|
|
|
$logFh = null; // filehandle of log file
|
2022-01-03 21:11:52 +01:00
|
|
|
$curlErr = 0; // Number of errors returned by curl
|
2022-01-05 00:01:41 +01:00
|
|
|
$configDir = "./config"; // default config dir (production value is /etc/moha/)
|
2022-01-23 09:46:06 +01:00
|
|
|
$hooksInitialized = 0; // are all hooks initialized ? false/true
|
2022-01-29 19:58:01 +01:00
|
|
|
$flagHooks = false;
|
2022-02-07 16:58:42 +01:00
|
|
|
|
|
|
|
|
2022-01-23 09:46:06 +01:00
|
|
|
if ($testMode)
|
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
$logLevel = ALL; // INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
|
2022-01-23 09:46:06 +01:00
|
|
|
$mqttServerIp = "192.168.1.253"; // IP address of mqttserver in test mode
|
2022-02-23 10:23:16 +01:00
|
|
|
$dataPath = "./";
|
2022-02-02 21:18:44 +01:00
|
|
|
$logFile = "./moha.log"; // Path of log file
|
|
|
|
$configDir = "./config"; // default config dir (production value is /etc/moha/)
|
|
|
|
}else
|
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT;
|
2022-02-02 21:18:44 +01:00
|
|
|
$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode
|
|
|
|
$dataPath = "/usr/share/moha/";
|
|
|
|
$logFile = "/var/log/moha.log"; // Path of log file
|
2022-02-07 16:58:42 +01:00
|
|
|
$configDir = "/etc/moha"; // default config dir (production value is /etc/moha/)
|
2022-01-23 09:46:06 +01:00
|
|
|
}
|
2022-01-17 21:01:11 +01:00
|
|
|
|
2022-01-08 23:45:38 +01:00
|
|
|
if (!init()) exit(1);
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
// gettext
|
|
|
|
bindtextdomain("moha", "./locale");
|
|
|
|
textdomain("moha");
|
|
|
|
|
|
|
|
function notify($message)
|
|
|
|
{
|
|
|
|
global $notificationMethods;
|
|
|
|
$result = false;
|
|
|
|
foreach($notificationMethods as $value)
|
|
|
|
{
|
2022-01-23 09:46:06 +01:00
|
|
|
$result = $result | $value->send($message);
|
2022-01-17 00:18:50 +01:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
function logger($level, $log, $pos = "", $notif = true)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-01-19 00:22:34 +01:00
|
|
|
global $logFh, $logLevel, $notificationLevel, $logLevels;
|
2022-01-28 23:05:58 +01:00
|
|
|
$logString = date("c") . ' ' . $logLevels[$level] . " " ;
|
|
|
|
if ($pos !== false)
|
|
|
|
{
|
|
|
|
$logString .= $pos;
|
|
|
|
}
|
|
|
|
$logString .= " - " . $log;
|
2022-01-08 11:51:50 +01:00
|
|
|
if ($level & $logLevel)
|
|
|
|
{
|
2022-01-19 00:22:34 +01:00
|
|
|
fwrite($logFh, $logString . EOL);
|
|
|
|
print ($logString . EOL);
|
2022-01-08 11:51:50 +01:00
|
|
|
}
|
|
|
|
$test = $level & $notificationLevel;
|
|
|
|
if (($test != 0) and ($notif === true))
|
|
|
|
{
|
2022-01-19 00:22:34 +01:00
|
|
|
if(notify("Moha\n" . $logString) === false)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false);
|
2022-01-08 11:51:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-23 09:46:06 +01:00
|
|
|
function init()
|
|
|
|
{
|
|
|
|
global $logFile, $logFh, $client;
|
|
|
|
date_default_timezone_set('Europe/Paris');
|
|
|
|
if (! $logFh = fopen($logFile, "w") )
|
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
echo _("error opening log file in ") . getcwd();
|
2022-01-23 09:46:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-02 13:07:02 +01:00
|
|
|
|
2022-01-23 09:46:06 +01:00
|
|
|
function listHooks($dir, &$hookList)
|
|
|
|
{
|
|
|
|
$files = scandir($dir);
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
if ($file != "." and $file != "..")
|
|
|
|
{
|
|
|
|
if (is_dir($dir . "/" . $file))
|
|
|
|
{
|
|
|
|
listHooks($dir . '/' . $file, $hookList);
|
|
|
|
}elseif (strpos($file, ".php", -4) !== false)
|
|
|
|
{
|
|
|
|
if (substr($file, -4) == ".php")
|
|
|
|
{
|
|
|
|
$hookList[] = $dir . "/" . $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function endMoha()
|
|
|
|
{
|
2022-02-02 21:18:44 +01:00
|
|
|
global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected, $dataPath;
|
2022-01-23 09:46:06 +01:00
|
|
|
$x = 0;
|
2022-01-28 23:05:58 +01:00
|
|
|
|
|
|
|
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
|
2022-02-07 16:58:42 +01:00
|
|
|
if (storeDB($devices, $dataPath . "moha.db") === false)
|
|
|
|
{
|
|
|
|
logger(ERROR, _("Can not store db" ), __FILE__ . ":" . __LINE__);
|
|
|
|
}
|
2022-02-02 21:18:44 +01:00
|
|
|
if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true));
|
2022-01-23 09:46:06 +01:00
|
|
|
if ($connected)
|
|
|
|
{
|
|
|
|
$mid = $client->unsubscribe("#");
|
|
|
|
$client->disconnect();
|
|
|
|
while ($connected)
|
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
if ( $x++ >= 60)
|
2022-01-23 09:46:06 +01:00
|
|
|
{
|
|
|
|
fclose($logFh);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
$client->loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($logFh);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect2mqttServer()
|
|
|
|
{
|
|
|
|
global $client;
|
|
|
|
$client->onConnect('connectResponse');
|
|
|
|
}
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(WARNING, _("starting moha"), __FILE__ . ":" . __LINE__);
|
2022-02-07 16:58:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
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), __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);
|
|
|
|
}
|
|
|
|
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG, _("requiring php modules"), __FILE__ . ":" . __LINE__);
|
2022-01-17 00:18:50 +01:00
|
|
|
require "class/main.php";
|
|
|
|
require "class/db.php";
|
|
|
|
require "class/hook_class.php";
|
2022-01-02 18:14:13 +01:00
|
|
|
require "utils.php";
|
2022-02-02 21:18:44 +01:00
|
|
|
require "config/properties2log.php";
|
2022-01-02 18:14:13 +01:00
|
|
|
require "mqtt_functions.php";
|
|
|
|
require "events.php";
|
|
|
|
require "db_functions.php";
|
2022-02-23 10:23:16 +01:00
|
|
|
require "webserver/webserver.php";
|
2022-01-02 18:14:13 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "moha.db", __FILE__ . ":" . __LINE__);
|
2022-02-07 16:58:42 +01:00
|
|
|
if (loadDB($devices, $dataPath . "moha.db") === false)
|
|
|
|
{
|
|
|
|
logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__);
|
|
|
|
}
|
2022-01-17 00:18:50 +01:00
|
|
|
|
|
|
|
|
2022-01-08 23:45:38 +01:00
|
|
|
// topics definition
|
2022-01-17 00:18:50 +01:00
|
|
|
listHooks("./topics_callbacks", $hooksList);
|
2022-01-08 23:45:38 +01:00
|
|
|
if (!empty($hooksList))
|
|
|
|
{
|
|
|
|
foreach ($hooksList as $callback)
|
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Including ") . $callback, __FILE__ . ":" . __LINE__);
|
2022-01-08 23:45:38 +01:00
|
|
|
include $callback;
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 11:51:50 +01:00
|
|
|
|
2022-01-05 00:01:41 +01:00
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
// making the list of hooks to include
|
|
|
|
listHooks("./hooks", $hooksList);
|
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
// Program start
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false);
|
2022-01-08 11:51:50 +01:00
|
|
|
$client = new Mosquitto\Client();
|
|
|
|
|
2022-01-02 13:07:02 +01:00
|
|
|
// defining callback functions
|
2022-01-23 09:46:06 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connect2mqttServer();
|
|
|
|
}catch ( Mosquitto\Exception $err)
|
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), __FILE__ . ":" . __LINE__, false);
|
2022-01-23 09:46:06 +01:00
|
|
|
sleep (10);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
$client->onDisconnect('disconnectResponse');
|
|
|
|
$client->onSubscribe('subscribeResponse');
|
2022-01-01 07:35:50 +01:00
|
|
|
$client->onUnsubscribe('unsubscribeResponse');
|
2022-01-08 11:51:50 +01:00
|
|
|
$client->onMessage('messageReceived');
|
2021-12-30 16:18:32 +01:00
|
|
|
$client->onLog('logger');
|
|
|
|
$client->onPublish('publishResponse');
|
|
|
|
|
2022-01-17 21:01:11 +01:00
|
|
|
// connectong to mqtt server
|
2022-01-23 09:46:06 +01:00
|
|
|
$client->connect($mqttServerIp, 1883, 5);
|
2022-01-08 11:51:50 +01:00
|
|
|
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false);
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach($topics as $name => $topic)
|
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
2021-12-30 16:18:32 +01:00
|
|
|
$mids[$topic->mid] = $name;
|
|
|
|
$topic->status = false;
|
|
|
|
}
|
2022-01-08 11:51:50 +01:00
|
|
|
|
2022-01-17 21:01:11 +01:00
|
|
|
// starting main loop
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, _("Starting loop"), __FILE__ . ":" . __LINE__, false);
|
2022-01-03 21:11:52 +01:00
|
|
|
$oneshot = false;
|
2022-02-23 10:23:16 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2022-01-17 21:01:11 +01:00
|
|
|
$client->loop(); // mqtt server loop()
|
|
|
|
if (! $included) // hooks not already included
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false);
|
2022-02-07 16:58:42 +01:00
|
|
|
|
2022-01-17 21:01:11 +01:00
|
|
|
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-02-07 16:58:42 +01:00
|
|
|
foreach ($hooksList as $hookFile) // loop to include hooks in hookslist
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-02-07 16:58:42 +01:00
|
|
|
logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false);
|
|
|
|
include $hookFile;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-02-02 21:18:44 +01:00
|
|
|
file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list
|
2022-01-17 00:18:50 +01:00
|
|
|
$included = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-17 00:18:50 +01:00
|
|
|
|
2022-01-17 21:01:11 +01:00
|
|
|
}else
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-23 09:46:06 +01:00
|
|
|
|
2022-02-12 15:23:58 +01:00
|
|
|
if ($oneshot === false) // execute while the first loop :WARNING hooks can to be not initialized
|
2022-01-03 21:11:52 +01:00
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false);
|
2022-02-07 16:58:42 +01:00
|
|
|
|
2022-01-03 21:11:52 +01:00
|
|
|
$oneshot = true;
|
|
|
|
}
|
2022-01-17 21:01:11 +01:00
|
|
|
if($hooksInitialized == 0) // all hooks are not initialized
|
2022-01-17 00:18:50 +01:00
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
$i = 1;
|
2022-01-17 00:18:50 +01:00
|
|
|
foreach($hooks as $hookName => $hook)
|
|
|
|
{
|
|
|
|
if ($hook->initialized === false)
|
|
|
|
{
|
2022-02-12 15:23:58 +01:00
|
|
|
logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
|
2022-01-17 00:18:50 +01:00
|
|
|
$i &= $hook->installHooks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$hooksInitialized = $i;
|
2022-01-29 19:58:01 +01:00
|
|
|
}elseif($flagHooks === false)
|
2022-01-17 00:18:50 +01:00
|
|
|
{
|
2022-01-29 19:58:01 +01:00
|
|
|
logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__);
|
|
|
|
$flagHooks = true;
|
2022-02-12 15:23:58 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
}else // executed when hooks initialization finished but database init not finished
|
2022-02-12 15:23:58 +01:00
|
|
|
{
|
2022-02-23 10:23:16 +01:00
|
|
|
//logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__);
|
2022-02-12 15:23:58 +01:00
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
}
|
2022-01-23 09:46:06 +01:00
|
|
|
checkEvents();
|
2022-01-27 18:41:16 +01:00
|
|
|
askWebServer($read);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endMoha();
|
|
|
|
|
|
|
|
?>
|