2021-12-31 23:09:58 +01:00
< ? php
2022-02-12 15:23:58 +01:00
2022-01-17 00:18:50 +01:00
class rdc_salon_eclairage extends hook
2021-12-31 23:09:58 +01:00
{
2022-01-03 21:11:52 +01:00
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 (
2022-03-13 22:33:26 +01:00
RDC_SALON_MVMT => " occupancy " ,
RDC_SALON_MVMT2 => " occupancy " ,
RDC_ENTREE_PORTE => " contact " ,
2022-08-27 00:02:24 +02:00
RDC_SALON_LUMINOSITE => " illuminance_lux " ,
2022-09-01 17:02:28 +02:00
RDC_SALON_ECLAIRAGE_PANNEAU => " state " ,
RDC_SALON_PRESENCE => " presence "
2022-01-05 00:01:41 +01:00
);
2022-06-20 10:34:55 +02:00
protected $actionneurs = array (
array ( RDC_SALON_MVMT , " occupancy " , 1 ),
2022-09-01 17:02:28 +02:00
array ( RDC_SALON_MVMT2 , " occupancy " , 1 ),
2022-09-02 17:59:33 +02:00
array ( RDC_SALON_PRESENCE , " presence " , 1 )
2022-06-20 10:34:55 +02:00
);
2022-01-27 18:41:16 +01:00
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-03-15 22:49:13 +01:00
public $luminance_min = 50 ;
2022-02-12 15:23:58 +01:00
public $luminance_max = 100 ;
2021-12-31 23:09:58 +01:00
2022-08-12 10:41:55 +02:00
function installHooks ( & $indexDevices )
{
return $this -> installHooksFunction ( $indexDevices );
}
2022-01-01 06:26:02 +01:00
// callback fonction. Is called with these 4 parameters
2022-03-13 22:33:26 +01:00
public function callBack ( $device , $param , $value )
2021-12-31 23:09:58 +01:00
{
2022-02-12 15:23:58 +01:00
global $indexDevices ;
2022-03-28 00:40:34 +02:00
logger ( DEBUG , " Callback : " . $this -> hookName , __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 ];
2022-08-27 00:02:24 +02:00
$method ;
2022-04-23 02:00:52 +02:00
$illuminance = getValue ( RDC_SALON_LUMINOSITE , " illuminance_lux " );
2022-02-25 20:42:33 +01:00
2022-04-23 02:00:52 +02:00
logger ( INFO , _ ( " property => " ) . $param . _ ( " value => " ) . bool2string ( $value ), __FILE__ . " : " . __LINE__ );
2021-12-31 23:09:58 +01:00
switch ( $param )
{
2022-09-01 17:02:28 +02:00
case " presence " :
2021-12-31 23:09:58 +01:00
case " occupancy " :
2022-08-27 00:02:24 +02:00
$method = $deviceTarget -> properties [ " state " ][ " method " ];
2022-09-09 16:53:19 +02:00
logger ( INFO , sprintf ( _ ( " CASE: %s => " ), $param ) . bool2string ( $value ), __FILE__ . " : " . __LINE__ );
2022-01-29 19:58:01 +01:00
//print_r(getValue(RDC_SALON_LUMINOSITE, "illuminance_lux"));
2022-12-05 12:23:43 +01:00
if ( $value == ON )
2022-01-02 18:14:13 +01:00
{
2022-12-05 12:23:43 +01:00
if ( $method == IDLE )
2022-03-13 22:33:26 +01:00
{
2022-12-05 12:23:43 +01:00
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 " );
}
2022-03-13 22:33:26 +01:00
}
2022-12-05 12:23:43 +01:00
} elseif ( $method == AUTO )
2022-01-27 18:41:16 +01:00
{
2022-09-09 16:53:19 +02:00
logger ( INFO , _ ( " Value is OFF and method is " ) . $method , __FILE__ . " : " . __LINE__ );
2022-12-05 12:23:43 +01:00
if ( testActionneurs ( $this -> actionneurs ) == 0 )
2022-01-27 18:41:16 +01:00
{
2022-02-25 20:42:33 +01:00
logger ( INFO , _ ( " Setting to OFF " ), __FILE__ . " : " . __LINE__ );
2022-08-27 00:02:24 +02:00
$this -> send ( $deviceTarget , " OFF " , false , IDLE );
removeEvent ( $deviceTarget , " state " , " OFF " );
2022-01-27 18:41:16 +01:00
}
2022-01-02 18:14:13 +01:00
}
2021-12-31 23:09:58 +01:00
break ;
2022-09-01 17:02:28 +02:00
2021-12-31 23:09:58 +01:00
case " contact " :
2022-08-27 00:02:24 +02:00
$method = $deviceTarget -> properties [ " state " ][ " method " ];
2022-02-25 20:42:33 +01:00
logger ( INFO , _ ( " CASE: Contact Door " ), __FILE__ . " : " . __LINE__ );
2022-08-27 00:02:24 +02:00
if ( $value == false and $illuminance <= $this -> luminance_min and $method == IDLE )
2022-01-02 18:14:13 +01:00
{
2022-06-20 10:34:55 +02:00
logger ( INFO , _ ( " Door is open and illumance < min and method => " ) . $deviceTarget -> properties [ " state " ][ " method " ], __FILE__ . " : " . __LINE__ );
2022-08-27 00:02:24 +02:00
if ( $method == IDLE )
2022-01-29 19:58:01 +01:00
{
2022-08-27 00:02:24 +02:00
$this -> send ( $deviceTarget , " ON " , false , AUTO );
removeEvent ( $deviceTarget , " state " , " OFF " );
2022-01-29 19:58:01 +01:00
}
2022-01-02 18:14:13 +01:00
}
2021-12-31 23:09:58 +01:00
break ;
2022-09-01 17:02:28 +02:00
2022-01-02 12:14:30 +01:00
case " illuminance_lux " :
2022-02-25 20:42:33 +01:00
logger ( INFO , _ ( " CASE : Illuminance " ), __FILE__ . " : " . __LINE__ );
2022-04-07 01:44:17 +02:00
if ( $value >= $this -> luminance_max and $deviceTarget -> properties [ " state " ][ " value " ] == " ON " )
2022-01-17 21:01:11 +01:00
{
2022-02-25 20:42:33 +01:00
logger ( INFO , _ ( " illuminace is > to max " ), __FILE__ . " : " . __LINE__ );
2022-08-27 00:02:24 +02:00
$this -> send ( $deviceTarget , " OFF " , false , IDLE );
removeEvent ( $deviceTarget , " state " , " OFF " );
}
break ;
2022-09-01 17:02:28 +02:00
2022-08-27 00:02:24 +02:00
case " state " :
logger ( INFO , _ ( " CASE : State " ), __FILE__ . " : " . __LINE__ );
$method = $deviceTarget -> properties [ " state " ][ " method " ];
2022-12-05 12:23:43 +01:00
if ( $value == " ON " )
2022-08-27 00:02:24 +02:00
{
2022-12-05 12:23:43 +01:00
if ( $method == IDLE )
{
$deviceTarget -> properties [ " state " ][ " method " ] = MANUAL ;
removeEvent ( $deviceTarget , " state " , " OFF " );
}
2022-08-27 00:02:24 +02:00
} else
2022-01-28 23:05:58 +01:00
{
2022-08-27 00:02:24 +02:00
$deviceTarget -> properties [ " state " ][ " method " ] = IDLE ;
}
2022-01-03 21:11:52 +01:00
break ;
2021-12-31 23:09:58 +01:00
}
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__ );
2021-12-31 23:09:58 +01:00
}
2022-06-20 10:34:55 +02:00
private function send ( & $deviceTarget , $state , $delayState = false , $method = AUTO )
2021-12-31 23:09:58 +01:00
{
2022-02-12 15:23:58 +01:00
global $indexDevices ;
2022-02-23 10:23:16 +01:00
2022-01-19 00:22:34 +01:00
$msg = array ( " state " => $state );
2022-08-27 00:02:24 +02:00
logger ( INFO , sprintf ( _ ( " publishing message: %s to %s " ), json_encode ( $msg ), $deviceTarget -> friendlyName ), __FILE__ . " : " . __LINE__ );
$deviceTarget -> payload = $msg ;
$deviceTarget -> set ( " state " , $method );
2022-02-23 10:23:16 +01:00
if ( $delayState !== false ) setDelay ( $deviceTarget , $this -> delay , $this -> timeUnit , " state " , $delayState , true );
2021-12-31 23:09:58 +01:00
}
}
2022-01-03 21:11:52 +01:00
$hooks [ " rdc_salon_eclairage " ] = new rdc_salon_eclairage ();
2021-12-31 23:09:58 +01:00
?>