285 lines
6.9 KiB
PHP
285 lines
6.9 KiB
PHP
<?php
|
|
$title = "moha";
|
|
cli_set_process_title($title);
|
|
file_put_contents("/proc/".getmypid()."/comm",$title);
|
|
|
|
//Constants
|
|
define( "EOL", "\n");
|
|
define("Z2M", "zigbee2mqtt");
|
|
define("ON", 1);
|
|
define("OFF", 0);
|
|
define("AUTO", 0);
|
|
define("MANUAL", 1);
|
|
define( "DEBUG", 16); // => 16
|
|
|
|
declare(ticks = 1);
|
|
|
|
$listProperties = array("powerSource" => "batterie");
|
|
$listPropertiesKeys = array("property");
|
|
|
|
//global variables
|
|
$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
|
|
$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 = 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
|
|
$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
|
|
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;
|
|
//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);
|
|
}
|
|
}
|
|
}
|
|
|
|
logger(DEBUG, _('assigning variable : $client'), false);
|
|
$client = new Mosquitto\Client();
|
|
|
|
// log levels
|
|
//define( "DEBUG", 16); // => 16
|
|
define( "INFO", $client::LOG_INFO); // => 1
|
|
define( "NOTICE", $client::LOG_NOTICE); // => 2
|
|
define( "WARNING", $client::LOG_WARNING); // => 4
|
|
define( "ERROR", $client::LOG_ERR); // => 8
|
|
define( "ALERT", 32);
|
|
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
|
|
$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_functions.php";
|
|
|
|
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);
|
|
|
|
logger(DEBUG, _("Program start"), false);
|
|
// Program start
|
|
$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');
|
|
|
|
$client->connect("192.168.1.253", 1883, 5);
|
|
/*while(!$connected)
|
|
{
|
|
sleep (1);
|
|
}*/
|
|
|
|
logger(DEBUG, _("Subsribing to bridge"), false);
|
|
foreach($topics as $name => $topic)
|
|
{
|
|
//echo $name;
|
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
|
$mids[$topic->mid] = $name;
|
|
$topic->status = false;
|
|
}
|
|
|
|
logger(DEBUG, _("Starting loop"), false);
|
|
$oneshot = false;
|
|
while (true)
|
|
{
|
|
$client->loop();
|
|
if (! $included)
|
|
{
|
|
logger(DEBUG, _("Making hooks list"), false);
|
|
getDevicesValues();
|
|
|
|
if (!empty($hooksList))
|
|
{
|
|
foreach ($hooksList as $hook)
|
|
{
|
|
logger(INFO, _("Including ") . $hook, false);
|
|
include $hook;
|
|
}
|
|
file_put_contents("moha.devices", print_r($devices, true));
|
|
$included = true;
|
|
}
|
|
|
|
}elseif($included)
|
|
{
|
|
if ($oneshot === false) // execute once initialization finished
|
|
{
|
|
logger(DEBUG, _("Oneshot part of loop"), false);
|
|
$oneshot = true;
|
|
}
|
|
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"));
|
|
}
|
|
}
|
|
}
|
|
|
|
endMoha();
|
|
|
|
function init()
|
|
{
|
|
global $logFile, $logFh, $client;
|
|
date_default_timezone_set('Europe/Paris');
|
|
if (! $logFh = fopen($logFile, "w") )
|
|
{
|
|
echo _("error opening log file");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function listHooks($dir, &$hookList)
|
|
{
|
|
$files = scandir($dir);
|
|
//print_r($files);
|
|
|
|
foreach ($files as $file)
|
|
{
|
|
//echo "=====> $file" . EOL;
|
|
if ($file != "." and $file != "..")
|
|
{
|
|
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
|
|
if (is_dir($dir . "/" . $file))
|
|
{
|
|
//echo "directory : " . $dir . '/' . $file . EOL;
|
|
listHooks($dir . '/' . $file, $hookList);
|
|
}elseif (strpos($file, ".php", -4) !== false)
|
|
{
|
|
//echo "file : " . $dir . "/" . $file . EOL;
|
|
if (substr($file, -4) == ".php")
|
|
{
|
|
$hookList[] = $dir . "/" . $file;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//print_r($hookList);
|
|
}
|
|
|
|
function endMoha()
|
|
{
|
|
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("#");
|
|
$client->disconnect();
|
|
//echo $nSubscribed;0x00124b0022ebac5c
|
|
while ($connected)
|
|
{
|
|
if ( $x++ <= 60)
|
|
{
|
|
|
|
fclose($logFh);
|
|
exit (1);
|
|
}
|
|
$client->loop();
|
|
}
|
|
}
|
|
fclose($logFh);
|
|
|
|
exit(0);
|
|
}
|
|
|
|
|
|
|
|
?>
|