202 lines
4.8 KiB
PHP
202 lines
4.8 KiB
PHP
<?php
|
|
|
|
//Constants
|
|
define( "EOL", "\n");
|
|
define("Z2M", "zigbee2mqtt");
|
|
define("ON", 1);
|
|
define("OFF", 0);
|
|
define("AUTO", 0);
|
|
define("MANUAL", 1);
|
|
|
|
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
|
|
$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 = false; // flag to indicate that desvices db is initialized
|
|
$connected = false; // connected to MQTT server
|
|
$included = false; // flag indicate scripts are loaded
|
|
$nSubscribed = 0; // Number of topics subsribed
|
|
$logFh = null; // filehandle of log file
|
|
$curlErr = 0; // Number of errors returned by curl
|
|
$configDir = "./config"; // default config dir (production value is /etc/moha/)
|
|
|
|
// topics definition
|
|
$topics["zigbee2mqtt"] = new topic;
|
|
|
|
// gettext
|
|
bindtextdomain("moha", "./locale");
|
|
textdomain("moha");
|
|
|
|
|
|
|
|
if (!init()) exit(1);
|
|
|
|
$client = new Mosquitto\Client();
|
|
|
|
// log levels
|
|
define( "DEBUG", $client::LOG_DEBUG); // => 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;
|
|
|
|
require "utils.php";
|
|
require "mqtt_functions.php";
|
|
require "events.php";
|
|
require "db_functions.php";
|
|
//require "class/availability.php";
|
|
|
|
//include predefined file witch define constants for devices
|
|
if (is_readable($configDir . "/" . "devices_constants.php"))
|
|
{
|
|
$hooksList[] = $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));
|
|
}else
|
|
{
|
|
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir));
|
|
}
|
|
|
|
// defining callback functions
|
|
$client->onConnect('connectResponse');
|
|
$client->onDisconnect('disconnectResponse');
|
|
$client->onSubscribe('subscribeResponse');
|
|
$client->onUnsubscribe('unsubscribeResponse');
|
|
$client->onMessage('message');
|
|
$client->onLog('logger');
|
|
$client->onPublish('publishResponse');
|
|
|
|
|
|
//signal handling
|
|
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
|
|
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
|
|
pcntl_signal(SIGINT, 'signalHandler');
|
|
|
|
// Program start
|
|
$client->connect("192.168.1.253", 1883, 5);
|
|
foreach($topics as $name => $topic)
|
|
{
|
|
//echo $name;
|
|
$topic->mid = $client->subscribe($name . "/bridge/#", 2);
|
|
$mids[$topic->mid] = $name;
|
|
$topic->status = false;
|
|
}
|
|
$oneshot = false;
|
|
while (true)
|
|
{
|
|
$client->loop();
|
|
if ($dbInit == 2 and ! $included)
|
|
{
|
|
getDevicesValues();
|
|
loadHooks("./hooks", $hooksList);
|
|
if (!empty($hooksList))
|
|
{
|
|
foreach ($hooksList as $hook)
|
|
{
|
|
logger(INFO, _("Including ") . $hook . EOL);
|
|
include $hook;
|
|
}
|
|
}
|
|
}elseif($dbInit == 2 and $included)
|
|
{
|
|
if ($oneshot === false) // execute once initialization finished
|
|
{
|
|
$oneshot = true;
|
|
foreach($topics as $name => $topic)
|
|
{
|
|
//echo $name;
|
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
|
$mids[$topic->mid] = $name;
|
|
$topic->status = false;
|
|
}
|
|
}
|
|
checkEvents();
|
|
}
|
|
}
|
|
|
|
endMoha();
|
|
|
|
function init()
|
|
{
|
|
global $logFh, $client;
|
|
date_default_timezone_set('Europe/Paris');
|
|
|
|
if (! $logFh = fopen("moha.log", "w") )
|
|
{
|
|
echo _("error opening log file");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function loadHooks($dir, &$hookList)
|
|
{
|
|
global $included;
|
|
$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;
|
|
loadHooks($dir . '/' . $file, $hookList);
|
|
}elseif (strpos($file, ".php", -4) !== false)
|
|
{
|
|
//echo "file : " . $dir . "/" . $file . EOL;
|
|
$hookList[] = $dir . "/" . $file;
|
|
}
|
|
}
|
|
}
|
|
//print_r($hookList);
|
|
$included = true;
|
|
}
|
|
|
|
function endMoha()
|
|
{
|
|
global $topics, $nSubscribed ,$client, $logFh, $connected;
|
|
$x = 0;
|
|
$mid = $client->unsubscribe("#");
|
|
|
|
while ($connected)
|
|
{
|
|
//echo $nSubscribed;0x00124b0022ebac5c
|
|
if ( $x++ > 60)
|
|
{
|
|
$client->disconnect();
|
|
fclose($logFh);
|
|
exit (1);
|
|
}
|
|
$client->loop();
|
|
}
|
|
fclose($logFh);
|
|
exit(0);
|
|
}
|
|
|
|
|
|
|
|
?>
|