debug\ncode optimization
This commit is contained in:
parent
11a2253804
commit
e4703dafc8
25
class/db.php
25
class/db.php
@ -26,9 +26,9 @@ class db extends mysqli
|
||||
|
||||
function logProperty($device, $propertyTree, $value, $oldValue = 0)
|
||||
{
|
||||
global $mohaDB, $properties2log;
|
||||
global $mohaDB, $properties2log, $testMode;
|
||||
$precision = 0;
|
||||
echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
|
||||
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
|
||||
if (array_key_exists($propertyTree, $properties2log))
|
||||
{
|
||||
//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) . "')";
|
||||
if (is_numeric($value) and !empty($properties2log[$propertyTree]))
|
||||
{
|
||||
$minMax = (float)$value * (float)$properties2log[$propertyTree] / 100;
|
||||
//echo "minMax = " .$minMax . EOL;
|
||||
// calculate a min/max value for storing data
|
||||
$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 "Value = " . $value . EOL;
|
||||
|
||||
if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax)
|
||||
{
|
||||
//echo "========>>>>>>>>>not changed" . EOL;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if ($testMode)
|
||||
{
|
||||
logger(INFO, _("Test mode on: not storing in DB "));
|
||||
}else
|
||||
{
|
||||
if(!$this->result = $this->query($query))
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ class topic {
|
||||
|
||||
class device
|
||||
{
|
||||
public $method; //auto or manual
|
||||
public $method; //0 = auto or 1 = manual
|
||||
public $topic;
|
||||
public $device;
|
||||
public $ieeeAddress;
|
||||
@ -31,16 +31,23 @@ class device
|
||||
public $description;
|
||||
public $functions;
|
||||
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()
|
||||
{
|
||||
publish($this->topic . "/" . $this->friendlyNames, $this->payload, "get", $event);
|
||||
publish($this->topic . "/" . $this->friendlyNames, $this->payload, "get"); //, $event);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ $deviceTable = array(
|
||||
"0x00124b001f900753" => "RDC_ENTREE_PORTE",
|
||||
"0x04cf8cdf3c78aff0" => "RDC_SALON_LUMINOSITE",
|
||||
"0x588e81fffe343e8f" => "RDC_SALON_ECLAIRAGE_PANNEAU",
|
||||
"0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE",
|
||||
"0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE_MVMT",
|
||||
"0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT",
|
||||
"0x00124b0022ec05dc" => "RDC_SDB_MVMT",
|
||||
"0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE",
|
||||
|
@ -5,37 +5,39 @@
|
||||
// superior or inferior at value +/- x%
|
||||
|
||||
$properties2log = array(
|
||||
"availability" => null,
|
||||
"action" => null,
|
||||
"state" =>null,
|
||||
"contact" => null,
|
||||
"temperature" =>2,
|
||||
"temperature" => 0.5,
|
||||
"local_temperature" => 0.5,
|
||||
"state_l1" => null,
|
||||
"state_l2" => null,
|
||||
"humidity" => null,
|
||||
"current_heating_setpoint" => null,
|
||||
"position" => null,
|
||||
"pressure" => 0.5,
|
||||
"pressure" => 10,
|
||||
"occupancy" => null,
|
||||
"tamper" => null,
|
||||
"illuminance_lux" => 8,
|
||||
"illuminance_lux" => '10%',
|
||||
// "illuminance" => 8,
|
||||
"requested_brightness_level" => 8,
|
||||
"tempf" => 5,
|
||||
"requested_brightness_level" => '10%',
|
||||
"tempf" => 0.5,
|
||||
"humidity" => null,
|
||||
"dewptf" => 5,
|
||||
"windchillf" => 5,
|
||||
"winddir" => 8,
|
||||
"dewptf" => 0.5,
|
||||
"windchillf" => 0.5,
|
||||
"winddir" => 40,
|
||||
"windspeedmph" => 20,
|
||||
"windgustmph" => 20,
|
||||
"rainin" => null,
|
||||
"dailyrainin" => null,
|
||||
"weeklyrainin" => null,
|
||||
"monthlyrainin" => null,
|
||||
"yearlyrainin" => null,
|
||||
//"dailyrainin" => null,
|
||||
//"weeklyrainin" => null,
|
||||
//"monthlyrainin" => null,
|
||||
//"yearlyrainin" => null,
|
||||
"solarradiation" => 10,
|
||||
"UV" => null,
|
||||
"indoortempf" => 5,
|
||||
"indoortempf" => 0.5,
|
||||
"indoorhumidity" => null,
|
||||
"baromin" => 5
|
||||
"baromin" => 10
|
||||
);
|
||||
|
||||
|
37
constants.php
Normal file
37
constants.php
Normal 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);
|
||||
?>
|
@ -183,13 +183,16 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
|
||||
}else
|
||||
{
|
||||
if (empty($device->$key) or $value != null)
|
||||
{
|
||||
//if (empty($device->$key) or $value != null)
|
||||
//{
|
||||
if (property_exists($device, $key))
|
||||
{
|
||||
if (is_array($device->$key))
|
||||
{
|
||||
if (!empty($device->$key["value"]))
|
||||
{
|
||||
$oldValue = $device->$key["value"];
|
||||
}
|
||||
}else
|
||||
{
|
||||
$oldValue = $device->$key;
|
||||
@ -202,16 +205,10 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
if ($oldValue !== $value)
|
||||
{
|
||||
$device->$key["value"] = $value;
|
||||
$changed[$fn]["key"] = $key;
|
||||
$changed[$fn]["value"] = $value;
|
||||
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value));
|
||||
if ($testMode === false)
|
||||
{
|
||||
//$changed[$fn]["key"] = $key;
|
||||
//$changed[$fn]["value"] = $value;
|
||||
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)));
|
||||
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
|
||||
}else
|
||||
{
|
||||
logger(INFO, _("Test mode on: not storing in DB "));
|
||||
}
|
||||
}
|
||||
if (!empty($device->$key["functions"]))
|
||||
{
|
||||
@ -221,7 +218,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
$function($device, $key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,8 +40,8 @@ function checkEvents()
|
||||
}
|
||||
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
|
||||
{
|
||||
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
|
||||
$mid = publish(mktopic($event->device), array($event->param => $event->value), "set", $key);
|
||||
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);
|
||||
$event->published = now();
|
||||
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
|
||||
unset($events[$key]);
|
||||
|
@ -2,7 +2,7 @@
|
||||
class rdc_salon_eclairage extends hook
|
||||
{
|
||||
public $hookName = "rdc_salon_eclairage";
|
||||
|
||||
public $active = true;
|
||||
// list of devices we are listening to
|
||||
protected $devicelist = array(
|
||||
RDC_SALON_MVMT => array("occupancy", false),
|
||||
@ -44,19 +44,19 @@ class rdc_salon_eclairage extends hook
|
||||
}
|
||||
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)
|
||||
{
|
||||
global $devices, $indexDevices;
|
||||
$device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
|
||||
$msg = array("state" => $state);
|
||||
if ($device->state["value"] != $state)
|
||||
{
|
||||
$msg = array("state" => $state);
|
||||
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName));
|
||||
$device->payload = $msg;
|
||||
$device->set(null);
|
||||
$device->set();
|
||||
setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
|
||||
}else
|
||||
{
|
||||
|
@ -12,17 +12,13 @@ class rdc_sdb_eclairage extends hook
|
||||
public $delayManual = 10; // amount of time in $timeunit for manual mode
|
||||
public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
|
||||
// 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][1] => initialized = true
|
||||
protected $devicelist = array(
|
||||
"0x00158d0003f0f3b4" => array("occupancy", false),
|
||||
"0x842e14fffe1c0cd1" => array("occupancy", false),
|
||||
"0x00124b0022ec05dc" => array("occupancy", false),
|
||||
RDC_SDB_DOUCHE_MVMT => array("occupancy", false),
|
||||
RDC_SDB_PLAFOND_MVMT => array("occupancy", false),
|
||||
RDC_SDB_MVMT => array("occupancy", false),
|
||||
RDC_SDB_WC_ECLAIRAGE => array("state_l1", false)
|
||||
);
|
||||
|
||||
@ -51,7 +47,7 @@ class rdc_sdb_eclairage extends hook
|
||||
}
|
||||
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)
|
||||
|
@ -27,7 +27,7 @@ class rdc_wc_eclairage extends hook
|
||||
}
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
27
moha.php
27
moha.php
@ -1,25 +1,11 @@
|
||||
<?php
|
||||
$title = "moha";
|
||||
$testMode = false;
|
||||
$testMode = true;
|
||||
|
||||
cli_set_process_title($title);
|
||||
file_put_contents("/proc/".getmypid()."/comm",$title);
|
||||
|
||||
//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);
|
||||
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
|
||||
require "constants.php";
|
||||
|
||||
declare(ticks = 1);
|
||||
|
||||
@ -68,19 +54,20 @@ function notify($message)
|
||||
|
||||
function logger($level, $log, $notif = true)
|
||||
{
|
||||
global $logFh, $logLevel, $notificationLevel;
|
||||
global $logFh, $logLevel, $notificationLevel, $logLevels;
|
||||
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
|
||||
//echo $log .EOL;
|
||||
$logString = date("c") . ' ' . $logLevels[$level] . " : $log";
|
||||
if ($level & $logLevel)
|
||||
{
|
||||
fwrite($logFh, "$level : $log" . EOL);
|
||||
print ("$level : $log" . EOL);
|
||||
fwrite($logFh, $logString . EOL);
|
||||
print ($logString . EOL);
|
||||
}
|
||||
$test = $level & $notificationLevel;
|
||||
//echo "notif =>" .$notif . EOL;
|
||||
if (($test != 0) and ($notif === true))
|
||||
{
|
||||
if(notify("Moha\n" . $log) === false)
|
||||
if(notify("Moha\n" . $logString) === false)
|
||||
{
|
||||
logger(INFO, _("Notification not sent"), false);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ function messageReceived($message)
|
||||
// payload is an array :
|
||||
// $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;
|
||||
$string = $topic . "/" . $commande;
|
||||
|
@ -21,6 +21,6 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
|
||||
}
|
||||
$device = & $device[$fn];
|
||||
changeDevice($topicName, $friendlyName, $device["device"], $payloadArray);
|
||||
print_r($device["device"]);
|
||||
//print_r($device["device"]);
|
||||
}
|
||||
?>
|
||||
|
@ -42,7 +42,7 @@ $callback = function($topic, $message)
|
||||
$fnTree = explode ("/" , $topic[1]); // get friendlyname
|
||||
echo $topic[0] . " => " . $topic[1] . EOL;
|
||||
//$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)]);
|
||||
$payloadArray = array("availability" => $message->payload);
|
||||
@ -60,7 +60,7 @@ $callback = function($topic, $message)
|
||||
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
|
||||
$device[$fn] = array();
|
||||
$device[$fn]["device"] = new device;
|
||||
//addDevice($device[$fn], $fn, );
|
||||
addDevice($device[$fn], $fn, $payloadArray);
|
||||
}
|
||||
$device = & $device[$fn];
|
||||
}
|
||||
|
16
utils.php
16
utils.php
@ -1,9 +1,23 @@
|
||||
<?php
|
||||
logger(DEBUG,"Including utils.php");
|
||||
|
||||
function bool2string($var)
|
||||
{
|
||||
if ($var === false)
|
||||
{
|
||||
return "false";
|
||||
}elseif($var === true)
|
||||
{
|
||||
return "true";
|
||||
}else
|
||||
{
|
||||
return $var;
|
||||
}
|
||||
}
|
||||
|
||||
function now()
|
||||
{
|
||||
return new DateTime("now");
|
||||
return date("c");
|
||||
}
|
||||
|
||||
function farenheit2celsius($value)
|
||||
|
Loading…
Reference in New Issue
Block a user