1
0
moha/events.php

99 lines
2.3 KiB
PHP
Raw Normal View History

2021-12-30 16:18:32 +01:00
<?php
function checkEvents()
{
global $events, $indexDevices, $devices;
//echo "===========> checking events" . EOL;
2021-12-30 16:18:32 +01:00
foreach ($events as $key => $event)
{
if (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
2021-12-30 16:18:32 +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();
//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;
$events[$key]->device = & $device;
2021-12-30 16:18:32 +01:00
echo "new event";
}
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);
//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;
}
}
}
function deleteEvent($eventKey)
{
global $events;
unset ($events[$eventKey]);
//echo "delete event " . $eventKey . EOL;
2021-12-30 16:18:32 +01:00
}
?>