1
0
moha/events.php

396 lines
9.9 KiB
PHP
Raw Permalink Normal View History

2021-12-30 16:18:32 +01:00
<?php
logger(DEBUG, _("Including events.php"), __FILE__ . ":" . __LINE__);
2021-12-30 16:18:32 +01:00
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 $logLevel, $events;
logger(DEBUG, _("Checking events"), __FILE__ . ":" . __LINE__);
2022-01-23 09:46:06 +01:00
$oldLevel = $logLevel;
2022-04-23 02:00:52 +02:00
//$logLevel = DEBUG;
2022-01-01 21:32:17 +01:00
$exception = false;
2022-09-01 17:02:28 +02:00
if ($events === null)
{
$events = array();
2022-09-01 17:02:28 +02:00
}
//var_dump($events);
$now = now();
2022-09-01 17:02:28 +02:00
if (!empty($events))
2021-12-30 16:18:32 +01:00
{
2022-09-01 17:02:28 +02:00
foreach ($events as $key => &$event)
2022-01-01 21:32:17 +01:00
{
2022-09-01 17:02:28 +02:00
if($event->dateTimeEvent < $now)
{
2022-09-01 17:02:28 +02:00
logger(DEBUG, _("Executing event") . $key, __FILE__ . ":" . __LINE__);
if (is_callable($event->function))
{
logger(DEBUG, _("executing function") . $event->function[1], __FILE__ . ":" . __LINE__);
$event->function($event);
}elseif (is_object($event->device))
{
logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__);
publish(mktopic($event->device), array($event->param => $event->value), "set");
if ($event->method !== null) $event->device->properties[$event->param]["method"] = $event->method;
}else
{
logger(ERROR, _("Event is malformed") . $key, __FILE__ . ":" . __LINE__);
}
2022-07-31 22:31:51 +02:00
2022-09-01 17:02:28 +02:00
$event->published = $now;
if (!empty($event->recurrence))
{
nextOccurence($event, $key);
}else
{
logger(DEBUG, _("Unsetting event") . $key, __FILE__ . ":" . __LINE__);
unset($events[$key]);
}
}
}
}
}
2022-07-31 22:31:51 +02:00
function nextIntervalDate(&$event)
{
if (($event->dateTimeEvent = $event->dateTimeEvent->add($event->recurrence)) === false)
{
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $eventKey, __FILE__ . ":" . __LINE__);
return false;
}
}
function nextDate(&$event)
{
$isDate = false;
$date = new DateTime;
$year = $event->dateTimeEvent->format("Y");
$month = $event->dateTimeEvent->format("M");
$day = $event->dateTimeEvent->format("D");
$hour = $event->dateTimeEvent->format("H");
$minutes = $event->dateTimeEvent->format("I");
$seconds = $event->dateTimeEvent->format("S");
$temp = "";
for ($x = 0; $x < strlen($event->recurrence); $x++)
{
switch ($event->recurrence[$x])
2021-12-30 16:18:32 +01:00
{
case "P":
$isDate = true;
break;
case "T":
$isDate = false;
break;
case "Y":
$year = $temp;
$temp = 0;
break;
case "M":
if ($isdate)
{
$month = $temp;
}else
{
$minutes = $temp;
}
$temp = 0;
break;
case "W":
$temp = 0;
break;
case "H":
$hour = $temp;
$temp = 0;
case "S":
$seconds = $temp;
$temp = 0;
default:
$temp = $event->recurrence[$x] . $temp;
break;
}
}
if ($month != 0)
{
$year += 1;
}elseif ($day != 0)
{
$month += 1;
}elseif ($hour != 0)
{
$day = +1;
}elseif ($minutes != 0)
{
$hour = +1;
}
$event->dateTimeEvent->setDate(intval($year), intval($month), intval($day));
$event->dateTimeEvent->setTime(intval($hour), intval($minutes), intval($seconds));
}
2022-09-01 17:02:28 +02:00
function exceptionsTest(&$event, $date)
{
$exception = false;
logger(DEBUG, _("Testing exceptions"), __FILE__ . ":" . __LINE__);
if (!empty($event->exceptionInterval))
{
foreach($event->exceptionInterval as $key => $value)
{
if($now > $value->start and $now < $value->end)
{
$exception = true;
}elseif($now > $value->stop)
{
unset($event->exceptionInterval[$key]);
}
2021-12-30 16:18:32 +01:00
}
}
return $exception;
}
2022-09-01 17:02:28 +02:00
function nextOccurence(&$event, $eventKey)
{
$n = 0;
2022-09-01 17:02:28 +02:00
logger(DEBUG, _("Calculating next occurence of event #") . $event->key, __FILE__ . ":" . __LINE__);
do
{
if ($event->isInterval === true)
{
nextIntervalDate($event);
}else
{
nextDate($event);
}
if ($n++ > 1000)
{
logger(ERROR, _("infinite loop"), __FILE__ . ":" . __LINE__);
return 1;
}
2022-09-01 17:02:28 +02:00
}while (exceptionsTest($event, $event->dateTimeEvent) === true); // loop until the date is not comprise in exceptions dates
2021-12-30 16:18:32 +01:00
}
2022-04-23 02:00:52 +02:00
// datetime format : "yyyy-mm-dd hh:mm:ss"
2022-07-31 22:31:51 +02:00
function setOneshotEvent(device &$deviceObject, string $datetime, $property, $value, $function=null, $replace=false, $method=null)
2022-01-01 21:32:17 +01:00
{
global $events;
$events[] = new event;
2022-09-01 17:02:28 +02:00
$key = array_key_last($events);
$events[$key]->key = $key;
2022-01-01 21:32:17 +01:00
$events[$key]->dateTimeEvent = new dateTime($datetime);
2022-02-23 10:23:16 +01:00
$events[$key]->ieeeAddress = $deviceObject->ieeeAddress;
$events[$key]->topic = $deviceObject->topic;
2022-01-20 00:26:57 +01:00
$events[$key]->param = $property;
2022-01-01 21:32:17 +01:00
$events[$key]->value = $value;
2022-02-23 10:23:16 +01:00
$events[$key]->device = & $deviceObject;
2022-07-31 22:31:51 +02:00
$events[$key]->function = $function;
if ($method !==null) $events[$key]->method =$method;
2022-01-01 21:32:17 +01:00
}
2022-04-23 02:00:52 +02:00
// startDatetime and stopDatetime format : "yyyy-mm-dd hh:mm:ss"
2022-09-01 17:02:28 +02:00
function setRecurrentEvent(string $id, $function, string $ieeeAddress, string $property, $value, int $method=0 , string $startDatetime, string $stopDatetime, bool $isInterval, int $hours=0, int $minutes=0, int $seconds=0, int $days=0, int $weeks=0, int $months=0, int $years=0)
2022-01-01 21:32:17 +01:00
{
global $events, $indexDevices;
2022-09-01 17:02:28 +02:00
$string ="";
$stringTemp = "";
logger(INFO, _("Setting recurrent event"), __FILE__ . ":" . __LINE__);
2022-07-31 22:31:51 +02:00
2022-09-01 17:02:28 +02:00
if (!empty($years)) $stringTemp .= $years . "Y";
if (!empty($months)) $stringTemp .= $months . "M";
if (!empty($week)) $stringTemp .= $week . "W";
if (!empty($days)) $stringTemp .= $days . "D";
if (!empty($stringTemp)) $string = "P" . $stringTemp;
$stringTemp = "";
if (!empty($hours)) $stringTemp .= $hours . "H";
if (!empty($minutes)) $stringTemp .= $minutes . "M";
if (!empty($seconds)) $stringTemp .= $seconds . "S";
if (!empty($stringTemp)) $string .= "T" . $stringTemp;
2022-07-31 22:31:51 +02:00
2022-09-01 17:02:28 +02:00
logger(DEBUG, _("reccurrent event string : ") . $string, __FILE__ . ":" . __LINE__);
2022-07-31 22:31:51 +02:00
2022-09-01 17:02:28 +02:00
$key = searchEventByID($id);
if($key !== false)
{
deleteEvent($key);
}
2022-01-01 21:32:17 +01:00
$event = new event;
2022-09-01 17:02:28 +02:00
$event->id = $id;
// pb in recurrent event in case of date and not interval
if ($isInterval === true)
{
if (($event->dateTimeEvent->add($event->recurrence)) === false)
{
2022-09-01 17:02:28 +02:00
logger(ERROR, _("Error in event recurrence. event: ") . $id , __FILE__ . ":" . __LINE__);
return true;
}
$event->recurrence = new DateInterval($string);
$event->isInterval = true;
}else
{
$event->recurrence = $string;
$event->isInterval = false;
}
2022-07-31 22:31:51 +02:00
if (!empty($startDatetime))
{
$event->startDatetime = new datetime($startDatetime);
}else
{
$event->startDatetime = now();
2022-09-01 17:02:28 +02:00
logger(DEBUG, _("Datetime is ") . $event->startDatetime->format("Y-m-d H:i:s"), __FILE__ . ":" . __LINE__);
}
2022-07-31 22:31:51 +02:00
if (!empty($stopDatetime))
{
2022-09-01 17:02:28 +02:00
$event->stopDatetime = new datetime($stopDatetime);
}
$event->dateTimeEvent = $event->startDatetime;
2022-09-01 17:02:28 +02:00
if (!empty($ieeeAddress))
{
$event->ieee_address = $ieeeAddress;
$event->topic = $indexDevices[$deviceObject]->topic;
$event->device = &$indexDevices[$deviceObject];
$event->param = $property;
$event->value = $value;
}
2022-07-31 22:31:51 +02:00
if ($method !== null) $event->method = $method;
$events[] = $event;
2022-09-01 17:02:28 +02:00
$r = key($events);
$events[$r]->key = $r;
$events[$r]->dateTimeEvent = nextOccurence($events[$r], $r);
return $r;
2022-01-01 21:32:17 +01:00
}
2022-09-01 17:02:28 +02:00
function setDelay(device &$deviceObject, float $delay, string $unit, string $property, $value, $function, bool $replace=false, int $method=null)
2021-12-30 16:18:32 +01:00
{
2022-01-23 09:46:06 +01:00
global $events, $logLevel;
$oldLevel = $logLevel;
$logLevel = DEBUG;
2021-12-30 16:18:32 +01:00
$datetime = new dateTime();
2022-01-23 09:46:06 +01:00
strtolower($unit);
2021-12-30 16:18:32 +01:00
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))
{
2022-01-28 23:05:58 +01:00
logger(ERROR, _("setDelay error: unit is empty"), __FILE__ . ":" . __LINE__);
2022-01-01 21:32:17 +01:00
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
2022-01-28 23:05:58 +01:00
logger(ERROR, _("setDelay error: datetime->add"), __FILE__ . ":" . __LINE__);
2022-01-01 21:32:17 +01:00
}
2022-01-02 18:14:13 +01:00
//print_r($datetime);
2021-12-30 16:18:32 +01:00
if ($replace)
{
2022-02-23 10:23:16 +01:00
$eventKey = searchEvent($deviceObject, $property, $value);
2022-04-23 02:00:52 +02:00
if ($eventKey !== false) deleteEvent($eventKey);
2021-12-30 16:18:32 +01:00
}
//$dt = $datetime->format("Y-m-d\TH:i:s\Z");
$events[] = new event;
2022-01-20 00:26:57 +01:00
$key = array_key_last($events);
2021-12-30 16:18:32 +01:00
$events[$key]->dateTimeEvent = $datetime;
2022-02-23 10:23:16 +01:00
$events[$key]->ieeeAddress = $deviceObject->ieeeAddress;
$events[$key]->topic = $deviceObject->topic;
2022-01-20 00:26:57 +01:00
$events[$key]->param = $property;
2021-12-30 16:18:32 +01:00
$events[$key]->value = $value;
2022-02-23 10:23:16 +01:00
$events[$key]->device = & $deviceObject;
2022-07-31 22:31:51 +02:00
$events[$key]->function = $function;
if ($method !== null) $events[$key]->method = $method;
2022-01-28 23:05:58 +01:00
logger (DEBUG, _('Setting new delay in $events[]'), __FILE__ . ":" . __LINE__);
2022-02-23 10:23:16 +01:00
//print_r($events[$key]);
$logLevel = $oldLevel;
2021-12-30 16:18:32 +01:00
}
2022-09-09 16:53:49 +02:00
function removeEvent(device $deviceObject, string $property , $value)
{
global $events;
2022-03-04 22:30:16 +01:00
$eventKey = searchEvent($deviceObject, $property , $value);
if ($eventKey !== false)
{
deleteEvent($eventKey);
}
}
2022-03-04 22:30:16 +01:00
2022-08-02 23:25:59 +02:00
function searchEvent(device $deviceObject, string $property, $value, $function=null)
2021-12-30 16:18:32 +01:00
{
global $events;
2022-02-23 10:23:16 +01:00
logger(DEBUG, sprintf(_("searching event for device %s, property %s and value %s"), $deviceObject->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__);
2021-12-30 16:18:32 +01:00
foreach($events as $key => $event)
{
//echo "Event : $event => $value" . EOL;
//echo "===>";print_r($event); echo EOL;
2022-02-23 10:23:16 +01:00
if($event->topic == $deviceObject->topic and $event->param == $property and $event->value == $value and $event->ieeeAddress == $deviceObject->ieeeAddress)
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
}
2022-09-01 17:02:28 +02:00
function searchEventByID($id)
{
global $events;
logger(DEBUG, sprintf(_("searching event by ID %s"), $id), __FILE__ . ":" . __LINE__);
foreach($events as $key => $event)
{
if ($event->id == $id)
{
return $key;
}
}
return false;
}
// warning delete event does not manage method of the device (IDLE, )
2022-04-23 02:00:52 +02:00
function deleteEvent(int $eventKey)
2021-12-30 16:18:32 +01:00
{
global $events;
2022-01-20 00:26:57 +01:00
if ($eventKey !== false)
{
2022-01-28 23:05:58 +01:00
logger(INFO, _("delete event key =") . $eventKey, __FILE__ . ":" . __LINE__);
if (array_key_exists($eventKey, $events))
{
unset ($events[$eventKey]);
}else
{
logger(ERROR, _("event key does not exists: ") . $eventKey, __FILE__ . ":" . __LINE__);
}
}
2021-12-30 16:18:32 +01:00
}
?>