From 11a22538043671a1b2adafd805432b370752fdde Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Mon, 17 Jan 2022 21:01:11 +0100 Subject: [PATCH] debugging hooks\ncleaning code --- class/hook_class.php | 2 +- db_functions.php | 18 ++----- hooks/scripts/rdc_salon_eclairage.php | 9 +++- moha.php | 76 +++++++++++---------------- mqtt_functions.php | 26 +++++---- topics_callbacks/linky2mqtt.php | 11 +--- topics_callbacks/pws2mqtt.php | 10 +--- 7 files changed, 61 insertions(+), 91 deletions(-) diff --git a/class/hook_class.php b/class/hook_class.php index d418d6b..f67a3a8 100644 --- a/class/hook_class.php +++ b/class/hook_class.php @@ -42,7 +42,7 @@ class hook logger(DEBUG, _("Callback already installed")); } } - echo "result => "; var_dump($result); + //echo "result => "; var_dump($result); if ($result === true) { $this->initialized = true; diff --git a/db_functions.php b/db_functions.php index 67dc1f1..2d530b0 100644 --- a/db_functions.php +++ b/db_functions.php @@ -80,7 +80,6 @@ function addDevice(& $device, $fn, $jsonDevice ) //indexing device $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - //print_r($device); } function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) @@ -96,7 +95,6 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) if ( isset($value->property)) { $string = $value->property; - //echo "property ===> " . $value->property . EOL; $device->{$string}["value"] = null; $device->$string["functions"] = array(); } @@ -134,7 +132,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray) function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="") { - global $changed, $mohaDB; + global $changed, $mohaDB, $testMode; $deviceType = (gettype($device) == "object"); // = true if object //echo "devicetype = "; var_dump($deviceType); echo EOL; //print_r($payloadArray); @@ -185,8 +183,6 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro }else { - //echo "db_functions".EOL; - //($device); if (empty($device->$key) or $value != null) { if (property_exists($device, $key)) @@ -209,25 +205,19 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro $changed[$fn]["key"] = $key; $changed[$fn]["value"] = $value; logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value)); - //print_r($device); - - //$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); TODO re-activate - //echo "oldvalue => " . print_r($oldValue, true) . EOL; - /*if (empty($oldValue)) + if ($testMode === false) { - echo "Initializing " . $key; + $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); }else { - echo "changed " . $key . " value " . $oldValue;; + logger(INFO, _("Test mode on: not storing in DB ")); } - echo " to " . $value . EOL;*/ } if (!empty($device->$key["functions"])) { logger(DEBUG,_("executing notifications functions")); foreach($device->$key["functions"] as $function) { - //print_r($function); $function($device, $key, $value); } } diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index b828427..7fe01d4 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -2,8 +2,8 @@ class rdc_salon_eclairage extends hook { public $hookName = "rdc_salon_eclairage"; - // list of devices we are listening to + // list of devices we are listening to protected $devicelist = array( RDC_SALON_MVMT => 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) { global $devices, $indexDevices; + logger(INFO, _("hook : rdc_salon_eclairage")); switch($param) { case "occupancy": + //print_r($indexDevices[RDC_SALON_LUMINOSITE]); if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) { $this->send("ON"); @@ -36,7 +38,10 @@ class rdc_salon_eclairage extends hook } break; case "illuminance_lux": - if ($value >= $this->luminance_max) $this->send("OFF"); + if ($value >= $this->luminance_max) + { + $this->send("OFF"); + } break; } logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value)); diff --git a/moha.php b/moha.php index c9461de..f06d0da 100644 --- a/moha.php +++ b/moha.php @@ -1,5 +1,7 @@ 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); @@ -18,6 +27,8 @@ $listProperties = array("powerSource" => "batterie"); $listPropertiesKeys = array("property"); //global variables +$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL; +$notificationLevel = WARNING | ERROR; $topics = array(); // list of topics $mids = array(); // list of message IDs $devices = array(); // array of device objetcs @@ -36,9 +47,8 @@ $logFh = null; // filehandle of log file $curlErr = 0; // Number of errors returned by curl $configDir = "./config"; // default config dir (production value is /etc/moha/) $hooksInitialized = 0; // are all hooks initialized ? false/true -require $configDir . "/properties2log.php"; -logger(DEBUG, _("lauching init function"), false); + if (!init()) exit(1); // 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); require "class/main.php"; require "class/db.php"; require "class/hook_class.php"; require "utils.php"; +require $configDir . "/properties2log.php"; require "mqtt_functions.php"; require "events.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")); loadDB($devices, "moha.db"); @@ -130,8 +132,8 @@ if (is_readable($configDir . "/" . "devices_constants.php")) // making the list of hooks to include listHooks("./hooks", $hooksList); -logger(DEBUG, _("Program start"), false); // Program start +logger(DEBUG, _("Program start"), false); $client = new Mosquitto\Client(); // defining callback functions @@ -143,51 +145,47 @@ $client->onMessage('messageReceived'); $client->onLog('logger'); $client->onPublish('publishResponse'); +// connectong to mqtt server $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) { - //echo $name; $topic->mid = $client->subscribe($name . "/#", 2); $mids[$topic->mid] = $name; $topic->status = false; } -logger(DEBUG, _("Starting loop"), false); +// starting main loop +logger(INFO, _("Starting loop"), false); $oneshot = false; while (true) { - $client->loop(); - if (! $included) + $client->loop(); // mqtt server loop() + if (! $included) // hooks not already included { logger(DEBUG, _("Making hooks list"), false); - getDevicesValues(); - - if (!empty($hooksList)) + getDevicesValues(); // TODO get the values of devices + if (!empty($hooksList)) // some hooks to include if hooklist is not empty { - foreach ($hooksList as $hook) + foreach ($hooksList as $hook) // loop to include hooks in hookslist { logger(INFO, _("Including ") . $hook, false); 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; } - }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); $oneshot = true; } checkEvents(); - if($hooksInitialized == 0) + if($hooksInitialized == 0) // all hooks are not initialized { $i = 1; foreach($hooks as $hookName => $hook) @@ -196,10 +194,6 @@ while (true) { logger(WARNING, _("Hook not completely initialized :") . $hookName); $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; @@ -227,21 +221,15 @@ function init() function listHooks($dir, &$hookList) { $files = scandir($dir); - //print_r($files); - foreach ($files as $file) { - //echo "=====> $file" . EOL; if ($file != "." and $file != "..") { - //echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL; if (is_dir($dir . "/" . $file)) { - //echo "directory : " . $dir . '/' . $file . EOL; listHooks($dir . '/' . $file, $hookList); }elseif (strpos($file, ".php", -4) !== false) { - //echo "file : " . $dir . "/" . $file . EOL; if (substr($file, -4) == ".php") { $hookList[] = $dir . "/" . $file; @@ -249,7 +237,6 @@ function listHooks($dir, &$hookList) } } } - //print_r($hookList); } function endMoha() @@ -262,12 +249,10 @@ function endMoha() { $mid = $client->unsubscribe("#"); $client->disconnect(); - //echo $nSubscribed;0x00124b0022ebac5c while ($connected) { if ( $x++ <= 60) { - fclose($logFh); exit (1); } @@ -275,7 +260,6 @@ function endMoha() } } fclose($logFh); - exit(0); } diff --git a/mqtt_functions.php b/mqtt_functions.php index b219e48..8dbfdbe 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -6,7 +6,7 @@ function messageReceived($message) global $topics, $logFh, $devices, $included; $topic = explode ("/", $message->topic); $callback = $topics[$topic[0]]->callback; - //logger(DEBUG, "===== topic " . print_r($topic, true)); + logger(DEBUG, "topic => " . print_r($topic, true)); $callback($topic, $message); } @@ -15,21 +15,25 @@ function messageReceived($message) function publish($topic, $payload, $commande="set", $eventKey) { - global $client, $mids, $logFh; - //print_r($payload); + global $client, $mids, $logFh, $testMode; $string = $topic . "/" . $commande; - //$mid = $client->publish($string, json_encode($payload) , 2); //TODO activer - if (isset($mids[$mid])) + if ($testMode === false) { - //echo "unsetting mids" .EOL; - unset ($mids[$mid]); + $mid = $client->publish($string, json_encode($payload) , 2); + 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 { - //echo "setting mids" .EOL; - $mids[$mid] = true; + logger(INFO, _("Test mode on: no publishing ")); } - //echo $string . " =>>>>>> " . json_encode($payload) . EOL; - logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload)); } function connectResponse($r, $message) diff --git a/topics_callbacks/linky2mqtt.php b/topics_callbacks/linky2mqtt.php index b331bf2..7d6e05a 100644 --- a/topics_callbacks/linky2mqtt.php +++ b/topics_callbacks/linky2mqtt.php @@ -1,20 +1,15 @@ callback = function($topic, $message) { global $topics, $logFh, $devices, $included; $topicName = $topic[0]; $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]]; $payloadArray = json_decode($message->payload); - //print_r($payloadArray); - //print_r($device) ; if (!isset($device[$fn])) //must not exists, but ... { 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"]->type = "mesure"; $device[$fn]["device"]->ieeeAddress = $payloadArray["ADSC"]; - //addDevice($device[$fn], $fn, ); + $indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"]; } $device = & $device[$fn]; - //print_r($device); - changeDevice($topicName, $friendlyName, $device["device"], $payloadArray); print_r($device["device"]); } diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index 7069e96..23f4575 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -1,17 +1,13 @@ callback = function($topic, $message) { global $topics, $logFh, $devices, $included; $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]]; $payloadArray = json_decode($message->payload); - //print_r($payloadArray); - //print_r($device) ; if (!isset($device[$fn])) //must not exists, but ... { 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"]->type = $payloadArray->type; $device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress; - //addDevice($device[$fn], $fn, ); + $indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"]; } - //print_r($device); $device = & $device[$fn]; changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); - //print_r($device["device"]); } ?>