2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
//Constants
|
|
|
|
define( "EOL", "\n");
|
|
|
|
define("Z2M", "zigbee2mqtt");
|
|
|
|
|
|
|
|
// log levels
|
|
|
|
/*define( "DEBUG", 0);
|
|
|
|
define( "INFO", 1);
|
|
|
|
define( "WARNING", 3);
|
|
|
|
define( "ERROR", 4);*/
|
|
|
|
$logLevel = LOG_DEBUG;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
class Message
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
public $state = false;
|
|
|
|
public $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
class topic {
|
|
|
|
public $mid;
|
|
|
|
public $status;
|
|
|
|
public $info;
|
|
|
|
public $devices;
|
|
|
|
public $groups;
|
|
|
|
public $extensions;
|
|
|
|
public $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
class device
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
public $method; //auto or manual
|
2021-12-30 16:18:32 +01:00
|
|
|
public $topic;
|
|
|
|
public $device;
|
|
|
|
public $ieeeAddress;
|
2021-12-31 14:34:20 +01:00
|
|
|
public $groupID;
|
2021-12-30 16:18:32 +01:00
|
|
|
public $friendlyName;
|
|
|
|
public $powerSource;
|
|
|
|
public $description;
|
|
|
|
public $functions;
|
|
|
|
public $payload;
|
2022-01-02 12:14:30 +01:00
|
|
|
public $availibility;
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
public function set($event)
|
|
|
|
{
|
|
|
|
publish($this, $this->payload, "set", $event);
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
publish($this, $this->payload, "get", $event);
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:32:17 +01:00
|
|
|
class ranges
|
|
|
|
{
|
|
|
|
public $start; //datetime
|
|
|
|
public $end; //datetime
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
class event
|
|
|
|
{
|
|
|
|
public $ieeeAddress;
|
|
|
|
public $topic;
|
|
|
|
public $param;
|
|
|
|
public $value;
|
2021-12-31 23:09:58 +01:00
|
|
|
public $device;
|
2021-12-30 16:18:32 +01:00
|
|
|
public $published;
|
2022-01-01 21:32:17 +01:00
|
|
|
public $dateTimeEvent; // datetime : next occurence for recurrent event
|
|
|
|
public $startDatetime;
|
|
|
|
public $stopDatetime;
|
|
|
|
public $recurrenceInterval; // interval : for recurrent event
|
|
|
|
public $exceptionInterval; // array of object ranges
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class interval
|
|
|
|
{
|
|
|
|
public $startDate;
|
|
|
|
public $endDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
class notificationMethod
|
|
|
|
{
|
|
|
|
public $url;
|
|
|
|
public $msg;
|
|
|
|
|
|
|
|
function __construct($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send($text=null)
|
|
|
|
{
|
|
|
|
if (empty($text)) $text= $this->msg;
|
|
|
|
if (!empty($msg))
|
|
|
|
{
|
|
|
|
$opts = array(
|
|
|
|
'http'=>array(
|
|
|
|
'method'=>"GET",
|
|
|
|
'header'=>"Accept-language: fr\r\n"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = stream_context_create($opts);
|
|
|
|
|
|
|
|
/* Envoi une requête HTTP vers $url
|
|
|
|
avec les en-têtes additionnels ci-dessus */
|
|
|
|
$fp = fopen($url . "/" . $msg, 'r', false, $context);
|
|
|
|
$response = stream_get_contents($fp, -1, 0);
|
|
|
|
fclose($fp);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
// TODO log_error("notificationMethod : $msg is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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-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
|
|
|
|
$nSubscribed = 0; // Number of topics subsribed
|
|
|
|
$included = false; // flag indicate scripts are loaded
|
2022-01-01 06:26:02 +01:00
|
|
|
$logFh = null; // filehandle of log file
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
// topics definition
|
|
|
|
$topics["zigbee2mqtt"] = new topic;
|
|
|
|
|
|
|
|
// gettext
|
|
|
|
bindtextdomain("moha", "./locale");
|
|
|
|
textdomain("moha");
|
|
|
|
|
2021-12-31 14:34:20 +01:00
|
|
|
if (!init()) exit(1);
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2021-12-31 14:34:20 +01:00
|
|
|
require "mqtt_functions.php";
|
|
|
|
require "utils.php";
|
|
|
|
require "events.php";
|
|
|
|
require "db_functions.php";
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
$client = new Mosquitto\Client();
|
|
|
|
$client->onConnect('connectResponse');
|
|
|
|
$client->onDisconnect('disconnectResponse');
|
|
|
|
$client->onSubscribe('subscribeResponse');
|
2022-01-01 07:35:50 +01:00
|
|
|
$client->onUnsubscribe('unsubscribeResponse');
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
$client->onMessage('message');
|
|
|
|
$client->onLog('logger');
|
|
|
|
$client->onPublish('publishResponse');
|
|
|
|
|
|
|
|
|
2021-12-31 14:34:20 +01:00
|
|
|
//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);
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach($topics as $name => $topic)
|
|
|
|
{
|
|
|
|
//echo $name;
|
|
|
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
|
|
|
$mids[$topic->mid] = $name;
|
|
|
|
$topic->status = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
$hooksList = array();
|
|
|
|
loadHooks("./hooks", $hooksList);
|
|
|
|
//print_r($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
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
echo "Including $hook" . EOL;
|
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
|
|
|
{
|
|
|
|
checkEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endMoha();
|
|
|
|
|
|
|
|
function init()
|
|
|
|
{
|
|
|
|
global $logFh, $client;
|
|
|
|
date_default_timezone_set('Europe/Paris');
|
2021-12-31 14:34:20 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
if (! $logFh = fopen("moha.log", "w") )
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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;
|
2021-12-30 16:18:32 +01:00
|
|
|
$hookList[] = $dir . "/" . $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//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-01 07:35:50 +01:00
|
|
|
/*foreach($topics as $topic => $object)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
if ($object->status)
|
2022-01-01 07:35:50 +01:00
|
|
|
{*/
|
|
|
|
$mid = $client->unsubscribe("#");
|
|
|
|
/*$mids[$mid] = $topic;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-01 07:35:50 +01:00
|
|
|
}*/
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-01 07:35:50 +01:00
|
|
|
while ($connected)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
//echo $nSubscribed;0x00124b0022ebac5c
|
2022-01-01 07:35:50 +01:00
|
|
|
if ( $x++ > 60)
|
|
|
|
{
|
|
|
|
$client->disconnect();
|
|
|
|
fclose($logFh);
|
|
|
|
exit (1);
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
$client->loop();
|
|
|
|
}
|
|
|
|
fclose($logFh);
|
2022-01-01 07:35:50 +01:00
|
|
|
exit(0);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|