2022-01-17 00:18:50 +01:00
< ? php
class hook
{
public $hookName = " " ;
2022-05-29 01:55:22 +02:00
public $active ;
2022-01-17 00:18:50 +01:00
public $initialized = false ;
protected $devicelist ;
2022-03-13 22:33:26 +01:00
protected $propertyInitialized ;
2022-01-17 00:18:50 +01:00
// list of devices we are listening to
function __construct ()
{
2022-08-12 10:41:55 +02:00
if ( $this -> active === true )
2022-03-13 22:33:26 +01:00
{
2022-08-12 10:41:55 +02:00
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
2022-01-23 09:46:06 +01:00
{
2022-08-12 10:41:55 +02:00
logger ( INFO , _ ( " Not initializing inactive hook: " ) . $this -> hookName );
2022-01-23 09:46:06 +01:00
}
2022-08-12 10:41:55 +02:00
2022-01-17 00:18:50 +01:00
}
2022-08-12 10:41:55 +02:00
function installHooksFunction ( & $indexDevices )
2022-01-17 00:18:50 +01:00
{
2022-04-23 02:00:52 +02:00
global $devices , $devicesRequest ;
static $requestflag = 0 ;
2022-01-17 00:18:50 +01:00
$result = true ;
// assigne the function to the sensors devices
2022-06-13 19:22:08 +02:00
foreach ( $this -> devicelist as $ieeeAddress => $property )
2022-01-17 00:18:50 +01:00
{
2022-06-13 19:22:08 +02:00
if ( array_key_exists ( $ieeeAddress , $indexDevices ))
2022-01-17 00:18:50 +01:00
{
2022-06-13 19:22:08 +02:00
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 ))
2022-01-17 00:18:50 +01:00
{
2022-06-13 19:22:08 +02:00
logger ( DEBUG , _ ( " Trying to store callback " ), __FILE__ . " : " . __LINE__ );
if ( array_key_exists ( " functions " , $indexDevices [ $ieeeAddress ] -> properties [ $property ]))
2022-01-17 00:18:50 +01:00
{
2022-06-13 19:22:08 +02:00
if ( $this -> active === true )
2022-04-07 01:44:17 +02:00
{
2022-06-13 19:22:08 +02:00
$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__ );
2022-03-13 22:33:26 +01:00
2022-06-13 19:22:08 +02:00
} elseif ( $this -> active === false )
2022-04-07 01:44:17 +02:00
{
2022-06-13 19:22:08 +02:00
unset ( $indexDevices [ $ieeeAddress ] -> properties [ $property ][ " functions " ][ $this -> hookName ]);
logger ( DEBUG , $indexDevices [ $ieeeAddress ] -> friendlyName . " / " . $property . _ ( " Unsetting " ) . $this -> hookName , __FILE__ . " : " . __LINE__ );
2022-04-07 01:44:17 +02:00
}
2022-01-17 00:18:50 +01:00
} else
{
2022-06-13 19:22:08 +02:00
logger ( WARNING , sprintf ( _ ( " Hook %s can not initialize Property '%s' of device %s " ), $this -> hookName , $property , $ieeeAddress ), __FILE__ . " : " . __LINE__ );
$result = false ;
2022-01-17 00:18:50 +01:00
}
} else
{
2022-06-13 19:22:08 +02:00
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__ );
2022-04-23 02:00:52 +02:00
$result = false ;
2022-06-13 19:22:08 +02:00
}
//}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 ;
2022-01-17 00:18:50 +01:00
}
2022-04-07 01:44:17 +02:00
2022-06-13 19:22:08 +02:00
$result = false ;
mkIndexes ();
2022-01-17 00:18:50 +01:00
}
2022-06-13 19:22:08 +02:00
2022-01-17 21:01:11 +01:00
//echo "result => "; var_dump($result);
2022-01-17 00:18:50 +01:00
if ( $result === true )
{
$this -> initialized = true ;
2022-03-04 22:30:16 +01:00
logger ( INFO , $this -> hookName . _ ( " initialized " ), __FILE__ . " : " . __LINE__ );
2022-01-17 00:18:50 +01:00
//var_dump($this);
}
2022-06-13 19:22:08 +02:00
}
2022-01-17 00:18:50 +01:00
return $result ;
}
}
?>