2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function checkEvents()
|
|
|
|
{
|
|
|
|
global $events, $indexDevices, $devices;
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "===========> checking events" . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach ($events as $key => $event)
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
if (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 23:09:58 +01:00
|
|
|
echo "---->sending command" . EOL;
|
|
|
|
publish($event->device, array($event->param => $event->value), "set", $key);
|
2021-12-30 16:18:32 +01:00
|
|
|
//$event->published = now();
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
|
|
|
|
unset($events[$key]);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//print_r($events);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDelay(&$device, $delay, $unit="sec", $param, $value, $replace=false)
|
|
|
|
{
|
|
|
|
global $events;
|
|
|
|
$datetime = new dateTime();
|
|
|
|
//print_r($datetime);
|
|
|
|
switch($unit)
|
|
|
|
{
|
|
|
|
case "sec":
|
|
|
|
$unit = "S";
|
|
|
|
break;
|
|
|
|
case "min":
|
|
|
|
$unit = "M";
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
$unit = "H";
|
|
|
|
break;
|
|
|
|
case "day":
|
|
|
|
$unit = "D";
|
|
|
|
break;
|
|
|
|
case "week":
|
|
|
|
$unit = "W";
|
|
|
|
break;
|
|
|
|
case "month":
|
|
|
|
$unit = "M";
|
|
|
|
break;
|
|
|
|
case "year":
|
|
|
|
$unit = "Y";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$datetime->add(new DateInterval('PT'. $delay . $unit));
|
|
|
|
//print_r($device);
|
|
|
|
if ($replace)
|
|
|
|
{
|
|
|
|
$eventKey = searchEvent($device, $param, $value);
|
|
|
|
if ($eventKey !== false) deleteEvent($eventKey);
|
|
|
|
}
|
|
|
|
//$dt = $datetime->format("Y-m-d\TH:i:s\Z");
|
|
|
|
$events[] = new event;
|
|
|
|
$key = key($events);
|
|
|
|
$events[$key]->dateTimeEvent = $datetime;
|
|
|
|
$events[$key]->ieeeAddress = $device->ieeeAddress;
|
|
|
|
$events[$key]->topic = $device->topic;
|
|
|
|
$events[$key]->param = $param;
|
|
|
|
$events[$key]->value = $value;
|
2021-12-31 23:09:58 +01:00
|
|
|
$events[$key]->device = & $device;
|
2021-12-30 16:18:32 +01:00
|
|
|
echo "new event";
|
|
|
|
}
|
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
function searchEvent($device, $param , $value): int
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
global $events;
|
|
|
|
echo "searching event" . EOL;
|
|
|
|
//$keys = array_keys($events, $device->ieeeAddress);
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "ieee_address =>" . $device->ieeeAddress . EOL;
|
|
|
|
//print_r($events);
|
2021-12-30 16:18:32 +01:00
|
|
|
foreach($events as $key => $event)
|
|
|
|
{
|
|
|
|
//echo "Event : $event => $value" . EOL;
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "===>";print_r($event); echo EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
if($event->topic == $device->topic and $event->param == $param and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress)
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "==============================\nfound " . $key . "\n=================================" . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEvent($eventKey)
|
|
|
|
{
|
|
|
|
global $events;
|
|
|
|
unset ($events[$eventKey]);
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "delete event " . $eventKey . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|