1
0
moha/hooks/scripts/availability.php

60 lines
1.3 KiB
PHP
Raw Normal View History

2022-01-02 18:14:13 +01:00
<?php
class availability
{
2022-01-17 00:18:50 +01:00
public $hookname = "availability";
public $initialized = true;
2022-01-02 18:14:13 +01:00
// by default all devices are listening for availability
2022-01-06 13:03:26 +01:00
function __construct()
{
global $devices;
2022-01-17 00:18:50 +01:00
$this->iterate();
2022-01-06 13:03:26 +01:00
}
2022-01-17 00:18:50 +01:00
private function iterate()
2022-01-06 13:03:26 +01:00
{
2022-01-17 00:18:50 +01:00
global $indexDevices;
foreach ($indexDevices as $value)
2022-01-06 13:03:26 +01:00
{
2022-01-17 00:18:50 +01:00
$value->availability["functions"][] = array($this,"callback");
2022-01-06 13:03:26 +01:00
}
}
2022-01-17 00:18:50 +01:00
2022-01-02 18:14:13 +01:00
// callback fonction. Is called with these 3 parameters
// $device -> calling device
2022-01-06 13:03:26 +01:00
// $property -> parameter passed by mqtt
2022-01-02 18:14:13 +01:00
// $value -> value of the parameter
2022-01-06 13:03:26 +01:00
public function callBack($device, $property, $value)
2022-01-02 18:14:13 +01:00
{
2022-01-06 13:03:26 +01:00
switch($property)
2022-01-02 18:14:13 +01:00
{
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;
2022-01-04 08:54:06 +01:00
logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value));
2022-01-02 18:14:13 +01:00
}
break;
}
2022-01-06 13:03:26 +01:00
//echo sprintf(_("notification received from MQTT -> device %s is %s"), $device->friendlyName , $value). EOL;
//echo $property . "=> " . $value . EOL;
2022-01-02 18:14:13 +01:00
}
2022-01-06 13:03:26 +01:00
/*
2022-01-02 18:14:13 +01:00
public function getHook()
{
return array($this,"callback");
2022-01-06 13:03:26 +01:00
}*/
2022-01-02 18:14:13 +01:00
}
2022-01-06 13:03:26 +01:00
$hooks["availability"] = new availability();
2022-01-02 18:14:13 +01:00
?>