diff --git a/class/db.php b/class/db.php index aaa5e17..6840bb6 100644 --- a/class/db.php +++ b/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(!$this->result = $this->query($query)) + if ($testMode) { - logger(ERROR, _("mysql query errror: ") . $this->error); + 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)); } } } diff --git a/class/main.php b/class/main.php index b2dd8c1..b69a31a 100644 --- a/class/main.php +++ b/class/main.php @@ -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); } } diff --git a/config/devices_constants.php b/config/devices_constants.php index e16532c..8dd4ca9 100644 --- a/config/devices_constants.php +++ b/config/devices_constants.php @@ -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", diff --git a/config/properties2log.php b/config/properties2log.php index e1835af..412204a 100644 --- a/config/properties2log.php +++ b/config/properties2log.php @@ -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 ); diff --git a/constants.php b/constants.php new file mode 100644 index 0000000..8bdb80e --- /dev/null +++ b/constants.php @@ -0,0 +1,37 @@ + 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); +?> diff --git a/db_functions.php b/db_functions.php index 2d530b0..114274c 100644 --- a/db_functions.php +++ b/db_functions.php @@ -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)) { - $oldValue = $device->$key["value"]; + 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) - { - $mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); - }else - { - logger(INFO, _("Test mode on: not storing in DB ")); - } + //$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); } if (!empty($device->$key["functions"])) { @@ -221,7 +218,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro $function($device, $key, $value); } } - } + //} } } } diff --git a/events.php b/events.php index 6696109..903f6d2 100644 --- a/events.php +++ b/events.php @@ -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]); diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 7fe01d4..0c0f919 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -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 { diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 9478fcf..3d62980 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -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) diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index 5cbc89e..0def5cb 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -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))); } } diff --git a/moha.php b/moha.php index f06d0da..87bccfe 100644 --- a/moha.php +++ b/moha.php @@ -1,25 +1,11 @@ 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); } diff --git a/mqtt_functions.php b/mqtt_functions.php index 8dbfdbe..dfcfcf0 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -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; diff --git a/topics_callbacks/linky2mqtt.php b/topics_callbacks/linky2mqtt.php index 7d6e05a..28b15c8 100644 --- a/topics_callbacks/linky2mqtt.php +++ b/topics_callbacks/linky2mqtt.php @@ -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"]); } ?> diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index 9f56f4f..281eaed 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -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]; } diff --git a/utils.php b/utils.php index cca5e44..5a5bb3a 100644 --- a/utils.php +++ b/utils.php @@ -1,9 +1,23 @@