95 lines
2.1 KiB
PHP
95 lines
2.1 KiB
PHP
<?php
|
|
|
|
function checkEvents()
|
|
{
|
|
global $events, $indexDevices, $devices;
|
|
echo "===========> checking events" . EOL;
|
|
foreach ($events as $key => $event)
|
|
{
|
|
if ($event->dateTimeEvent <= now())
|
|
{
|
|
echo "---->sending command" . EOL;
|
|
publish($devices[$event->topic][$indexDevices[$event->ieeeAddress]], array($event->param => $event->value), "set", $key);
|
|
//$event->published = now();
|
|
unset($key);
|
|
}
|
|
}
|
|
//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;
|
|
echo "new event";
|
|
}
|
|
|
|
function searchEvent($device, $param , $value)
|
|
{
|
|
global $events;
|
|
echo "searching event" . EOL;
|
|
//$keys = array_keys($events, $device->ieeeAddress);
|
|
echo "ieee_address =>" . $device->ieeeAddress . EOL;
|
|
print_r($events);
|
|
foreach($events as $key => $event)
|
|
{
|
|
//echo "Event : $event => $value" . EOL;
|
|
echo "===>";print_r($event); echo EOL;
|
|
if($event->topic == $device->topic and $event->param == $param and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress)
|
|
{
|
|
echo "==============================\nfound " . $key . "\n=================================" . EOL;
|
|
return $key;
|
|
}
|
|
}
|
|
}
|
|
|
|
function deleteEvent($eventKey)
|
|
{
|
|
global $events;
|
|
unset ($events[$eventKey]);
|
|
echo "delete event " . $eventKey . EOL;
|
|
}
|
|
|
|
|
|
?>
|