1
0
moha/events.php

203 lines
5.6 KiB
PHP
Raw Normal View History

2021-12-30 16:18:32 +01:00
<?php
2022-01-17 00:18:50 +01:00
logger(DEBUG,"Including events.php");
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()
{
2022-01-23 09:46:06 +01:00
global $logLevel, $events, $indexDevices, $devices;
$oldLevel = $logLevel;
$loglevel = DEBUG;
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-20 00:26:57 +01:00
$now = now();
2022-01-01 21:32:17 +01:00
if (!empty($event->startDatetime))
{
2022-01-20 00:26:57 +01:00
logger(DEBUG, _("StarDatetime is set"));
if($event->dateTimeEvent < $now)
2022-01-01 21:32:17 +01:00
{
2022-01-20 00:26:57 +01:00
logger(DEBUG, _("Event must be executed"));
2022-01-01 21:32:17 +01:00
if (!empty($event->exceptionInterval))
{
2022-01-20 00:26:57 +01:00
logger(DEBUG, _("Testing exceptions"));
2022-01-01 21:32:17 +01:00
foreach($event->exceptionInterval as $key => $value)
{
2022-01-20 00:26:57 +01:00
if($now > $value->start and $now < $value->end)
2022-01-01 21:32:17 +01:00
{
$exception = true;
2022-01-20 00:26:57 +01:00
}elseif($now > $value->stop)
2022-01-01 21:32:17 +01:00
{
unset($event->exceptionInterval[$key]);
}
}
}
if ($exception === false)
{
2022-01-20 00:26:57 +01:00
logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName));
publish(mktopic($event->device), array($event->param => $event->value), "set", $key);
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
2022-01-20 00:26:57 +01:00
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key);
}
2022-01-01 21:32:17 +01:00
}
}
2022-01-20 00:26:57 +01:00
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent < now())
2021-12-30 16:18:32 +01:00
{
2022-01-20 00:26:57 +01:00
logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName));
2022-01-19 00:22:34 +01:00
$mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key);
2022-01-20 00:26:57 +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-23 09:46:06 +01:00
$logLevel = $oldLevel;
2021-12-30 16:18:32 +01:00
}
2022-01-20 00:26:57 +01:00
function setOneshotEvent(&$device, $datetime, $property, $value, $replace=false)
2022-01-01 21:32:17 +01:00
{
global $events;
$events[] = new event;
$key = key($events);
$events[$key]->dateTimeEvent = new dateTime($datetime);
$events[$key]->ieeeAddress = $device->ieeeAddress;
$events[$key]->topic = $device->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;
$events[$key]->device = & $device;
}
2022-01-20 00:26:57 +01:00
function setRecurrentEvent(&$device, $property, $value, $startDatetime, $stopDatetime, $hours, $minutes, $seconds, $days, $weeks, $months, $years)
2022-01-01 21:32:17 +01:00
{
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;
2022-01-20 00:26:57 +01:00
$event->param = $property;
$event->value = $value;
$event->dateTimeEvent = $event->startDatetime;
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
2022-01-02 18:14:13 +01:00
logger(ERROR, _("Error in event recurrence. event: ") . $key);
}
2022-01-01 21:32:17 +01:00
}
2022-01-20 00:26:57 +01:00
function setDelay(&$device, $delay, $unit="second", $property, $value, $replace=false)
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-02 18:14:13 +01:00
logger(ERROR, _("setDelay error: unit is empty"));
2022-01-01 21:32:17 +01:00
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
2022-01-02 18:14:13 +01:00
logger(ERROR, _("setDelay error: datetime->add"));
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-01-20 00:26:57 +01:00
$eventKey = searchEvent($device, $property, $value);
2021-12-30 16:18:32 +01:00
if ($eventKey !== false) deleteEvent($eventKey);
}
//$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;
$events[$key]->ieeeAddress = $device->ieeeAddress;
$events[$key]->topic = $device->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;
$events[$key]->device = & $device;
2022-01-20 00:26:57 +01:00
logger (DEBUG, _('Setting new delay in $events[]'));
2022-01-23 09:46:06 +01:00
print_r($events[$key]);
$loglevel = $oldLevel;
2021-12-30 16:18:32 +01:00
}
function removeEvent($device, $property , $value)
{
if (($eventKey = searchEvent($device, $property , $value)) === true)
{
deleteEvent($eventKey);
}
}
2022-01-20 00:26:57 +01:00
function searchEvent($device, $property , $value)
2021-12-30 16:18:32 +01:00
{
global $events;
2022-01-20 00:26:57 +01:00
logger(DEBUG, _("searching event for device %s, property %s and value %s"), $device->friendlyName, $property, bool2string($value));
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-01-20 00:26:57 +01:00
if($event->topic == $device->topic and $event->param == $property and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress)
2021-12-30 16:18:32 +01:00
{
//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;
2022-01-20 00:26:57 +01:00
if ($eventKey !== false)
{
unset ($events[$eventKey]);
logger(INFO, _("delete event key =") . $eventKey);
}else
{
//try to delete an flase event
logger(WARNING, _("Try to delete event with key = ") . var_dump($eventKey));
}
2021-12-30 16:18:32 +01:00
}
?>