From d79463fbd411722bb1c36710200cf8d859926121 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Sun, 2 Jan 2022 18:14:13 +0100 Subject: [PATCH] some debbuging --- class/availability.php | 41 ++++++++ class/availibility.php | 31 ------ class/main.php | 37 ------- class/main.php~ | 106 --------------------- db_functions.php | 73 +++++++++----- events.php | 12 +-- hooks/notifiers/notificationfreemobile.php | 16 +++- hooks/scripts/panneau_salon.php | 33 ++++--- moha.php | 29 +++--- mqtt_functions.php | 2 +- utils.php | 9 +- 11 files changed, 147 insertions(+), 242 deletions(-) create mode 100644 class/availability.php delete mode 100644 class/availibility.php delete mode 100644 class/main.php~ diff --git a/class/availability.php b/class/availability.php new file mode 100644 index 0000000..7fdd5ee --- /dev/null +++ b/class/availability.php @@ -0,0 +1,41 @@ + calling device + // $param -> parameter passed by mqtt + // $value -> value of the parameter + public function callBack($device, $param, $value) + { + switch($param) + { + case "availability": // theorically can't be other, but .... + if ($device->availability != $value) + { + //echo "==========>>>>>> Availability $value" . EOL; + if (!empty($device->availability)) + { + $log = WARNING; + }else + { + $log = INFO; + } + $device->availability = $value + logger($log, sprintf(_("Device: %s/%s is %s"), $topic, $fn , $value)); + } + break; + } + echo _("notification received from MQTT") . EOL; + //echo $param . "=> " . $value . EOL; + } + public function getHook() + { + return array($this,"callback"); + } +} + +$hooks["availability"] = new availability; +?> diff --git a/class/availibility.php b/class/availibility.php deleted file mode 100644 index fda3ff0..0000000 --- a/class/availibility.php +++ /dev/null @@ -1,31 +0,0 @@ - " . $value . EOL; - } -} - -$hooks["availability"] = new availability; -?> - - - -?> diff --git a/class/main.php b/class/main.php index f000b7b..ee6fb3e 100644 --- a/class/main.php +++ b/class/main.php @@ -68,41 +68,4 @@ class interval public $endDate; } -class notificationMethod -{ - public $url; - public $msg; - public $active; - public $level; - - function __construct($url) - { - $this->url = $url; - } - - public function send($text=null) - { - if (empty($text)) $text= $this->msg; - if (!empty($msg)) - { - $opts = array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Accept-language: fr\r\n" - ) - ); - $context = stream_context_create($opts); - - /* Envoi une requête HTTP vers $url - avec les en-têtes additionnels ci-dessus */ - $fp = fopen($url . "/" . $msg, 'r', false, $context); - $response = stream_get_contents($fp, -1, 0); - fclose($fp); - }else - { - // TODO log_error("notificationMethod : $msg is null"); - } - - } -} ?> diff --git a/class/main.php~ b/class/main.php~ deleted file mode 100644 index a1d87f3..0000000 --- a/class/main.php~ +++ /dev/null @@ -1,106 +0,0 @@ -class Message -{ - public $id; - public $state = false; - public $msg; -} - -class topic { - public $mid; - public $status; - public $info; - public $devices; - public $groups; - public $extensions; - public $config; -} - -class device -{ - public $method; //auto or manual - public $topic; - public $device; - public $ieeeAddress; - public $groupID; - public $friendlyName; - public $powerSource; - public $description; - public $functions; - public $payload; - public $availibility; - - public function set($event) - { - publish($this, $this->payload, "set", $event); - } - - public function get() - { - publish($this, $this->payload, "get", $event); - } -} - -class ranges -{ - public $start; //datetime - public $end; //datetime -} - -class event -{ - public $ieeeAddress; - public $topic; - public $param; - public $value; - public $device; - public $published; - public $dateTimeEvent; // datetime : next occurence for recurrent event - public $startDatetime; - public $stopDatetime; - public $recurrenceInterval; // interval : for recurrent event - public $exceptionInterval; // array of object ranges -} - -class interval -{ - public $startDate; - public $endDate; -} - -class notificationMethod -{ - public $url; - public $msg; - public $active; - public $level; - - function __construct($url) - { - $this->url = $url; - } - - public function send($text=null) - { - if (empty($text)) $text= $this->msg; - if (!empty($msg)) - { - $opts = array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Accept-language: fr\r\n" - ) - ); - $context = stream_context_create($opts); - - /* Envoi une requête HTTP vers $url - avec les en-têtes additionnels ci-dessus */ - $fp = fopen($url . "/" . $msg, 'r', false, $context); - $response = stream_get_contents($fp, -1, 0); - fclose($fp); - }else - { - // TODO log_error("notificationMethod : $msg is null"); - } - - } -} diff --git a/db_functions.php b/db_functions.php index f6a5308..4f55a5d 100644 --- a/db_functions.php +++ b/db_functions.php @@ -10,8 +10,11 @@ function storeDB($db, $filepath) function mkDevicesDB($topic, $json, $group=false) { - global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh; - if (!isset($devices[$topic])) $devices[$topic]= array(); + global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks; + if (!isset($devices[$topic])) + { + $devices[$topic]= array(); + } foreach ($json as $jsonDevice) { //print_r($device); @@ -20,11 +23,16 @@ function mkDevicesDB($topic, $json, $group=false) $device = & $devices[$topic]; foreach($fnTree as $fnPart) { - $device[$fnPart] = array(); + if (!isset($device[$fnPart])) + { + $device[$fnPart] = array(); + } $device = & $device[$fnPart]; } - - $device["device"] = new device; + if (!isset($device["device"])) + { + $device["device"] = new device; + } $device["device"]->topic = $topic; //$device["device"]->device = $jsonDevice; $device["device"]->friendlyName = $jsonDevice->friendly_name; @@ -35,20 +43,7 @@ function mkDevicesDB($topic, $json, $group=false) $indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name; }else { - $device["device"]->type = $jsonDevice->type; - $device["device"]->ieeeAddress = $jsonDevice->ieee_address; - if ( !empty($jsonDevice->power_source ) ) - { - $device["device"]->powerSource = $jsonDevice->power_source; - } - if ($jsonDevice->definition != null) - { - $device["device"]->description = $jsonDevice->definition->description; - searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys); - } - searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties); - $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - //print_r($device); + addDevice($device, $fn, $jsonDevice); } } $dbInit += 1; @@ -60,6 +55,32 @@ function mkDevicesDB($topic, $json, $group=false) //print_r($devices); } +function addDevice(& $device, $fn, $jsonDevice ) +{ + global $listProperties, $listPropertiesKeys, $hooks, $indexDevices; + $device["device"]->type = $jsonDevice->type; + $device["device"]->ieeeAddress = $jsonDevice->ieee_address; + if ( !empty($jsonDevice->power_source ) ) + { + $device["device"]->powerSource = $jsonDevice->power_source; + } + if ($jsonDevice->definition != null) + { + $device["device"]->description = $jsonDevice->definition->description; + searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys); + } + searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties); + + // adding callback function for availability + //print_r($hooks); + $device["device"]->functions[] = $hooks["availability"]->getHook(); + + //indexing device + $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; + + //print_r($device); +} + function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) { foreach($listPropertiesKeys as $property) @@ -101,7 +122,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray) function iterateDevice($topic, $fn, &$device, $payloadArray) { global $changed; - print_r($payloadArray); + //print_r($payloadArray); //echo "device =>";print_r($device);echo EOL; foreach($payloadArray as $key => $value) { @@ -128,8 +149,11 @@ function iterateDevice($topic, $fn, &$device, $payloadArray) { if (empty($device->$key) or $value != null) { - if (isset($device->$key)) $oldValue = $device->$key; - if ($oldValue != $value) + if (isset($device->$key)) + { + $oldValue = $device->$key; + + }if ($oldValue != $value) { $device->{$key} = $value; $changed[$fn]["key"] = $key; @@ -145,14 +169,15 @@ function iterateDevice($topic, $fn, &$device, $payloadArray) } echo " to " . $value . EOL;*/ } - //print_r($device->functions); + //print_r($device->functions); print_r($value); + if (!empty($device->functions)) { echo "executing notifications functions " . EOL; foreach($device->functions as $function) { //print_r($function); - $function($topic, $fn, $key, $value); + $function($device, $key, $value); } } } diff --git a/events.php b/events.php index 61aec1d..0c6b945 100644 --- a/events.php +++ b/events.php @@ -33,7 +33,7 @@ function checkEvents() publish($event->device, array($event->param => $event->value), "set", $key); if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) { - logger(ERROR, __("Error in event recurrence. event: ") . $key); + logger(ERROR, _("Error in event recurrence. event: ") . $key); } } } @@ -88,7 +88,7 @@ function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDateti $event->dateTimeEvent = $event->startDatetime; if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) { - logger(ERROR, __("Error in event recurrence. event: ") . $key); + logger(ERROR, _("Error in event recurrence. event: ") . $key); } } @@ -124,13 +124,13 @@ function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=fal } if (empty($s)) { - logger(ERROR, __("setDelay error: unit is empty")); + logger(ERROR, _("setDelay error: unit is empty")); } if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false) { - logger(ERROR, __("setDelay error: datetime->add")); + logger(ERROR, _("setDelay error: datetime->add")); } - print_r($datetime); + //print_r($datetime); if ($replace) { $eventKey = searchEvent($device, $param, $value); @@ -169,7 +169,7 @@ function deleteEvent($eventKey) { global $events; unset ($events[$eventKey]); - logger(INFO, __("delete event ") . $eventKey); + logger(INFO, _("delete event ") . $eventKey); } diff --git a/hooks/notifiers/notificationfreemobile.php b/hooks/notifiers/notificationfreemobile.php index 0f5b8dd..7353387 100644 --- a/hooks/notifiers/notificationfreemobile.php +++ b/hooks/notifiers/notificationfreemobile.php @@ -3,12 +3,12 @@ class notificationFreemobile { private $url = "https://smsapi.free-mobile.fr/sendmsg?user=32886706&pass=JTGUY6l5OG73zX&msg="; private $name = "freemobile"; + public $active = true; + public $level; function __construct() { - global $notificationMethods; - $notificationMethods[$name]->active = true; - $notificationMethods[$name]->level = ALL; + $this->level = WARNING | ERROR; } function send($message) @@ -23,7 +23,13 @@ class notificationFreemobile curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string - logger(INFO, curl_exec($ch)); + if (($result = curl_exec($ch)) === false) + { + logger(ERROR, _(sprintf( "Curl return error: %s when sending notification", curl_error($ch)))); + }else + { + logger(INFO, _("Curl return: ") . $result . _(" when sending notification")); + } // close curl resource to free up system resources curl_close($ch); @@ -33,6 +39,6 @@ class notificationFreemobile } } -$notificationMethods["freemobile"] = new notificationMethod(); +$notificationMethods["freemobile"] = new notificationFreemobile(); ?> diff --git a/hooks/scripts/panneau_salon.php b/hooks/scripts/panneau_salon.php index 91f7688..06443eb 100644 --- a/hooks/scripts/panneau_salon.php +++ b/hooks/scripts/panneau_salon.php @@ -4,7 +4,7 @@ class rdc_panneau_salon // list of devices we are listening to private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0"); public $delay = 3; // amount of time in $timeunit - public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year + public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year public $luminance_min = 80; public $luminance_max = 3000; @@ -21,36 +21,39 @@ class rdc_panneau_salon } // callback fonction. Is called with these 4 parameters - public function callBack($topic, $fn, $param, $value) + public function callBack(&$device, $param, $value) { global $devices, $indexDevices; switch($param) { case "occupancy": - if ($value == 1) $this->send(); + if ($value == 1 and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min) + { + $this->send("ON"); + } break; case "contact": - if ($value == false) $this->send(); + if ($value == false and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min) + { + $this->send("ON"); + } break; case "illuminance_lux": - if ($value >= $this->luminance_max) $this->send(); + if ($value >= $this->luminance_max) $this->send("OFF"); } echo _("notification received from MQTT") . EOL; //echo $param . "=> " . $value . EOL; } - private function send() + private function send($state) { global $devices, $indexDevices; - if ($indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min) - { - $msg = array("state" => "ON"); - $device = & $indexDevices["0x588e81fffe343e8f"]; - logger(INFO, __("publishing ") . $msg . __(" message: ") . $device->friendlyName); - $device->payload = $msg; - $device->set(null); - setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); - } + $msg = array("state" => $state); + $device = & $indexDevices["0x588e81fffe343e8f"]; + logger(INFO, _("publishing ") . $msg . _(" message: ") . $device->friendlyName); + $device->payload = $msg; + $device->set(null); + setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); } } diff --git a/moha.php b/moha.php index b970181..94ede2d 100644 --- a/moha.php +++ b/moha.php @@ -4,15 +4,13 @@ define( "EOL", "\n"); define("Z2M", "zigbee2mqtt"); - - - declare(ticks = 1); $listProperties = array("powerSource" => "batterie"); $listPropertiesKeys = array("property"); -include "class/main.php"; + +require "class/main.php"; //global variables $topics = array(); // list of topics @@ -36,25 +34,28 @@ $topics["zigbee2mqtt"] = new topic; bindtextdomain("moha", "./locale"); textdomain("moha"); -if (!init()) exit(1); -require "mqtt_functions.php"; -require "utils.php"; -require "events.php"; -require "db_functions.php"; + +if (!init()) exit(1); $client = new Mosquitto\Client(); // log levels -define( "DEBUG", $client->LOG_DEBUG); -define( "INFO", $client->LOG_INFO); -define( "NOTICE", $client->LOG_NOTICE); -define( "WARNING", $client->LOG_WARNING); -define( "ERROR", $client->LOG_ERR); +define( "DEBUG", $client::LOG_DEBUG); +define( "INFO", $client::LOG_INFO); +define( "NOTICE", $client::LOG_NOTICE); +define( "WARNING", $client::LOG_WARNING); +define( "ERROR", $client::LOG_ERR); define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR); $logLevel = DEBUG; $notificationLevel = WARNING | ERROR; +require "utils.php"; +require "mqtt_functions.php"; +require "events.php"; +require "db_functions.php"; +require "class/availability.php"; + // defining callback functions $client->onConnect('connectResponse'); $client->onDisconnect('disconnectResponse'); diff --git a/mqtt_functions.php b/mqtt_functions.php index 96f8df7..e697bf4 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -46,7 +46,7 @@ function message($message) { unset ($fnTree[array_key_last($fnTree)]); $payloadArray = array("availability" => $message->payload); - print_r($payloadArray); + //print_r($payloadArray); }else { $payloadArray = json_decode($message->payload); diff --git a/utils.php b/utils.php index aa82d7c..9d034f7 100644 --- a/utils.php +++ b/utils.php @@ -12,6 +12,8 @@ function signalHandler($signal) function notify($message) { + global $notificationMethods; + $result = false; foreach($notificationMethods as $value) { $result |= $value->send($message); @@ -21,7 +23,8 @@ function notify($message) function logger($level, $log) { - global $logFh, $logLevel; + global $logFh, $logLevel, $notificationLevel; + echo "$level" if ( $level >= $logLevel) { fwrite($logFh, "$level : $log" . EOL); @@ -29,9 +32,9 @@ function logger($level, $log) } if ($level >= $notificationLevel) { - if(notify($message) == true) + if(notify(" Moha\n" . $log) == false) { - logger(INFO, __("Notification not sent")); + logger(INFO, _("Notification not sent")); } } }