1
0
moha/hooks/scripts/rdc_salon_eclairage.php

107 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2022-01-17 00:18:50 +01:00
class rdc_salon_eclairage extends hook
{
public $hookName = "rdc_salon_eclairage";
2022-01-19 00:22:34 +01:00
public $active = true;
2022-01-17 21:01:11 +01:00
// list of devices we are listening to
2022-01-17 00:18:50 +01:00
protected $devicelist = array(
RDC_SALON_MVMT => array("occupancy", false),
RDC_SALON_MVMT2 => array("occupancy", false),
RDC_ENTREE_PORTE => array("contact", false),
RDC_SALON_LUMINOSITE => array("illuminance_lux", false)
);
public $delay = 3; // amount of time in $timeunit
2022-01-23 09:46:06 +01:00
public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year
2022-02-07 16:58:42 +01:00
public $luminance_min = 60;
public $luminance_max = 100;
2022-01-01 06:26:02 +01:00
// callback fonction. Is called with these 4 parameters
2022-01-02 18:14:13 +01:00
public function callBack(&$device, $param, $value)
{
global $indexDevices;
2022-01-28 23:05:58 +01:00
logger(INFO, _("hook : rdc_salon_eclairage"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
$deviceTarget = &$indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
$lux = $indexDevices[RDC_SALON_LUMINOSITE];
$mvmt = $indexDevices[RDC_SALON_MVMT];
$mvmt2 = $indexDevices[RDC_SALON_MVMT2];
switch($param)
{
case "occupancy":
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__);
2022-01-29 19:58:01 +01:00
//print_r(getValue(RDC_SALON_LUMINOSITE, "illuminance_lux"));
2022-01-28 23:05:58 +01:00
if ($value == ON and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min)
2022-01-02 18:14:13 +01:00
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("setting to ON"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
$this->send($deviceTarget, "ON", null, AUTO);
removeEvent($deviceTarget, "state", "OFF");
2022-01-28 23:05:58 +01:00
}elseif ($value == OFF)
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("Value is OFF"), __FILE__ . ":" . __LINE__);
2022-01-29 19:58:01 +01:00
if (getValue(RDC_SALON_MVMT, "occupancy") == OFF and (getValue(RDC_SALON_MVMT2, "occupancy") == OFF))
{
2022-01-29 19:58:01 +01:00
logger(DEBUG, _("Setting to OFF"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
setDelay($deviceTarget, $this->delay, $this->timeUnit, "state", "OFF", true);
//$this->send($deviceTarget, "ON", "OFF", AUTO);
}
2022-01-02 18:14:13 +01:00
}
break;
case "contact":
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("CASE: Contact Door"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
if ($value == false and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min and getValue($deviceTarget->ieeeAddress, "state") == "OFF")
2022-01-02 18:14:13 +01:00
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("Door is open and illumance < min"), __FILE__ . ":" . __LINE__);
2022-01-29 19:58:01 +01:00
if (getValue(RDC_SALON_MVMT, "occupancy") == ON or getValue(RDC_SALON_MVMT2, "occupancy") == ON)
{
2022-02-23 10:23:16 +01:00
$this->send($deviceTarget, "ON", null, AUTO);
2022-01-29 19:58:01 +01:00
}else
{
2022-02-23 10:23:16 +01:00
$this->send($deviceTarget, "ON", "OFF", AUTO);
2022-01-29 19:58:01 +01:00
}
2022-01-02 18:14:13 +01:00
}
break;
case "illuminance_lux":
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("CASE : Illuminance"), __FILE__ . ":" . __LINE__);
2022-01-17 21:01:11 +01:00
if ($value >= $this->luminance_max)
{
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("illuminace is > to max"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
//$this->send($deviceTarget, "OFF", null, AUTO);
2022-01-29 19:58:01 +01:00
//removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
2022-02-23 10:23:16 +01:00
if (searchEvent($deviceTarget, "state", "OFF") === false)
{
2022-02-23 10:23:16 +01:00
setDelay($deviceTarget, $this->delay, $this->timeUnit, "state", "OFF", true);
}
2022-01-28 23:05:58 +01:00
}elseif ($value <= $this->luminance_min and (getValue(RDC_SALON_MVMT, "occupancy") == ON OR getValue(RDC_SALON_MVMT2,"occupancy") == ON))
{
logger(DEBUG, _("illuminance < min and movement detected"), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
$this->send($deviceTarget, "ON", null, AUTO);
2022-01-17 21:01:11 +01:00
}
break;
}
2022-01-28 23:05:58 +01:00
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, bool2string($value)), __FILE__ . ":" . __LINE__);
}
2022-02-23 10:23:16 +01:00
private function send(&$deviceTarget, $state, $delayState = false, $method = MANUAL)
{
global $indexDevices;
2022-02-23 10:23:16 +01:00
2022-01-19 00:22:34 +01:00
$msg = array("state" => $state);
2022-02-23 10:23:16 +01:00
if ($deviceTarget->properties["state"]["value"] != $state)
2022-01-06 13:03:26 +01:00
{
2022-02-23 10:23:16 +01:00
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__);
$deviceTarget->payload = $msg;
$deviceTarget->set();
$deviceTarget->method = $method;
2022-01-06 13:03:26 +01:00
}else
{
2022-02-23 10:23:16 +01:00
logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__);
2022-01-20 00:26:57 +01:00
}
//echo 'delaystate = ' . var_dump($delayState);
2022-02-23 10:23:16 +01:00
if ($delayState !== false) setDelay($deviceTarget, $this->delay, $this->timeUnit, "state", $delayState, true);
}
}
$hooks["rdc_salon_eclairage"] = new rdc_salon_eclairage();
?>