1
0

added notify command to webserver

This commit is contained in:
daniel Tartavel 2022-01-30 00:21:50 +01:00
parent 719a45fa4e
commit 4b226c1d0a
7 changed files with 97 additions and 8 deletions

View File

@ -73,6 +73,53 @@ class event
public $exceptionInterval; // array of object ranges public $exceptionInterval; // array of object ranges
} }
class watch
{
public $topic;
public $property;
public $device;
public $function;
private $conditions = array(
"<", ">", "==", ">=", "<="
);
public function __construct($topic, $fn, $property, $condition, $value)
{
logger(DEBUG, _("New Notify object"), __FILE__ . ":" . __LINE__);
if (($this->device = getDevice($topic, $fn)) === false)
{
$this->topic = $topic;
if (array_key_exist($property, $this->device))
{
$this->property = $property;
if ( !is_numeric($value))
{
logger(ERROR, _("Value is not numeric"), __FILE__ . ":" . __LINE__ );
return false;
}
if (array_search($condition, $conditions))
{
$this->function = '$arg ' . $condition . " " . $value;
logger(DEBUG, _("Test function is ") . $this->function);
}else
{
logger(ERROR, _("Condition is not one of the permitted once"), __FILE__ . ":" . __LINE__ );
return false;
}
}else
{
logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ );
return false;
}
}else
{
logger(ERROR, _("Device do not exists"), __FILE__ . ":" . __LINE__ );
return false;
}
return $this;
}
}
class interval class interval
{ {
public $startDate; public $startDate;

View File

@ -125,7 +125,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
iterateDevice($topic, $fn, $device, $device, $payloadArray); iterateDevice($topic, $fn, $device, $device, $payloadArray);
}else }else
{ {
logger(ERROR, _("payloadArray is empty!"), __FILE__ . ":" . __LINE__); logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
} }
} }

View File

@ -17,7 +17,7 @@ class rdc_sdb_eclairage extends hook
// devicelist[$ieeAddress][0] => property to watch // devicelist[$ieeAddress][0] => property to watch
// devicelist[$ieeAddress][1] => initialized = true // devicelist[$ieeAddress][1] => initialized = true
protected $devicelist = array( protected $devicelist = array(
RDC_SDB_DOUCHE_MVMT => array("occupancy", false), //RDC_SDB_DOUCHE_MVMT => array("occupancy", false),
RDC_SDB_PLAFOND_MVMT => array("occupancy", false), RDC_SDB_PLAFOND_MVMT => array("occupancy", false),
RDC_SDB_MVMT => array("occupancy", false), RDC_SDB_MVMT => array("occupancy", false),
RDC_SDB_WC_ECLAIRAGE => array("state_l1", false) RDC_SDB_WC_ECLAIRAGE => array("state_l1", false)

View File

@ -23,6 +23,7 @@ $hooksList = array(); // list of hooks to be included
$hooks = array(); // array of hooks $hooks = array(); // array of hooks
$notificationMethods = array(); // array of notification methods objects $notificationMethods = array(); // array of notification methods objects
$events = array(); // list of event objects $events = array(); // list of event objects
$monitored = array(); // list of device to watch
$changed = array(); // list of changed devices $changed = array(); // list of changed devices
$dbInit = 0; // flag to indicate that devices db is initializedl $dbInit = 0; // flag to indicate that devices db is initializedl
$connected = false; // connected to MQTT server $connected = false; // connected to MQTT server

View File

@ -41,6 +41,32 @@ function mktopic($device)
return $device->topic . "/" . $device->friendlyName; return $device->topic . "/" . $device->friendlyName;
} }
function getDevice($topic, $fn)
{
global $topics, $devices;
if (array_key_exists($topic, $topics))
{
$var = $devices[$topic];
$path = explode("/", $fn);
foreach($path as $tmp)
{
if (array_key_exists($tmp, $var))
{
$var = $var[$tmp];
}else
{
return false;
}
}
return $var;
}else
{
return false;
}
}
function getValue($ieeeAddress, $property) function getValue($ieeeAddress, $property)
{ {
global $indexDevices; global $indexDevices;

11
watch.php Normal file
View File

@ -0,0 +1,11 @@
<?php
function watch($topic, $fn, $property, $function)
{
global $monitored;
$monitored[] = new watch($topic, $fn, $property, $function);
}
?>

View File

@ -72,12 +72,7 @@ function askWebServer($read)
{ {
if(!empty($topic)) if(!empty($topic))
{ {
$var = $var[$topic]; $var = getDevice($topic, $argList["fn"]);
$path = explode("/", $argList["fn"]);
foreach($path as $tmp)
{
$var = $var[$tmp];
}
}else }else
{ {
$str = _("topic is not defining: add &topic=\nThese topics are availables: "); $str = _("topic is not defining: add &topic=\nThese topics are availables: ");
@ -103,6 +98,15 @@ function askWebServer($read)
} }
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
case "notify":
if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList))
{
fwrite($spawn, _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value"));
}else
{
$monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]);
}
break;
default: default:
logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__);
fwrite($spawn, _("unknown command")); fwrite($spawn, _("unknown command"));