From df94a6becf380d9b01378dd7718a700aaeee0ed6 Mon Sep 17 00:00:00 2001 From: Daniel Tartavel Date: Fri, 12 Aug 2022 10:41:55 +0200 Subject: [PATCH] debugged notification of availability of devices --- apiserver/apiserver.php | 5 +- apiserver/cmd_functions.php | 67 ++++++++++++---------- class/hook_class.php | 28 ++++++--- config/logger.php | 5 ++ constants.php | 1 - hooks/notifiers/notificationfreemobile.php | 10 +++- hooks/scripts/availability.php | 32 +++++++---- hooks/scripts/radiateurs.php | 5 ++ hooks/scripts/rdc_chambre_eclairage.php | 5 ++ hooks/scripts/rdc_portes_ouvertes.php | 5 ++ hooks/scripts/rdc_salon_eclairage.php | 5 ++ hooks/scripts/rdc_sdb_eclairage.php | 6 ++ hooks/scripts/rdc_store.php | 5 ++ hooks/scripts/rdc_store.php~ | 9 ++- hooks/scripts/rdc_temperature_int_ext.php | 19 ++++-- hooks/scripts/rdc_wc_eclairage.php | 5 ++ hooks/scripts/rdc_wc_eclairage.php~ | 5 ++ moha.php | 29 +++++++--- 18 files changed, 176 insertions(+), 70 deletions(-) create mode 100644 config/logger.php diff --git a/apiserver/apiserver.php b/apiserver/apiserver.php index 1baf94c..bc32968 100644 --- a/apiserver/apiserver.php +++ b/apiserver/apiserver.php @@ -153,11 +153,14 @@ function apiServer($read) apiDashboard($spawn, $argList[0]); }else { - logger(DEBUG, $command . _(" so default action"), __FILE__ . ":" . __LINE__); + logger(DEBUG, $command . _(" does not exists ... so default action"), __FILE__ . ":" . __LINE__); if (file_exists("php://temp/". $command)) { logger(DEBUG, $command . _(" is a file"), __FILE__ . ":" . __LINE__); htmlSend($spawn, file_get_contents("php://temp/". $command), 'Content-Type: image/png'); + }else + { + htmlSend($spawn, "command does not exists"); } } } diff --git a/apiserver/cmd_functions.php b/apiserver/cmd_functions.php index bb1ed46..26f9442 100644 --- a/apiserver/cmd_functions.php +++ b/apiserver/cmd_functions.php @@ -342,7 +342,7 @@ function apiSet($argList) $device = $indexFriendlyNames[$argList["fn"]]; $device->payload = array($argList["property"] => $argList["value"]); $device->set($argList["property"], IDLE); - publish($argList["topic"] . "/" . $argList["fn"], $payload, "set"); + publish($argList["topic"] . "/" . $argList["fn"], $device->payload, "set"); //removeEvent($indexFriendlyNames($argList["fn"]), $argList["property"], "OFF"); } return $response; @@ -482,41 +482,48 @@ function PropertiesDashboard($argList) logger(INFO, _("propertiesDashboard function"), __FILE__ . ":" . __LINE__); $response = insertJavascript(); $property = $argList["property"]; - foreach ($indexProperties[$property] as $device) + print_r($indexProperties[$property]); + foreach($indexProperties as $propertyName => $index) { - $propertyObject = $device->properties[$property]; - $value = $propertyObject["value"]; - logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__); - $response .= $device->friendlyName . aliases($device->friendlyName, $property) . ' => '; - if (array_key_exists("access", $propertyObject)) + if (str_contains($propertyName, $property )) { - logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__); - if(($propertyObject["access"] & 2)) + foreach($index as $device) { - logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__); - $response .= "     " . displayChoice($device, $property, $value); + $propertyObject = $device->properties[$propertyName]; + $value = $propertyObject["value"]; + logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__); + $response .= $device->friendlyName . aliases($device->friendlyName, $propertyName) . ' => '; + if (array_key_exists("access", $propertyObject)) + { + logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__); + if(($propertyObject["access"] & 2)) + { + logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__); + $response .= "     " . displayChoice($device, $propertyName, $value); + } + if(($propertyObject["access"] & 4)) + { + logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__); + $response .= 'friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">"; + }else + { + $response .= $value; + } + $response .= EOLH; + }else + { + $response .= bool2string(_($value)); + if(array_key_exists("unit", $propertyObject)) + { + $response .= $propertyObject["unit"]; + logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__); + } + $response .= EOLH; + } } - if(($propertyObject["access"] & 4)) - { - logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__); - $response .= 'friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">"; - }else - { - $response .= $value; - } - $response .= EOLH; - }else - { - $response .= bool2string(_($value)); - if(array_key_exists("unit", $propertyObject)) - { - $response .= $propertyObject["unit"]; - logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__); - } - $response .= EOLH; } } - return "json=" . $response; + return $response; } function apiHookActive($argList, $status) diff --git a/class/hook_class.php b/class/hook_class.php index a1075f4..72b9d62 100644 --- a/class/hook_class.php +++ b/class/hook_class.php @@ -10,19 +10,31 @@ class hook // list of devices we are listening to function __construct() { - logger(INFO, _("Initializing hook: ") . $this->hookName); - //$this->installHooks(); - foreach ($this->devicelist as $ieeeAddress => $property) + if ($this->active === true) { - $this->propertyInitialized[$ieeeAddress][$property] = false; - } - if (method_exists($this, "init")) + logger(INFO, _("Initializing hook: ") . $this->hookName); + //$this->installHooks(); + if (method_exists($this, "init")) + { + $this->init(); + } + if (method_exists($this, "iterate")) + { + $this->iterate(); + } + foreach ($this->devicelist as $ieeeAddress => $property) + { + $this->propertyInitialized[$ieeeAddress][$property] = false; + } + }else { - $this->init(); + logger(INFO, _("Not initializing inactive hook: ") . $this->hookName); } + + } - function installHooks(&$indexDevices) + function installHooksFunction(&$indexDevices) { global $devices, $devicesRequest; static $requestflag = 0; diff --git a/config/logger.php b/config/logger.php new file mode 100644 index 0000000..0b2df93 --- /dev/null +++ b/config/logger.php @@ -0,0 +1,5 @@ + + diff --git a/constants.php b/constants.php index e0ee741..15bb311 100644 --- a/constants.php +++ b/constants.php @@ -29,5 +29,4 @@ foreach ($logLevels as $key => $string) $logLevels[6] = "NOTICE & WARNING"; define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT); -define("NOTIF_DEFAULT_DEST", "daniel"); ?> diff --git a/hooks/notifiers/notificationfreemobile.php b/hooks/notifiers/notificationfreemobile.php index 6ee2f51..76ea45f 100644 --- a/hooks/notifiers/notificationfreemobile.php +++ b/hooks/notifiers/notificationfreemobile.php @@ -31,18 +31,18 @@ class notificationFreemobile // $result contains the output string if ($this->curlErr <= 5) { - logger(DEBUG, _("Curl sending message"), false, __FILE__ . ":" . __LINE__); + logger(DEBUG, _("Curl sending message"), __FILE__ . ":" . __LINE__); echo $this->url . urlencode(trim($message)) . EOL; curl_exec($ch); if (curl_errno($ch) != 0) { $this->curlErr += 1; $this->lastTry = time(); - logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), false, __FILE__ . ":" . __LINE__); + logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), __FILE__ . ":" . __LINE__); $error = true; }else { - logger(DEBUG, sprintf(_("Curl return: %s when sending notification"), curl_error($ch)), false, __FILE__ . ":" . __LINE__); + logger(DEBUG, sprintf(_("Curl return: %s when sending notification"), curl_error($ch)), __FILE__ . ":" . __LINE__); $this->curlErr = 0; } }else @@ -61,5 +61,9 @@ class notificationFreemobile } } +// constant definition +define("NOTIF_DEFAULT_DEST", "daniel"); + +//init class $notificationMethods["freemobile"] = new notificationFreemobile(); ?> diff --git a/hooks/scripts/availability.php b/hooks/scripts/availability.php index 198c98b..409d87a 100644 --- a/hooks/scripts/availability.php +++ b/hooks/scripts/availability.php @@ -1,31 +1,40 @@ iterate(); } - private function iterate() + protected function iterate() { global $indexDevices; - foreach ($indexDevices as &$value) + foreach ($indexDevices as $ieeeAddress => $value) { - $value->properties["availability"]["functions"][] = array($this,"callback"); + $deviceList[] = array( $ieeeAdress => "availability"); } + $this->installHooksFunction($indexDevices); } function installHooks(&$indexDevices) { - return true; + static $indexDevicesSize; + $tmp = count($indexDevices); + if ($tmp != $indexDevicesSize) + { + $this->iterate(); + $indexDevicesSize = $tmp; + } + return false; } + // callback fonction. Is called with these 3 parameters // $device -> calling device // $property -> parameter passed by mqtt @@ -35,18 +44,19 @@ class availability switch($property) { case "availability": // theorically can't be other, but .... - if ($device->availability != $value) + if ($device->properties["availability"]["value]"] != $value) { //echo "==========>>>>>> Availability $value" . EOL; //if (!empty($device->availability)) - //{ + //{[ Torrent911.com] $log = ALERT; //}else //{ // $log = INFO; //} //$device->availability = $value; - logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__); + + logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__, $device); } break; } diff --git a/hooks/scripts/radiateurs.php b/hooks/scripts/radiateurs.php index 865f479..4a90e48 100644 --- a/hooks/scripts/radiateurs.php +++ b/hooks/scripts/radiateurs.php @@ -49,6 +49,11 @@ class radiateurs extends hook // ETAGE_CHAMBRE_RADIATEUR => 0 ); + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 3 parameters public function callBack(&$device, $property, $value) { diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index 9b8b05b..62a682c 100644 --- a/hooks/scripts/rdc_chambre_eclairage.php +++ b/hooks/scripts/rdc_chambre_eclairage.php @@ -16,6 +16,11 @@ class rdc_chambre_eclairage extends hook RDC_CHAMBRE_AMBIANCE_INTER => "action" ); + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 3 parameters // $device -> calling device // $property -> property of the device (given by mqtt) diff --git a/hooks/scripts/rdc_portes_ouvertes.php b/hooks/scripts/rdc_portes_ouvertes.php index b3a5ca0..5750a6e 100644 --- a/hooks/scripts/rdc_portes_ouvertes.php +++ b/hooks/scripts/rdc_portes_ouvertes.php @@ -14,6 +14,11 @@ class alerte_intrusion extends hook GARAGE_PORTAIL => "contact" ); + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 4 parameters public function callBack($device, $property, $value) { diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index c87e69b..3f8f0c3 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -20,6 +20,11 @@ class rdc_salon_eclairage extends hook public $luminance_min = 50; public $luminance_max = 100; + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 4 parameters public function callBack($device, $param, $value) { diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index c1b50fb..94eb4bc 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -27,6 +27,12 @@ class rdc_sdb_eclairage extends hook array(RDC_SDB_PLAFOND_MVMT, "occupancy", 1), array(RDC_SDB_MVMT, "occupancy", 1) ); + + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 3 parameters public function callBack(&$device, $property, $value) { diff --git a/hooks/scripts/rdc_store.php b/hooks/scripts/rdc_store.php index a173ba1..d872f77 100644 --- a/hooks/scripts/rdc_store.php +++ b/hooks/scripts/rdc_store.php @@ -21,6 +21,11 @@ class rdc_store extends hook METEO => "windgustkmh" ); + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 3 parameters // $device -> calling device // $property -> property of the device (given by mqtt) diff --git a/hooks/scripts/rdc_store.php~ b/hooks/scripts/rdc_store.php~ index 934648d..b98e37e 100644 --- a/hooks/scripts/rdc_store.php~ +++ b/hooks/scripts/rdc_store.php~ @@ -21,6 +21,11 @@ class rdc_store extends hook METEO => "windgustkmh" ); + function installHooks(&$indexDevices) + { + $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 3 parameters // $device -> calling device // $property -> property of the device (given by mqtt) @@ -153,7 +158,7 @@ class rdc_store extends hook } }elseif ($exterieurLuxMoyen < 6000 and $salon_lux < 200 and $soleil < 100) { - logger(DEBUG, "exterieurLuxMoyen < 11000 and salon_lux < 1000", __FILE__ . ":" . __LINE__); + logger(DEBUG, "exterieurLuxMoyen < 6000 and salon_lux < 200 and soleil < 100", __FILE__ . ":" . __LINE__); $this->close("Luminosité faible"); } @@ -237,7 +242,7 @@ class rdc_store extends hook } } - private function send($level) + private function send($level, $method) { global $indexDevices; $deviceObject = $indexDevices[RDC_STORE]; diff --git a/hooks/scripts/rdc_temperature_int_ext.php b/hooks/scripts/rdc_temperature_int_ext.php index e7690d5..976181e 100644 --- a/hooks/scripts/rdc_temperature_int_ext.php +++ b/hooks/scripts/rdc_temperature_int_ext.php @@ -4,26 +4,33 @@ class rdc_temperature_int_ext extends hook { public $hookName = "rdc_wc_eclairage"; public $active = true; //enable/disable hook (true => enabled) + public $wantedTemp = 24; protected $devicelist = array( METEO=> "tempc", ); + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 4 parameters public function callBack(&$device, $property, $value) { global $indexDevices; - static $status = 0; - if ($device->properties["indoortempc"]["value"] > $value and $status === 1) + static $status = -1; + $indoorTemp = $device->properties["indoortempc"]["value"]; + if ( (($value > $this->wantedTemp and $indoorTemp < $this->wantedTemp) or ( $value < $this->wantedTemp and $indoorTemp >= $this->wantedTemp)) and $status === 1) { $status = 0; - logger(ALERT, _("Indoor temperature is superior to outdoor one")); - }elseif ($device->properties["indoortempc"]["value"] < $value and $status === 0) + logger(ALERT, _("Open doors to climate"), null ,$device); + }elseif ( (($value > $this->wantedTemp and $indoorTemp >= $this->wantedTemp) or ( $value < $this->wantedTemp and $indoorTemp <= $this->wantedTemp)) and $status === 0 ) { $status = 1; - logger(ALERT, _("Indoor temperature is inferior to outdoor one")); + logger(ALERT, _("Close doors to climate"), null, $device); } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__, $device); } } diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index f861244..c679cd2 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -14,6 +14,11 @@ class rdc_wc_eclairage extends hook public $delayManual = 10; // amount of time in $timeunit for manual mode public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year + function installHooks(&$indexDevices) + { + return $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 4 parameters public function callBack(&$device, $property, $value) { diff --git a/hooks/scripts/rdc_wc_eclairage.php~ b/hooks/scripts/rdc_wc_eclairage.php~ index f861244..9537833 100644 --- a/hooks/scripts/rdc_wc_eclairage.php~ +++ b/hooks/scripts/rdc_wc_eclairage.php~ @@ -14,6 +14,11 @@ class rdc_wc_eclairage extends hook public $delayManual = 10; // amount of time in $timeunit for manual mode public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year + function installHooks(&$indexDevices) + { + $this->installHooksFunction($indexDevices); + } + // callback fonction. Is called with these 4 parameters public function callBack(&$device, $property, $value) { diff --git a/moha.php b/moha.php index b866e5a..7472df7 100644 --- a/moha.php +++ b/moha.php @@ -50,7 +50,7 @@ if ($testMode) $httpServerIp = "192.168.1.253"; }else { - $logLevel = DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT; + $logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; $mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode $dataPath = "/usr/share/moha/"; $logFile = "/var/log/moha.log"; // Path of log file @@ -64,18 +64,25 @@ if (!init()) exit(1); bindtextdomain("moha", "./locale"); textdomain("moha"); -function notify($message, $destinataire=NOTIF_DEFAULT_DEST) +function notify($message, $device) { global $notificationMethods; $result = false; + + //TODO destinataire selon fichier de config + $destinataire = array("daniel"); + foreach($notificationMethods as $value) { - $result = $result | $value->send($message, $destinataire); + foreach($destinataire as $dest) + { + $result = $result | $value->send($message, $dest); + } } return $result; } -function logger($level, $log, $pos = "", $notif = true) +function logger($level, $log, $pos = "", $device=null, $notif = true) { global $logFh, $logLevel, $notificationLevel, $logLevels; $logString = $logLevels[$level] . " " ; @@ -92,7 +99,7 @@ function logger($level, $log, $pos = "", $notif = true) $test = $level & $notificationLevel; if (($test != 0) and ($notif === true)) { - if(notify("Moha\n" . $logString) === false) + if(notify("Moha\n" . $logString, $device) === false) { logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); return true; @@ -331,10 +338,16 @@ while (true) $i = 1; foreach($hooks as $hookName => &$hook) { - if ($hook->initialized === false); // and $hook->active === true) + if ($hook->active === false) { - logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__); - $i &= $hook->installHooks($indexDevices); + $i = 1; + }else + { + if ($hook->initialized === false); // and $hook->active === true) + { + logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__); + $i &= $hook->installHooks($indexDevices); + } } } $hooksInitialized = $i;