189 lines
5.3 KiB
PHP
189 lines
5.3 KiB
PHP
<?php
|
|
logger(DEBUG,"Including events.php");
|
|
|
|
/* for all functions, datetime parameter format is 'dd/mm/yy hh:mm:ss' */
|
|
|
|
|
|
function checkEvents()
|
|
{
|
|
global $events, $indexDevices, $devices;
|
|
$exception = false;
|
|
foreach ($events as $key => $event)
|
|
{
|
|
$now = now();
|
|
if (!empty($event->startDatetime))
|
|
{
|
|
logger(DEBUG, _("StarDatetime is set"));
|
|
if($event->dateTimeEvent < $now)
|
|
{
|
|
logger(DEBUG, _("Event must be executed"));
|
|
if (!empty($event->exceptionInterval))
|
|
{
|
|
logger(DEBUG, _("Testing exceptions"));
|
|
foreach($event->exceptionInterval as $key => $value)
|
|
{
|
|
if($now > $value->start and $now < $value->end)
|
|
{
|
|
$exception = true;
|
|
}elseif($now > $value->stop)
|
|
{
|
|
unset($event->exceptionInterval[$key]);
|
|
}
|
|
}
|
|
}
|
|
if ($exception === false)
|
|
{
|
|
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)
|
|
{
|
|
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key);
|
|
}
|
|
}
|
|
}
|
|
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent < now())
|
|
{
|
|
logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName));
|
|
$mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key);
|
|
$event->published = $now;
|
|
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
|
|
unset($events[$key]);
|
|
}
|
|
}
|
|
//print_r($events);
|
|
}
|
|
|
|
function setOneshotEvent(&$device, $datetime, $property, $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 = $property;
|
|
$events[$key]->value = $value;
|
|
$events[$key]->device = & $device;
|
|
}
|
|
|
|
function setRecurrentEvent(&$device, $property, $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 = $property;
|
|
$event->value = $value;
|
|
$event->dateTimeEvent = $event->startDatetime;
|
|
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
|
|
{
|
|
logger(ERROR, _("Error in event recurrence. event: ") . $key);
|
|
|
|
}
|
|
}
|
|
|
|
function setDelay(&$device, $delay, $unit="second", $property, $value, $replace=false)
|
|
{
|
|
global $events;
|
|
$datetime = new dateTime();
|
|
//print_r($datetime);
|
|
switch($unit)
|
|
{
|
|
case "second":
|
|
$s = "S";
|
|
break;
|
|
case "minute":
|
|
$s = "M";
|
|
break;
|
|
case "hour":
|
|
$s = "H";
|
|
break;
|
|
case "day":
|
|
$s = "D";
|
|
break;
|
|
case "week":
|
|
$s = "W";
|
|
break;
|
|
case "month":
|
|
$s = "M";
|
|
break;
|
|
case "year":
|
|
$s = "Y";
|
|
break;
|
|
}
|
|
if (empty($s))
|
|
{
|
|
logger(ERROR, _("setDelay error: unit is empty"));
|
|
}
|
|
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
|
|
{
|
|
logger(ERROR, _("setDelay error: datetime->add"));
|
|
}
|
|
//print_r($datetime);
|
|
if ($replace)
|
|
{
|
|
$eventKey = searchEvent($device, $property, $value);
|
|
if ($eventKey !== false) deleteEvent($eventKey);
|
|
}
|
|
//$dt = $datetime->format("Y-m-d\TH:i:s\Z");
|
|
$events[] = new event;
|
|
$key = array_key_last($events);
|
|
$events[$key]->dateTimeEvent = $datetime;
|
|
$events[$key]->ieeeAddress = $device->ieeeAddress;
|
|
$events[$key]->topic = $device->topic;
|
|
$events[$key]->param = $property;
|
|
$events[$key]->value = $value;
|
|
$events[$key]->device = & $device;
|
|
logger (DEBUG, _('Setting new delay in $events[]'));
|
|
}
|
|
|
|
function searchEvent($device, $property , $value)
|
|
{
|
|
global $events;
|
|
logger(DEBUG, _("searching event for device %s, property %s and value %s"), $device->friendlyName, $property, bool2string($value));
|
|
foreach($events as $key => $event)
|
|
{
|
|
//echo "Event : $event => $value" . EOL;
|
|
//echo "===>";print_r($event); echo EOL;
|
|
if($event->topic == $device->topic and $event->param == $property and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress)
|
|
{
|
|
//echo "==============================\nfound " . $key . "\n=================================" . EOL;
|
|
return $key;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function deleteEvent($eventKey)
|
|
{
|
|
global $events;
|
|
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));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
?>
|