2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
//Constants
|
|
|
|
define( "EOL", "\n");
|
|
|
|
define("Z2M", "zigbee2mqtt");
|
2022-01-03 21:11:52 +01:00
|
|
|
define("ON", 1);
|
|
|
|
define("OFF", 0);
|
|
|
|
define("AUTO", 0);
|
|
|
|
define("MANUAL", 1);
|
2022-01-08 11:51:50 +01:00
|
|
|
define( "DEBUG", 16); // => 16
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
declare(ticks = 1);
|
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
$listProperties = array("powerSource" => "batterie");
|
|
|
|
$listPropertiesKeys = array("property");
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-02 18:14:13 +01:00
|
|
|
|
|
|
|
require "class/main.php";
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
//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
|
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
|
|
|
|
$changed = array(); // list of changed devices
|
2021-12-30 16:18:32 +01:00
|
|
|
$dbInit = false; // flag to indicate that desvices db is initialized
|
|
|
|
$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-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
|
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/)
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-08 23:45:38 +01:00
|
|
|
if (!init()) exit(1);
|
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 23:45:38 +01:00
|
|
|
/*
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("Require topics definition -> zigbee3mqtt"), false);
|
2021-12-30 16:18:32 +01:00
|
|
|
$topics["zigbee2mqtt"] = new topic;
|
2022-01-08 11:51:50 +01:00
|
|
|
require "topics_callbacks/zigbee2mqtt.php";
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("Require topics definition -> pws2mqtt"), false);
|
|
|
|
$topics["pws2mqtt"] = new topic;
|
|
|
|
require "topics_callbacks/pws2mqtt.php";
|
2022-01-08 23:45:38 +01:00
|
|
|
*/
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("gettext init"), false);
|
2021-12-30 16:18:32 +01:00
|
|
|
// gettext
|
|
|
|
bindtextdomain("moha", "./locale");
|
|
|
|
textdomain("moha");
|
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("lauching init function"), false);
|
2022-01-08 23:45:38 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _('assigning variable : $client'), false);
|
2021-12-30 16:18:32 +01:00
|
|
|
$client = new Mosquitto\Client();
|
2022-01-02 13:07:02 +01:00
|
|
|
|
|
|
|
// log levels
|
2022-01-08 11:51:50 +01:00
|
|
|
//define( "DEBUG", 16); // => 16
|
2022-01-03 21:11:52 +01:00
|
|
|
define( "INFO", $client::LOG_INFO); // => 1
|
|
|
|
define( "NOTICE", $client::LOG_NOTICE); // => 2
|
|
|
|
define( "WARNING", $client::LOG_WARNING); // => 4
|
|
|
|
define( "ERROR", $client::LOG_ERR); // => 8
|
2022-01-06 13:03:26 +01:00
|
|
|
define( "ALERT", 32);
|
|
|
|
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
|
2022-01-03 21:11:52 +01:00
|
|
|
$logLevel = ALL;
|
2022-01-02 13:47:41 +01:00
|
|
|
$notificationLevel = WARNING | ERROR;
|
2022-01-02 13:07:02 +01:00
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("requiring php modules"), false);
|
2022-01-02 18:14:13 +01:00
|
|
|
require "utils.php";
|
|
|
|
require "mqtt_functions.php";
|
|
|
|
require "events.php";
|
|
|
|
require "db_functions.php";
|
|
|
|
|
2022-01-08 23:45:38 +01:00
|
|
|
// topics definition
|
|
|
|
loadHooks("./topics_callbacks", $hooksList);
|
|
|
|
if (!empty($hooksList))
|
|
|
|
{
|
|
|
|
foreach ($hooksList as $callback)
|
|
|
|
{
|
|
|
|
logger(INFO, _("Including ") . $callback, false);
|
|
|
|
include $callback;
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 11:51:50 +01:00
|
|
|
|
|
|
|
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
|
2022-01-05 00:01:41 +01:00
|
|
|
//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;
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir), false);
|
2022-01-05 00:01:41 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false);
|
2022-01-05 00:01:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("Program start"), false);
|
|
|
|
// Program start
|
|
|
|
$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');
|
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');
|
|
|
|
|
2021-12-31 14:34:20 +01:00
|
|
|
$client->connect("192.168.1.253", 1883, 5);
|
2022-01-08 11:51:50 +01:00
|
|
|
/*while(!$connected)
|
|
|
|
{
|
|
|
|
sleep (1);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
logger(DEBUG, _("Subsribing to bridge"), false);
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach($topics as $name => $topic)
|
|
|
|
{
|
|
|
|
//echo $name;
|
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
|
|
|
|
|
|
|
logger(DEBUG, _("Starting loop"), false);
|
2022-01-03 21:11:52 +01:00
|
|
|
$oneshot = false;
|
2021-12-30 16:18:32 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
$client->loop();
|
2021-12-31 23:09:58 +01:00
|
|
|
if ($dbInit == 2 and ! $included)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("Making hooks list"), false);
|
2022-01-06 13:03:26 +01:00
|
|
|
getDevicesValues();
|
2021-12-31 23:09:58 +01:00
|
|
|
loadHooks("./hooks", $hooksList);
|
|
|
|
if (!empty($hooksList))
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
foreach ($hooksList as $hook)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(INFO, _("Including ") . $hook, false);
|
2021-12-30 16:18:32 +01:00
|
|
|
include $hook;
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 23:09:58 +01:00
|
|
|
}elseif($dbInit == 2 and $included)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-03 21:11:52 +01:00
|
|
|
if ($oneshot === false) // execute once initialization finished
|
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
logger(DEBUG, _("Oneshot part of loop"), false);
|
2022-01-03 21:11:52 +01:00
|
|
|
$oneshot = true;
|
2022-01-08 11:51:50 +01:00
|
|
|
/*foreach($topics as $name => $topic)
|
2022-01-06 13:03:26 +01:00
|
|
|
{
|
|
|
|
//echo $name;
|
2022-01-08 11:51:50 +01:00
|
|
|
|
2022-01-06 13:03:26 +01:00
|
|
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
|
|
|
$mids[$topic->mid] = $name;
|
|
|
|
$topic->status = false;
|
2022-01-08 11:51:50 +01:00
|
|
|
}*/
|
2022-01-03 21:11:52 +01:00
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
checkEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2021-12-31 14:34:20 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
function loadHooks($dir, &$hookList)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
global $included;
|
|
|
|
$files = scandir($dir);
|
|
|
|
//print_r($files);
|
2022-01-05 00:01:41 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
//echo "=====> $file" . EOL;
|
|
|
|
if ($file != "." and $file != "..")
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
|
|
|
|
if (is_dir($dir . "/" . $file))
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
//echo "directory : " . $dir . '/' . $file . EOL;
|
|
|
|
loadHooks($dir . '/' . $file, $hookList);
|
|
|
|
}elseif (strpos($file, ".php", -4) !== false)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
//echo "file : " . $dir . "/" . $file . EOL;
|
2022-01-08 23:45:38 +01:00
|
|
|
if (substr($file, -4) == ".php")
|
|
|
|
{
|
|
|
|
$hookList[] = $dir . "/" . $file;
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//print_r($hookList);
|
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function endMoha()
|
|
|
|
{
|
2022-01-01 07:35:50 +01:00
|
|
|
global $topics, $nSubscribed ,$client, $logFh, $connected;
|
2021-12-30 16:18:32 +01:00
|
|
|
$x = 0;
|
2022-01-08 11:51:50 +01:00
|
|
|
if ($connected)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
$mid = $client->unsubscribe("#");
|
|
|
|
$client->disconnect();
|
2021-12-30 16:18:32 +01:00
|
|
|
//echo $nSubscribed;0x00124b0022ebac5c
|
2022-01-08 11:51:50 +01:00
|
|
|
while ($connected)
|
2022-01-01 07:35:50 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
if ( $x++ <= 60)
|
|
|
|
{
|
|
|
|
|
|
|
|
fclose($logFh);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
$client->loop();
|
2022-01-01 07:35:50 +01:00
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
fclose($logFh);
|
2022-01-01 07:35:50 +01:00
|
|
|
exit(0);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|