2021-12-30 16:18:32 +01:00
< ? php
2022-02-12 15:23:58 +01:00
logger ( DEBUG , _ ( " Including events.php " ), __FILE__ . " : " . __LINE__ );
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-02-12 15:23:58 +01:00
global $logLevel , $events ;
2022-07-10 19:33:19 +02:00
logger ( DEBUG , _ ( " Checking events " ), __FILE__ . " : " . __LINE__ );
2022-01-23 09:46:06 +01:00
$oldLevel = $logLevel ;
2022-04-23 02:00:52 +02:00
//$logLevel = DEBUG;
2022-01-01 21:32:17 +01:00
$exception = false ;
2022-07-10 19:33:19 +02:00
/* if ( $events === null )
2022-03-28 00:40:34 +02:00
{
$events = array ();
2022-07-10 19:33:19 +02:00
} */
2022-03-13 22:33:26 +01:00
foreach ( $events as $key => & $event )
2021-12-30 16:18:32 +01:00
{
2022-01-20 00:26:57 +01:00
$now = now ();
2022-01-01 21:32:17 +01:00
if ( ! empty ( $event -> startDatetime ))
{
2022-03-15 22:49:13 +01:00
logger ( DEBUG , _ ( " StartDatetime is set " ), __FILE__ . " : " . __LINE__ );
2022-01-20 00:26:57 +01:00
if ( $event -> dateTimeEvent < $now )
2022-01-01 21:32:17 +01:00
{
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " Event must be executed " ), __FILE__ . " : " . __LINE__ );
2022-07-31 22:31:51 +02:00
2022-07-10 19:33:19 +02:00
if ( ! empty ( $event -> exceptionInterval ) and $event -> isInterval == true )
2022-01-01 21:32:17 +01:00
{
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " Testing exceptions " ), __FILE__ . " : " . __LINE__ );
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 ]);
}
}
}
2022-07-31 22:31:51 +02:00
2022-01-01 21:32:17 +01:00
if ( $exception === false )
{
2022-07-10 19:33:19 +02:00
if ( is_callable ( $event -> function ))
2022-01-02 12:14:30 +01:00
{
2022-07-10 19:33:19 +02:00
logger ( DEBUG , _ ( " executing function " ) . $event -> function [ 1 ], __FILE__ . " : " . __LINE__ );
$event -> function ();
$event -> published = $now ;
2022-03-15 22:49:13 +01:00
} else
{
logger ( DEBUG , sprintf ( _ ( " sending command set %s => %s for %s " ), $event -> param , bool2string ( $event -> value ), $event -> device -> friendlyName ), __FILE__ . " : " . __LINE__ );
publish ( mktopic ( $event -> device ), array ( $event -> param => $event -> value ), " set " );
2022-07-10 19:33:19 +02:00
$event -> published = $now ;
}
if ( $event -> method !== null ) $event -> device -> properties [ $event -> param ][ " method " ] = $event -> method ;
if (( $event -> dateTimeEvent -> add ( $event -> recurrenceInterval )) === false )
{
logger ( ERROR , _ ( " Error in adding interval to event recurrence. event: " ) . $key , __FILE__ . " : " . __LINE__ );
2022-01-02 12:14:30 +01:00
}
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-28 23:05:58 +01:00
logger ( DEBUG , sprintf ( _ ( " Sending command set %s => %s to %s " ), $event -> param , bool2string ( $event -> value ), $event -> device -> friendlyName ), __FILE__ . " : " . __LINE__ );
2022-04-07 01:44:17 +02:00
$mid = publish ( mktopic ( $event -> device ), array ( $event -> param => $event -> value ), " set " ); //, $key);null
2022-01-20 00:26:57 +01:00
$event -> published = $now ;
2022-04-07 01:44:17 +02:00
if ( $event -> method !== null )
{
$event -> device -> properties [ $event -> param ][ " method " ] = $event -> method ;
}
2021-12-31 14:34:20 +01:00
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
unset ( $events [ $key ]);
2021-12-30 16:18:32 +01:00
}
}
//print_r($events);
2022-04-23 02:00:52 +02:00
//$logLevel = $oldLevel;
2021-12-30 16:18:32 +01:00
}
2022-04-23 02:00:52 +02:00
// datetime format : "yyyy-mm-dd hh:mm:ss"
2022-07-31 22:31:51 +02:00
function setOneshotEvent ( device & $deviceObject , string $datetime , $property , $value , $function = null , $replace = false , $method = null )
2022-01-01 21:32:17 +01:00
{
global $events ;
$events [] = new event ;
$key = key ( $events );
$events [ $key ] -> dateTimeEvent = new dateTime ( $datetime );
2022-02-23 10:23:16 +01:00
$events [ $key ] -> ieeeAddress = $deviceObject -> ieeeAddress ;
$events [ $key ] -> topic = $deviceObject -> 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 ;
2022-02-23 10:23:16 +01:00
$events [ $key ] -> device = & $deviceObject ;
2022-07-31 22:31:51 +02:00
$events [ $key ] -> function = $function ;
2022-04-07 01:44:17 +02:00
if ( $method !== null ) $events [ $key ] -> method = $method ;
2022-01-01 21:32:17 +01:00
}
2022-04-23 02:00:52 +02:00
// startDatetime and stopDatetime format : "yyyy-mm-dd hh:mm:ss"
2022-07-10 19:33:19 +02:00
function setRecurrentEvent ( $function , $ieeeAddress , string $property , $value , bool $isInterval , string $startDatetime , string $stopDatetime , int $hours , int $minutes , int $seconds , int $days , int $weeks , int $months , int $years , int $method = null )
2022-01-01 21:32:17 +01:00
{
2022-07-10 19:33:19 +02:00
global $events , $indexDevices ;
logger ( INFO , _ ( " Setting recurrent event " ), __FILE__ . " : " . __LINE__ );
2022-07-31 22:31:51 +02:00
2022-01-01 21:32:17 +01:00
$string = " P " ;
2022-04-23 02:00:52 +02:00
if ( ! empty ( $years )) $string .= $years . " Y " ;
if ( ! empty ( $months )) $string .= $months . " M " ;
2022-01-01 21:32:17 +01:00
if ( ! empty ( $week )) $string .= $week . " W " ;
if ( ! empty ( $days )) $string .= $days . " D " ;
$string .= " T " ;
2022-04-23 02:00:52 +02:00
if ( ! empty ( $hours )) $string .= $hours . " H " ;
if ( ! empty ( $months )) $string .= $months . " M " ;
if ( ! empty ( $seconds )) $string .= $seconds . " S " ;
2022-07-10 19:33:19 +02:00
logger ( DEBUG , _ ( " reccurrent event string : " ) . $string , __FILE__ . " : " . __LINE__ );
2022-07-31 22:31:51 +02:00
2022-01-01 21:32:17 +01:00
$event = new event ;
2022-07-10 19:33:19 +02:00
// pb in recurrent event in case of date and not interval
if ( $isInterval === true )
2022-01-02 12:14:30 +01:00
{
2022-07-10 19:33:19 +02:00
$event -> recurrenceInterval = new DateInterval ( $string );
} else
{
$event -> recurrenceInterval = 0 ;
}
2022-07-31 22:31:51 +02:00
2022-07-10 19:33:19 +02:00
if ( ! empty ( $startDatetime ))
{
$event -> startDatetime = new datetime ( $startDatetime );
} else
{
$event -> startDatetime = now ();
}
$event -> dateTimeEvent = $event -> startDatetime ;
2022-07-31 22:31:51 +02:00
2022-07-10 19:33:19 +02:00
if ( ! empty ( $stopDatetime ))
{
$event -> stopDatetime = new datetime ( $stopDatetime );
}
if ( ! empty ( $ieeeAddress ))
{
$event -> ieee_address = $ieeeAddress ;
$event -> topic = $indexDevices [ $deviceObject ] -> topic ;
$event -> device = & $indexDevices [ $deviceObject ];
$event -> param = $property ;
$event -> value = $value ;
}
2022-07-31 22:31:51 +02:00
2022-07-10 19:33:19 +02:00
if ( $event -> recurrenceInterval != 0 )
{
if (( $event -> dateTimeEvent -> add ( $event -> recurrenceInterval )) === false )
{
logger ( ERROR , _ ( " Error in event recurrence. event: " ) . $key , __FILE__ . " : " . __LINE__ );
return 1 ;
}
2022-01-02 12:14:30 +01:00
}
2022-04-07 01:44:17 +02:00
if ( $method !== null ) $event -> method = $method ;
2022-07-10 19:33:19 +02:00
return 0 ;
2022-01-01 21:32:17 +01:00
}
2022-07-31 22:31:51 +02:00
function setDelay ( device & $deviceObject , float $delay , string $unit , string $property , $value , $function , bool $replace = false , int $method = null )
2021-12-30 16:18:32 +01:00
{
2022-01-23 09:46:06 +01:00
global $events , $logLevel ;
$oldLevel = $logLevel ;
2022-02-25 20:42:33 +01:00
$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-28 23:05:58 +01:00
logger ( ERROR , _ ( " setDelay error: unit is empty " ), __FILE__ . " : " . __LINE__ );
2022-01-01 21:32:17 +01:00
}
if (( $datetime -> add ( new DateInterval ( 'PT' . $delay . $s ))) === false )
{
2022-01-28 23:05:58 +01:00
logger ( ERROR , _ ( " setDelay error: datetime->add " ), __FILE__ . " : " . __LINE__ );
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-02-23 10:23:16 +01:00
$eventKey = searchEvent ( $deviceObject , $property , $value );
2022-04-23 02:00:52 +02:00
if ( $eventKey !== false ) deleteEvent ( $eventKey );
2021-12-30 16:18:32 +01:00
}
//$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 ;
2022-02-23 10:23:16 +01:00
$events [ $key ] -> ieeeAddress = $deviceObject -> ieeeAddress ;
$events [ $key ] -> topic = $deviceObject -> 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 ;
2022-02-23 10:23:16 +01:00
$events [ $key ] -> device = & $deviceObject ;
2022-07-31 22:31:51 +02:00
$events [ $key ] -> function = $function ;
2022-04-07 01:44:17 +02:00
if ( $method !== null ) $events [ $key ] -> method = $method ;
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( 'Setting new delay in $events[]' ), __FILE__ . " : " . __LINE__ );
2022-02-23 10:23:16 +01:00
//print_r($events[$key]);
2022-02-25 20:42:33 +01:00
$logLevel = $oldLevel ;
2021-12-30 16:18:32 +01:00
}
2022-05-05 21:28:26 +02:00
function removeEvent ( device $deviceObject , string $property , $value , int $method = IDLE )
2022-01-27 18:41:16 +01:00
{
2022-04-07 01:44:17 +02:00
global $events ;
2022-03-04 22:30:16 +01:00
$eventKey = searchEvent ( $deviceObject , $property , $value );
if ( $eventKey !== false )
2022-01-27 18:41:16 +01:00
{
2022-04-07 01:44:17 +02:00
if ( $method !== null ) $events [ $eventKey ] -> device -> method = $method ;
2022-01-27 18:41:16 +01:00
deleteEvent ( $eventKey );
}
}
2022-03-04 22:30:16 +01:00
2022-07-31 22:31:51 +02:00
function searchEvent ( device $deviceObject , string $property , $function , $value )
2021-12-30 16:18:32 +01:00
{
global $events ;
2022-02-23 10:23:16 +01:00
logger ( DEBUG , sprintf ( _ ( " searching event for device %s, property %s and value %s " ), $deviceObject -> friendlyName , $property , bool2string ( $value )), __FILE__ . " : " . __LINE__ );
2021-12-30 16:18:32 +01:00
foreach ( $events as $key => $event )
{
//echo "Event : $event => $value" . EOL;
2021-12-31 14:34:20 +01:00
//echo "===>";print_r($event); echo EOL;
2022-02-23 10:23:16 +01:00
if ( $event -> topic == $deviceObject -> topic and $event -> param == $property and $event -> value == $value and $event -> ieeeAddress == $deviceObject -> ieeeAddress )
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
}
2022-04-07 01:44:17 +02:00
// warning delete event does not manage method of the device (IDLE, )
2022-04-23 02:00:52 +02:00
function deleteEvent ( int $eventKey )
2021-12-30 16:18:32 +01:00
{
global $events ;
2022-01-20 00:26:57 +01:00
if ( $eventKey !== false )
2022-01-03 21:11:52 +01:00
{
2022-01-28 23:05:58 +01:00
logger ( INFO , _ ( " delete event key = " ) . $eventKey , __FILE__ . " : " . __LINE__ );
2022-04-07 01:44:17 +02:00
if ( array_key_exists ( $eventKey , $events ))
{
unset ( $events [ $eventKey ]);
} else
{
logger ( ERROR , _ ( " event key does not exists: " ) . $eventKey , __FILE__ . " : " . __LINE__ );
}
2022-01-03 21:11:52 +01:00
}
2021-12-30 16:18:32 +01:00
}
?>