1
0
moha/hooks/scripts/availability.php

65 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2022-01-02 18:14:13 +01:00
<?php
class availability extends hook
2022-01-02 18:14:13 +01:00
{
2022-01-17 00:18:50 +01:00
public $hookname = "availability";
2022-06-13 19:22:08 +02:00
public $active = true;
2022-01-02 18:14:13 +01:00
// by default all devices are listening for availability
protected $devicelist = array();
2022-01-02 18:14:13 +01:00
protected function iterate()
2022-01-06 13:03:26 +01:00
{
2022-01-17 00:18:50 +01:00
global $indexDevices;
foreach ($indexDevices as $ieeeAddress => $value)
2022-01-06 13:03:26 +01:00
{
2022-12-05 12:23:43 +01:00
if (!key_exists("availability", $value->properties))
{
$value->properties["availability"] = "";
}
2022-01-06 13:03:26 +01:00
}
$this->installHooksFunction($indexDevices);
2022-01-06 13:03:26 +01:00
}
2022-01-17 00:18:50 +01:00
2022-06-13 19:22:08 +02:00
function installHooks(&$indexDevices)
{
2022-12-05 12:23:43 +01:00
$this->installHooksFunction($indexDevices);
return false;
2022-06-13 19:22:08 +02:00
}
2022-12-05 12:23:43 +01:00
function installHook(&$device)
{
global $indexDevices;
$this->devicelist = array($device["device"]->ieeeAddress => "availability");
$this->installHooksFunction($indexDevices);
}
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->properties["availability"]["value]"] != $value)
2022-01-02 18:14:13 +01:00
{
//echo "==========>>>>>> Availability $value" . EOL;
2022-01-20 00:26:57 +01:00
//if (!empty($device->availability))
2022-12-05 12:23:43 +01:00
//{
2022-01-20 00:26:57 +01:00
$log = ALERT;
//}else
//{
// $log = INFO;
//}
//$device->availability = $value;
logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__, $device);
2022-01-02 18:14:13 +01:00
}
break;
}
}
}
2022-01-06 13:03:26 +01:00
$hooks["availability"] = new availability();
2022-01-02 18:14:13 +01:00
?>