1
0

debug\ncode optimization

This commit is contained in:
daniel Tartavel 2022-01-19 00:22:34 +01:00
parent 11a2253804
commit e4703dafc8
15 changed files with 135 additions and 82 deletions

View File

@ -26,9 +26,9 @@ class db extends mysqli
function logProperty($device, $propertyTree, $value, $oldValue = 0) function logProperty($device, $propertyTree, $value, $oldValue = 0)
{ {
global $mohaDB, $properties2log; global $mohaDB, $properties2log, $testMode;
$precision = 0; $precision = 0;
echo "############## logProperty ################\nproperty => " . $propertyTree .EOL; //echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
if (array_key_exists($propertyTree, $properties2log)) if (array_key_exists($propertyTree, $properties2log))
{ {
//echo "logging in database"; //echo "logging in database";
@ -37,22 +37,35 @@ class db extends mysqli
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($propertyTree) . "', '" . $this->protect($value) . "')"; $query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($propertyTree) . "', '" . $this->protect($value) . "')";
if (is_numeric($value) and !empty($properties2log[$propertyTree])) if (is_numeric($value) and !empty($properties2log[$propertyTree]))
{ {
$minMax = (float)$value * (float)$properties2log[$propertyTree] / 100; // calculate a min/max value for storing data
//echo "minMax = " .$minMax . EOL; $var = $properties2log[$propertyTree];
if (!is_numeric($var))
{
$minMax = (float)$value * (float)$var / 100;
}else
{
$minMax = $var;
}
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)
{ {
//echo "========>>>>>>>>>not changed" . EOL; //echo "========>>>>>>>>>not changed" . EOL;
return 0; return 0;
} }
} }
if ($testMode)
{
logger(INFO, _("Test mode on: not storing in DB "));
}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);
} }
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));
} }
} }
} }

View File

@ -21,7 +21,7 @@ class topic {
class device class device
{ {
public $method; //auto or manual public $method; //0 = auto or 1 = manual
public $topic; public $topic;
public $device; public $device;
public $ieeeAddress; public $ieeeAddress;
@ -31,16 +31,23 @@ class device
public $description; public $description;
public $functions; public $functions;
public $payload; public $payload;
public $availability = array(); public $availability;
public function set($event) private function __construct()
{ {
publish($this->topic . "/" . $this->friendlyName, $this->payload, "set", $event); $this->availability = array("value" => null, "functions" => array());
var_dump($this->availability);
}
public function set($method=0) //, $event = null)
{
$this->method = $method;
publish($this->topic . "/" . $this->friendlyName, $this->payload, "set"); //, $event);
} }
public function get() public function get()
{ {
publish($this->topic . "/" . $this->friendlyNames, $this->payload, "get", $event); publish($this->topic . "/" . $this->friendlyNames, $this->payload, "get"); //, $event);
} }
} }

View File

@ -12,7 +12,7 @@ $deviceTable = array(
"0x00124b001f900753" => "RDC_ENTREE_PORTE", "0x00124b001f900753" => "RDC_ENTREE_PORTE",
"0x04cf8cdf3c78aff0" => "RDC_SALON_LUMINOSITE", "0x04cf8cdf3c78aff0" => "RDC_SALON_LUMINOSITE",
"0x588e81fffe343e8f" => "RDC_SALON_ECLAIRAGE_PANNEAU", "0x588e81fffe343e8f" => "RDC_SALON_ECLAIRAGE_PANNEAU",
"0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE", "0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE_MVMT",
"0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT", "0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT",
"0x00124b0022ec05dc" => "RDC_SDB_MVMT", "0x00124b0022ec05dc" => "RDC_SDB_MVMT",
"0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE", "0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE",

View File

@ -5,37 +5,39 @@
// superior or inferior at value +/- x% // superior or inferior at value +/- x%
$properties2log = array( $properties2log = array(
"availability" => null,
"action" => null, "action" => null,
"state" =>null, "state" =>null,
"contact" => null, "contact" => null,
"temperature" =>2, "temperature" => 0.5,
"local_temperature" => 0.5,
"state_l1" => null, "state_l1" => null,
"state_l2" => null, "state_l2" => null,
"humidity" => null, "humidity" => null,
"current_heating_setpoint" => null, "current_heating_setpoint" => null,
"position" => null, "position" => null,
"pressure" => 0.5, "pressure" => 10,
"occupancy" => null, "occupancy" => null,
"tamper" => null, "tamper" => null,
"illuminance_lux" => 8, "illuminance_lux" => '10%',
// "illuminance" => 8, // "illuminance" => 8,
"requested_brightness_level" => 8, "requested_brightness_level" => '10%',
"tempf" => 5, "tempf" => 0.5,
"humidity" => null, "humidity" => null,
"dewptf" => 5, "dewptf" => 0.5,
"windchillf" => 5, "windchillf" => 0.5,
"winddir" => 8, "winddir" => 40,
"windspeedmph" => 20, "windspeedmph" => 20,
"windgustmph" => 20, "windgustmph" => 20,
"rainin" => null, "rainin" => null,
"dailyrainin" => null, //"dailyrainin" => null,
"weeklyrainin" => null, //"weeklyrainin" => null,
"monthlyrainin" => null, //"monthlyrainin" => null,
"yearlyrainin" => null, //"yearlyrainin" => null,
"solarradiation" => 10, "solarradiation" => 10,
"UV" => null, "UV" => null,
"indoortempf" => 5, "indoortempf" => 0.5,
"indoorhumidity" => null, "indoorhumidity" => null,
"baromin" => 5 "baromin" => 10
); );

37
constants.php Normal file
View File

@ -0,0 +1,37 @@
<?php
//Constants
define( "EOL", "\n");
define("Z2M", "zigbee2mqtt");
define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
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(
1 => "INFO",
2 => "NOTICE",
4 => "WARNING",
8 => "ERROR",
16 => "DEBUG",
32 => "ALERT"
);
foreach ($logLevels as $key => $string)
{
define($string, $key);
}
$logLevels[6] = "NOTICE & WARNING";
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
?>

View File

@ -183,13 +183,16 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}else }else
{ {
if (empty($device->$key) or $value != null) //if (empty($device->$key) or $value != null)
{ //{
if (property_exists($device, $key)) if (property_exists($device, $key))
{ {
if (is_array($device->$key)) if (is_array($device->$key))
{
if (!empty($device->$key["value"]))
{ {
$oldValue = $device->$key["value"]; $oldValue = $device->$key["value"];
}
}else }else
{ {
$oldValue = $device->$key; $oldValue = $device->$key;
@ -202,16 +205,10 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
if ($oldValue !== $value) if ($oldValue !== $value)
{ {
$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, value changed to %s"), $fn, $propertyTree . $key, $value)); logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)));
if ($testMode === false)
{
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
}else
{
logger(INFO, _("Test mode on: not storing in DB "));
}
} }
if (!empty($device->$key["functions"])) if (!empty($device->$key["functions"]))
{ {
@ -221,7 +218,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$function($device, $key, $value); $function($device, $key, $value);
} }
} }
} //}
} }
} }
} }

View File

@ -40,8 +40,8 @@ function checkEvents()
} }
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now()) }elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
{ {
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL; logger(DEBUG, sprintf(_("---->sending command set %s => %s to %s"),$event->param, $event->value, $event->device->friendlyName));
$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;
unset($events[$key]); unset($events[$key]);

View File

@ -2,7 +2,7 @@
class rdc_salon_eclairage extends hook class rdc_salon_eclairage extends hook
{ {
public $hookName = "rdc_salon_eclairage"; public $hookName = "rdc_salon_eclairage";
public $active = true;
// list of devices we are listening to // list of devices we are listening to
protected $devicelist = array( protected $devicelist = array(
RDC_SALON_MVMT => array("occupancy", false), RDC_SALON_MVMT => array("occupancy", false),
@ -44,19 +44,19 @@ class rdc_salon_eclairage extends hook
} }
break; break;
} }
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value)); logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, bool2string($value)));
} }
private function send($state) private function send($state)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
$device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU]; $device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
$msg = array("state" => $state);
if ($device->state["value"] != $state) if ($device->state["value"] != $state)
{ {
$msg = array("state" => $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));
$device->payload = $msg; $device->payload = $msg;
$device->set(null); $device->set();
setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
}else }else
{ {

View File

@ -12,17 +12,13 @@ class rdc_sdb_eclairage extends hook
public $delayManual = 10; // amount of time in $timeunit for manual mode public $delayManual = 10; // amount of time in $timeunit for manual mode
public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
// list of devices we are listening to // list of devices we are listening to
// 0x00158d0003f0f3b4 douche mvmnt
// 0x842e14fffe1c0cd1 plafond mvmnt
// 0x00124b0022ec05dc mvmnt
// 0x00158d0005c1a998 module commutateur => state_l1
// devicelist[$ieeAddress][0] => property to watch // devicelist[$ieeAddress][0] => property to watch
// devicelist[$ieeAddress][1] => initialized = true // devicelist[$ieeAddress][1] => initialized = true
protected $devicelist = array( protected $devicelist = array(
"0x00158d0003f0f3b4" => array("occupancy", false), RDC_SDB_DOUCHE_MVMT => array("occupancy", false),
"0x842e14fffe1c0cd1" => array("occupancy", false), RDC_SDB_PLAFOND_MVMT => array("occupancy", false),
"0x00124b0022ec05dc" => array("occupancy", false), RDC_SDB_MVMT => array("occupancy", false),
RDC_SDB_WC_ECLAIRAGE => array("state_l1", false) RDC_SDB_WC_ECLAIRAGE => array("state_l1", false)
); );
@ -51,7 +47,7 @@ class rdc_sdb_eclairage extends hook
} }
break; break;
} }
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, bool2string($value)));
} }
private function send($state) private function send($state)

View File

@ -27,7 +27,7 @@ class rdc_wc_eclairage extends hook
} }
break; break;
} }
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, bool2string($value)));
} }
} }

View File

@ -1,25 +1,11 @@
<?php <?php
$title = "moha"; $title = "moha";
$testMode = false; $testMode = true;
cli_set_process_title($title); cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title); file_put_contents("/proc/".getmypid()."/comm",$title);
//Constants require "constants.php";
define( "EOL", "\n");
define("Z2M", "zigbee2mqtt");
define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
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);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
declare(ticks = 1); declare(ticks = 1);
@ -68,19 +54,20 @@ function notify($message)
function logger($level, $log, $notif = true) function logger($level, $log, $notif = true)
{ {
global $logFh, $logLevel, $notificationLevel; global $logFh, $logLevel, $notificationLevel, $logLevels;
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ; //echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
//echo $log .EOL; //echo $log .EOL;
$logString = date("c") . ' ' . $logLevels[$level] . " : $log";
if ($level & $logLevel) if ($level & $logLevel)
{ {
fwrite($logFh, "$level : $log" . EOL); fwrite($logFh, $logString . EOL);
print ("$level : $log" . EOL); print ($logString . EOL);
} }
$test = $level & $notificationLevel; $test = $level & $notificationLevel;
//echo "notif =>" .$notif . EOL; //echo "notif =>" .$notif . EOL;
if (($test != 0) and ($notif === true)) if (($test != 0) and ($notif === true))
{ {
if(notify("Moha\n" . $log) === false) if(notify("Moha\n" . $logString) === false)
{ {
logger(INFO, _("Notification not sent"), false); logger(INFO, _("Notification not sent"), false);
} }

View File

@ -13,7 +13,7 @@ function messageReceived($message)
// payload is an array : // payload is an array :
// $key is property and $value is value of the property // $key is property and $value is value of the property
function publish($topic, $payload, $commande="set", $eventKey) function publish($topic, $payload, $commande="set") //, $eventKey)
{ {
global $client, $mids, $logFh, $testMode; global $client, $mids, $logFh, $testMode;
$string = $topic . "/" . $commande; $string = $topic . "/" . $commande;

View File

@ -21,6 +21,6 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
} }
$device = & $device[$fn]; $device = & $device[$fn];
changeDevice($topicName, $friendlyName, $device["device"], $payloadArray); changeDevice($topicName, $friendlyName, $device["device"], $payloadArray);
print_r($device["device"]); //print_r($device["device"]);
} }
?> ?>

View File

@ -42,7 +42,7 @@ $callback = function($topic, $message)
$fnTree = explode ("/" , $topic[1]); // get friendlyname $fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL; echo $topic[0] . " => " . $topic[1] . EOL;
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload); //$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
if ($fnTree[array_key_last($fnTree)] == "availability") if ($fnTree[array_key_last($fnTree)] == "availability" and is_string($message->payload))
{ {
unset ($fnTree[array_key_last($fnTree)]); unset ($fnTree[array_key_last($fnTree)]);
$payloadArray = array("availability" => $message->payload); $payloadArray = array("availability" => $message->payload);
@ -60,7 +60,7 @@ $callback = function($topic, $message)
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
$device[$fn] = array(); $device[$fn] = array();
$device[$fn]["device"] = new device; $device[$fn]["device"] = new device;
//addDevice($device[$fn], $fn, ); addDevice($device[$fn], $fn, $payloadArray);
} }
$device = & $device[$fn]; $device = & $device[$fn];
} }

View File

@ -1,9 +1,23 @@
<?php <?php
logger(DEBUG,"Including utils.php"); logger(DEBUG,"Including utils.php");
function bool2string($var)
{
if ($var === false)
{
return "false";
}elseif($var === true)
{
return "true";
}else
{
return $var;
}
}
function now() function now()
{ {
return new DateTime("now"); return date("c");
} }
function farenheit2celsius($value) function farenheit2celsius($value)