1
0
moha/events.php

177 lines
4.7 KiB
PHP
Raw Normal View History

2021-12-30 16:18:32 +01:00
<?php
2022-01-01 21:32:17 +01:00
/* for all functions, datetime parameter format is 'dd/mm/yy hh:mm:ss' */
2021-12-30 16:18:32 +01:00
function checkEvents()
{
global $events, $indexDevices, $devices;
//echo "===========> checking events" . EOL;
2022-01-01 21:32:17 +01:00
$exception = false;
2021-12-30 16:18:32 +01:00
foreach ($events as $key => $event)
{
2022-01-01 21:32:17 +01:00
if (!empty($event->startDatetime))
{
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(LOG_ERROR, "Error in event recurrence. event: " . $key);
}elseif($event->dateTimeEvent <= now())
{
if (!empty($event->exceptionInterval))
{
foreach($event->exceptionInterval as $key => $value)
{
if($datetime("now") >= $value->start and $datetime("now") <= $value->end)
{
$exception = true;
}elseif($datetime("now") >= $value->stop)
{
unset($event->exceptionInterval[$key]);
}
}
}
if ($exception === false)
{
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key);
}
}
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
2021-12-30 16:18:32 +01:00
{
2022-01-01 06:26:02 +01:00
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key);
2021-12-30 16:18:32 +01:00
//$event->published = now();
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
unset($events[$key]);
2021-12-30 16:18:32 +01:00
}
}
//print_r($events);
}
2022-01-01 21:32:17 +01:00
function setOneshotEvent(&$device, $datetime, $param, $value, $replace=false)
{
global $events;
$events[] = new event;
$key = key($events);
$events[$key]->dateTimeEvent = new dateTime($datetime);
$events[$key]->ieeeAddress = $device->ieeeAddress;
$events[$key]->topic = $device->topic;
$events[$key]->param = $param;
$events[$key]->value = $value;
$events[$key]->device = & $device;
}
function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDatetime, $hours, $minutes, $seconds, $days, $weeks, $months, $years)
{
global $events;
$string = "P";
if (!empty($years)) $string .= $days . "Y";
if (!empty($months)) $string .= $days . "M";
if (!empty($week)) $string .= $week . "W";
if (!empty($days)) $string .= $days . "D";
$string .= "T";
if (!empty($hours)) $string .= $days . "H";
if (!empty($months)) $string .= $days . "M";
if (!empty($seconds)) $string .= $days . "S";
$event = new event;
$event->recurrenceInterval = new DateInterval($string);
$event->startDatetime = new datetime($startDatetime);
$event->stopDatetime = new datetime($stopDatetime);
$event->ieee_address = $device->ieeeAddress;
$event->topic = $device->topic;
$event->device = & $device;
$event->param = $param;
$event->value = $value;
$event->dateTimeEvent = $event->startDatetime;
}
function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=false)
2021-12-30 16:18:32 +01:00
{
global $events;
$datetime = new dateTime();
//print_r($datetime);
switch($unit)
{
2022-01-01 06:26:02 +01:00
case "second":
2022-01-01 21:32:17 +01:00
$s = "S";
2021-12-30 16:18:32 +01:00
break;
2022-01-01 06:26:02 +01:00
case "minute":
2022-01-01 21:32:17 +01:00
$s = "M";
2021-12-30 16:18:32 +01:00
break;
case "hour":
2022-01-01 21:32:17 +01:00
$s = "H";
2021-12-30 16:18:32 +01:00
break;
case "day":
2022-01-01 21:32:17 +01:00
$s = "D";
2021-12-30 16:18:32 +01:00
break;
case "week":
2022-01-01 21:32:17 +01:00
$s = "W";
2021-12-30 16:18:32 +01:00
break;
case "month":
2022-01-01 21:32:17 +01:00
$s = "M";
2021-12-30 16:18:32 +01:00
break;
case "year":
2022-01-01 21:32:17 +01:00
$s = "Y";
2021-12-30 16:18:32 +01:00
break;
}
2022-01-01 21:32:17 +01:00
if (empty($s))
{
logger(LOG_ERROR, __("setDelay error: unit is empty"));
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
logger(LOG_ERROR, __("setDelay error: datetime->add"));
}
print_r($datetime);
2021-12-30 16:18:32 +01:00
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;
$events[$key]->device = & $device;
2021-12-30 16:18:32 +01:00
echo "new event";
}
2022-01-01 06:26:02 +01:00
function searchEvent($device, $param , $value)
2021-12-30 16:18:32 +01:00
{
global $events;
echo "searching event" . EOL;
//$keys = array_keys($events, $device->ieeeAddress);
//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;
//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)
{
//echo "==============================\nfound " . $key . "\n=================================" . EOL;
2021-12-30 16:18:32 +01:00
return $key;
}
}
2022-01-01 06:26:02 +01:00
return false;
2021-12-30 16:18:32 +01:00
}
function deleteEvent($eventKey)
{
global $events;
unset ($events[$eventKey]);
//echo "delete event " . $eventKey . EOL;
2021-12-30 16:18:32 +01:00
}
?>