modified init and some debugging
This commit is contained in:
62
hooks/scripts/availability.php
Normal file
62
hooks/scripts/availability.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
class availability
|
||||
{
|
||||
// by default all devices are listening for availability
|
||||
|
||||
function __construct()
|
||||
{
|
||||
global $devices;
|
||||
$this->iterate($devices);
|
||||
}
|
||||
|
||||
private function iterate(& $device)
|
||||
{
|
||||
foreach ($device as $key => $value)
|
||||
{
|
||||
if (gettype($value) == "array")
|
||||
{
|
||||
$this->iterate($value);
|
||||
}elseif (is_a($value, "device"))
|
||||
{
|
||||
|
||||
$value->availability["functions"][] = array($this,"callback");
|
||||
}
|
||||
}
|
||||
}
|
||||
// callback fonction. Is called with these 3 parameters
|
||||
// $device -> calling device
|
||||
// $property -> parameter passed by mqtt
|
||||
// $value -> value of the parameter
|
||||
public function callBack($device, $property, $value)
|
||||
{
|
||||
switch($property)
|
||||
{
|
||||
case "availability": // theorically can't be other, but ....
|
||||
if ($device->availability != $value)
|
||||
{
|
||||
//echo "==========>>>>>> Availability $value" . EOL;
|
||||
if (!empty($device->availability))
|
||||
{
|
||||
$log = WARNING;
|
||||
}else
|
||||
{
|
||||
$log = INFO;
|
||||
}
|
||||
$device->availability = $value;
|
||||
logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
//echo sprintf(_("notification received from MQTT -> device %s is %s"), $device->friendlyName , $value). EOL;
|
||||
//echo $property . "=> " . $value . EOL;
|
||||
}
|
||||
/*
|
||||
public function getHook()
|
||||
{
|
||||
return array($this,"callback");
|
||||
}*/
|
||||
}
|
||||
|
||||
$hooks["availability"] = new availability();
|
||||
?>
|
Reference in New Issue
Block a user