1
0

debugging hooks\ncleaning code

This commit is contained in:
daniel Tartavel 2022-01-17 21:01:11 +01:00
parent 4fb5504cdc
commit 11a2253804
7 changed files with 61 additions and 91 deletions

View File

@ -42,7 +42,7 @@ class hook
logger(DEBUG, _("Callback already installed")); logger(DEBUG, _("Callback already installed"));
} }
} }
echo "result => "; var_dump($result); //echo "result => "; var_dump($result);
if ($result === true) if ($result === true)
{ {
$this->initialized = true; $this->initialized = true;

View File

@ -80,7 +80,6 @@ function addDevice(& $device, $fn, $jsonDevice )
//indexing device //indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
} }
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
@ -96,7 +95,6 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
if ( isset($value->property)) if ( isset($value->property))
{ {
$string = $value->property; $string = $value->property;
//echo "property ===> " . $value->property . EOL;
$device->{$string}["value"] = null; $device->{$string}["value"] = null;
$device->$string["functions"] = array(); $device->$string["functions"] = array();
} }
@ -134,7 +132,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="") function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
{ {
global $changed, $mohaDB; global $changed, $mohaDB, $testMode;
$deviceType = (gettype($device) == "object"); // = true if object $deviceType = (gettype($device) == "object"); // = true if object
//echo "devicetype = "; var_dump($deviceType); echo EOL; //echo "devicetype = "; var_dump($deviceType); echo EOL;
//print_r($payloadArray); //print_r($payloadArray);
@ -185,8 +183,6 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}else }else
{ {
//echo "db_functions".EOL;
//($device);
if (empty($device->$key) or $value != null) if (empty($device->$key) or $value != null)
{ {
if (property_exists($device, $key)) if (property_exists($device, $key))
@ -209,25 +205,19 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$changed[$fn]["key"] = $key; $changed[$fn]["key"] = $key;
$changed[$fn]["value"] = $value; $changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value)); logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value));
//print_r($device); if ($testMode === false)
//$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); TODO re-activate
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
/*if (empty($oldValue))
{ {
echo "Initializing " . $key; $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
}else }else
{ {
echo "changed " . $key . " value " . $oldValue;; logger(INFO, _("Test mode on: not storing in DB "));
} }
echo " to " . $value . EOL;*/
} }
if (!empty($device->$key["functions"])) if (!empty($device->$key["functions"]))
{ {
logger(DEBUG,_("executing notifications functions")); logger(DEBUG,_("executing notifications functions"));
foreach($device->$key["functions"] as $function) foreach($device->$key["functions"] as $function)
{ {
//print_r($function);
$function($device, $key, $value); $function($device, $key, $value);
} }
} }

View File

@ -2,8 +2,8 @@
class rdc_salon_eclairage extends hook class rdc_salon_eclairage extends hook
{ {
public $hookName = "rdc_salon_eclairage"; public $hookName = "rdc_salon_eclairage";
// list of devices we are listening to
// list of devices we are listening to
protected $devicelist = array( protected $devicelist = array(
RDC_SALON_MVMT => array("occupancy", false), RDC_SALON_MVMT => array("occupancy", false),
RDC_SALON_MVMT2 => array("occupancy", false), RDC_SALON_MVMT2 => array("occupancy", false),
@ -21,9 +21,11 @@ class rdc_salon_eclairage extends hook
public function callBack(&$device, $param, $value) public function callBack(&$device, $param, $value)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
logger(INFO, _("hook : rdc_salon_eclairage"));
switch($param) switch($param)
{ {
case "occupancy": case "occupancy":
//print_r($indexDevices[RDC_SALON_LUMINOSITE]);
if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min)
{ {
$this->send("ON"); $this->send("ON");
@ -36,7 +38,10 @@ class rdc_salon_eclairage extends hook
} }
break; break;
case "illuminance_lux": case "illuminance_lux":
if ($value >= $this->luminance_max) $this->send("OFF"); if ($value >= $this->luminance_max)
{
$this->send("OFF");
}
break; break;
} }
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value)); logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value));

View File

@ -1,5 +1,7 @@
<?php <?php
$title = "moha"; $title = "moha";
$testMode = false;
cli_set_process_title($title); cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title); file_put_contents("/proc/".getmypid()."/comm",$title);
@ -10,7 +12,14 @@ define("ON", 1);
define("OFF", 0); define("OFF", 0);
define("AUTO", 0); define("AUTO", 0);
define("MANUAL", 1); define("MANUAL", 1);
// log levels
define( "DEBUG", 16); // => 16 define( "DEBUG", 16); // => 16
define( "INFO", 1); // => 1
define( "NOTICE", 2); // => 2
define( "WARNING", 4); // => 4
define( "ERROR", 8); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
declare(ticks = 1); declare(ticks = 1);
@ -18,6 +27,8 @@ $listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("property"); $listPropertiesKeys = array("property");
//global variables //global variables
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = WARNING | ERROR;
$topics = array(); // list of topics $topics = array(); // list of topics
$mids = array(); // list of message IDs $mids = array(); // list of message IDs
$devices = array(); // array of device objetcs $devices = array(); // array of device objetcs
@ -36,9 +47,8 @@ $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/)
$hooksInitialized = 0; // are all hooks initialized ? false/true $hooksInitialized = 0; // are all hooks initialized ? false/true
require $configDir . "/properties2log.php";
logger(DEBUG, _("lauching init function"), false);
if (!init()) exit(1); if (!init()) exit(1);
// gettext // gettext
@ -77,29 +87,21 @@ function logger($level, $log, $notif = true)
} }
} }
logger(DEBUG, _('assigning variable : $client'), false);
$client = new Mosquitto\Client();
// log levels
//define( "DEBUG", 16); // => 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;
logger(DEBUG, _("requiring php modules"), false); logger(DEBUG, _("requiring php modules"), false);
require "class/main.php"; require "class/main.php";
require "class/db.php"; require "class/db.php";
require "class/hook_class.php"; require "class/hook_class.php";
require "utils.php"; require "utils.php";
require $configDir . "/properties2log.php";
require "mqtt_functions.php"; require "mqtt_functions.php";
require "events.php"; require "events.php";
require "db_functions.php"; require "db_functions.php";
logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
$client = new Mosquitto\Client();
logger(DEBUG, _("Loading stored devices datas")); logger(DEBUG, _("Loading stored devices datas"));
loadDB($devices, "moha.db"); loadDB($devices, "moha.db");
@ -130,8 +132,8 @@ if (is_readable($configDir . "/" . "devices_constants.php"))
// making the list of hooks to include // making the list of hooks to include
listHooks("./hooks", $hooksList); listHooks("./hooks", $hooksList);
logger(DEBUG, _("Program start"), false);
// Program start // Program start
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client(); $client = new Mosquitto\Client();
// defining callback functions // defining callback functions
@ -143,51 +145,47 @@ $client->onMessage('messageReceived');
$client->onLog('logger'); $client->onLog('logger');
$client->onPublish('publishResponse'); $client->onPublish('publishResponse');
// connectong to mqtt server
$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); logger(INFO, _("Subscribing to bridge"), false);
foreach($topics as $name => $topic) foreach($topics as $name => $topic)
{ {
//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;
} }
logger(DEBUG, _("Starting loop"), false); // starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false; $oneshot = false;
while (true) while (true)
{ {
$client->loop(); $client->loop(); // mqtt server loop()
if (! $included) if (! $included) // hooks not already included
{ {
logger(DEBUG, _("Making hooks list"), false); logger(DEBUG, _("Making hooks list"), false);
getDevicesValues(); getDevicesValues(); // TODO get the values of devices
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
if (!empty($hooksList))
{ {
foreach ($hooksList as $hook) foreach ($hooksList as $hook) // loop to include hooks in hookslist
{ {
logger(INFO, _("Including ") . $hook, false); logger(INFO, _("Including ") . $hook, false);
include $hook; include $hook;
} }
file_put_contents("moha.devices", print_r($devices, true)); file_put_contents("moha.devices", print_r($devices, true)); // debugging : save device list
$included = true; $included = true;
} }
}elseif($included) }else
{ {
if ($oneshot === false) // execute once initialization finished if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
{ {
logger(DEBUG, _("Oneshot part of loop"), false); logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true; $oneshot = true;
} }
checkEvents(); checkEvents();
if($hooksInitialized == 0) if($hooksInitialized == 0) // all hooks are not initialized
{ {
$i = 1; $i = 1;
foreach($hooks as $hookName => $hook) foreach($hooks as $hookName => $hook)
@ -196,10 +194,6 @@ while (true)
{ {
logger(WARNING, _("Hook not completely initialized :") . $hookName); logger(WARNING, _("Hook not completely initialized :") . $hookName);
$i &= $hook->installHooks(); $i &= $hook->installHooks();
echo "hook->initialized";var_dump($hook->initialized);echo EOL;
// (int)$hook->initialized;
var_dump($hook);
echo "i =======> " . $i;var_dump($i); echo EOL;
} }
} }
$hooksInitialized = $i; $hooksInitialized = $i;
@ -227,21 +221,15 @@ function init()
function listHooks($dir, &$hookList) function listHooks($dir, &$hookList)
{ {
$files = scandir($dir); $files = scandir($dir);
//print_r($files);
foreach ($files as $file) foreach ($files as $file)
{ {
//echo "=====> $file" . EOL;
if ($file != "." and $file != "..") if ($file != "." and $file != "..")
{ {
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
if (is_dir($dir . "/" . $file)) if (is_dir($dir . "/" . $file))
{ {
//echo "directory : " . $dir . '/' . $file . EOL;
listHooks($dir . '/' . $file, $hookList); listHooks($dir . '/' . $file, $hookList);
}elseif (strpos($file, ".php", -4) !== false) }elseif (strpos($file, ".php", -4) !== false)
{ {
//echo "file : " . $dir . "/" . $file . EOL;
if (substr($file, -4) == ".php") if (substr($file, -4) == ".php")
{ {
$hookList[] = $dir . "/" . $file; $hookList[] = $dir . "/" . $file;
@ -249,7 +237,6 @@ function listHooks($dir, &$hookList)
} }
} }
} }
//print_r($hookList);
} }
function endMoha() function endMoha()
@ -262,12 +249,10 @@ function endMoha()
{ {
$mid = $client->unsubscribe("#"); $mid = $client->unsubscribe("#");
$client->disconnect(); $client->disconnect();
//echo $nSubscribed;0x00124b0022ebac5c
while ($connected) while ($connected)
{ {
if ( $x++ <= 60) if ( $x++ <= 60)
{ {
fclose($logFh); fclose($logFh);
exit (1); exit (1);
} }
@ -275,7 +260,6 @@ function endMoha()
} }
} }
fclose($logFh); fclose($logFh);
exit(0); exit(0);
} }

View File

@ -6,7 +6,7 @@ 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 = $topics[$topic[0]]->callback;
//logger(DEBUG, "===== topic " . print_r($topic, true)); logger(DEBUG, "topic => " . print_r($topic, true));
$callback($topic, $message); $callback($topic, $message);
} }
@ -15,21 +15,25 @@ function messageReceived($message)
function publish($topic, $payload, $commande="set", $eventKey) function publish($topic, $payload, $commande="set", $eventKey)
{ {
global $client, $mids, $logFh; global $client, $mids, $logFh, $testMode;
//print_r($payload);
$string = $topic . "/" . $commande; $string = $topic . "/" . $commande;
//$mid = $client->publish($string, json_encode($payload) , 2); //TODO activer if ($testMode === false)
if (isset($mids[$mid]))
{ {
//echo "unsetting mids" .EOL; $mid = $client->publish($string, json_encode($payload) , 2);
unset ($mids[$mid]); if (isset($mids[$mid]))
{
//echo "unsetting mids" .EOL;
unset ($mids[$mid]);
}else
{
//echo "setting mids" .EOL;
$mids[$mid] = true;
}
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
}else }else
{ {
//echo "setting mids" .EOL; logger(INFO, _("Test mode on: no publishing "));
$mids[$mid] = true;
} }
//echo $string . " =>>>>>> " . json_encode($payload) . EOL;
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
} }
function connectResponse($r, $message) function connectResponse($r, $message)

View File

@ -1,20 +1,15 @@
<?php <?php
//TODO to test and debug //TODO to test and debug
$topics["linky2mqtt"] = new topic; $topics["linky2mqtt"] = new topic;
/*$devices["linky2mqtt"]["linky"]["device"] = new device;
$device = &$devices["linky2mqtt"]["linky"]["device"];
*/
$topics["linky2mqtt"]->callback = function($topic, $message) $topics["linky2mqtt"]->callback = function($topic, $message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topicName = $topic[0]; $topicName = $topic[0];
$friendlyName = $topic[1]; // get friendlyName $friendlyName = $topic[1]; // get friendlyName
logger(INFO, sprintf(_("Icoming notification of device %s"), $topic[0], $topic[1])); logger(INFO, sprintf(_("Incoming notification of device %s"), $topic[0], $topic[1]));
$device = & $devices[$topic[0]]; $device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload); $payloadArray = json_decode($message->payload);
//print_r($payloadArray);
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ... if (!isset($device[$fn])) //must not exists, but ...
{ {
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
@ -22,11 +17,9 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
$device[$fn]["device"]->type = "mesure"; $device[$fn]["device"]->type = "mesure";
$device[$fn]["device"]->ieeeAddress = $payloadArray["ADSC"]; $device[$fn]["device"]->ieeeAddress = $payloadArray["ADSC"];
//addDevice($device[$fn], $fn, ); $indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
} }
$device = & $device[$fn]; $device = & $device[$fn];
//print_r($device);
changeDevice($topicName, $friendlyName, $device["device"], $payloadArray); changeDevice($topicName, $friendlyName, $device["device"], $payloadArray);
print_r($device["device"]); print_r($device["device"]);
} }

View File

@ -1,17 +1,13 @@
<?php <?php
$topics["pws2mqtt"] = new topic; $topics["pws2mqtt"] = new topic;
/*$devices["pws2mqtt"]["WH2650A"]["device"] = new device;
$device = &$devices["pws2mqtt"]["WH2650A"]["device"];*/
$topics["pws2mqtt"]->callback = function($topic, $message) $topics["pws2mqtt"]->callback = function($topic, $message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$fn = $topic[1]; // get friendlyname $fn = $topic[1]; // get friendlyname
logger(INFO, sprintf(_("Icoming notification of device %s => friendly name : %s"), $topic[0], $topic[1])); logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
$device = & $devices[$topic[0]]; $device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload); $payloadArray = json_decode($message->payload);
//print_r($payloadArray);
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ... if (!isset($device[$fn])) //must not exists, but ...
{ {
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
@ -19,11 +15,9 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
$device[$fn]["device"]->type = $payloadArray->type; $device[$fn]["device"]->type = $payloadArray->type;
$device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress; $device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress;
//addDevice($device[$fn], $fn, ); $indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
} }
//print_r($device);
$device = & $device[$fn]; $device = & $device[$fn];
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//print_r($device["device"]);
} }
?> ?>