diff --git a/apiserver/cmd_functions.php b/apiserver/cmd_functions.php index 5bccbfa..b398bc0 100644 --- a/apiserver/cmd_functions.php +++ b/apiserver/cmd_functions.php @@ -330,9 +330,11 @@ function apiSet($argList) }else { $response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"]; - $payload = array($argList["property"] => $argList["value"]); + $device = $indexFriendlyNames[$argList["fn"]]; + $device->payload = array($argList["property"] => $argList["value"]); + $device->set($argList["property"], IDLE); publish($argList["topic"] . "/" . $argList["fn"], $payload, "set"); - //removeEvent($indexFriendlyName($argList["fn"]), $argList["property"], "OFF"); + //removeEvent($indexFriendlyNames($argList["fn"]), $argList["property"], "OFF"); } return $response; } diff --git a/class/main.php b/class/main.php index 772a336..4b2ed4e 100644 --- a/class/main.php +++ b/class/main.php @@ -39,9 +39,10 @@ class device $this->availability = array("value" => null, "functions" => array()); } - public function set() //, $event = null) + public function set($property, $method = IDLE) //, $event = null) { - publish($this->topic . "/" . $this->friendlyName, $this->payload, "set"); //, $event); + publish($this->topic . "/" . $this->friendlyName, $this->payload, "set");//, $event); + $this->properties[$property]["method"] = $method; } public function get() diff --git a/db_functions.php b/db_functions.php index e54da0e..9763fad 100644 --- a/db_functions.php +++ b/db_functions.php @@ -286,6 +286,8 @@ function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key) logger(DEBUG, sprintf(_("old value was %s, new is %s" ), bool2string($property["value"]), bool2string($value)), __FILE__ . ":" . __LINE__); } $property["value"] = $value; + $r = $parentDevice->friendlyName; + logger(DEBUG, _("Changed value of ") . $r . "=> " . bool2string($value) , __FILE__ . ":" . __LINE__); if (!empty($property["functions"])) { logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__); diff --git a/hooks/scripts/radiateurs.php b/hooks/scripts/radiateurs.php index b1e1f78..865f479 100644 --- a/hooks/scripts/radiateurs.php +++ b/hooks/scripts/radiateurs.php @@ -121,7 +121,7 @@ class radiateurs extends hook $msg = array("current_heating_setpoint" => (!empty($this->prevTemp[$device->ieeeAddress])?$this->prevTemp[$device->ieeeAddress]:19)); logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__); $device->payload = $msg; - $device->set(); + $device->set("current_heating_setpoint", AUTO); $device->properties["current_heating_setpoint"]["method"] = AUTO; } } diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index 7f626cb..66f88f7 100644 --- a/hooks/scripts/rdc_chambre_eclairage.php +++ b/hooks/scripts/rdc_chambre_eclairage.php @@ -27,20 +27,23 @@ class rdc_chambre_eclairage extends hook $lux = $indexDevices[RDC_CHAMBRE_LUMINOSITE]->properties["illuminance_lux"]; $targetAmbiance = $indexDevices[RDC_CHAMBRE_AMBIANCE]; $targetEclairage = $indexDevices[RDC_CHAMBRE_ECLAIRAGE]; - if ($property == "occupancy" and $value == ON) + if ($property == "occupancy") { - logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); - if ($targetAmbiance->properties["state"]["method"] == MANUAL) + if ($value == ON and $lux < 150) { - $method = false; - $delayState = false; - }else - { - $method = AUTO; - $delayState = "OFF"; - } + logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); + if ($targetAmbiance->properties["state"]["method"] == MANUAL) + { + $method = false; + $delayState = false; + }else + { + $method = AUTO; + $delayState = "OFF"; + } - $this->send($targetAmbiance, "state", "ON", $delayState, $method); + $this->send($targetAmbiance, "state", "ON", $delayState, $method); + } }elseif ($property == "contact") { logger(DEBUG, _("CASE: contact"), __FILE__ . ":" . __LINE__); @@ -51,14 +54,22 @@ class rdc_chambre_eclairage extends hook { $this->send($targetEclairage, "state_l1", "OFF", false, IDLE); } - }elseif ($property == "state" and $value == "OFF") + }elseif ($property == "state") { - logger(DEBUG, _("CASE: state => value = 'OFF'"), __FILE__ . ":" . __LINE__); - $targetAmbiance->properties[$property]["method"] = IDLE; - }elseif ($property == "state_l1" and $value == "OFF") + if ($value == "OFF") + { + logger(DEBUG, _("CASE: state => value = 'OFF'"), __FILE__ . ":" . __LINE__); + $targetAmbiance->properties[$property]["method"] = IDLE; + removeEvent($targetAmbiance, $property, "OFF", IDLE); + } + }elseif ($property == "state_l1") { - logger(DEBUG, _("CASE: state_l1 => value = 'OFF'"), __FILE__ . ":" . __LINE__); - $targetEclairage->properties[$property]["method"] = IDLE; + if ($value == "OFF") + { + logger(DEBUG, _("CASE: state_l1 => value = 'OFF'"), __FILE__ . ":" . __LINE__); + //$targetEclairage->properties[$property]["method"] = IDLE; + removeEvent($targetAmbiance, $property, "OFF", IDLE); + } }elseif ($property == "action") { logger(DEBUG, _("CASE: action"), __FILE__ . ":" . __LINE__); @@ -70,7 +81,7 @@ class rdc_chambre_eclairage extends hook logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); } - private function send(&$deviceObject, $property, $state, $delayState = false, $method = MANUAL) + private function send(&$deviceObject, $property, $state, $delayState = false, $method = AUTO) { global $indexDevices; $msg = array($property => $state); @@ -78,15 +89,14 @@ class rdc_chambre_eclairage extends hook //{ logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); $deviceObject->payload = $msg; - $deviceObject->set(); - $deviceObject->properties[$property]["method"] = $method; + $deviceObject->set($property, $method); ///}else /*{ logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); }*/ //echo 'delaystate = ' . var_dump($delayState); - if ($delayState !== false) setDelay($deviceObject, $this->delay, $this->timeUnit, "state", $delayState, true); + if ($delayState !== false) setDelay($deviceObject, $this->delay, $this->timeUnit, "state", $delayState, true, IDLE); } } diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 4a26b56..b77c620 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -11,7 +11,10 @@ class rdc_salon_eclairage extends hook RDC_ENTREE_PORTE => "contact", RDC_SALON_LUMINOSITE => "illuminance_lux" ); - + protected $actionneurs = array( + array(RDC_SALON_MVMT, "occupancy", 1), + array(RDC_SALON_MVMT2, "occupancy", 1) + ); public $delay = 3; // amount of time in $timeunit public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year public $luminance_min = 50; @@ -58,11 +61,8 @@ class rdc_salon_eclairage extends hook logger(INFO, _("CASE: Contact Door"), __FILE__ . ":" . __LINE__); if ($value == false and $illuminance <= $this->luminance_min) { - logger(INFO, _("Door is open and illumance < min"), __FILE__ . ":" . __LINE__); - if ($deviceTarget->properties["state"]["method"] !== MANUAL) - { - $this->send($deviceTarget, "ON", false); - }else + logger(INFO, _("Door is open and illumance < min and method =>") . $deviceTarget->properties["state"]["method"], __FILE__ . ":" . __LINE__); + if ($deviceTarget->properties["state"]["method"] == IDLE) { $this->send($deviceTarget, "ON", "OFF", AUTO); } @@ -89,7 +89,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)), __FILE__ . ":" . __LINE__); } - private function send(&$deviceTarget, $state, $delayState = false, $method = MANUAL) + private function send(&$deviceTarget, $state, $delayState = false, $method = AUTO) { global $indexDevices; @@ -98,8 +98,8 @@ class rdc_salon_eclairage extends hook //{ logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__); $deviceTarget->payload = $msg; - $deviceTarget->set(); - $deviceTarget->properties["state"]["method"] = $method; + $deviceTarget->set("state", "method"); + //$deviceTarget->properties["state"]["method"] = $method; /*}else { logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__); diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index c5fc105..84f271b 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -17,12 +17,16 @@ class rdc_sdb_eclairage extends hook // devicelist[$ieeAddress][0] => property to watch // devicelist[$ieeAddress][1] => initialized = true protected $devicelist = array( - //RDC_SDB_DOUCHE_MVMT => "occupancy", + RDC_SDB_DOUCHE_MVMT => "occupancy", RDC_SDB_PLAFOND_MVMT => "occupancy", RDC_SDB_MVMT => "occupancy", RDC_SDB_WC_ECLAIRAGE => "state_l1" ); - + protected $actionneurs = array( + array(RDC_SDB_DOUCHE_MVMT, "occupancy", 1), + array(RDC_SDB_PLAFOND_MVMT, "occupancy", 1), + array(RDC_SDB_MVMT, "occupancy", 1) + ); // callback fonction. Is called with these 3 parameters public function callBack(&$device, $property, $value) { @@ -36,42 +40,40 @@ class rdc_sdb_eclairage extends hook { case "occupancy": logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); - if ($value == ON ) + if ($value == ON) { - $this->send("ON"); - $deviceTarget->properties["state_l1"]["method"] = AUTO; - }else - { - //$delay = $this->delay; - //setDelay($deviceTarget, $delay, $this->timeUnit, "state_l1", "OFF", true, IDLE); - if ($deviceTarget->properties["state_l1"]["method"] != AUTO) + if($deviceTarget->properties["state_l1"]["method"] == IDLE) { - $delay = $this->delayManual; - $deviceTarget->properties["state_l1"]["method"] = MANUAL; - }else - { - $delay = $this->delay; - $deviceTarget->properties["state_l1"]["method"] = MANUAL; - } - setDelay($deviceTarget, $delay, $this->timeUnit, "state_l1", "OFF", true, IDLE); - } + logger(DEBUG, _("lighting with method auto"), __FILE__ . ":" . __LINE__); + //$deviceTarget->properties["state_l1"]["method"] = AUTO; + $this->send("ON", AUTO); + } + }elseif (testActionneurs($this->actionneurs)) + { + logger(DEBUG, _("Actionneurs are all false"), __FILE__ . ":" . __LINE__); + setDelay($deviceTarget, $this->delay, $this->timeUnit, "state_l1", "OFF", true, IDLE); + } break; case "state_l1": logger(DEBUG, _("CASE: state_l1"), __FILE__ . ":" . __LINE__); - /*if ($value == "ON") + if ($value == "ON") { - - - }else*/ - if ($value == "OFF") + if ($deviceTarget->properties["state_l1"]["method"] == IDLE) + { + logger(DEBUG, _("State_l1 is IDLE"), __FILE__ . ":" . __LINE__); + $deviceTarget->properties["state_l1"]["method"] = MANUAL; + setDelay($deviceTarget, $this->delayManual, $this->timeUnit, "state_l1", "OFF", true, IDLE); + } + }elseif ($value == "OFF") { - removeEvent($deviceTarget, "state_l1", "OFF"); + removeEvent($deviceTarget, "state_l1", "OFF", IDLE); $deviceTarget->properties["state_l1"]["method"] = IDLE; + logger(DEBUG, _("State_l1 is false light is off and method is ") . $deviceTarget->properties["state_l1"]["method"], __FILE__ . ":" . __LINE__); } break; } - 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__); + logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); }else { logger (ERROR, RDC_SDB_WC_ECLAIRAGE . _(" does not exists"), __FILE__ . ":" . __LINE__); @@ -79,14 +81,14 @@ class rdc_sdb_eclairage extends hook } - private function send($state) + private function send($state, $method) { global $indexDevices; $msg = array("state_l1" => $state); $device = &$indexDevices[RDC_SDB_WC_ECLAIRAGE]; logger(INFO, sprintf(_("publishing message: %s to %s"), $state, $device->friendlyName), __FILE__ . ":" . __LINE__); $device->payload = $msg; - $device->set(null); + $device->set("state_l1", $method); } } $hooks["rdc_sdb_eclairage"] = new rdc_sdb_eclairage(); diff --git a/hooks/scripts/rdc_store.php b/hooks/scripts/rdc_store.php index 4cc6d7a..3e741dc 100644 --- a/hooks/scripts/rdc_store.php +++ b/hooks/scripts/rdc_store.php @@ -164,7 +164,7 @@ class rdc_store extends hook }elseif ($rafale != 0) { //calcul du maxlevel par rapport a la vitesse des rafales - $r = round(($rafale-30)/4, 0, PHP_ROUND_HALF_UP)*10; + $r = round(($rafale-20)/4, 0, PHP_ROUND_HALF_UP)*10; if ($r < 0) $r = 0; if ($motorReversal) { @@ -191,17 +191,17 @@ class rdc_store extends hook logger(DEBUG, "maxLevel = " . $this->maxLevel . ' result => $r = ' . $r, __FILE__ . ":" . __LINE__); if ($this->storeLevel > $this->maxLevel) { - $this->set($this->maxLevel); + $this->setTo($this->maxLevel, AUTO); }elseif ($store2level !== false) //$store2level = false then no change on store position { if ( $store2level > $this->maxLevel ) { logger(DEBUG, "store2level > maxLevel", __FILE__ . ":" . __LINE__); - $this->set($this->maxLevel); + $this->setTo($this->maxLevel, AUTO); }else { logger(DEBUG, "store2level <= maxLevel", __FILE__ . ":" . __LINE__); - $this->set($store2level); + $this->setTo($store2level, AUTO); } } }else @@ -211,13 +211,13 @@ class rdc_store extends hook } - private function set ($level) + private function setTo($level, $method) { logger(DEBUG, "function 'set' to level : " . $level, __FILE__ . ":" . __LINE__); if ($level <= 100) { logger(DEBUG, "Open store :" . $level, __FILE__ . ":" . __LINE__); - $this->send(100 - $level); + $this->send(100 - $level, $method); }//else /*{ logger(DEBUG, "store is already at level" . $this->storeLevel . " so < at " . $level, __FILE__ . ":" . __LINE__); @@ -230,7 +230,7 @@ class rdc_store extends hook if ($this->storeLevel > 0) { logger(DEBUG, "Close store :" . $reason, __FILE__ . ":" . __LINE__); - $this->send(100); + $this->send(100, IDLE); } } @@ -241,7 +241,7 @@ class rdc_store extends hook $msg = array("position" => $level); logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); $deviceObject->payload = $msg; - $deviceObject->set(AUTO); + $deviceObject->set("position", $method); } } diff --git a/hooks/scripts/rdc_store.php~ b/hooks/scripts/rdc_store.php~ index b2f37bc..f4e8816 100644 --- a/hooks/scripts/rdc_store.php~ +++ b/hooks/scripts/rdc_store.php~ @@ -164,7 +164,7 @@ class rdc_store extends hook }elseif ($rafale != 0) { //calcul du maxlevel par rapport a la vitesse des rafales - $r = round(($rafale-30)/4, 0, PHP_ROUND_HALF_UP)*10; + $r = round(($rafale-20)/4, 0, PHP_ROUND_HALF_UP)*10; if ($r < 0) $r = 0; if ($motorReversal) { @@ -191,17 +191,17 @@ class rdc_store extends hook logger(DEBUG, "maxLevel = " . $this->maxLevel . ' result => $r = ' . $r, __FILE__ . ":" . __LINE__); if ($this->storeLevel > $this->maxLevel) { - $this->set($this->maxLevel); - }elseif ($store2level !== false) + $this->setTo($this->maxLevel); + }elseif ($store2level !== false) //$store2level = false then no change on store position { if ( $store2level > $this->maxLevel ) { logger(DEBUG, "store2level > maxLevel", __FILE__ . ":" . __LINE__); - $this->set($this->maxLevel); + $this->setTo($this->maxLevel); }else { logger(DEBUG, "store2level <= maxLevel", __FILE__ . ":" . __LINE__); - $this->set($store2level); + $this->setTo($store2level); } } }else @@ -211,13 +211,13 @@ class rdc_store extends hook } - private function set ($level) + private function setTo($level) { logger(DEBUG, "function 'set' to level : " . $level, __FILE__ . ":" . __LINE__); if ($level <= 100) { logger(DEBUG, "Open store :" . $level, __FILE__ . ":" . __LINE__); - $this->send(100 - $level); + $this->send(100 - $level, AUTO); }//else /*{ logger(DEBUG, "store is already at level" . $this->storeLevel . " so < at " . $level, __FILE__ . ":" . __LINE__); @@ -230,7 +230,7 @@ class rdc_store extends hook if ($this->storeLevel > 0) { logger(DEBUG, "Close store :" . $reason, __FILE__ . ":" . __LINE__); - $this->send(100); + $this->send(100, IDLE); } } @@ -241,7 +241,7 @@ class rdc_store extends hook $msg = array("position" => $level); logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); $deviceObject->payload = $msg; - $deviceObject->set(AUTO); + $deviceObject->set("position", $method); } } diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index 3b95d72..094c152 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -27,6 +27,7 @@ class rdc_wc_eclairage extends hook logger(DEBUG, "CASE: state_l2", __FILE__ . ":" . __LINE__); if ($value == "ON") { + logger(DEBUG, "method =" . $targetDevice->properties["state_l2"]["method"], __FILE__ . ":" . __LINE__); if ($targetDevice->properties["state_l2"]["method"] == IDLE) { $targetDevice->properties["state_l2"]["method"] = MANUAL; @@ -42,8 +43,8 @@ class rdc_wc_eclairage extends hook logger(DEBUG, "CASE: occupancy", __FILE__ . ":" . __LINE__); if ($value === true and $targetDevice->properties["state_l2"]["method"] == IDLE) { - $targetDevice->properties["state_l2"]["method"] = AUTO; - $this->send($targetDevice, "ON"); + //$targetDevice->properties["state_l2"]["method"] = AUTO; + $this->send($targetDevice, "ON", AUTO); }elseif ($value === false and $targetDevice->properties["state_l2"]["method"] == AUTO) { //$targetDevice->properties["state_l2"]["method"] = IDLE; @@ -53,14 +54,14 @@ class rdc_wc_eclairage extends hook } logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); } - private function send($targetDevice, $state) + private function send($targetDevice, $state, $method = IDLE) { global $indexDevices; $msg = array("state_l2" => $state); logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $targetDevice->friendlyName), __FILE__ . ":" . __LINE__); $targetDevice->payload = $msg; - $targetDevice->set(null); + $targetDevice->set("state_l2", $method); } } diff --git a/hooks/scripts/rdc_wc_eclairage.php~ b/hooks/scripts/rdc_wc_eclairage.php~ index ac61147..678832f 100644 --- a/hooks/scripts/rdc_wc_eclairage.php~ +++ b/hooks/scripts/rdc_wc_eclairage.php~ @@ -27,6 +27,7 @@ class rdc_wc_eclairage extends hook logger(DEBUG, "CASE: state_l2", __FILE__ . ":" . __LINE__); if ($value == "ON") { + logger(DEBUG, "method =" . $targetDevice->properties["state_l2"]["method"], __FILE__ . ":" . __LINE__); if ($targetDevice->properties["state_l2"]["method"] == IDLE) { $targetDevice->properties["state_l2"]["method"] = MANUAL; @@ -44,7 +45,7 @@ class rdc_wc_eclairage extends hook { $targetDevice->properties["state_l2"]["method"] = AUTO; $this->send($targetDevice, "ON"); - }elseif ($value === false and $targetDevice->properties["state_l2"]["method"] != MANUAL) + }elseif ($value === false and $targetDevice->properties["state_l2"]["method"] == AUTO) { //$targetDevice->properties["state_l2"]["method"] = IDLE; //$this->send($targetDevice, "OFF"); diff --git a/moha.php b/moha.php index 4e4a476..fdfa988 100644 --- a/moha.php +++ b/moha.php @@ -5,7 +5,7 @@ file_put_contents("/proc/".getmypid()."/comm",$title); declare(ticks = 1); $testMode = false; -$webServerIsActive = true; +$apiServerIsActive = true; require "constants.php"; @@ -338,7 +338,7 @@ while (true) //logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__); } checkEvents(); - apiServer($read); + if ($apiServerIsActive) apiServer($read); } endMoha(); diff --git a/utils.php b/utils.php index 04a481a..4177a0b 100644 --- a/utils.php +++ b/utils.php @@ -16,6 +16,30 @@ print ("MOHA-" . $logString . EOL); } }*/ +function testActionneurs(array $actionneurs) +{ + global $indexDevices; + $r = 0; + foreach ($actionneurs as $device) + { + if (array_key_exists($device[0], $indexDevices)) + { + if (array_key_exists($device[1], $indexDevices[$device[0]]->properties)) + { + $s = ($indexDevices[$device[0]]->properties[$device[1]]["value"] == $device[2]); + logger(DEBUG, _("testActionneurs : result is ") . bool2string($indexDevices[$device[0]]->friendlyName) . "=>" . bool2string($s), __FILE__ . ":" . __LINE__ ); + $r |= $s; + }else + { + logger(ERROR, _("no property : ") . $device[1] . bool2string($r), __FILE__ . ":" . __LINE__ ); + } + }else + { + logger(ERROR, _("no device : ") . $device[0] . bool2string($r), __FILE__ . ":" . __LINE__ ); + } + } + return !$r; +} function htmlGetFriendlyNames($ieeeAddress = '') {