1
0

pws2mqtt is fonctional\ndebugging

This commit is contained in:
daniel Tartavel 2022-01-08 23:45:38 +01:00
parent e7618f9d86
commit 7acec3a3c4
5 changed files with 56 additions and 17 deletions

View File

@ -138,7 +138,7 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
//echo "device =>";print_r($device);echo EOL; //echo "device =>";print_r($device);echo EOL;
foreach($payloadArray as $key => $value) foreach($payloadArray as $key => $value)
{ {
$oldValue = 0; $oldValue = null;
//echo "key =>"; print_r($key); echo EOL; //echo "key =>"; print_r($key); echo EOL;
//echo "value =>"; print_r($value); echo EOL; //echo "value =>"; print_r($value); echo EOL;
@ -170,7 +170,7 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
$device->{$key}["value"] = null; $device->{$key}["value"] = null;
$device->$key["functions"] = array(); $device->$key["functions"] = array();
} }
if ($oldValue != $value) if ($oldValue !== $value)
{ {
$device->$key["value"] = $value; $device->$key["value"] = $value;
$changed[$fn]["key"] = $key; $changed[$fn]["key"] = $key;

View File

@ -31,10 +31,13 @@ $dbInit = false; // flag to indicate that desvices db is initialized
$connected = false; // connected to MQTT server $connected = false; // connected to MQTT server
$included = false; // flag indicate scripts are loaded $included = false; // flag indicate scripts are loaded
$nSubscribed = 0; // Number of topics subsribed $nSubscribed = 0; // Number of topics subsribed
$logFile = "/var/log/moha.log"; // Path of log file
$logFh = null; // filehandle of log file $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/)
if (!init()) exit(1);
function logger($level, $log, $notif = true) function logger($level, $log, $notif = true)
{ {
global $logFh, $logLevel, $notificationLevel; global $logFh, $logLevel, $notificationLevel;
@ -55,9 +58,7 @@ function logger($level, $log, $notif = true)
} }
} }
} }
/*
// topics definition
logger(DEBUG, _("Require topics definition -> zigbee3mqtt"), false); logger(DEBUG, _("Require topics definition -> zigbee3mqtt"), false);
$topics["zigbee2mqtt"] = new topic; $topics["zigbee2mqtt"] = new topic;
require "topics_callbacks/zigbee2mqtt.php"; require "topics_callbacks/zigbee2mqtt.php";
@ -65,14 +66,14 @@ require "topics_callbacks/zigbee2mqtt.php";
logger(DEBUG, _("Require topics definition -> pws2mqtt"), false); logger(DEBUG, _("Require topics definition -> pws2mqtt"), false);
$topics["pws2mqtt"] = new topic; $topics["pws2mqtt"] = new topic;
require "topics_callbacks/pws2mqtt.php"; require "topics_callbacks/pws2mqtt.php";
*/
logger(DEBUG, _("gettext init"), false); logger(DEBUG, _("gettext init"), false);
// gettext // gettext
bindtextdomain("moha", "./locale"); bindtextdomain("moha", "./locale");
textdomain("moha"); textdomain("moha");
logger(DEBUG, _("lauching init function"), false); logger(DEBUG, _("lauching init function"), false);
if (!init()) exit(1);
logger(DEBUG, _('assigning variable : $client'), false); logger(DEBUG, _('assigning variable : $client'), false);
$client = new Mosquitto\Client(); $client = new Mosquitto\Client();
@ -94,7 +95,16 @@ require "mqtt_functions.php";
require "events.php"; require "events.php";
require "db_functions.php"; require "db_functions.php";
// topics definition
loadHooks("./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); 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
@ -177,10 +187,10 @@ endMoha();
function init() function init()
{ {
global $logFh, $client; global $logFile, $logFh, $client;
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
if (! $logFh = fopen("moha.log", "w") ) if (! $logFh = fopen($logFile, "w") )
{ {
echo _("error opening log file"); echo _("error opening log file");
return false; return false;
@ -197,7 +207,6 @@ function loadHooks($dir, &$hookList)
foreach ($files as $file) foreach ($files as $file)
{ {
//echo "=====> $file" . EOL; //echo "=====> $file" . EOL;
if ($file != "." and $file != "..") if ($file != "." and $file != "..")
{ {
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL; //echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
@ -208,7 +217,10 @@ function loadHooks($dir, &$hookList)
}elseif (strpos($file, ".php", -4) !== false) }elseif (strpos($file, ".php", -4) !== false)
{ {
//echo "file : " . $dir . "/" . $file . EOL; //echo "file : " . $dir . "/" . $file . EOL;
$hookList[] = $dir . "/" . $file; if (substr($file, -4) == ".php")
{
$hookList[] = $dir . "/" . $file;
}
} }
} }
} }

View File

@ -4,6 +4,9 @@ function messageReceived($message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic); $topic = explode ("/", $message->topic);
$callback = $topics[$topic[0]]->callback;
$callback($topic, $message);
/*
if ($topic[1] == "bridge") if ($topic[1] == "bridge")
{ {
switch ($topic[2]) switch ($topic[2])
@ -65,7 +68,7 @@ function messageReceived($message)
} }
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//fwrite($logFh, print_r($msg, true)); //fwrite($logFh, print_r($msg, true));
} }*/
} }
// payload is an array : // payload is an array :

View File

@ -1,8 +1,28 @@
<?php <?php
$topics["pws2mqtt"] = new topic;
$topics["pws2mqtt"]->callback = function() $topics["pws2mqtt"]->callback = function($topic, $message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic, 2); // get topic name
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL;
$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];
print_r($device);
}
$payloadArray = json_decode($message->payload);
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
print_r($device["device"]);
} }
?> ?>

View File

@ -1,5 +1,6 @@
<?php <?php
$topics["zigbee2mqtt"]->callback = function()
$callback = function($topic, $message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
if ($topic[1] == "bridge") if ($topic[1] == "bridge")
@ -64,5 +65,8 @@ $topics["zigbee2mqtt"]->callback = function()
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//fwrite($logFh, print_r($msg, true)); //fwrite($logFh, print_r($msg, true));
} }
} };
$topics["zigbee2mqtt"] = new topic;
$topics["zigbee2mqtt"]->callback = & $callback;
?> ?>