some debugging and separating callbacks of the main program
This commit is contained in:
parent
e702d333c8
commit
e7618f9d86
@ -14,6 +14,7 @@ class topic {
|
|||||||
public $groups;
|
public $groups;
|
||||||
public $extensions;
|
public $extensions;
|
||||||
public $config;
|
public $config;
|
||||||
|
public $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
class device
|
class device
|
||||||
|
100
moha.php
100
moha.php
@ -7,6 +7,7 @@ define("ON", 1);
|
|||||||
define("OFF", 0);
|
define("OFF", 0);
|
||||||
define("AUTO", 0);
|
define("AUTO", 0);
|
||||||
define("MANUAL", 1);
|
define("MANUAL", 1);
|
||||||
|
define( "DEBUG", 16); // => 16
|
||||||
|
|
||||||
declare(ticks = 1);
|
declare(ticks = 1);
|
||||||
|
|
||||||
@ -34,21 +35,50 @@ $logFh = null; // filehandle of log file
|
|||||||
$curlErr = 0; // Number of errors returned by curl
|
$curlErr = 0; // Number of errors returned by curl
|
||||||
$configDir = "./config"; // default config dir (production value is /etc/moha/)
|
$configDir = "./config"; // default config dir (production value is /etc/moha/)
|
||||||
|
|
||||||
// topics definition
|
function logger($level, $log, $notif = true)
|
||||||
$topics["zigbee2mqtt"] = new topic;
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// topics definition
|
||||||
|
logger(DEBUG, _("Require topics definition -> zigbee3mqtt"), false);
|
||||||
|
$topics["zigbee2mqtt"] = new topic;
|
||||||
|
require "topics_callbacks/zigbee2mqtt.php";
|
||||||
|
|
||||||
|
logger(DEBUG, _("Require topics definition -> pws2mqtt"), false);
|
||||||
|
$topics["pws2mqtt"] = new topic;
|
||||||
|
require "topics_callbacks/pws2mqtt.php";
|
||||||
|
|
||||||
|
logger(DEBUG, _("gettext init"), false);
|
||||||
// gettext
|
// gettext
|
||||||
bindtextdomain("moha", "./locale");
|
bindtextdomain("moha", "./locale");
|
||||||
textdomain("moha");
|
textdomain("moha");
|
||||||
|
|
||||||
|
logger(DEBUG, _("lauching init function"), false);
|
||||||
|
|
||||||
if (!init()) exit(1);
|
if (!init()) exit(1);
|
||||||
|
|
||||||
|
logger(DEBUG, _('assigning variable : $client'), false);
|
||||||
$client = new Mosquitto\Client();
|
$client = new Mosquitto\Client();
|
||||||
|
|
||||||
// log levels
|
// log levels
|
||||||
define( "DEBUG", $client::LOG_DEBUG); // => 16
|
//define( "DEBUG", 16); // => 16
|
||||||
define( "INFO", $client::LOG_INFO); // => 1
|
define( "INFO", $client::LOG_INFO); // => 1
|
||||||
define( "NOTICE", $client::LOG_NOTICE); // => 2
|
define( "NOTICE", $client::LOG_NOTICE); // => 2
|
||||||
define( "WARNING", $client::LOG_WARNING); // => 4
|
define( "WARNING", $client::LOG_WARNING); // => 4
|
||||||
@ -58,60 +88,69 @@ define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
|
|||||||
$logLevel = ALL;
|
$logLevel = ALL;
|
||||||
$notificationLevel = WARNING | ERROR;
|
$notificationLevel = WARNING | ERROR;
|
||||||
|
|
||||||
|
logger(DEBUG, _("requiring php modules"), false);
|
||||||
require "utils.php";
|
require "utils.php";
|
||||||
require "mqtt_functions.php";
|
require "mqtt_functions.php";
|
||||||
require "events.php";
|
require "events.php";
|
||||||
require "db_functions.php";
|
require "db_functions.php";
|
||||||
//require "class/availability.php";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
logger(DEBUG, _("requiring config files -> devices_constants.php"), false);
|
||||||
//include predefined file witch define constants for devices
|
//include predefined file witch define constants for devices
|
||||||
if (is_readable($configDir . "/" . "devices_constants.php"))
|
if (is_readable($configDir . "/" . "devices_constants.php"))
|
||||||
{
|
{
|
||||||
$hooksList[] = $configDir . "/" . "devices_constants.php";
|
$hooksList[] = $configDir . "/" . "devices_constants.php";
|
||||||
//echo "hooklist"; print_r($hooksList); echo EOL;
|
//echo "hooklist"; print_r($hooksList); echo EOL;
|
||||||
logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir));
|
logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir), false);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir));
|
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger(DEBUG, _("Program start"), false);
|
||||||
|
// Program start
|
||||||
|
$client = new Mosquitto\Client();
|
||||||
|
|
||||||
// defining callback functions
|
// defining callback functions
|
||||||
$client->onConnect('connectResponse');
|
$client->onConnect('connectResponse');
|
||||||
$client->onDisconnect('disconnectResponse');
|
$client->onDisconnect('disconnectResponse');
|
||||||
$client->onSubscribe('subscribeResponse');
|
$client->onSubscribe('subscribeResponse');
|
||||||
$client->onUnsubscribe('unsubscribeResponse');
|
$client->onUnsubscribe('unsubscribeResponse');
|
||||||
$client->onMessage('message');
|
$client->onMessage('messageReceived');
|
||||||
$client->onLog('logger');
|
$client->onLog('logger');
|
||||||
$client->onPublish('publishResponse');
|
$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);
|
$client->connect("192.168.1.253", 1883, 5);
|
||||||
|
/*while(!$connected)
|
||||||
|
{
|
||||||
|
sleep (1);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
logger(DEBUG, _("Subsribing to bridge"), false);
|
||||||
foreach($topics as $name => $topic)
|
foreach($topics as $name => $topic)
|
||||||
{
|
{
|
||||||
//echo $name;
|
//echo $name;
|
||||||
$topic->mid = $client->subscribe($name . "/bridge/#", 2);
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
||||||
$mids[$topic->mid] = $name;
|
$mids[$topic->mid] = $name;
|
||||||
$topic->status = false;
|
$topic->status = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger(DEBUG, _("Starting loop"), false);
|
||||||
$oneshot = false;
|
$oneshot = false;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
$client->loop();
|
$client->loop();
|
||||||
if ($dbInit == 2 and ! $included)
|
if ($dbInit == 2 and ! $included)
|
||||||
{
|
{
|
||||||
|
logger(DEBUG, _("Making hooks list"), false);
|
||||||
getDevicesValues();
|
getDevicesValues();
|
||||||
loadHooks("./hooks", $hooksList);
|
loadHooks("./hooks", $hooksList);
|
||||||
if (!empty($hooksList))
|
if (!empty($hooksList))
|
||||||
{
|
{
|
||||||
foreach ($hooksList as $hook)
|
foreach ($hooksList as $hook)
|
||||||
{
|
{
|
||||||
logger(INFO, _("Including ") . $hook . EOL);
|
logger(INFO, _("Including ") . $hook, false);
|
||||||
include $hook;
|
include $hook;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,14 +158,16 @@ while (true)
|
|||||||
{
|
{
|
||||||
if ($oneshot === false) // execute once initialization finished
|
if ($oneshot === false) // execute once initialization finished
|
||||||
{
|
{
|
||||||
|
logger(DEBUG, _("Oneshot part of loop"), false);
|
||||||
$oneshot = true;
|
$oneshot = true;
|
||||||
foreach($topics as $name => $topic)
|
/*foreach($topics as $name => $topic)
|
||||||
{
|
{
|
||||||
//echo $name;
|
//echo $name;
|
||||||
|
|
||||||
$topic->mid = $client->subscribe($name . "/#", 2);
|
$topic->mid = $client->subscribe($name . "/#", 2);
|
||||||
$mids[$topic->mid] = $name;
|
$mids[$topic->mid] = $name;
|
||||||
$topic->status = false;
|
$topic->status = false;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
checkEvents();
|
checkEvents();
|
||||||
}
|
}
|
||||||
@ -179,18 +220,21 @@ function endMoha()
|
|||||||
{
|
{
|
||||||
global $topics, $nSubscribed ,$client, $logFh, $connected;
|
global $topics, $nSubscribed ,$client, $logFh, $connected;
|
||||||
$x = 0;
|
$x = 0;
|
||||||
$mid = $client->unsubscribe("#");
|
if ($connected)
|
||||||
|
|
||||||
while ($connected)
|
|
||||||
{
|
{
|
||||||
|
$mid = $client->unsubscribe("#");
|
||||||
|
$client->disconnect();
|
||||||
//echo $nSubscribed;0x00124b0022ebac5c
|
//echo $nSubscribed;0x00124b0022ebac5c
|
||||||
if ( $x++ > 60)
|
while ($connected)
|
||||||
{
|
{
|
||||||
$client->disconnect();
|
if ( $x++ <= 60)
|
||||||
fclose($logFh);
|
{
|
||||||
exit (1);
|
|
||||||
|
fclose($logFh);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
$client->loop();
|
||||||
}
|
}
|
||||||
$client->loop();
|
|
||||||
}
|
}
|
||||||
fclose($logFh);
|
fclose($logFh);
|
||||||
exit(0);
|
exit(0);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function message($message)
|
function messageReceived($message)
|
||||||
{
|
{
|
||||||
global $topics, $logFh, $devices, $included;
|
global $topics, $logFh, $devices, $included;
|
||||||
$topic = explode ("/", $message->topic);
|
$topic = explode ("/", $message->topic);
|
||||||
@ -69,7 +69,7 @@ function message($message)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// payload is an array :
|
// payload is an array :
|
||||||
// $key is parameter => $value is value of the parameter
|
// $key is property => $value is value of the property
|
||||||
|
|
||||||
function publish($topic, $payload, $commande="set", $eventKey)
|
function publish($topic, $payload, $commande="set", $eventKey)
|
||||||
{
|
{
|
||||||
|
8
topics_callbacks/pws2mqtt.php
Normal file
8
topics_callbacks/pws2mqtt.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$topics["pws2mqtt"]->callback = function()
|
||||||
|
{
|
||||||
|
global $topics, $logFh, $devices, $included;
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
68
topics_callbacks/zigbee2mqtt.php
Normal file
68
topics_callbacks/zigbee2mqtt.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
$topics["zigbee2mqtt"]->callback = function()
|
||||||
|
{
|
||||||
|
global $topics, $logFh, $devices, $included;
|
||||||
|
if ($topic[1] == "bridge")
|
||||||
|
{
|
||||||
|
switch ($topic[2])
|
||||||
|
{
|
||||||
|
case "info":
|
||||||
|
$topics[$topic[0]]->info = json_decode($message->payload);
|
||||||
|
break;
|
||||||
|
case "devices":
|
||||||
|
$topics[$topic[0]]->devices = json_decode($message->payload);
|
||||||
|
fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
|
||||||
|
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
|
||||||
|
break;
|
||||||
|
case "groups":
|
||||||
|
$topics[$topic[0]]->groups = json_decode($message->payload);
|
||||||
|
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
|
||||||
|
break;
|
||||||
|
case "extensions":
|
||||||
|
$topics[$topic[0]]->extensions = json_decode($message->payload);
|
||||||
|
break;
|
||||||
|
case "config":
|
||||||
|
$topics[$topic[0]]->config = json_decode($message->payload);
|
||||||
|
break;
|
||||||
|
case "logging":
|
||||||
|
//TODO
|
||||||
|
break;
|
||||||
|
case "state":
|
||||||
|
$topics[$topic[0]]->state = $message->payload;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set" and $included)
|
||||||
|
{
|
||||||
|
$topic = explode ("/", $message->topic, 2); // get topic name
|
||||||
|
$fnTree = explode ("/" , $topic[1]); // get friendlyname
|
||||||
|
echo $topic[0] . " => " . $topic[1] . EOL;
|
||||||
|
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
|
||||||
|
if ($fnTree[array_key_last($fnTree)] == "availability")
|
||||||
|
{
|
||||||
|
unset ($fnTree[array_key_last($fnTree)]);
|
||||||
|
$payloadArray = array("availability" => $message->payload);
|
||||||
|
//print_r($payloadArray);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
$payloadArray = json_decode($message->payload);
|
||||||
|
}
|
||||||
|
$device = & $devices[$topic[0]];
|
||||||
|
foreach($fnTree as $fn)
|
||||||
|
{
|
||||||
|
//print_r($device) ;
|
||||||
|
if (!isset($device[$fn])) //must not exists, but ...
|
||||||
|
{
|
||||||
|
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
|
||||||
|
$device[$fn] = array();
|
||||||
|
$device[$fn]["device"] = new device;
|
||||||
|
//addDevice($device[$fn], $fn, );
|
||||||
|
}
|
||||||
|
$device = & $device[$fn];
|
||||||
|
}
|
||||||
|
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
|
||||||
|
//fwrite($logFh, print_r($msg, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
36
utils.php
36
utils.php
@ -5,11 +5,6 @@ function now()
|
|||||||
return new DateTime("now");
|
return new DateTime("now");
|
||||||
}
|
}
|
||||||
|
|
||||||
function signalHandler($signal)
|
|
||||||
{
|
|
||||||
endMoha();
|
|
||||||
}
|
|
||||||
|
|
||||||
function notify($message)
|
function notify($message)
|
||||||
{
|
{
|
||||||
global $notificationMethods;
|
global $notificationMethods;
|
||||||
@ -21,29 +16,18 @@ function notify($message)
|
|||||||
return $result;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function mktopic($device)
|
function mktopic($device)
|
||||||
{
|
{
|
||||||
return $device->topic . "/" . $device->friendlyName;
|
return $device->topic . "/" . $device->friendlyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger(DEBUG, _("signal handling"), false);
|
||||||
|
//signal handling
|
||||||
|
function signalHandler($signal)
|
||||||
|
{
|
||||||
|
endMoha();
|
||||||
|
}
|
||||||
|
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
|
||||||
|
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
|
||||||
|
pcntl_signal(SIGINT, 'signalHandler');
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user