From 85a7fd7ed988d7a1e05892f43f06631cdfa48fd9 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Wed, 5 Jan 2022 00:01:41 +0100 Subject: [PATCH] some debugging and modifying hooks functions to fetch parameters --- config/devices_constants.php | 28 ++++++++++++++++++++++ db_functions.php | 23 +++++++++++------- hooks/notifiers/notificationfreemobile.php | 8 +++---- hooks/scripts/rdc_salon_eclairage.php | 19 ++++++++++----- hooks/scripts/rdc_sdb_eclairage.php | 4 ++-- moha.php | 21 +++++++++++----- utils.php | 9 ++++--- 7 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 config/devices_constants.php diff --git a/config/devices_constants.php b/config/devices_constants.php new file mode 100644 index 0000000..87e08a4 --- /dev/null +++ b/config/devices_constants.php @@ -0,0 +1,28 @@ + => , ...); */ +/************************************************************************************/ + +$deviceTable = array( + "0x00124b0022ebac5c" => "RDC_SALON_MVMT2", + "0x588e81fffe2cf695" => "RDC_SALON_MVMT", + "0x00124b001f900753" => "RDC_ENTREE_PORTE", + "0x04cf8cdf3c78aff0" => "RDC_SALON_LUMINOSITE", + "0x588e81fffe343e8f" => "RDC_SALON_ECLAIRAGE_PANNEAU", + "0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE", + "0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT", + "0x00124b0022ec05dc" => "RDC_SDB_MVMT", + "0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE" +); + +foreach($deviceTable as $device => $name) +{ + define($name, $device); + logger(INFO, _("defining constant ") . $name); +} + +?> + diff --git a/db_functions.php b/db_functions.php index 5dd193c..6766854 100644 --- a/db_functions.php +++ b/db_functions.php @@ -93,8 +93,10 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) } if ( isset($value->property)) { + $string = $value->property; //echo "property ===> " . $value->property . EOL; - $device->{$value->property} = null; + $device->{$string}["value"] = null; + $device->$string["functions"] = array(); } } //print_r($device); @@ -163,9 +165,14 @@ function iterateDevice($topic, $fn, &$device, $payloadArray) { $oldValue = $device->$key; - }if ($oldValue != $value) + }else { - $device->{$key} = $value; + $device->{$key}["value"] = null; + $device->$key["functions"] = array(); + } + if ($oldValue != $value) + { + $device->$key["value"] = $value; $changed[$fn]["key"] = $key; $changed[$fn]["value"] = $value; @@ -180,13 +187,13 @@ function iterateDevice($topic, $fn, &$device, $payloadArray) echo " to " . $value . EOL;*/ } //print_r($device->functions); print_r($value); - - if (!empty($device->functions)) + print_r($device); + if (!empty($device->$key["functions"])) { - //echo "executing notifications functions " . EOL; - foreach($device->functions as $function) + echo "executing notifications functions " . EOL; + foreach($device->$key["functions"] as $function) { - //print_r($function); + print_r($function); $function($device, $key, $value); } } diff --git a/hooks/notifiers/notificationfreemobile.php b/hooks/notifiers/notificationfreemobile.php index 2df5658..c44b272 100644 --- a/hooks/notifiers/notificationfreemobile.php +++ b/hooks/notifiers/notificationfreemobile.php @@ -25,19 +25,19 @@ class notificationFreemobile //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $result contains the output string - echo "1 curlErr =>"; var_dump($curlErr); echo EOL; + //echo "1 curlErr =>"; var_dump($curlErr); echo EOL; if ($curlErr <= 10) { $result = curl_exec($ch); - echo "1 result => "; var_dump($result);echo EOL; + //echo "1 result => "; var_dump($result);echo EOL; if ($result === false) { $curlErr += 1; logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), false); - }else + }/*else { logger(INFO, sprintf(_("Curl return: %s when sending notification"), $result), false); - } + }*/ } // close curl resource to free up system resources curl_close($ch); diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 5cab8de..8d2e8d2 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -4,7 +4,14 @@ class rdc_salon_eclairage public $hookName = "rdc_salon_eclairage"; public $active = true; // list of devices we are listening to - private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0"); + + private $devicelist = array( + RDC_SALON_MVMT => "occupancy", + RDC_SALON_MVMT2 => "occupancy", + RDC_ENTREE_PORTE => "contact", + RDC_SALON_LUMINOSITE => "illuminance_lux" + ); + public $delay = 3; // amount of time in $timeunit public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year public $luminance_min = 80; @@ -18,9 +25,9 @@ class rdc_salon_eclairage // assigne the function to the sensors devices if ($this->active === true) { - foreach ($this->devicelist as $ieeeAddress) + foreach ($this->devicelist as $ieeeAddress => $param) { - $indexDevices[$ieeeAddress]->functions[] = array($this,"callback"); + $indexDevices[$ieeeAddress]->$param["functions"][] = array($this,"callback"); } } } @@ -32,13 +39,13 @@ class rdc_salon_eclairage switch($param) { case "occupancy": - if ($value == 1 and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min) + if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) { $this->send("ON"); } break; case "contact": - if ($value == false and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min) + if ($value == false and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) { $this->send("ON"); } @@ -54,7 +61,7 @@ class rdc_salon_eclairage { global $devices, $indexDevices; $msg = array("state" => $state); - $device = & $indexDevices["0x588e81fffe343e8f"]; + $device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU]; logger(INFO, sprintf(_("publishing message: %s to %s"), $msg, $device->friendlyName)); $device->payload = $msg; $device->set(null); diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 42c1513..e2cca99 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -23,9 +23,9 @@ class rdc_sdb_eclairage // assigne the function to the sensors devices if ($this->active === true) { - foreach ($this->devicelist as $ieeeAddress) + foreach ($this->devicelist as $ieeeAddress => $param) { - $indexDevices[$ieeeAddress]->functions[] = array($this,"callback"); + $indexDevices[$ieeeAddress]->$param["functions"][] = array($this,"callback"); } } } diff --git a/moha.php b/moha.php index 6aa9292..f2190ee 100644 --- a/moha.php +++ b/moha.php @@ -21,6 +21,7 @@ $topics = array(); // list of topics $mids = array(); // list of message IDs $devices = array(); // array of device objetcs $indexDevices = array(); // index devices by ieee_address +$hooksList = array(); // list of hooks to be included $hooks = array(); // array of hooks $notificationMethods = array(); // array of notification methods objects $events = array(); // list of event objects @@ -31,6 +32,7 @@ $included = false; // flag indicate scripts are loaded $nSubscribed = 0; // Number of topics subsribed $logFh = null; // filehandle of log file $curlErr = 0; // Number of errors returned by curl +$configDir = "./config"; // default config dir (production value is /etc/moha/) // topics definition $topics["zigbee2mqtt"] = new topic; @@ -55,13 +57,23 @@ define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR); $logLevel = ALL; $notificationLevel = WARNING | ERROR; - require "utils.php"; require "mqtt_functions.php"; require "events.php"; require "db_functions.php"; require "class/availability.php"; +//include predefined file witch define constants for devices +if (is_readable($configDir . "/" . "devices_constants.php")) +{ + $hooksList[] = $configDir . "/" . "devices_constants.php"; + //echo "hooklist"; print_r($hooksList); echo EOL; + logger(INFO, sprintf(_("%s/devices_constants.define found, so it will be included :-)"), $configDir)); +}else +{ + logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir)); +} + // defining callback functions $client->onConnect('connectResponse'); $client->onDisconnect('disconnectResponse'); @@ -92,14 +104,12 @@ while (true) $client->loop(); if ($dbInit == 2 and ! $included) { - $hooksList = array(); loadHooks("./hooks", $hooksList); - //print_r($hooksList); if (!empty($hooksList)) { foreach ($hooksList as $hook) { - echo "Including $hook" . EOL; + logger(INFO, _("Including ") . $hook . EOL); include $hook; } } @@ -133,6 +143,7 @@ function loadHooks($dir, &$hookList) global $included; $files = scandir($dir); //print_r($files); + foreach ($files as $file) { //echo "=====> $file" . EOL; @@ -155,8 +166,6 @@ function loadHooks($dir, &$hookList) $included = true; } - - function endMoha() { global $topics, $nSubscribed ,$client, $logFh, $connected; diff --git a/utils.php b/utils.php index 4d9dcbb..d311b37 100644 --- a/utils.php +++ b/utils.php @@ -24,16 +24,15 @@ function notify($message) function logger($level, $log, $notif = true) { global $logFh, $logLevel, $notificationLevel; - echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ; - echo $log .EOL; - $test = $level & $logLevel; - if ($test != 0) + //echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ; + //echo $log .EOL; + if ($level & $logLevel) { fwrite($logFh, "$level : $log" . EOL); print ("$level : $log" . EOL); } $test = $level & $notificationLevel; - echo "test => " . $test . "notif =>" .$notif . EOL; + //echo "notif =>" .$notif . EOL; if (($test != 0) and ($notif === true)) { if(notify("Moha\n" . $log) === false)