1
0

added managing events

This commit is contained in:
daniel Tartavel 2022-01-01 21:32:17 +01:00
parent f2b24bc0fe
commit 260d220326
4 changed files with 100 additions and 29 deletions

View File

@ -8,11 +8,6 @@ function storeDB($db, $filepath)
fclose($fp); fclose($fp);
} }
function deviceTree(string $topic, array $fnTree, array &$device)
{
}
function mkDevicesDB($topic, $json, $group=false) function mkDevicesDB($topic, $json, $group=false)
{ {
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh; global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh;

View File

@ -1,12 +1,43 @@
<?php <?php
/* for all functions, datetime parameter format is 'dd/mm/yy hh:mm:ss' */
function checkEvents() function checkEvents()
{ {
global $events, $indexDevices, $devices; global $events, $indexDevices, $devices;
//echo "===========> checking events" . EOL; //echo "===========> checking events" . EOL;
$exception = false;
foreach ($events as $key => $event) foreach ($events as $key => $event)
{ {
if (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now()) if (!empty($event->startDatetime))
{
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(LOG_ERROR, "Error in event recurrence. event: " . $key);
}elseif($event->dateTimeEvent <= now())
{
if (!empty($event->exceptionInterval))
{
foreach($event->exceptionInterval as $key => $value)
{
if($datetime("now") >= $value->start and $datetime("now") <= $value->end)
{
$exception = true;
}elseif($datetime("now") >= $value->stop)
{
unset($event->exceptionInterval[$key]);
}
}
}
if ($exception === false)
{
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key);
}
}
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
{ {
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL; echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key); publish($event->device, array($event->param => $event->value), "set", $key);
@ -18,7 +49,46 @@ function checkEvents()
//print_r($events); //print_r($events);
} }
function setDelay(&$device, $delay, $unit="sec", $param, $value, $replace=false) function setOneshotEvent(&$device, $datetime, $param, $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 = $param;
$events[$key]->value = $value;
$events[$key]->device = & $device;
}
function setRecurrentEvent(&$device, $param, $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 = $param;
$event->value = $value;
$event->dateTimeEvent = $event->startDatetime;
}
function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=false)
{ {
global $events; global $events;
$datetime = new dateTime(); $datetime = new dateTime();
@ -26,29 +96,36 @@ function setDelay(&$device, $delay, $unit="sec", $param, $value, $replace=false)
switch($unit) switch($unit)
{ {
case "second": case "second":
$unit = "S"; $s = "S";
break; break;
case "minute": case "minute":
$unit = "M"; $s = "M";
break; break;
case "hour": case "hour":
$unit = "H"; $s = "H";
break; break;
case "day": case "day":
$unit = "D"; $s = "D";
break; break;
case "week": case "week":
$unit = "W"; $s = "W";
break; break;
case "month": case "month":
$unit = "M"; $s = "M";
break; break;
case "year": case "year":
$unit = "Y"; $s = "Y";
break; break;
} }
$datetime->add(new DateInterval('PT'. $delay . $unit)); if (empty($s))
//print_r($device); {
logger(LOG_ERROR, __("setDelay error: unit is empty"));
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
logger(LOG_ERROR, __("setDelay error: datetime->add"));
}
print_r($datetime);
if ($replace) if ($replace)
{ {
$eventKey = searchEvent($device, $param, $value); $eventKey = searchEvent($device, $param, $value);

View File

@ -4,7 +4,7 @@ class rdc_panneau_salon
// list of devices we are listening to // list of devices we are listening to
private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0"); private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0");
public $delay = 3; // amount of time in $timeunit public $delay = 3; // amount of time in $timeunit
public $timeUnit = "minutes"; // unit of time for delay, second, minute, day, week, month, year public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
function __construct() function __construct()
{ {

View File

@ -59,6 +59,12 @@ class device
} }
} }
class ranges
{
public $start; //datetime
public $end; //datetime
}
class event class event
{ {
public $ieeeAddress; public $ieeeAddress;
@ -67,18 +73,11 @@ class event
public $value; public $value;
public $device; public $device;
public $published; public $published;
public $dateTimeEvent; public $dateTimeEvent; // datetime : next occurence for recurrent event
public $recurrenceDay; public $startDatetime;
public $recurrenceMonth; public $stopDatetime;
public $recurrenceYear; public $recurrenceInterval; // interval : for recurrent event
public $recurrenceWeek; public $exceptionInterval; // array of object ranges
public $recurrenceHours;
public $recurrenceMinutes;
public $time;
public $day;
public $week;
public $month;
public $exceptionInterval;
} }
class interval class interval