109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| class hook
 | |
| {
 | |
| 	public $hookName = "";
 | |
| 	public $active;
 | |
| 	public $initialized = false;
 | |
| 	protected $devicelist;
 | |
| 	protected $propertyInitialized;
 | |
| 
 | |
| 	// list of devices we are listening to
 | |
| 	function __construct()
 | |
| 	{
 | |
| 		if ($this->active === true)
 | |
| 		{
 | |
| 			logger(INFO, _("Initializing hook: ") . $this->hookName);
 | |
| 			//$this->installHooks();
 | |
| 			if (method_exists($this, "init"))
 | |
| 			{
 | |
| 				$this->init();
 | |
| 			}
 | |
| 			if (method_exists($this, "iterate"))
 | |
| 			{
 | |
| 				$this->iterate();
 | |
| 			}
 | |
| 			foreach ($this->devicelist as $ieeeAddress => $property)
 | |
| 			{
 | |
| 				$this->propertyInitialized[$ieeeAddress][$property] = false;
 | |
| 			}
 | |
| 		}else
 | |
| 		{
 | |
| 			logger(INFO, _("Not initializing inactive hook: ") . $this->hookName);
 | |
| 		}
 | |
| 
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	function installHooksFunction(&$indexDevices)
 | |
| 	{
 | |
| 		global $devices, $devicesRequest;
 | |
| 		static $requestflag = 0;
 | |
| 		$result = true;
 | |
| 		// assigne the function to the sensors devices
 | |
| 		foreach ($this->devicelist as $ieeeAddress => $property)
 | |
| 		{
 | |
| 
 | |
| 			if (array_key_exists($ieeeAddress, $indexDevices))
 | |
| 			{
 | |
| 				logger(DEBUG, _("Device: ") . $ieeeAddress, __FILE__ . ":" . __LINE__);
 | |
| 				//if (!array_key_exists($this->hookName, $indexDevices[$ieeeAddress]->properties[$property]["functions"]))
 | |
| 				//{
 | |
| 				if (array_key_exists($property, $indexDevices[$ieeeAddress]->properties))
 | |
| 				{
 | |
| 					logger(DEBUG, _("Trying to store callback"), __FILE__ . ":" . __LINE__);
 | |
| 					if (array_key_exists("functions", $indexDevices[$ieeeAddress]->properties[$property]))
 | |
| 					{
 | |
| 						if ($this->active === true)
 | |
| 						{
 | |
| 							$indexDevices[$ieeeAddress]->properties[$property]["functions"][$this->hookName] = array($this,"callback");
 | |
| 							logger(DEBUG, sprintf(_("Property '%s' of %s is initialized with callback"), $property, $indexDevices[$ieeeAddress]->friendlyName), __FILE__ . ":" . __LINE__);
 | |
| 
 | |
| 						}elseif ($this->active === false)
 | |
| 						{
 | |
| 							unset ($indexDevices[$ieeeAddress]->properties[$property]["functions"][$this->hookName]);
 | |
| 							logger(DEBUG, $indexDevices[$ieeeAddress]->friendlyName . "/" . $property . _(" Unsetting ") .$this->hookName  , __FILE__ . ":" . __LINE__);
 | |
| 						}
 | |
| 					}else
 | |
| 					{
 | |
| 						logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property, $ieeeAddress), __FILE__ . ":" . __LINE__);
 | |
| 						$result = false;
 | |
| 					}
 | |
| 				}else
 | |
| 				{
 | |
| 					logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s : property does not exists"), $this->hookName, $property, $indexDevices[$ieeeAddress]->friendlyName), __FILE__ . ":" . __LINE__);
 | |
| 					$result = false;
 | |
| 				}
 | |
| 			//}else
 | |
| 			//{
 | |
| 			//	logger(DEBUG, _("Callback already installed"), __FILE__ . ":" . __LINE__);
 | |
| 			//}
 | |
| 			}else
 | |
| 			{
 | |
| 				logger(ERROR, $ieeeAddress . (" does not exists"), __FILE__ . ":" . __LINE__);
 | |
| 				if ($devicesRequest === false)
 | |
| 				{
 | |
| 					publish("zigbee2mqtt", array("bridge/devices" => ""), "get");
 | |
| 					$devicesRequest = true;
 | |
| 				}elseif ($requestflag++ > 30)
 | |
| 				{
 | |
| 					$requestflag = 0;
 | |
| 				}
 | |
| 
 | |
| 				$result = false;
 | |
| 				mkIndexes();
 | |
| 			}
 | |
| 
 | |
| 			//echo "result => "; var_dump($result);
 | |
| 			if ($result === true)
 | |
| 			{
 | |
| 				$this->initialized = true;
 | |
| 				logger(INFO, $this->hookName . _(" initialized"), __FILE__ . ":" . __LINE__);
 | |
| 				//var_dump($this);
 | |
| 			}
 | |
| 		}
 | |
| 		return $result;
 | |
| 	}
 | |
| }
 | |
| 
 | |
| ?>
 |