1
0
moha/hooks/scripts/rdc_salon_eclairage.php

101 lines
4.0 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 => 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
public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year
public $luminance_min = 60;
public $luminance_max = 3000;
// callback fonction. Is called with these 4 parameters
public function callBack(&$device, $param, $value)
{
global $devices, $indexDevices;
logger(INFO, _("hook : rdc_salon_eclairage"), __FILE__ . ":" . __LINE__);
switch($param)
{
case "occupancy":
logger(DEBUG, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__);
//print_r(getValue(RDC_SALON_LUMINOSITE, "illuminance_lux"));
if ($value == ON and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min)
{
logger(DEBUG, _("setting to ON"), __FILE__ . ":" . __LINE__);
$this->send("ON", null, AUTO);
removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
}elseif ($value == OFF)
{
logger(DEBUG, _("Value is OFF"), __FILE__ . ":" . __LINE__);
if (getValue(RDC_SALON_MVMT, "occupancy") == OFF and (getValue(RDC_SALON_MVMT2, "occupancy") == OFF))
{
logger(DEBUG, _("Setting to OFF"), __FILE__ . ":" . __LINE__);
setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
//$this->send("ON", "OFF", AUTO);
}
}
break;
case "contact":
logger(DEBUG, _("CASE: Contact Door"), __FILE__ . ":" . __LINE__);
if ($value == false and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min and getValue(RDC_SALON_ECLAIRAGE_PANNEAU, "state") == "OFF")
{
logger(DEBUG, _("Door is open and illumance < min"), __FILE__ . ":" . __LINE__);
if (getValue(RDC_SALON_MVMT, "occupancy") == ON or getValue(RDC_SALON_MVMT2, "occupancy") == ON)
{
$this->send("ON", null, AUTO);
}else
{
$this->send("ON", "OFF", AUTO);
}
}
break;
case "illuminance_lux":
logger(DEBUG, _("CASE : Illuminance"), __FILE__ . ":" . __LINE__);
if ($value >= $this->luminance_max)
{
logger(DEBUG, _("illuminace is > to max"), __FILE__ . ":" . __LINE__);
//$this->send("OFF", null, AUTO);
//removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
}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__);
$this->send("ON", null, AUTO);
}
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($state, $delayState = false, $method = MANUAL)
{
global $devices, $indexDevices;
$device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
$msg = array("state" => $state);
if ($device->state["value"] != $state)
{
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__);
$device->payload = $msg;
$device->set();
$device->method = $method;
}else
{
logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__);
}
//echo 'delaystate = ' . var_dump($delayState);
if ($delayState !== false) setDelay($device, $this->delay, $this->timeUnit, "state", $delayState, true);
}
}
$hooks["rdc_salon_eclairage"] = new rdc_salon_eclairage();
?>