1
0

debugging

This commit is contained in:
daniel Tartavel 2022-01-28 23:05:58 +01:00
parent 1361cb9395
commit 425107cec7
22 changed files with 167 additions and 136 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
logger(DEBUG,"Including db.php"); logger(DEBUG, _("Including db.php"));
class db extends mysqli class db extends mysqli
{ {
@ -19,13 +19,13 @@ class db extends mysqli
} }
while ($this->connect($this->mysqlServer, $this->username, $this->passwd, $this->database) === false) while ($this->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); sleep(5);
$flagError = true; $flagError = true;
} }
if ($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); $result = new mysqli_result($this);
} }
@ -56,7 +56,7 @@ class db extends mysqli
$minMax = $minMax($value); $minMax = $minMax($value);
} }
echo "minMax = " .$minMax . EOL; //echo "minMax = " .$minMax . EOL;
//echo "oldValue = " . $oldValue . EOL; //echo "oldValue = " . $oldValue . EOL;
//echo "Value = " . $value . EOL; //echo "Value = " . $value . EOL;
if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax) if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax)
@ -67,15 +67,15 @@ class db extends mysqli
} }
if ($testMode) if ($testMode)
{ {
logger(INFO, _("Test mode on: not storing in DB ")); logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__);
}else }else
{ {
if(!$this->result = $this->query($query)) 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__);
} }
} }
} }

View File

@ -26,31 +26,31 @@ class hook
{ {
foreach ($this->devicelist as $ieeeAddress => $property2change) foreach ($this->devicelist as $ieeeAddress => $property2change)
{ {
logger(DEBUG, _("Device: ") . $ieeeAddress); logger(DEBUG, _("Device: ") . $ieeeAddress, __FILE__ . ":" . __LINE__);
if ($property2change[1] === false) if ($property2change[1] === false)
{ {
logger(DEBUG, _("Trying to store callback")); logger(DEBUG, _("Trying to store callback"), __FILE__ . ":" . __LINE__);
if (isset($indexDevices[$ieeeAddress])) if (isset($indexDevices[$ieeeAddress]))
{ {
$property = $property2change[0]; $property = $property2change[0];
$indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback"); $indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback");
$property2change[1] = true; $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 }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; $result = false;
} }
}else }else
{ {
logger(DEBUG, _("Callback already installed")); logger(DEBUG, _("Callback already installed"), __FILE__ . ":" . __LINE__);
} }
} }
//echo "result => "; var_dump($result); //echo "result => "; var_dump($result);
if ($result === true) if ($result === true)
{ {
$this->initialized = true; $this->initialized = true;
logger(DEBUG, $this->hookName . (" initialized")); logger(DEBUG, $this->hookName . _(" initialized"), __FILE__ . ":" . __LINE__);
//var_dump($this); //var_dump($this);
} }
} }

View File

@ -1,5 +1,5 @@
<?php <?php
logger(DEBUG,"Including class main.php"); logger(DEBUG, _("Including class main.php"), __FILE__ . ":" . __LINE__);
class Message class Message
{ {

View File

@ -8,16 +8,6 @@ define("OFF", 0);
define("AUTO", 0); define("AUTO", 0);
define("MANUAL", 1); define("MANUAL", 1);
// log levels
/*define( "DEBUG", 16); // => 16
define( "INFO", 1); // => 1
define( "NOTICE", 2); // => 2
define( "WARNING", 4); // => 4
define( "ERROR", 8); // => 8
define( "ALERT", 32);
*/
$logLevels = array( $logLevels = array(
1 => "INFO", 1 => "INFO",
2 => "NOTICE", 2 => "NOTICE",

View File

@ -1,6 +1,6 @@
<?php <?php
logger(DEBUG,"Including db_functions.php"); logger(DEBUG,_("Including db_functions.php"), __FILE__ . ":" . __LINE__);
// to save or not to save the DB, that is the question ... // to save or not to save the DB, that is the question ...
function storeDB($db, $filepath) function storeDB($db, $filepath)
@ -58,7 +58,7 @@ function mkDevicesDB($topic, $json, $group=false)
fwrite($logFh, print_r($devices, true)); fwrite($logFh, print_r($devices, true));
fwrite($logFh, "################################END##################################################"); fwrite($logFh, "################################END##################################################");
echo "Devices DB made" . EOL; logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
//print_r($devices); //print_r($devices);
} }
@ -125,7 +125,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
iterateDevice($topic, $fn, $device, $device, $payloadArray); iterateDevice($topic, $fn, $device, $device, $payloadArray);
}else }else
{ {
logger(ERROR, _("payloadArray is empty!")); logger(ERROR, _("payloadArray is empty!"), __FILE__ . ":" . __LINE__);
} }
} }
@ -205,11 +205,11 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$device->$key["value"] = $value; $device->$key["value"] = $value;
//$changed[$fn]["key"] = $key; //$changed[$fn]["key"] = $key;
//$changed[$fn]["value"] = $value; //$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); $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
if (!empty($device->$key["functions"])) if (!empty($device->$key["functions"]))
{ {
logger(DEBUG,_("executing notifications functions")); logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($device->$key["functions"] as $function) foreach($device->$key["functions"] as $function)
{ {
$function($device, $key, $value); $function($device, $key, $value);

View File

@ -5,7 +5,7 @@ function logDevice(& $device, $key, $value)
$db = new mysqli($mysqlServer, $username, $passwd, $database); $db = new mysqli($mysqlServer, $username, $passwd, $database);
if ($conn->connect_error) if ($conn->connect_error)
{ {
logger(ERROR, _("Mysql connection failed: ") . $db->connect_error); logger(ERROR, _("Mysql connection failed: ") . $db->connect_error, __FILE__ . ":" . __LINE__);
} }
} }

View File

@ -1,25 +1,19 @@
<?php <?php
class <name_of_class> class <name_of_class> extends hook
{ {
public $hookName = <public name of the hook>; public $hookName = <public name of the hook>;
public $active = true; enable/disable hook (true => enabled) public $active = true; //enable/disable hook (true => enabled)
private $devicelist = array(<ieee address or constant defined in config/devices_constants.php>) => <property of the device to watch>); protected $devicelist = array(<ieee address or constant defined in config/devices_constants.php> => array(<property of the device to watch>, false));
// assigne the function to the sensors devices // if you need some initialisation when creating the object, uncomment andput your code here.
function __construct() // If existing, This function is called by __construct
/*protected function init()
{ {
global $indexDevices; // Your code here
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
} }
*/
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
@ -29,7 +23,7 @@ class <name_of_class>
{ {
// here your code // 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__);
} }
} }

View File

@ -1,5 +1,5 @@
<?php <?php
logger(DEBUG,"Including events.php"); logger(DEBUG, _("Including events.php"));
/* for all functions, datetime parameter format is 'dd/mm/yy hh:mm:ss' */ /* for all functions, datetime parameter format is 'dd/mm/yy hh:mm:ss' */
@ -15,13 +15,13 @@ function checkEvents()
$now = now(); $now = now();
if (!empty($event->startDatetime)) if (!empty($event->startDatetime))
{ {
logger(DEBUG, _("StarDatetime is set")); logger(DEBUG, _("StarDatetime is set"), __FILE__ . ":" . __LINE__);
if($event->dateTimeEvent < $now) if($event->dateTimeEvent < $now)
{ {
logger(DEBUG, _("Event must be executed")); logger(DEBUG, _("Event must be executed"), __FILE__ . ":" . __LINE__);
if (!empty($event->exceptionInterval)) if (!empty($event->exceptionInterval))
{ {
logger(DEBUG, _("Testing exceptions")); logger(DEBUG, _("Testing exceptions"), __FILE__ . ":" . __LINE__);
foreach($event->exceptionInterval as $key => $value) foreach($event->exceptionInterval as $key => $value)
{ {
if($now > $value->start and $now < $value->end) if($now > $value->start and $now < $value->end)
@ -35,17 +35,17 @@ function checkEvents()
} }
if ($exception === false) 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); publish(mktopic($event->device), array($event->param => $event->value), "set", $key);
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) 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()) }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); $mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key);
$event->published = $now; $event->published = $now;
//echo "#################################\nUnsetting event $key \n###########################" . EOL; //echo "#################################\nUnsetting event $key \n###########################" . EOL;
@ -95,7 +95,7 @@ function setRecurrentEvent(&$device, $property, $value, $startDatetime, $stopDat
$event->dateTimeEvent = $event->startDatetime; $event->dateTimeEvent = $event->startDatetime;
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) 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)) 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) 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); //print_r($datetime);
if ($replace) if ($replace)
@ -154,7 +154,7 @@ function setDelay(&$device, $delay, $unit="second", $property, $value, $replace=
$events[$key]->param = $property; $events[$key]->param = $property;
$events[$key]->value = $value; $events[$key]->value = $value;
$events[$key]->device = & $device; $events[$key]->device = & $device;
logger (DEBUG, _('Setting new delay in $events[]')); logger (DEBUG, _('Setting new delay in $events[]'), __FILE__ . ":" . __LINE__);
print_r($events[$key]); print_r($events[$key]);
$loglevel = $oldLevel; $loglevel = $oldLevel;
} }
@ -170,7 +170,7 @@ function removeEvent($device, $property , $value)
function searchEvent($device, $property , $value) function searchEvent($device, $property , $value)
{ {
global $events; 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) foreach($events as $key => $event)
{ {
//echo "Event : $event => $value" . EOL; //echo "Event : $event => $value" . EOL;
@ -190,11 +190,11 @@ function deleteEvent($eventKey)
if ($eventKey !== false) if ($eventKey !== false)
{ {
unset ($events[$eventKey]); unset ($events[$eventKey]);
logger(INFO, _("delete event key =") . $eventKey); logger(INFO, _("delete event key =") . $eventKey, __FILE__ . ":" . __LINE__);
}else }else
{ {
//try to delete an flase event //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__);
} }
} }

View File

@ -33,12 +33,12 @@ class notificationFreemobile
if ($result === false) if ($result === false)
{ {
$curlErr += 1; $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 }/*else
{ {
logger(INFO, sprintf(_("Curl return: %s when sending notification"), $result), false); logger(INFO, sprintf(_("Curl return: %s when sending notification"), $result), false);
}*/ }*/
} } //TODO managing curl errors
// close curl resource to free up system resources // close curl resource to free up system resources
curl_close($ch); curl_close($ch);
return true; return true;

View File

@ -41,7 +41,7 @@ class availability
// $log = INFO; // $log = INFO;
//} //}
//$device->availability = $value; //$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; break;
} }

View File

@ -27,7 +27,7 @@ class rdc_chambre_eclairage extends hook
{ {
$this->send(RDC_CHAMBRE_LUMINOSITE, "ON", "OFF", AUTO); $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) private function send($device, $state, $delayState = false, $method = MANUAL)
@ -37,13 +37,13 @@ class rdc_chambre_eclairage extends hook
$msg = array("state" => $state); $msg = array("state" => $state);
if ($device->state["value"] != $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->payload = $msg;
$device->set(); $device->set();
$device->method = $method; $device->method = $method;
}else }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); //echo 'delaystate = ' . var_dump($delayState);

View File

@ -23,7 +23,7 @@ class alerte_intrusion extends hook
case "contact": case "contact":
if ($value == false) 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; break;
} }

View File

@ -21,40 +21,54 @@ class rdc_salon_eclairage extends hook
public function callBack(&$device, $param, $value) public function callBack(&$device, $param, $value)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
logger(INFO, _("hook : rdc_salon_eclairage")); logger(INFO, _("hook : rdc_salon_eclairage"), __FILE__ . ":" . __LINE__);
switch($param) switch($param)
{ {
case "occupancy": case "occupancy":
logger(DEBUG, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__);
//print_r($indexDevices[RDC_SALON_LUMINOSITE]); //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); $this->send("ON", null, AUTO);
removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF"); removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
} }elseif ($value == OFF)
if ($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)
{ {
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); setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
//$this->send("ON", "OFF", AUTO); //$this->send("ON", "OFF", AUTO);
} }
} }
}
break; break;
case "contact": 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; break;
case "illuminance_lux": case "illuminance_lux":
logger(DEBUG, _("CASE : Illuminance"), __FILE__ . ":" . __LINE__);
if ($value >= $this->luminance_max) if ($value >= $this->luminance_max)
{ {
logger(DEBUG, _("illuminace is > to max"), __FILE__ . ":" . __LINE__);
//$this->send("OFF", null, AUTO); //$this->send("OFF", null, AUTO);
removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF"); 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; 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) private function send($state, $delayState = false, $method = MANUAL)
@ -64,13 +78,13 @@ class rdc_salon_eclairage extends hook
$msg = array("state" => $state); $msg = array("state" => $state);
if ($device->state["value"] != $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->payload = $msg;
$device->set(); $device->set();
$device->method = $method; $device->method = $method;
}else }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); //echo 'delaystate = ' . var_dump($delayState);

View File

@ -49,7 +49,7 @@ class rdc_sdb_eclairage extends hook
} }
break; 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) private function send($state)
@ -57,7 +57,7 @@ class rdc_sdb_eclairage extends hook
global $devices, $indexDevices; global $devices, $indexDevices;
$msg = array("state_l1" => $state); $msg = array("state_l1" => $state);
$device = & $indexDevices[RDC_SDB_WC_ECLAIRAGE]; $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->payload = $msg;
$device->set(null); $device->set(null);

View File

@ -28,7 +28,7 @@ class rdc_wc_eclairage extends hook
} }
break; 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__);
} }
} }

View File

@ -56,10 +56,15 @@ function notify($message)
return $result; return $result;
} }
function logger($level, $log, $notif = true) function logger($level, $log, $pos = false, $notif = true)
{ {
global $logFh, $logLevel, $notificationLevel, $logLevels; 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) if ($level & $logLevel)
{ {
fwrite($logFh, $logString . EOL); fwrite($logFh, $logString . EOL);
@ -70,7 +75,7 @@ function logger($level, $log, $notif = true)
{ {
if(notify("Moha\n" . $logString) === false) 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; global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0; $x = 0;
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
storeDB($devices, "/usr/share/moha/moha.db"); storeDB($devices, "/usr/share/moha/moha.db");
if($testMode) file_put_contents("moha.devices", print_r($devices, true)); if($testMode) file_put_contents("moha.devices", print_r($devices, true));
if ($connected) if ($connected)
@ -137,8 +144,8 @@ function connect2mqttServer()
global $client; global $client;
$client->onConnect('connectResponse'); $client->onConnect('connectResponse');
} }
logger(WARNING, _("starting moha"), __FILE__ . ":" . __LINE__);
logger(DEBUG, _("requiring php modules"), false); logger(DEBUG, _("requiring php modules"), __FILE__ . ":" . __LINE__);
require "class/main.php"; require "class/main.php";
require "class/db.php"; require "class/db.php";
require "class/hook_class.php"; require "class/hook_class.php";
@ -152,7 +159,7 @@ require "webserver.php";
//logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false); //logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
//$client = new Mosquitto\Client(); //$client = new Mosquitto\Client();
logger(DEBUG, _("Loading stored devices datas")); logger(DEBUG, _("Loading stored devices datas"), __FILE__ . ":" . __LINE__);
loadDB($devices, "moha.db"); loadDB($devices, "moha.db");
@ -162,28 +169,28 @@ if (!empty($hooksList))
{ {
foreach ($hooksList as $callback) foreach ($hooksList as $callback)
{ {
logger(INFO, _("Including ") . $callback, false); logger(INFO, _("Including ") . $callback, __FILE__ . ":" . __LINE__);
include $callback; 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 //include predefined file witch define constants for devices
if (is_readable($configDir . "/" . "devices_constants.php")) if (is_readable($configDir . "/" . "devices_constants.php"))
{ {
include $configDir . "/" . "devices_constants.php"; include $configDir . "/" . "devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL; //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 }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 // making the list of hooks to include
listHooks("./hooks", $hooksList); listHooks("./hooks", $hooksList);
// Program start // Program start
logger(DEBUG, _("Program start"), false); logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false);
$client = new Mosquitto\Client(); $client = new Mosquitto\Client();
// defining callback functions // defining callback functions
@ -193,7 +200,7 @@ try
connect2mqttServer(); connect2mqttServer();
}catch ( Mosquitto\Exception $err) }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); sleep (10);
exit(1); exit(1);
} }
@ -208,7 +215,7 @@ $client->onPublish('publishResponse');
// connectong to mqtt server // connectong to mqtt server
$client->connect($mqttServerIp, 1883, 5); $client->connect($mqttServerIp, 1883, 5);
logger(INFO, _("Subscribing to bridge"), false); logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false);
foreach($topics as $name => $topic) foreach($topics as $name => $topic)
{ {
$topic->mid = $client->subscribe($name . "/#", 2); $topic->mid = $client->subscribe($name . "/#", 2);
@ -217,20 +224,20 @@ foreach($topics as $name => $topic)
} }
// starting main loop // starting main loop
logger(INFO, _("Starting loop"), false); logger(INFO, _("Starting loop"), __FILE__ . ":" . __LINE__, false);
$oneshot = false; $oneshot = false;
while (true) while (true)
{ {
$client->loop(); // mqtt server loop() $client->loop(); // mqtt server loop()
if (! $included) // hooks not already included 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 getDevicesValues(); // TODO get the values of devices
if (!empty($hooksList)) // some hooks to include if hooklist is not empty if (!empty($hooksList)) // some hooks to include if hooklist is not empty
{ {
foreach ($hooksList as $hook) // loop to include hooks in hookslist foreach ($hooksList as $hook) // loop to include hooks in hookslist
{ {
logger(INFO, _("Including ") . $hook, false); logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false);
include $hook; include $hook;
} }
file_put_contents("/usr/share/moha/moha.devices", print_r($devices, true)); // debugging : save device list 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 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; $oneshot = true;
} }
if($hooksInitialized == 0) // all hooks are not initialized if($hooksInitialized == 0) // all hooks are not initialized
@ -252,14 +259,14 @@ while (true)
{ {
if ($hook->initialized === false) if ($hook->initialized === false)
{ {
logger(WARNING, _("Hook not completely initialized :") . $hookName); logger(WARNING, _("Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
$i &= $hook->installHooks(); $i &= $hook->installHooks();
} }
} }
$hooksInitialized = $i; $hooksInitialized = $i;
}else }else
{ {
logger(DEBUG,_("All hooks initialized")); logger(DEBUG,_("All hooks initialized"), __FILE__ . ":" . __LINE__);
} }
checkEvents(); checkEvents();
askWebServer($read); askWebServer($read);

View File

@ -6,7 +6,7 @@ function messageReceived($message)
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic); $topic = explode ("/", $message->topic);
$callback = $topics[$topic[0]]->callback; $callback = $topics[$topic[0]]->callback;
logger(DEBUG, "topic => " . print_r($topic, true)); logger(DEBUG, "topic => " . print_r($topic, true), __FILE__ . ":" . __LINE__);
$callback($topic, $message); $callback($topic, $message);
} }
@ -29,33 +29,33 @@ function publish($topic, $payload, $commande="set") //, $eventKey)
//echo "setting mids" .EOL; //echo "setting mids" .EOL;
$mids[$mid] = true; $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 }else
{ {
logger(INFO, _("Test mode on: no publishing ")); logger(INFO, _("Test mode on: no publishing "), __FILE__ . ":" . __LINE__);
} }
} }
function connectResponse($r, $message) function connectResponse($r, $message)
{ {
global $connected; 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) switch ($r)
{ {
case 0: case 0:
echo _("Successfull connection") . EOL; logger(INFO, _("Successfull connection"), __FILE__ . ":" . __LINE__);
$connected = true; $connected = true;
break; break;
case 1: case 1:
echo _("Connection refused : unacceptable protocol version") . EOL; logger(ERROR, _("Connection refused : unacceptable protocol version"), __FILE__ . ":" . __LINE__);
$connected = true; $connected = true;
break; break;
case 2: case 2:
echo _("Connection refused : identifier rejected") . EOL; logger(ERROR, _("Connection refused : identifier rejected"), __FILE__ . ":" . __LINE__);
$connected = true; $connected = true;
break; break;
case 3: case 3:
echo _("Connection refused (broker unavailable )") . EOL; logger(ERROR, _("Connection refused (broker unavailable )"), __FILE__ . ":" . __LINE__);
$connected = true; $connected = true;
break; break;
} }
@ -66,7 +66,7 @@ function subscribeResponse($mid, $qosCount)
global $topics, $mids, $nSubscribed; global $topics, $mids, $nSubscribed;
//print_r($mids); //print_r($mids);
$key = $mids[$mid]; $key = $mids[$mid];
echo _("Subscribed to ") . $key . EOL; logger(INFO, _("Subscribed to ") . $key, __FILE__ . ":" . __LINE__);
$topics[$key]->status = true; $topics[$key]->status = true;
$nSubscribed += 1; $nSubscribed += 1;
} }
@ -75,7 +75,7 @@ function unsubscribeResponse($mid)
{ {
global $client; //$topics, $mids, $nSubscribed; global $client; //$topics, $mids, $nSubscribed;
//$key = $mids[$mid]; //$key = $mids[$mid];
echo _("Unsubscribed"); // from ") . $topics[$key]->name . EOL; logger(INFO, _("Unsubscribed", __FILE__ . ":" . __LINE__)); // from ") . $topics[$key]->name);
//$topics[$key]->status = false; //$topics[$key]->status = false;
//$nSubscribed -= 1; //$nSubscribed -= 1;
$client->disconnect(); $client->disconnect();
@ -86,19 +86,19 @@ function disconnectResponse($r)
global $connected; global $connected;
if ($r != 0) if ($r != 0)
{ {
echo _('Badly '); $str = _('Badly ');
}else }else
{ {
echo _('Cleanly '); $str = _('Cleanly ');
} }
echo _("disconnected from server") . EOL; logger(ERROR, $str . _("disconnected from server"));
$connected = false; $connected = false;
} }
function publishResponse($mid) function publishResponse($mid)
{ {
global $mids, $events; 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])) if (isset($mids[$mid]))
{ {
//echo "unsetting mids" . EOL; //echo "unsetting mids" . EOL;

View File

@ -7,12 +7,12 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topicName = $topic[0]; $topicName = $topic[0];
$friendlyName = $topic[1]; // get friendlyName $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]]; $device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload); $payloadArray = json_decode($message->payload);
if (!isset($device[$fn])) //must not exists, but ... 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] = array();
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
$device[$fn]["device"]->type = "mesure"; $device[$fn]["device"]->type = "mesure";

View File

@ -5,13 +5,13 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
{ {
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$fn = $topic[1]; // get friendlyname $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]]; $device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload); $payloadArray = json_decode($message->payload);
if (!isset($device[$fn])) if (!isset($device[$fn]))
{ {
logger(WARNING, $logFh, "init of " . $fn .EOL); logger(WARNING, $logFh, _("init of ") . $fn, __FILE__ . ":" . __LINE__);
$device[$fn] = array(); $device[$fn] = array();
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
$device[$fn]["device"]->type = $payloadArray->type; $device[$fn]["device"]->type = $payloadArray->type;

View File

@ -11,13 +11,13 @@ $callback = function($topic, $message)
$topics[$topic[0]]->info = json_decode($message->payload); $topics[$topic[0]]->info = json_decode($message->payload);
break; break;
case "devices": 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); $topics[$topic[0]]->devices = json_decode($message->payload);
fwrite($logFh, print_r($topics[$topic[0]]->devices, true)); fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
mkDevicesDB($topic[0], $topics[$topic[0]]->devices); mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
break; break;
case "groups": 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); $topics[$topic[0]]->groups = json_decode($message->payload);
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true); mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
break; break;
@ -57,7 +57,7 @@ $callback = function($topic, $message)
//print_r($device) ; //print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ... 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] = array();
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
addDevice($device[$fn], $fn, $payloadArray); addDevice($device[$fn], $fn, $payloadArray);

View File

@ -1,5 +1,5 @@
<?php <?php
logger(DEBUG,"Including utils.php"); logger(DEBUG, _("Including utils.php"), __FILE__ . ":" . __LINE__);
function bool2string($var) function bool2string($var)
{ {
@ -41,7 +41,19 @@ function mktopic($device)
return $device->topic . "/" . $device->friendlyName; return $device->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 //signal handling
function signalHandler($signal) function signalHandler($signal)
{ {

View File

@ -20,13 +20,13 @@ function askWebServer($read)
if ( stream_select( $read, $array, $array, 0 )) 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]); $spawn = stream_socket_accept($read[0]);
if ($spawn !== false) if ($spawn !== false)
{ {
logger(DEBUG,_("socket accepted")); logger(DEBUG, _("socket accepted"), __FILE__ . ":" . __LINE__);
$input = fgets($spawn, 4096); $input = fgets($spawn, 4096);
logger(DEBUG, $input); logger(DEBUG, $input, __FILE__ . ":" . __LINE__);
if (!empty($input)) if (!empty($input))
{ {
$input = substr($input,5); $input = substr($input,5);
@ -34,17 +34,18 @@ function askWebServer($read)
$argTmp = explode("&", $input[0]); $argTmp = explode("&", $input[0]);
foreach($argTmp as $tmp) foreach($argTmp as $tmp)
{ {
logger(DEBUG, $tmp); logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__);
$array = explode("=", $tmp); $array = explode("=", $tmp);
print_r($array); //print_r($array);
if (isset($array[1])) $argList[$array[0]] = $array[1]; if (isset($array[1])) $argList[$array[0]] = $array[1];
} }
if(array_key_exists("cmd", $argList)) if(array_key_exists("cmd", $argList))
{ {
switch(strtolower($argList["cmd"])) $command = strtolower($argList["cmd"]);
switch($command)
{ {
case "get": case "get":
logger(DEBUG, "get reached"); logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__);
if(empty($argList["device"])) if(empty($argList["device"]))
{ {
$response = "<html><header><body>get passed</body></header><html>"; $response = "<html><header><body>get passed</body></header><html>";
@ -52,12 +53,13 @@ function askWebServer($read)
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
case "set": case "set":
logger(DEBUG, "set reached"); logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__);
$response = "<html><header><body>set passed</body></header><html>"; $response = "<html><header><body>set passed</body></header><html>";
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
case "dump":
case "print": case "print":
logger(DEBUG, "print reached"); logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
$var = $GLOBALS[$argList["object"]]; $var = $GLOBALS[$argList["object"]];
if (isset($argList["topic"])) if (isset($argList["topic"]))
{ {
@ -78,7 +80,12 @@ function askWebServer($read)
} }
}else }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(); $error = error_get_last();
@ -86,12 +93,19 @@ function askWebServer($read)
{ {
$response = "<html><header><body>" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "</body></header><html>"; $response = "<html><header><body>" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "</body></header><html>";
} }
$response = "<html><header><body>" . print_r($var, true) . "</body></header><html>"; if ($command === "print")
{
$response = print_r($var, true);
}elseif($command === "dump")
{
$response = "Dump" . EOL;
$response .= var_export($var, true);
}
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
default: default:
logger(DEBUG, "not understanding command"); logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__);
fwrite($spawn, "not understanding command"); fwrite($spawn, _("unknown command"));
} }
} }
} }