diff --git a/class/db.php b/class/db.php index 6840bb6..84a4600 100644 --- a/class/db.php +++ b/class/db.php @@ -3,7 +3,7 @@ logger(DEBUG,"Including db.php"); class db extends mysqli { - public $mysqlServer = "192.168.1.253"; // "127.0.0.1"; + public $mysqlServer = "localhost"; // "127.0.0.1"; public $username = "moha"; public $passwd = "MysqlMoha"; public $database = "moha"; diff --git a/db_functions.php b/db_functions.php index 114274c..e1bdc0c 100644 --- a/db_functions.php +++ b/db_functions.php @@ -189,10 +189,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro { if (is_array($device->$key)) { - if (!empty($device->$key["value"])) - { - $oldValue = $device->$key["value"]; - } + $oldValue = $device->$key["value"]; }else { $oldValue = $device->$key; @@ -202,6 +199,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro $device->{$key} = array("value" => null); $device->$key["functions"] = array(); } + echo $key . 'oldvalue = ' . $oldValue . " value = " . $value . EOL; if ($oldValue !== $value) { $device->$key["value"] = $value; @@ -209,13 +207,14 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro //$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"])) - { - logger(DEBUG,_("executing notifications functions")); - foreach($device->$key["functions"] as $function) +print_r($device->$key); + if (!empty($device->$key["functions"])) { - $function($device, $key, $value); + logger(DEBUG,_("executing notifications functions")); + foreach($device->$key["functions"] as $function) + { + $function($device, $key, $value); + } } } //} diff --git a/events.php b/events.php index 903f6d2..cedc65c 100644 --- a/events.php +++ b/events.php @@ -10,19 +10,22 @@ function checkEvents() $exception = false; foreach ($events as $key => $event) { + $now = now(); if (!empty($event->startDatetime)) { - if($event->dateTimeEvent <= now()) + logger(DEBUG, _("StarDatetime is set")); + if($event->dateTimeEvent < $now) { - + logger(DEBUG, _("Event must be executed")); if (!empty($event->exceptionInterval)) { + logger(DEBUG, _("Testing exceptions")); foreach($event->exceptionInterval as $key => $value) { - if($datetime("now") >= $value->start and $datetime("now") <= $value->end) + if($now > $value->start and $now < $value->end) { $exception = true; - }elseif($datetime("now") >= $value->stop) + }elseif($now > $value->stop) { unset($event->exceptionInterval[$key]); } @@ -30,19 +33,19 @@ function checkEvents() } if ($exception === false) { - echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL; + logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName)); publish(mktopic($event->device), array($event->param => $event->value), "set", $key); if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) { - logger(ERROR, _("Error in event recurrence. event: ") . $key); + logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key); } } } - }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, $event->value, $event->device->friendlyName)); + logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName)); $mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key); - $event->published = now(); + $event->published = $now; //echo "#################################\nUnsetting event $key \n###########################" . EOL; unset($events[$key]); } @@ -50,7 +53,7 @@ function checkEvents() //print_r($events); } -function setOneshotEvent(&$device, $datetime, $param, $value, $replace=false) +function setOneshotEvent(&$device, $datetime, $property, $value, $replace=false) { global $events; $events[] = new event; @@ -58,12 +61,12 @@ function setOneshotEvent(&$device, $datetime, $param, $value, $replace=false) $events[$key]->dateTimeEvent = new dateTime($datetime); $events[$key]->ieeeAddress = $device->ieeeAddress; $events[$key]->topic = $device->topic; - $events[$key]->param = $param; + $events[$key]->param = $property; $events[$key]->value = $value; $events[$key]->device = & $device; } -function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDatetime, $hours, $minutes, $seconds, $days, $weeks, $months, $years) +function setRecurrentEvent(&$device, $property, $value, $startDatetime, $stopDatetime, $hours, $minutes, $seconds, $days, $weeks, $months, $years) { global $events; $string = "P"; @@ -84,7 +87,7 @@ function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDateti $event->ieee_address = $device->ieeeAddress; $event->topic = $device->topic; $event->device = & $device; - $event->param = $param; + $event->param = $property; $event->value = $value; $event->dateTimeEvent = $event->startDatetime; if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) @@ -94,7 +97,7 @@ function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDateti } } -function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=false) +function setDelay(&$device, $delay, $unit="second", $property, $value, $replace=false) { global $events; $datetime = new dateTime(); @@ -134,30 +137,30 @@ function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=fal //print_r($datetime); if ($replace) { - $eventKey = searchEvent($device, $param, $value); + $eventKey = searchEvent($device, $property, $value); if ($eventKey !== false) deleteEvent($eventKey); } //$dt = $datetime->format("Y-m-d\TH:i:s\Z"); $events[] = new event; - $key = key($events); + $key = array_key_last($events); $events[$key]->dateTimeEvent = $datetime; $events[$key]->ieeeAddress = $device->ieeeAddress; $events[$key]->topic = $device->topic; - $events[$key]->param = $param; + $events[$key]->param = $property; $events[$key]->value = $value; $events[$key]->device = & $device; - echo "new event"; + logger (DEBUG, _('Setting new delay in $events[]')); } -function searchEvent($device, $param , $value) +function searchEvent($device, $property , $value) { global $events; - echo "searching event" . EOL; + logger(DEBUG, _("searching event for device %s, property %s and value %s"), $device->friendlyName, $property, bool2string($value)); foreach($events as $key => $event) { //echo "Event : $event => $value" . EOL; //echo "===>";print_r($event); echo EOL; - if($event->topic == $device->topic and $event->param == $param and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress) + if($event->topic == $device->topic and $event->param == $property and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress) { //echo "==============================\nfound " . $key . "\n=================================" . EOL; return $key; @@ -169,7 +172,7 @@ function searchEvent($device, $param , $value) function deleteEvent($eventKey) { global $events; - if ($eventKey !==false) + if ($eventKey !== false) { unset ($events[$eventKey]); logger(INFO, _("delete event key =") . $eventKey); diff --git a/hooks/scripts/availability.php b/hooks/scripts/availability.php index 41cd6f2..02ea739 100644 --- a/hooks/scripts/availability.php +++ b/hooks/scripts/availability.php @@ -33,14 +33,14 @@ class availability if ($device->availability != $value) { //echo "==========>>>>>> Availability $value" . EOL; - if (!empty($device->availability)) - { - $log = WARNING; - }else - { - $log = INFO; - } - $device->availability = $value; + //if (!empty($device->availability)) + //{ + $log = ALERT; + //}else + //{ + // $log = INFO; + //} + //$device->availability = $value; logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value)); } break; diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 0c0f919..409bb47 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -11,7 +11,7 @@ class rdc_salon_eclairage extends hook RDC_SALON_LUMINOSITE => array("illuminance_lux", false) ); - public $delay = 3; // amount of time in $timeunit + public $delay = 5; // amount of time in $timeunit public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year public $luminance_min = 60; public $luminance_max = 3000; @@ -26,15 +26,15 @@ class rdc_salon_eclairage extends hook { case "occupancy": //print_r($indexDevices[RDC_SALON_LUMINOSITE]); - if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) + if ($value == ON and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) { - $this->send("ON"); + $this->send("ON", "OFF", AUTO); } break; case "contact": if ($value == false and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min) { - $this->send("ON"); + $this->send("ON", "OFF", AUTO); } break; case "illuminance_lux": @@ -47,7 +47,7 @@ class rdc_salon_eclairage extends hook 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, $delayState = false, $method = MANUAL) { global $devices, $indexDevices; $device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU]; @@ -57,12 +57,14 @@ class rdc_salon_eclairage extends hook logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName)); $device->payload = $msg; $device->set(); - setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); + $device->method = $method; }else { logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName)); - } + } + //echo 'delaystate = ' . var_dump($delayState); + if ($delayState !== false) setDelay($device, $this->delay, $this->timeUnit, "state", $delayState, true); } } diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 3d62980..177953d 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -9,7 +9,7 @@ class rdc_sdb_eclairage extends hook //public $initlialized = false; public $delay = 3; // amount of time in $timeunit - public $delayManual = 10; // amount of time in $timeunit for manual mode + public $delayManual = 15; // 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 @@ -26,6 +26,7 @@ class rdc_sdb_eclairage extends hook public function callBack(&$device, $property, $value) { global $devices, $indexDevices; + var_dump($value); switch($property) { case "occupancy": @@ -47,14 +48,14 @@ class rdc_sdb_eclairage extends hook } break; } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value))); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, ($value == 0 ? "OFF" : "ON"))); } private function send($state) { global $devices, $indexDevices; $msg = array("state_l1" => $state); - $device = & $indexDevices["0x00158d0005c1a998"]; + $device = & $indexDevices[RDC_SDB_WC_ECLAIRAGE]; logger(INFO, sprintf(_("publishing message: %s to %s"), $msg, $device->friendlyName)); $device->payload = $msg; $device->set(null); diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index 0def5cb..2ca5b62 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -7,27 +7,28 @@ class rdc_wc_eclairage extends hook protected $devicelist = array(RDC_SDB_WC_ECLAIRAGE => array("state_l2", false)); public $delay = 3; // amount of time in $timeunit - public $delayManual = 10; // amount of time in $timeunit for manual mode + public $delayManual = 8; // amount of time in $timeunit for manual mode public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year // callback fonction. Is called with these 4 parameters public function callBack(&$device, $property, $value) { global $devices, $indexDevices; + var_dump($value); switch($property) { case "state_l2": - if ($value == ON) + if ($value == "ON") { setDelay($device, $this->delayManual, $this->timeUnit, "state_l2", "OFF", true); $device->method = MANUAL; - }elseif ($value = OFF) + }elseif ($value = "OFF") { deleteEvent(searchEvent($device, "state_l2", "OFF")); } break; } - logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value))); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value)); } } diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index 23f4575..cf5dd24 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -8,16 +8,17 @@ $topics["pws2mqtt"]->callback = function($topic, $message) logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1])); $device = & $devices[$topic[0]]; $payloadArray = json_decode($message->payload); - if (!isset($device[$fn])) //must not exists, but ... + if (!isset($device[$fn])) { logger(LOG_WARNING, $logFh, "init of " . $fn .EOL); $device[$fn] = array(); $device[$fn]["device"] = new device; $device[$fn]["device"]->type = $payloadArray->type; $device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress; + $device[$fn]["device"]->friendlyname = $fn; $indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"]; } $device = & $device[$fn]; - changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); + changeDevice($topic[0], $fn, $device["device"], $payloadArray); } ?> diff --git a/utils.php b/utils.php index 5a5bb3a..0af4226 100644 --- a/utils.php +++ b/utils.php @@ -17,7 +17,8 @@ function bool2string($var) function now() { - return date("c"); + $now = new datetime(); + return $now; } function farenheit2celsius($value)