From 4b226c1d0a07eaa68c86cc15c01be91be88b3f1a Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Sun, 30 Jan 2022 00:21:50 +0100 Subject: [PATCH] added notify command to webserver --- class/main.php | 47 +++++++++++++++++++++++++++++ db_functions.php | 2 +- hooks/scripts/rdc_sdb_eclairage.php | 2 +- moha.php | 1 + utils.php | 26 ++++++++++++++++ watch.php | 11 +++++++ webserver.php | 16 ++++++---- 7 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 watch.php diff --git a/class/main.php b/class/main.php index f08ab0b..14ece1c 100644 --- a/class/main.php +++ b/class/main.php @@ -73,6 +73,53 @@ class event public $exceptionInterval; // array of object ranges } +class watch +{ + public $topic; + public $property; + public $device; + public $function; + private $conditions = array( + "<", ">", "==", ">=", "<=" + ); + + public function __construct($topic, $fn, $property, $condition, $value) + { + logger(DEBUG, _("New Notify object"), __FILE__ . ":" . __LINE__); + if (($this->device = getDevice($topic, $fn)) === false) + { + $this->topic = $topic; + if (array_key_exist($property, $this->device)) + { + $this->property = $property; + if ( !is_numeric($value)) + { + logger(ERROR, _("Value is not numeric"), __FILE__ . ":" . __LINE__ ); + return false; + } + if (array_search($condition, $conditions)) + { + $this->function = '$arg ' . $condition . " " . $value; + logger(DEBUG, _("Test function is ") . $this->function); + }else + { + logger(ERROR, _("Condition is not one of the permitted once"), __FILE__ . ":" . __LINE__ ); + return false; + } + }else + { + logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ ); + return false; + } + }else + { + logger(ERROR, _("Device do not exists"), __FILE__ . ":" . __LINE__ ); + return false; + } + return $this; + } +} + class interval { public $startDate; diff --git a/db_functions.php b/db_functions.php index 0add78c..d24904c 100644 --- a/db_functions.php +++ b/db_functions.php @@ -125,7 +125,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray) iterateDevice($topic, $fn, $device, $device, $payloadArray); }else { - logger(ERROR, _("payloadArray is empty!"), __FILE__ . ":" . __LINE__); + logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__); } } diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 775d04b..43f9be1 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -17,7 +17,7 @@ class rdc_sdb_eclairage extends hook // devicelist[$ieeAddress][0] => property to watch // devicelist[$ieeAddress][1] => initialized = true protected $devicelist = array( - RDC_SDB_DOUCHE_MVMT => array("occupancy", false), + //RDC_SDB_DOUCHE_MVMT => array("occupancy", false), RDC_SDB_PLAFOND_MVMT => array("occupancy", false), RDC_SDB_MVMT => array("occupancy", false), RDC_SDB_WC_ECLAIRAGE => array("state_l1", false) diff --git a/moha.php b/moha.php index cce2504..66d30cd 100644 --- a/moha.php +++ b/moha.php @@ -23,6 +23,7 @@ $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 +$monitored = array(); // list of device to watch $changed = array(); // list of changed devices $dbInit = 0; // flag to indicate that devices db is initializedl $connected = false; // connected to MQTT server diff --git a/utils.php b/utils.php index 00c1953..febe2a5 100644 --- a/utils.php +++ b/utils.php @@ -41,6 +41,32 @@ function mktopic($device) return $device->topic . "/" . $device->friendlyName; } +function getDevice($topic, $fn) +{ + global $topics, $devices; + if (array_key_exists($topic, $topics)) + { + $var = $devices[$topic]; + $path = explode("/", $fn); + foreach($path as $tmp) + { + if (array_key_exists($tmp, $var)) + { + $var = $var[$tmp]; + }else + { + return false; + } + + } + return $var; + }else + { + return false; + } +} + + function getValue($ieeeAddress, $property) { global $indexDevices; diff --git a/watch.php b/watch.php new file mode 100644 index 0000000..395a8b2 --- /dev/null +++ b/watch.php @@ -0,0 +1,11 @@ + diff --git a/webserver.php b/webserver.php index 04ce248..4a3102d 100644 --- a/webserver.php +++ b/webserver.php @@ -72,12 +72,7 @@ function askWebServer($read) { if(!empty($topic)) { - $var = $var[$topic]; - $path = explode("/", $argList["fn"]); - foreach($path as $tmp) - { - $var = $var[$tmp]; - } + $var = getDevice($topic, $argList["fn"]); }else { $str = _("topic is not defining: add &topic=\nThese topics are availables: "); @@ -103,6 +98,15 @@ function askWebServer($read) } fwrite($spawn, $response); break; + case "notify": + if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList)) + { + fwrite($spawn, _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value")); + }else + { + $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); + } + break; default: logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); fwrite($spawn, _("unknown command"));