2022-01-23 09:46:06 +01:00
< ? php
class rdc_chambre_eclairage extends hook
{
public $hookName = " rdc_chambre_eclairage " ;
2022-01-29 19:58:01 +01:00
public $active = true ; //enable/disable hook (true => enabled)
2022-01-27 18:41:16 +01:00
public $delay = 5 ; // 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 ;
protected $devicelist = array (
2022-03-13 22:33:26 +01:00
RDC_CHAMBRE_AMBIANCE => " state " , // "ON"/"OFF"
RDC_CHAMBRE_ECLAIRAGE => " state_l1 " , // "ON"/"OFF"
RDC_CHAMBRE_MVMT => " occupancy " ,
2022-04-07 01:44:17 +02:00
RDC_CHAMBRE_ARMOIRE_GAUCHE => " contact " ,
RDC_CHAMBRE_AMBIANCE_INTER => " action "
2022-01-27 18:41:16 +01:00
);
2022-01-23 09:46:06 +01:00
2022-08-12 10:41:55 +02:00
function installHooks ( & $indexDevices )
{
return $this -> installHooksFunction ( $indexDevices );
}
2022-01-23 09:46:06 +01:00
// callback fonction. Is called with these 3 parameters
// $device -> calling device
// $property -> property of the device (given by mqtt)
// $value -> value of the property
2022-02-23 10:23:16 +01:00
public function callBack ( $device , $property , $value )
2022-01-23 09:46:06 +01:00
{
2022-01-27 18:41:16 +01:00
global $devices , $indexDevices ;
2022-03-28 00:40:34 +02:00
logger ( DEBUG , sprintf ( _ ( " property=%s, value=%s " ), $property , $value ), __FILE__ . " : " . __LINE__ );
2022-06-21 08:57:10 +02:00
$lux = $indexDevices [ RDC_CHAMBRE_LUMINOSITE ] -> properties [ " illuminance_lux " ][ " value " ];
2022-02-23 10:23:16 +01:00
$targetAmbiance = $indexDevices [ RDC_CHAMBRE_AMBIANCE ];
$targetEclairage = $indexDevices [ RDC_CHAMBRE_ECLAIRAGE ];
2022-06-20 10:34:55 +02:00
if ( $property == " occupancy " )
2022-01-27 18:41:16 +01:00
{
2022-06-21 08:57:10 +02:00
logger ( DEBUG , _ ( " CASE: occupancy " ), __FILE__ . " : " . __LINE__ );
logger ( DEBUG , " lux = " . bool2string ( $lux ) . " luminance_min = " . $this -> luminance_min , __FILE__ . " : " . __LINE__ );
if ( $value === true and $lux < $this -> luminance_min )
2022-04-07 01:44:17 +02:00
{
2022-06-21 08:57:10 +02:00
logger ( DEBUG , _ ( " lux < luminance_min " ), __FILE__ . " : " . __LINE__ );
2022-06-20 10:34:55 +02:00
if ( $targetAmbiance -> properties [ " state " ][ " method " ] == MANUAL )
{
2022-06-21 08:57:10 +02:00
logger ( DEBUG , _ ( " method => MANUAL " ), __FILE__ . " : " . __LINE__ );
2022-06-20 10:34:55 +02:00
$method = false ;
$delayState = false ;
} else
{
2022-06-21 08:57:10 +02:00
logger ( DEBUG , _ ( " method => AUTO " ), __FILE__ . " : " . __LINE__ );
2022-06-20 10:34:55 +02:00
$method = AUTO ;
$delayState = " OFF " ;
}
2022-04-07 01:44:17 +02:00
2022-06-20 10:34:55 +02:00
$this -> send ( $targetAmbiance , " state " , " ON " , $delayState , $method );
}
2022-03-04 22:30:16 +01:00
} elseif ( $property == " contact " )
2022-01-29 19:58:01 +01:00
{
2022-04-07 01:44:17 +02:00
logger ( DEBUG , _ ( " CASE: contact " ), __FILE__ . " : " . __LINE__ );
if ( $value === false )
2022-03-04 22:30:16 +01:00
{
$this -> send ( $targetEclairage , " state_l1 " , " ON " , " OFF " , AUTO );
2022-04-07 01:44:17 +02:00
} elseif ( $value === true )
{
$this -> send ( $targetEclairage , " state_l1 " , " OFF " , false , IDLE );
}
2022-06-20 10:34:55 +02:00
} elseif ( $property == " state " )
2022-04-07 01:44:17 +02:00
{
2022-06-20 10:34:55 +02:00
if ( $value == " OFF " )
{
logger ( DEBUG , _ ( " CASE: state => value = 'OFF' " ), __FILE__ . " : " . __LINE__ );
$targetAmbiance -> properties [ $property ][ " method " ] = IDLE ;
2022-09-09 16:53:19 +02:00
removeEvent ( $targetAmbiance , $property , " OFF " );
2022-06-20 10:34:55 +02:00
}
} elseif ( $property == " state_l1 " )
2022-04-07 01:44:17 +02:00
{
2022-06-20 10:34:55 +02:00
if ( $value == " OFF " )
{
logger ( DEBUG , _ ( " CASE: state_l1 => value = 'OFF' " ), __FILE__ . " : " . __LINE__ );
2022-09-09 16:53:19 +02:00
$targetEclairage -> properties [ $property ][ " method " ] = IDLE ;
removeEvent ( $targetAmbiance , $property , " OFF " );
2022-06-20 10:34:55 +02:00
}
2022-04-07 01:44:17 +02:00
} elseif ( $property == " action " )
{
logger ( DEBUG , _ ( " CASE: action " ), __FILE__ . " : " . __LINE__ );
if ( $targetAmbiance -> properties [ $property ][ " method " ] == IDLE )
2022-03-04 22:30:16 +01:00
{
2022-04-07 01:44:17 +02:00
$targetAmbiance -> properties [ $property ][ " method " ] == MANUAL ;
2022-03-04 22:30:16 +01:00
}
2022-01-27 18:41:16 +01:00
}
2022-02-12 15:23:58 +01:00
logger ( INFO , sprintf ( _ ( " %s: notification received from MQTT from %s => parameter: %s value: %s " ), $this -> hookName , $device -> friendlyName , $property , bool2string ( $value )), __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
}
2022-06-20 10:34:55 +02:00
private function send ( & $deviceObject , $property , $state , $delayState = false , $method = AUTO )
2022-01-27 18:41:16 +01:00
{
2022-03-06 01:30:33 +01:00
global $indexDevices ;
2022-03-04 22:30:16 +01:00
$msg = array ( $property => $state );
2022-03-28 00:40:34 +02:00
//if ($deviceObject->properties[$property]["value"] != $state)
//{
2022-02-23 10:23:16 +01:00
logger ( INFO , sprintf ( _ ( " publishing message: %s to %s " ), json_encode ( $msg ), $deviceObject -> friendlyName ), __FILE__ . " : " . __LINE__ );
$deviceObject -> payload = $msg ;
2022-06-20 10:34:55 +02:00
$deviceObject -> set ( $property , $method );
2022-03-28 00:40:34 +02:00
///}else
/* {
2022-02-23 10:23:16 +01:00
logger ( INFO , sprintf ( _ ( " not publishing message: %s to %s, already set " ), json_encode ( $msg ), $deviceObject -> friendlyName ), __FILE__ . " : " . __LINE__ );
2022-01-23 09:46:06 +01:00
2022-03-28 00:40:34 +02:00
} */
2022-01-27 18:41:16 +01:00
//echo 'delaystate = ' . var_dump($delayState);
2022-06-20 10:34:55 +02:00
if ( $delayState !== false ) setDelay ( $deviceObject , $this -> delay , $this -> timeUnit , " state " , $delayState , true , IDLE );
2022-01-23 09:46:06 +01:00
}
}
$hooks [ " rdc_chambre_eclairage " ] = new rdc_chambre_eclairage ();
?>