1
0
moha/moha.php

269 lines
6.6 KiB
PHP
Raw Normal View History

2021-12-30 16:18:32 +01:00
<?php
$title = "moha";
2022-01-17 21:01:11 +01:00
$testMode = false;
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
2021-12-30 16:18:32 +01:00
//Constants
define( "EOL", "\n");
define("Z2M", "zigbee2mqtt");
define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
define("MANUAL", 1);
2022-01-17 21:01:11 +01:00
// log levels
define( "DEBUG", 16); // => 16
2022-01-17 21:01:11 +01:00
define( "INFO", 1); // => 1
define( "NOTICE", 2); // => 2
define( "WARNING", 4); // => 4
define( "ERROR", 8); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
2021-12-30 16:18:32 +01:00
declare(ticks = 1);
$listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("property");
2021-12-30 16:18:32 +01:00
//global variables
2022-01-17 21:01:11 +01:00
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = WARNING | 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
$indexDevices = array(); // index devices by ieee_address
$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
$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
$nSubscribed = 0; // Number of topics subsribed
2022-01-08 23:45:38 +01:00
$logFile = "/var/log/moha.log"; // Path of log file
2022-01-01 06:26:02 +01:00
$logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl
$configDir = "./config"; // default config dir (production value is /etc/moha/)
2022-01-17 00:18:50 +01:00
$hooksInitialized = 0; // are all hooks initialized ? false/true
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)
{
//$result = $result | $value->send($message);
}
return $result;
}
function logger($level, $log, $notif = true)
{
global $logFh, $logLevel, $notificationLevel;
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
//echo $log .EOL;
if ($level & $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
print ("$level : $log" . EOL);
}
$test = $level & $notificationLevel;
//echo "notif =>" .$notif . EOL;
if (($test != 0) and ($notif === true))
{
if(notify("Moha\n" . $log) === false)
{
logger(INFO, _("Notification not sent"), false);
}
}
}
2021-12-30 16:18:32 +01:00
2022-01-02 13:07:02 +01:00
logger(DEBUG, _("requiring php modules"), false);
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-01-17 21:01:11 +01:00
require $configDir . "/properties2log.php";
2022-01-02 18:14:13 +01:00
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
2022-01-17 21:01:11 +01:00
logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
$client = new Mosquitto\Client();
2022-01-17 00:18:50 +01:00
logger(DEBUG, _("Loading stored devices datas"));
loadDB($devices, "moha.db");
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)
{
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"))
{
2022-01-17 00:18:50 +01:00
include $configDir . "/" . "devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL;
2022-01-17 00:18:50 +01:00
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);
}
2022-01-17 00:18:50 +01:00
// making the list of hooks to include
listHooks("./hooks", $hooksList);
// Program start
2022-01-17 21:01:11 +01:00
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client();
2022-01-02 13:07:02 +01:00
// defining callback functions
2021-12-30 16:18:32 +01:00
$client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$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
$client->connect("192.168.1.253", 1883, 5);
2022-01-17 21:01:11 +01:00
logger(INFO, _("Subscribing to bridge"), false);
2021-12-30 16:18:32 +01:00
foreach($topics as $name => $topic)
{
$topic->mid = $client->subscribe($name . "/#", 2);
2021-12-30 16:18:32 +01:00
$mids[$topic->mid] = $name;
$topic->status = false;
}
2022-01-17 21:01:11 +01:00
// starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false;
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
{
logger(DEBUG, _("Making hooks list"), false);
2022-01-17 21:01:11 +01:00
getDevicesValues(); // TODO get the values of devices
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
2021-12-30 16:18:32 +01:00
{
2022-01-17 21:01:11 +01:00
foreach ($hooksList as $hook) // loop to include hooks in hookslist
2021-12-30 16:18:32 +01:00
{
logger(INFO, _("Including ") . $hook, false);
2021-12-30 16:18:32 +01:00
include $hook;
}
2022-01-17 21:01:11 +01:00
file_put_contents("moha.devices", print_r($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-17 21:01:11 +01:00
if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
{
logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true;
}
2021-12-30 16:18:32 +01:00
checkEvents();
2022-01-17 21:01:11 +01:00
if($hooksInitialized == 0) // all hooks are not initialized
2022-01-17 00:18:50 +01:00
{
$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"));
}
2021-12-30 16:18:32 +01:00
}
}
endMoha();
function init()
{
2022-01-08 23:45:38 +01:00
global $logFile, $logFh, $client;
2021-12-30 16:18:32 +01:00
date_default_timezone_set('Europe/Paris');
2022-01-08 23:45:38 +01:00
if (! $logFh = fopen($logFile, "w") )
2021-12-30 16:18:32 +01:00
{
echo _("error opening log file");
return false;
}
return true;
}
2022-01-17 00:18:50 +01:00
function listHooks($dir, &$hookList)
2021-12-30 16:18:32 +01:00
{
$files = scandir($dir);
foreach ($files as $file)
{
if ($file != "." and $file != "..")
2021-12-30 16:18:32 +01:00
{
if (is_dir($dir . "/" . $file))
2021-12-30 16:18:32 +01:00
{
2022-01-17 00:18:50 +01:00
listHooks($dir . '/' . $file, $hookList);
}elseif (strpos($file, ".php", -4) !== false)
2021-12-30 16:18:32 +01:00
{
2022-01-08 23:45:38 +01:00
if (substr($file, -4) == ".php")
{
$hookList[] = $dir . "/" . $file;
}
2021-12-30 16:18:32 +01:00
}
}
}
}
function endMoha()
{
2022-01-17 00:18:50 +01:00
global $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
2021-12-30 16:18:32 +01:00
$x = 0;
2022-01-17 00:18:50 +01:00
storeDB($devices, "moha.db");
file_put_contents("moha.devices", print_r($devices, true));
if ($connected)
2021-12-30 16:18:32 +01:00
{
$mid = $client->unsubscribe("#");
$client->disconnect();
while ($connected)
{
if ( $x++ <= 60)
{
fclose($logFh);
exit (1);
}
$client->loop();
}
2021-12-30 16:18:32 +01:00
}
fclose($logFh);
exit(0);
2021-12-30 16:18:32 +01:00
}
?>