1
0
moha/hooks/scripts/rdc_salon_eclairage.php
2022-08-27 00:02:24 +02:00

120 lines
4.4 KiB
PHP

<?php
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 => "occupancy",
RDC_SALON_MVMT2 => "occupancy",
RDC_ENTREE_PORTE => "contact",
RDC_SALON_LUMINOSITE => "illuminance_lux",
RDC_SALON_ECLAIRAGE_PANNEAU => "state"
);
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;
public $luminance_max = 100;
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters
public function callBack($device, $param, $value)
{
global $indexDevices;
logger(DEBUG, "Callback : " . $this->hookName, __FILE__ . ":" . __LINE__);
$deviceTarget = &$indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
$lux = $indexDevices[RDC_SALON_LUMINOSITE];
$mvmt = $indexDevices[RDC_SALON_MVMT];
$mvmt2 = $indexDevices[RDC_SALON_MVMT2];
$method;
$illuminance = getValue(RDC_SALON_LUMINOSITE, "illuminance_lux");
logger (INFO, _("property => ") . $param . _("value =>") . bool2string($value), __FILE__ . ":" . __LINE__);
switch($param)
{
case "occupancy":
$method = $deviceTarget->properties["state"]["method"];
logger(INFO, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__);
//print_r(getValue(RDC_SALON_LUMINOSITE, "illuminance_lux"));
if ($value == ON and $method == IDLE)
{
logger(INFO, _("illuminance value : ") . $illuminance, __FILE__ . ":" . __LINE__);
if ($illuminance <= $this->luminance_min)
{
logger(INFO, _("setting to ON"), __FILE__ . ":" . __LINE__);
$this->send($deviceTarget, "ON", false, AUTO);
removeEvent($deviceTarget, "state", "OFF");
}
}else
{
logger(INFO, _("Value is OFF"), __FILE__ . ":" . __LINE__);
if ((getValue(RDC_SALON_MVMT, "occupancy") == OFF) and (getValue(RDC_SALON_MVMT2, "occupancy") == OFF) and $method == AUTO)
{
logger(INFO, _("Setting to OFF"), __FILE__ . ":" . __LINE__);
$this->send($deviceTarget, "OFF", false, IDLE);
removeEvent($deviceTarget, "state", "OFF");
}
}
break;
case "contact":
$method = $deviceTarget->properties["state"]["method"];
logger(INFO, _("CASE: Contact Door"), __FILE__ . ":" . __LINE__);
if ($value == false and $illuminance <= $this->luminance_min and $method == IDLE)
{
logger(INFO, _("Door is open and illumance < min and method =>") . $deviceTarget->properties["state"]["method"], __FILE__ . ":" . __LINE__);
if ($method == IDLE)
{
$this->send($deviceTarget, "ON", false, AUTO);
removeEvent($deviceTarget, "state", "OFF");
}
}
break;
case "illuminance_lux":
logger(INFO, _("CASE : Illuminance"), __FILE__ . ":" . __LINE__);
if ($value >= $this->luminance_max and $deviceTarget->properties["state"]["value"] == "ON")
{
logger(INFO, _("illuminace is > to max"), __FILE__ . ":" . __LINE__);
$this->send($deviceTarget, "OFF", false, IDLE);
removeEvent($deviceTarget, "state", "OFF");
}
break;
case "state":
logger(INFO, _("CASE : State"), __FILE__ . ":" . __LINE__);
$method = $deviceTarget->properties["state"]["method"];
if($value == "ON" and $method == IDLE)
{
$deviceTarget->properties["state"]["method"] = MANUAL;
removeEvent($deviceTarget, "state", "OFF");
}else
{
$deviceTarget->properties["state"]["method"] = IDLE;
}
break;
}
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 = AUTO)
{
global $indexDevices;
$msg = array("state" => $state);
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__);
$deviceTarget->payload = $msg;
$deviceTarget->set("state", $method);
if ($delayState !== false) setDelay($deviceTarget, $this->delay, $this->timeUnit, "state", $delayState, true);
}
}
$hooks["rdc_salon_eclairage"] = new rdc_salon_eclairage();
?>