111 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
class rdc_chambre_eclairage extends hook
 | 
						|
{
 | 
						|
	public $hookName = "rdc_chambre_eclairage";
 | 
						|
	public $active = true;			//enable/disable hook (true => enabled)
 | 
						|
	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(
 | 
						|
		RDC_CHAMBRE_AMBIANCE => "state",		// "ON"/"OFF"
 | 
						|
		RDC_CHAMBRE_ECLAIRAGE => "state_l1",	// "ON"/"OFF"
 | 
						|
		RDC_CHAMBRE_MVMT => "occupancy",
 | 
						|
		RDC_CHAMBRE_ARMOIRE_GAUCHE => "contact",
 | 
						|
		RDC_CHAMBRE_AMBIANCE_INTER => "action"
 | 
						|
	);
 | 
						|
 | 
						|
	// callback fonction. Is called with these 3 parameters
 | 
						|
	// $device 		-> calling device
 | 
						|
	// $property 	-> property of the device (given by mqtt)
 | 
						|
	// $value 		-> value of the property
 | 
						|
	public function callBack($device, $property, $value)
 | 
						|
	{
 | 
						|
		global $devices, $indexDevices;
 | 
						|
		logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__);
 | 
						|
		$lux = $indexDevices[RDC_CHAMBRE_LUMINOSITE]->properties["illuminance_lux"]["value"];
 | 
						|
		$targetAmbiance = $indexDevices[RDC_CHAMBRE_AMBIANCE];
 | 
						|
		$targetEclairage = $indexDevices[RDC_CHAMBRE_ECLAIRAGE];
 | 
						|
		if ($property == "occupancy")
 | 
						|
		{
 | 
						|
			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)
 | 
						|
			{
 | 
						|
				logger(DEBUG, _("lux < luminance_min"), __FILE__ . ":" . __LINE__);
 | 
						|
				if ($targetAmbiance->properties["state"]["method"] == MANUAL)
 | 
						|
				{
 | 
						|
				logger(DEBUG, _("method  => MANUAL"), __FILE__ . ":" . __LINE__);
 | 
						|
 | 
						|
					$method = false;
 | 
						|
					$delayState = false;
 | 
						|
				}else
 | 
						|
				{
 | 
						|
				logger(DEBUG, _("method => AUTO"), __FILE__ . ":" . __LINE__);
 | 
						|
					$method = AUTO;
 | 
						|
					$delayState = "OFF";
 | 
						|
				}
 | 
						|
 | 
						|
				$this->send($targetAmbiance, "state", "ON", $delayState, $method);
 | 
						|
			}
 | 
						|
		}elseif ($property == "contact")
 | 
						|
		{
 | 
						|
			logger(DEBUG, _("CASE: contact"), __FILE__ . ":" . __LINE__);
 | 
						|
			if ($value === false)
 | 
						|
			{
 | 
						|
				$this->send($targetEclairage, "state_l1", "ON", "OFF", AUTO);
 | 
						|
			}elseif ($value === true)
 | 
						|
			{
 | 
						|
				$this->send($targetEclairage, "state_l1", "OFF", false, IDLE);
 | 
						|
			}
 | 
						|
		}elseif ($property == "state")
 | 
						|
		{
 | 
						|
			if ($value == "OFF")
 | 
						|
			{
 | 
						|
				logger(DEBUG, _("CASE: state => value = 'OFF'"), __FILE__ . ":" . __LINE__);
 | 
						|
				$targetAmbiance->properties[$property]["method"] = IDLE;
 | 
						|
				removeEvent($targetAmbiance, $property, "OFF", IDLE);
 | 
						|
			}
 | 
						|
		}elseif ($property == "state_l1")
 | 
						|
		{
 | 
						|
			if ($value == "OFF")
 | 
						|
			{
 | 
						|
				logger(DEBUG, _("CASE: state_l1 => value = 'OFF'"), __FILE__ . ":" . __LINE__);
 | 
						|
				//$targetEclairage->properties[$property]["method"] = IDLE;
 | 
						|
				removeEvent($targetAmbiance, $property, "OFF", IDLE);
 | 
						|
			}
 | 
						|
		}elseif ($property == "action")
 | 
						|
		{
 | 
						|
			logger(DEBUG, _("CASE: action"), __FILE__ . ":" . __LINE__);
 | 
						|
			if ($targetAmbiance->properties[$property]["method"] == IDLE)
 | 
						|
			{
 | 
						|
				$targetAmbiance->properties[$property]["method"] == MANUAL;
 | 
						|
			}
 | 
						|
		}
 | 
						|
		logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__);
 | 
						|
	}
 | 
						|
 | 
						|
	private function send(&$deviceObject, $property, $state, $delayState = false, $method = AUTO)
 | 
						|
	{
 | 
						|
		global $indexDevices;
 | 
						|
		$msg = array($property => $state);
 | 
						|
		//if ($deviceObject->properties[$property]["value"] != $state)
 | 
						|
		//{
 | 
						|
			logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__);
 | 
						|
			$deviceObject->payload = $msg;
 | 
						|
			$deviceObject->set($property, $method);
 | 
						|
		///}else
 | 
						|
		/*{
 | 
						|
			logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__);
 | 
						|
 | 
						|
		}*/
 | 
						|
		//echo 'delaystate = ' . var_dump($delayState);
 | 
						|
		if ($delayState !== false) 	setDelay($deviceObject, $this->delay, $this->timeUnit, "state", $delayState, true, IDLE);
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
$hooks["rdc_chambre_eclairage"] = new rdc_chambre_eclairage();
 | 
						|
?>
 |