From 425107cec7202a90f1bac80a31aa512ecb419e1f Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Fri, 28 Jan 2022 23:05:58 +0100 Subject: [PATCH] debugging --- class/db.php | 14 +++---- class/hook_class.php | 12 +++--- class/main.php | 2 +- constants.php | 10 ----- db_functions.php | 10 ++--- devices_logs.php | 2 +- docs/template.php | 24 +++++------- events.php | 28 +++++++------- hooks/notifiers/notificationfreemobile.php | 4 +- hooks/scripts/availability.php | 2 +- hooks/scripts/rdc_chambre_eclairage.php | 6 +-- hooks/scripts/rdc_portes_ouvertes.php | 2 +- hooks/scripts/rdc_salon_eclairage.php | 38 ++++++++++++------ hooks/scripts/rdc_sdb_eclairage.php | 4 +- hooks/scripts/rdc_wc_eclairage.php | 2 +- moha.php | 45 +++++++++++++--------- mqtt_functions.php | 28 +++++++------- topics_callbacks/linky2mqtt.php | 4 +- topics_callbacks/pws2mqtt.php | 4 +- topics_callbacks/zigbee2mqtt.php | 6 +-- utils.php | 16 +++++++- webserver.php | 40 ++++++++++++------- 22 files changed, 167 insertions(+), 136 deletions(-) diff --git a/class/db.php b/class/db.php index b381501..c38b765 100644 --- a/class/db.php +++ b/class/db.php @@ -1,5 +1,5 @@ connect($this->mysqlServer, $this->username, $this->passwd, $this->database) === false) { - logger(ERROR,_("Connection to sql server error :") . $this->connect_error); + logger(ERROR,_("Connection to sql server error :") . $this->connect_error, __FILE__ . ":" . __LINE__); sleep(5); $flagError = true; } if ($flagError === true) { - logger(ERROR, _("Connection to sql server ready")); + logger(ERROR, _("Connection to sql server ready"), __FILE__ . ":" . __LINE__); } $result = new mysqli_result($this); } @@ -56,7 +56,7 @@ class db extends mysqli $minMax = $minMax($value); } - echo "minMax = " .$minMax . EOL; + //echo "minMax = " .$minMax . EOL; //echo "oldValue = " . $oldValue . EOL; //echo "Value = " . $value . EOL; if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax) @@ -67,15 +67,15 @@ class db extends mysqli } if ($testMode) { - logger(INFO, _("Test mode on: not storing in DB ")); + logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__); }else { if(!$this->result = $this->query($query)) { - logger(ERROR, _("mysql query errror: ") . $this->error); + logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__); } } - logger(INFO, sprintf(_("New value of property: '%s' of device: %s stored in database"), $propertyTree, $device->friendlyName, $value)); + logger(INFO, sprintf(_("New value of property: '%s' of device: %s stored in database"), $propertyTree, $device->friendlyName, $value), __FILE__ . ":" . __LINE__); } } } diff --git a/class/hook_class.php b/class/hook_class.php index bf2603f..7bfa5d2 100644 --- a/class/hook_class.php +++ b/class/hook_class.php @@ -26,31 +26,31 @@ class hook { foreach ($this->devicelist as $ieeeAddress => $property2change) { - logger(DEBUG, _("Device: ") . $ieeeAddress); + logger(DEBUG, _("Device: ") . $ieeeAddress, __FILE__ . ":" . __LINE__); if ($property2change[1] === false) { - logger(DEBUG, _("Trying to store callback")); + logger(DEBUG, _("Trying to store callback"), __FILE__ . ":" . __LINE__); if (isset($indexDevices[$ieeeAddress])) { $property = $property2change[0]; $indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback"); $property2change[1] = true; - logger(DEBUG, sprintf(_("Property '%s' is initialized with callback"), $property2change[0])); + logger(DEBUG, sprintf(_("Property '%s' is initialized with callback"), $property2change[0]), __FILE__ . ":" . __LINE__); }else { - logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property2change[0], $ieeeAddress)); + logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property2change[0], $ieeeAddress), __FILE__ . ":" . __LINE__); $result = false; } }else { - logger(DEBUG, _("Callback already installed")); + logger(DEBUG, _("Callback already installed"), __FILE__ . ":" . __LINE__); } } //echo "result => "; var_dump($result); if ($result === true) { $this->initialized = true; - logger(DEBUG, $this->hookName . (" initialized")); + logger(DEBUG, $this->hookName . _(" initialized"), __FILE__ . ":" . __LINE__); //var_dump($this); } } diff --git a/class/main.php b/class/main.php index 954f257..f08ab0b 100644 --- a/class/main.php +++ b/class/main.php @@ -1,5 +1,5 @@ 16 -define( "INFO", 1); // => 1 -define( "NOTICE", 2); // => 2 -define( "WARNING", 4); // => 4 -define( "ERROR", 8); // => 8 -define( "ALERT", 32); - -*/ - $logLevels = array( 1 => "INFO", 2 => "NOTICE", diff --git a/db_functions.php b/db_functions.php index 0c5cc90..0add78c 100644 --- a/db_functions.php +++ b/db_functions.php @@ -1,6 +1,6 @@ $key["value"] = $value; //$changed[$fn]["key"] = $key; //$changed[$fn]["value"] = $value; - logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value))); + logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__); $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); if (!empty($device->$key["functions"])) { - logger(DEBUG,_("executing notifications functions")); + logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__); foreach($device->$key["functions"] as $function) { $function($device, $key, $value); diff --git a/devices_logs.php b/devices_logs.php index 7309939..94e1ae2 100644 --- a/devices_logs.php +++ b/devices_logs.php @@ -5,7 +5,7 @@ function logDevice(& $device, $key, $value) $db = new mysqli($mysqlServer, $username, $passwd, $database); if ($conn->connect_error) { - logger(ERROR, _("Mysql connection failed: ") . $db->connect_error); + logger(ERROR, _("Mysql connection failed: ") . $db->connect_error, __FILE__ . ":" . __LINE__); } } diff --git a/docs/template.php b/docs/template.php index 7e1d134..b13e4cc 100644 --- a/docs/template.php +++ b/docs/template.php @@ -1,25 +1,19 @@ +class extends hook { public $hookName = ; - public $active = true; enable/disable hook (true => enabled) + public $active = true; //enable/disable hook (true => enabled) - private $devicelist = array() => ); + protected $devicelist = array( => array(, false)); - // assigne the function to the sensors devices - function __construct() + // if you need some initialisation when creating the object, uncomment andput your code here. + // If existing, This function is called by __construct + /*protected function init() { - global $indexDevices; - if ($this->active === true) - { - foreach ($this->devicelist as $ieeeAddress => $property) - { - $indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback"); - } - } - // put here your code to init the class + // Your code here } + */ // callback fonction. Is called with these 3 parameters // $device -> calling device @@ -29,7 +23,7 @@ class { // here your code - logger (INFO, _("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value); + logger (INFO,sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value), __FILE__ . ":" . __LINE__); } } diff --git a/events.php b/events.php index a00cec9..04a694e 100644 --- a/events.php +++ b/events.php @@ -1,5 +1,5 @@ startDatetime)) { - logger(DEBUG, _("StarDatetime is set")); + logger(DEBUG, _("StarDatetime is set"), __FILE__ . ":" . __LINE__); if($event->dateTimeEvent < $now) { - logger(DEBUG, _("Event must be executed")); + logger(DEBUG, _("Event must be executed"), __FILE__ . ":" . __LINE__); if (!empty($event->exceptionInterval)) { - logger(DEBUG, _("Testing exceptions")); + logger(DEBUG, _("Testing exceptions"), __FILE__ . ":" . __LINE__); foreach($event->exceptionInterval as $key => $value) { if($now > $value->start and $now < $value->end) @@ -35,17 +35,17 @@ function checkEvents() } if ($exception === false) { - logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName)); + logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__); publish(mktopic($event->device), array($event->param => $event->value), "set", $key); if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) { - logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key); + logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__); } } } }elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent < now()) { - logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName)); + logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__); $mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key); $event->published = $now; //echo "#################################\nUnsetting event $key \n###########################" . EOL; @@ -95,7 +95,7 @@ function setRecurrentEvent(&$device, $property, $value, $startDatetime, $stopDat $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, __FILE__ . ":" . __LINE__); } } @@ -133,11 +133,11 @@ function setDelay(&$device, $delay, $unit="second", $property, $value, $replace= } if (empty($s)) { - logger(ERROR, _("setDelay error: unit is empty")); + logger(ERROR, _("setDelay error: unit is empty"), __FILE__ . ":" . __LINE__); } if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false) { - logger(ERROR, _("setDelay error: datetime->add")); + logger(ERROR, _("setDelay error: datetime->add"), __FILE__ . ":" . __LINE__); } //print_r($datetime); if ($replace) @@ -154,7 +154,7 @@ function setDelay(&$device, $delay, $unit="second", $property, $value, $replace= $events[$key]->param = $property; $events[$key]->value = $value; $events[$key]->device = & $device; - logger (DEBUG, _('Setting new delay in $events[]')); + logger (DEBUG, _('Setting new delay in $events[]'), __FILE__ . ":" . __LINE__); print_r($events[$key]); $loglevel = $oldLevel; } @@ -170,7 +170,7 @@ function removeEvent($device, $property , $value) function searchEvent($device, $property , $value) { global $events; - logger(DEBUG, _("searching event for device %s, property %s and value %s"), $device->friendlyName, $property, bool2string($value)); + logger(DEBUG, _("searching event for device %s, property %s and value %s"), $device->friendlyName, $property, bool2string($value), __FILE__ . ":" . __LINE__); foreach($events as $key => $event) { //echo "Event : $event => $value" . EOL; @@ -190,11 +190,11 @@ function deleteEvent($eventKey) if ($eventKey !== false) { unset ($events[$eventKey]); - logger(INFO, _("delete event key =") . $eventKey); + logger(INFO, _("delete event key =") . $eventKey, __FILE__ . ":" . __LINE__); }else { //try to delete an flase event - logger(WARNING, _("Try to delete event with key = ") . var_dump($eventKey)); + logger(WARNING, _("Try to delete event with key = ") . var_dump($eventKey), __FILE__ . ":" . __LINE__); } } diff --git a/hooks/notifiers/notificationfreemobile.php b/hooks/notifiers/notificationfreemobile.php index 1a17fa9..cc43b1d 100644 --- a/hooks/notifiers/notificationfreemobile.php +++ b/hooks/notifiers/notificationfreemobile.php @@ -33,12 +33,12 @@ class notificationFreemobile if ($result === false) { $curlErr += 1; - logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), false); + logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), false, __FILE__ . ":" . __LINE__); }/*else { logger(INFO, sprintf(_("Curl return: %s when sending notification"), $result), false); }*/ - } + } //TODO managing curl errors // close curl resource to free up system resources curl_close($ch); return true; diff --git a/hooks/scripts/availability.php b/hooks/scripts/availability.php index 02ea739..7c26d0e 100644 --- a/hooks/scripts/availability.php +++ b/hooks/scripts/availability.php @@ -41,7 +41,7 @@ class availability // $log = INFO; //} //$device->availability = $value; - logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value)); + logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value), __FILE__ . ":" . __LINE__); } break; } diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index fb768cd..964c89d 100644 --- a/hooks/scripts/rdc_chambre_eclairage.php +++ b/hooks/scripts/rdc_chambre_eclairage.php @@ -27,7 +27,7 @@ class rdc_chambre_eclairage extends hook { $this->send(RDC_CHAMBRE_LUMINOSITE, "ON", "OFF", AUTO); } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value)); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value), __FILE__ . ":" . __LINE__); } private function send($device, $state, $delayState = false, $method = MANUAL) @@ -37,13 +37,13 @@ class rdc_chambre_eclairage extends hook $msg = array("state" => $state); if ($device->state["value"] != $state) { - logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName)); + logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__); $device->payload = $msg; $device->set(); $device->method = $method; }else { - logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName)); + logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__); } //echo 'delaystate = ' . var_dump($delayState); diff --git a/hooks/scripts/rdc_portes_ouvertes.php b/hooks/scripts/rdc_portes_ouvertes.php index 072fe56..3658f23 100644 --- a/hooks/scripts/rdc_portes_ouvertes.php +++ b/hooks/scripts/rdc_portes_ouvertes.php @@ -23,7 +23,7 @@ class alerte_intrusion extends hook case "contact": if ($value == false) { - logger(ALERT, sprintf(_("%s est ouverte alors que personne n'est présent"), $device->friendlyName)); + logger(ALERT, sprintf(_("%s est ouverte alors que personne n'est présent"), $device->friendlyName), __FILE__ . ":" . __LINE__); } break; } diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 6833f27..f47de5b 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -21,40 +21,54 @@ class rdc_salon_eclairage extends hook public function callBack(&$device, $param, $value) { global $devices, $indexDevices; - logger(INFO, _("hook : rdc_salon_eclairage")); + logger(INFO, _("hook : rdc_salon_eclairage"), __FILE__ . ":" . __LINE__); switch($param) { case "occupancy": + logger(DEBUG, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__); //print_r($indexDevices[RDC_SALON_LUMINOSITE]); - if ($value == ON and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) + if ($value == ON and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min) { + logger(DEBUG, _("setting to ON"), __FILE__ . ":" . __LINE__); $this->send("ON", null, AUTO); removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF"); - } - if ($value == OFF) + }elseif ($value == OFF) { - if ($indexDevices(RDC_SALON_MVMT)->occupancy == OFF and $indexDevices(RDC_SALON_MVMT2)->occupancy == OFF) + logger(DEBUG, _("Value is OFF"), __FILE__ . ":" . __LINE__); + if (getValue(RDC_SALON_MVMT, "occupancy") == OFF) { - setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true); - //$this->send("ON", "OFF", AUTO); + if (getValue(RDC_SALON_MVMT2, "occupancy") == OFF) + { + logger(DEBUG, _("Setting to OFF"), __FILE__ . ":" . __LINE__); + setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true); + //$this->send("ON", "OFF", AUTO); + } } } break; case "contact": - if ($value == false and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) + logger(DEBUG, _("CASE: Contact Door"), __FILE__ . ":" . __LINE__); + if ($value == false and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min) { - //$this->send("ON", "OFF", AUTO); + logger(DEBUG, _("Door is open and illumance < min"), __FILE__ . ":" . __LINE__); + $this->send("ON", "OFF", AUTO); } break; case "illuminance_lux": + logger(DEBUG, _("CASE : Illuminance"), __FILE__ . ":" . __LINE__); if ($value >= $this->luminance_max) { + logger(DEBUG, _("illuminace is > to max"), __FILE__ . ":" . __LINE__); //$this->send("OFF", null, AUTO); removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF"); + }elseif ($value <= $this->luminance_min and (getValue(RDC_SALON_MVMT, "occupancy") == ON OR getValue(RDC_SALON_MVMT2,"occupancy") == ON)) + { + logger(DEBUG, _("illuminance < min and movement detected"), __FILE__ . ":" . __LINE__); + $this->send("ON", null, AUTO); } break; } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, bool2string($value))); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, bool2string($value)), __FILE__ . ":" . __LINE__); } private function send($state, $delayState = false, $method = MANUAL) @@ -64,13 +78,13 @@ class rdc_salon_eclairage extends hook $msg = array("state" => $state); if ($device->state["value"] != $state) { - logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName)); + logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__); $device->payload = $msg; $device->set(); $device->method = $method; }else { - logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName)); + logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__); } //echo 'delaystate = ' . var_dump($delayState); diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 1615aa1..8e5ba96 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -49,7 +49,7 @@ class rdc_sdb_eclairage extends hook } break; } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, ($value == 0 ? "OFF" : "ON"))); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, ($value == 0 ? "OFF" : "ON")), __FILE__ . ":" . __LINE__); } private function send($state) @@ -57,7 +57,7 @@ class rdc_sdb_eclairage extends hook global $devices, $indexDevices; $msg = array("state_l1" => $state); $device = & $indexDevices[RDC_SDB_WC_ECLAIRAGE]; - logger(INFO, sprintf(_("publishing message: %s to %s"), $state, $device->friendlyName)); + logger(INFO, sprintf(_("publishing message: %s to %s"), $state, $device->friendlyName), __FILE__ . ":" . __LINE__); $device->payload = $msg; $device->set(null); diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index 6ca396c..79f30e2 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -28,7 +28,7 @@ class rdc_wc_eclairage extends hook } break; } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value)); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value), __FILE__ . ":" . __LINE__); } } diff --git a/moha.php b/moha.php index a364add..5a50f5a 100644 --- a/moha.php +++ b/moha.php @@ -56,10 +56,15 @@ function notify($message) return $result; } -function logger($level, $log, $notif = true) +function logger($level, $log, $pos = false, $notif = true) { global $logFh, $logLevel, $notificationLevel, $logLevels; - $logString = date("c") . ' ' . $logLevels[$level] . " " . __FILE__ . ":" . __LINE__ . " : " . $log; + $logString = date("c") . ' ' . $logLevels[$level] . " " ; + if ($pos !== false) + { + $logString .= $pos; + } + $logString .= " - " . $log; if ($level & $logLevel) { fwrite($logFh, $logString . EOL); @@ -70,7 +75,7 @@ function logger($level, $log, $notif = true) { if(notify("Moha\n" . $logString) === false) { - logger(INFO, _("Notification not sent"), false); + logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); } } } @@ -112,6 +117,8 @@ function endMoha() { global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected; $x = 0; + + logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__); storeDB($devices, "/usr/share/moha/moha.db"); if($testMode) file_put_contents("moha.devices", print_r($devices, true)); if ($connected) @@ -137,8 +144,8 @@ function connect2mqttServer() global $client; $client->onConnect('connectResponse'); } - -logger(DEBUG, _("requiring php modules"), false); +logger(WARNING, _("starting moha"), __FILE__ . ":" . __LINE__); +logger(DEBUG, _("requiring php modules"), __FILE__ . ":" . __LINE__); require "class/main.php"; require "class/db.php"; require "class/hook_class.php"; @@ -152,7 +159,7 @@ require "webserver.php"; //logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false); //$client = new Mosquitto\Client(); -logger(DEBUG, _("Loading stored devices datas")); +logger(DEBUG, _("Loading stored devices datas"), __FILE__ . ":" . __LINE__); loadDB($devices, "moha.db"); @@ -162,28 +169,28 @@ if (!empty($hooksList)) { foreach ($hooksList as $callback) { - logger(INFO, _("Including ") . $callback, false); + logger(INFO, _("Including ") . $callback, __FILE__ . ":" . __LINE__); include $callback; } } -logger(DEBUG, _("requiring config files -> devices_constants.php"), false); +logger(DEBUG, _("requiring config files -> devices_constants.php"), __FILE__ . ":" . __LINE__); //include predefined file witch define constants for devices if (is_readable($configDir . "/" . "devices_constants.php")) { include $configDir . "/" . "devices_constants.php"; //echo "hooklist"; print_r($hooksList); echo EOL; - logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), false); + logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); }else { - logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), false); + logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); } // making the list of hooks to include listHooks("./hooks", $hooksList); // Program start -logger(DEBUG, _("Program start"), false); +logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false); $client = new Mosquitto\Client(); // defining callback functions @@ -193,7 +200,7 @@ try connect2mqttServer(); }catch ( Mosquitto\Exception $err) { - logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), false); + logger(ALERT, sprintf(_("Error connecting to mosquitto server, retrying in 10 seconds:"), $err), __FILE__ . ":" . __LINE__, false); sleep (10); exit(1); } @@ -208,7 +215,7 @@ $client->onPublish('publishResponse'); // connectong to mqtt server $client->connect($mqttServerIp, 1883, 5); -logger(INFO, _("Subscribing to bridge"), false); +logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false); foreach($topics as $name => $topic) { $topic->mid = $client->subscribe($name . "/#", 2); @@ -217,20 +224,20 @@ foreach($topics as $name => $topic) } // starting main loop -logger(INFO, _("Starting loop"), false); +logger(INFO, _("Starting loop"), __FILE__ . ":" . __LINE__, false); $oneshot = false; while (true) { $client->loop(); // mqtt server loop() if (! $included) // hooks not already included { - logger(DEBUG, _("Making hooks list"), false); + logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false); getDevicesValues(); // TODO get the values of devices if (!empty($hooksList)) // some hooks to include if hooklist is not empty { foreach ($hooksList as $hook) // loop to include hooks in hookslist { - logger(INFO, _("Including ") . $hook, false); + logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false); include $hook; } file_put_contents("/usr/share/moha/moha.devices", print_r($devices, true)); // debugging : save device list @@ -242,7 +249,7 @@ while (true) if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized { - logger(DEBUG, _("Oneshot part of loop"), false); + logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false); $oneshot = true; } if($hooksInitialized == 0) // all hooks are not initialized @@ -252,14 +259,14 @@ while (true) { if ($hook->initialized === false) { - logger(WARNING, _("Hook not completely initialized :") . $hookName); + logger(WARNING, _("Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__); $i &= $hook->installHooks(); } } $hooksInitialized = $i; }else { - logger(DEBUG,_("All hooks initialized")); + logger(DEBUG,_("All hooks initialized"), __FILE__ . ":" . __LINE__); } checkEvents(); askWebServer($read); diff --git a/mqtt_functions.php b/mqtt_functions.php index dfcfcf0..bb5c3bf 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), __FILE__ . ":" . __LINE__); $callback($topic, $message); } @@ -29,33 +29,33 @@ function publish($topic, $payload, $commande="set") //, $eventKey) //echo "setting mids" .EOL; $mids[$mid] = true; } - logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload)); + logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload), __FILE__ . ":" . __LINE__); }else { - logger(INFO, _("Test mode on: no publishing ")); + logger(INFO, _("Test mode on: no publishing "), __FILE__ . ":" . __LINE__); } } function connectResponse($r, $message) { global $connected; - echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL; + //echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL; switch ($r) { case 0: - echo _("Successfull connection") . EOL; + logger(INFO, _("Successfull connection"), __FILE__ . ":" . __LINE__); $connected = true; break; case 1: - echo _("Connection refused : unacceptable protocol version") . EOL; + logger(ERROR, _("Connection refused : unacceptable protocol version"), __FILE__ . ":" . __LINE__); $connected = true; break; case 2: - echo _("Connection refused : identifier rejected") . EOL; + logger(ERROR, _("Connection refused : identifier rejected"), __FILE__ . ":" . __LINE__); $connected = true; break; case 3: - echo _("Connection refused (broker unavailable )") . EOL; + logger(ERROR, _("Connection refused (broker unavailable )"), __FILE__ . ":" . __LINE__); $connected = true; break; } @@ -66,7 +66,7 @@ function subscribeResponse($mid, $qosCount) global $topics, $mids, $nSubscribed; //print_r($mids); $key = $mids[$mid]; - echo _("Subscribed to ") . $key . EOL; + logger(INFO, _("Subscribed to ") . $key, __FILE__ . ":" . __LINE__); $topics[$key]->status = true; $nSubscribed += 1; } @@ -75,7 +75,7 @@ function unsubscribeResponse($mid) { global $client; //$topics, $mids, $nSubscribed; //$key = $mids[$mid]; - echo _("Unsubscribed"); // from ") . $topics[$key]->name . EOL; + logger(INFO, _("Unsubscribed", __FILE__ . ":" . __LINE__)); // from ") . $topics[$key]->name); //$topics[$key]->status = false; //$nSubscribed -= 1; $client->disconnect(); @@ -86,19 +86,19 @@ function disconnectResponse($r) global $connected; if ($r != 0) { - echo _('Badly '); + $str = _('Badly '); }else { - echo _('Cleanly '); + $str = _('Cleanly '); } - echo _("disconnected from server") . EOL; + logger(ERROR, $str . _("disconnected from server")); $connected = false; } function publishResponse($mid) { global $mids, $events; - logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker"); + logger(LOG_INFO, sprintf(_("Event with mid = %d published by MQTT broker"), $mid), __FILE__ . ":" . __LINE__); if (isset($mids[$mid])) { //echo "unsetting mids" . EOL; diff --git a/topics_callbacks/linky2mqtt.php b/topics_callbacks/linky2mqtt.php index 28b15c8..5117dd8 100644 --- a/topics_callbacks/linky2mqtt.php +++ b/topics_callbacks/linky2mqtt.php @@ -7,12 +7,12 @@ $topics["linky2mqtt"]->callback = function($topic, $message) global $topics, $logFh, $devices, $included; $topicName = $topic[0]; $friendlyName = $topic[1]; // get friendlyName - logger(INFO, sprintf(_("Incoming notification of device %s"), $topic[0], $topic[1])); + logger(INFO, sprintf(_("Incoming notification of device %s"), $topic[0], $topic[1]), __FILE__ . ":" . __LINE__); $device = & $devices[$topic[0]]; $payloadArray = json_decode($message->payload); if (!isset($device[$fn])) //must not exists, but ... { - logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); + logger(LOG_WARNING, $logFh, _("init of ") . $fn, __FILE__ . ":" . __LINE__); $device[$fn] = array(); $device[$fn]["device"] = new device; $device[$fn]["device"]->type = "mesure"; diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index c12fbd8..d0729a3 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -5,13 +5,13 @@ $topics["pws2mqtt"]->callback = function($topic, $message) { global $topics, $logFh, $devices, $included; $fn = $topic[1]; // get friendlyname - logger(INFO, sprintf(_("Incoming 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]), __FILE__ . ":" . __LINE__); $device = & $devices[$topic[0]]; $payloadArray = json_decode($message->payload); if (!isset($device[$fn])) { - logger(WARNING, $logFh, "init of " . $fn .EOL); + logger(WARNING, $logFh, _("init of ") . $fn, __FILE__ . ":" . __LINE__); $device[$fn] = array(); $device[$fn]["device"] = new device; $device[$fn]["device"]->type = $payloadArray->type; diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index 281eaed..83ff25d 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -11,13 +11,13 @@ $callback = function($topic, $message) $topics[$topic[0]]->info = json_decode($message->payload); break; case "devices": - logger(DEBUG,_("Inserting zigbee devices in DB")); + logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__); $topics[$topic[0]]->devices = json_decode($message->payload); fwrite($logFh, print_r($topics[$topic[0]]->devices, true)); mkDevicesDB($topic[0], $topics[$topic[0]]->devices); break; case "groups": - logger(DEBUG,_("Inserting zigbee groups in DB")); + logger(DEBUG,_("Inserting zigbee groups in DB"), __FILE__ . ":" . __LINE__); $topics[$topic[0]]->groups = json_decode($message->payload); mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true); break; @@ -57,7 +57,7 @@ $callback = function($topic, $message) //print_r($device) ; if (!isset($device[$fn])) //must not exists, but ... { - logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); + logger(LOG_WARNING, $logFh, _("init of ") . $fn, __FILE__ . ":" . __LINE__); $device[$fn] = array(); $device[$fn]["device"] = new device; addDevice($device[$fn], $fn, $payloadArray); diff --git a/utils.php b/utils.php index 471c00d..9dfc67a 100644 --- a/utils.php +++ b/utils.php @@ -1,5 +1,5 @@ topic . "/" . $device->friendlyName; } -logger(DEBUG, _("signal handling"), false); +function getValue($fn, $property) +{ + global $indexDevices; + return $indexDevices[$fn]->$property["value"]; +} + +function setValue($fn, $property, $value) +{ + global $indexDevices; + $indexDevices(RDC_SALON_MVMT2)->occupancy["value"] = $value; +} + +logger(DEBUG, _("signal handling"), __FILE__ . ":" . __LINE__, false); //signal handling function signalHandler($signal) { diff --git a/webserver.php b/webserver.php index a59e456..04ce248 100644 --- a/webserver.php +++ b/webserver.php @@ -20,13 +20,13 @@ function askWebServer($read) if ( stream_select( $read, $array, $array, 0 )) { - logger(DEBUG,_("socket ready to read")); + logger(DEBUG, _("socket ready to read"), __FILE__ . ":" . __LINE__); $spawn = stream_socket_accept($read[0]); if ($spawn !== false) { - logger(DEBUG,_("socket accepted")); + logger(DEBUG, _("socket accepted"), __FILE__ . ":" . __LINE__); $input = fgets($spawn, 4096); - logger(DEBUG, $input); + logger(DEBUG, $input, __FILE__ . ":" . __LINE__); if (!empty($input)) { $input = substr($input,5); @@ -34,17 +34,18 @@ function askWebServer($read) $argTmp = explode("&", $input[0]); foreach($argTmp as $tmp) { - logger(DEBUG, $tmp); + logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); $array = explode("=", $tmp); - print_r($array); + //print_r($array); if (isset($array[1])) $argList[$array[0]] = $array[1]; } if(array_key_exists("cmd", $argList)) { - switch(strtolower($argList["cmd"])) + $command = strtolower($argList["cmd"]); + switch($command) { case "get": - logger(DEBUG, "get reached"); + logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__); if(empty($argList["device"])) { $response = "
get passed
"; @@ -52,12 +53,13 @@ function askWebServer($read) fwrite($spawn, $response); break; case "set": - logger(DEBUG, "set reached"); + logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__); $response = "
set passed
"; fwrite($spawn, $response); break; + case "dump": case "print": - logger(DEBUG, "print reached"); + logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); $var = $GLOBALS[$argList["object"]]; if (isset($argList["topic"])) { @@ -78,7 +80,12 @@ function askWebServer($read) } }else { - logger(ERROR, _("topic is not defining: add &topic=zigbee2mqtt to the resquest")); + $str = _("topic is not defining: add &topic=\nThese topics are availables: "); + foreach($topics as $key => $value) + { + $str .= $key . EOL; + } + logger(ERROR, $str, __FILE__ . ":" . __LINE__); } } $error = error_get_last(); @@ -86,12 +93,19 @@ function askWebServer($read) { $response = "
" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "
"; } - $response = "
" . print_r($var, true) . "
"; + if ($command === "print") + { + $response = print_r($var, true); + }elseif($command === "dump") + { + $response = "Dump" . EOL; + $response .= var_export($var, true); + } fwrite($spawn, $response); break; default: - logger(DEBUG, "not understanding command"); - fwrite($spawn, "not understanding command"); + logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); + fwrite($spawn, _("unknown command")); } } }