2022-08-05 17:24:11 +02:00
< ? php
// script to prevent when exterior temperature become inferior or superior to interior one
class rdc_temperature_int_ext extends hook
{
2022-09-01 17:02:28 +02:00
public $hookName = " rdc_temperature_int_ext " ;
2022-08-05 17:24:11 +02:00
public $active = true ; //enable/disable hook (true => enabled)
2022-08-27 00:02:24 +02:00
public $tempSup = 25 ;
public $tempInf = 20 ;
2022-08-05 17:24:11 +02:00
protected $devicelist = array (
2022-09-19 23:40:29 +02:00
METEO => " tempc " ,
2022-10-05 20:37:01 +02:00
//RDC_CHAMBRE_TEMPERATURE => "temperature",
//RDC_SALON_TEMPERATURE => "temperature"
2022-08-05 17:24:11 +02:00
);
2022-09-01 17:02:28 +02:00
protected $portesList = array (
ENTREE_PORTE => " contact " ,
//GARAGE_PORTE => "contact",
RDC_CHAMBRE_BAIE => " contact " ,
RDC_SALON_BAIE => " contact "
);
2022-08-12 10:41:55 +02:00
function installHooks ( & $indexDevices )
{
return $this -> installHooksFunction ( $indexDevices );
}
2022-08-05 17:24:11 +02:00
// callback fonction. Is called with these 4 parameters
public function callBack ( & $device , $property , $value )
{
2022-12-05 12:23:43 +01:00
global $indexDevices , $scripts ;
2022-08-27 00:02:24 +02:00
static $time ;
$portes = array ();
2022-08-28 00:06:24 +02:00
$status = - 1 ;
2022-08-27 00:02:24 +02:00
$msg = " " ;
2022-09-01 17:02:28 +02:00
logger ( DEBUG , _ ( " rdc_temperature_int_ext hook " ), null , $device );
2022-08-28 00:06:24 +02:00
if ( empty ( $time )) $time = now ();
2022-09-01 17:02:28 +02:00
//echo "Time is " . var_dump($time) . EOL;
//echo "minutes since time :" . now()->format("U") - $time->format("U");
if (( now () -> format ( " U " ) - $time -> format ( " U " )) > 300 )
{
2022-08-28 00:06:24 +02:00
2022-10-05 20:37:01 +02:00
if ( $property == " tempc " )
{
$indoorTemp = $device -> properties [ " indoortempc " ][ " value " ];
2022-08-27 00:02:24 +02:00
2022-09-01 17:02:28 +02:00
2022-10-05 20:37:01 +02:00
if ( $value > $indoorTemp )
2022-09-01 17:02:28 +02:00
{
2022-10-05 20:37:01 +02:00
if ( ( $indoorTemp <= $this -> tempSup ) and empty ( $portes ))
{
$status = 1 ;
//logger(ALERT, _("Open doors to climate"), null ,$device);
} elseif ( $indoorTemp >= $this -> tempSup and ! empty ( $portes ))
{
$status = 0 ;
//logger(ALERT, _("Close doors to climate"), null, $device);
}
} elseif ( $value < $indoorTemp )
2022-09-01 17:02:28 +02:00
{
2022-10-05 20:37:01 +02:00
if (( $indoorTemp >= $this -> tempSup ) and empty ( $portes ) )
{
$status = 1 ;
//logger(ALERT, _("Open doors to climate"), null ,$device);
} elseif ( $indoorTemp <= $this -> tempSup )
{
$status = 0 ;
//logger(ALERT, _("Close doors to climate"), null, $device);
}
logger ( INFO , sprintf ( _ ( " %s: notification received from MQTT from %s => parameter: %s value: %s " ), $this -> hookName , $device -> friendlyName , $property , bool2string ( $value )), __FILE__ . " : " . __LINE__ , $device );
2022-09-01 17:02:28 +02:00
}
2022-10-05 20:37:01 +02:00
//print_r($portes);
if ( $status == 1 )
2022-09-01 17:02:28 +02:00
{
2022-12-05 12:23:43 +01:00
$portes = $scripts [ " test_portes " ] -> testPortes ( $this -> portesList , false , false );
2022-10-05 20:37:01 +02:00
if ( empty ( $portes ))
2022-09-02 17:59:33 +02:00
{
2022-10-05 20:37:01 +02:00
$time = now ();
foreach ( $portes as $porte )
{
$msg .= $porte . " \n " ;
}
logger ( ALERT , _ ( " Open doors to climate " ) . $msg , null , $device );
2022-09-02 17:59:33 +02:00
}
2022-10-05 20:37:01 +02:00
} else
2022-08-28 00:06:24 +02:00
{
2022-12-05 12:23:43 +01:00
$portes = $scripts [ " test_portes " ] -> testPortes ( $this -> portesList , false , true );
2022-10-05 20:37:01 +02:00
if ( ! empty ( $portes ))
2022-09-01 17:02:28 +02:00
{
2022-10-05 20:37:01 +02:00
$time = now ();
foreach ( $portes as $porte )
{
$msg .= $porte . " \n " ;
}
logger ( ALERT , _ ( " Close doors to climate \n " ) . $msg , null , $device );
2022-09-01 17:02:28 +02:00
}
2022-08-28 00:06:24 +02:00
}
}
2022-08-05 17:24:11 +02:00
}
}
}
$hooks [ " rdc_temperature_int_ext " ] = new rdc_temperature_int_ext ();
?>