Bug 246 - Manually execute filters with actions.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@2027 e3e1d417-86f3-4887-817a-d78f3d33393f
pull/27/merge
stan 2006-10-23 15:32:22 +00:00
parent 9654f3d927
commit 471c78c544
9 changed files with 153 additions and 97 deletions

View File

@ -35,7 +35,7 @@ use bytes;
# ==========================================================================
use constant DBG_ID => "zmfilter"; # Tag that appears in debug to identify source
use constant DBG_LEVEL => 0; # 0 is errors, warnings and info only, > 0 for debug
use constant DBG_LEVEL => 1; # 0 is errors, warnings and info only, > 0 for debug
use constant START_DELAY => 5; # How long to wait before starting
@ -109,13 +109,14 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $delay = ZM_FILTER_EXECUTE_INTERVAL;
my $event_id = 0;
my $filter_parm = "";
sub Usage
{
print( "
Usage: zmfilter.pl [-d <seconds>,--delay=<seconds>]
Usage: zmfilter.pl [-f <filter name>,--filter=<filter name>]
Parameters are :-
-d<seconds>, --delay=<seconds> - How long to delay between each check, default ".ZM_FILTER_EXECUTE_INTERVAL."
-f<filter name>, --filter=<filter name> - The name of a specific filter to run
");
exit( -1 );
}
@ -152,7 +153,7 @@ sub DateTimeToSQL
return( strftime( "%Y-%m-%d %H:%M:%S", localtime( $dt_val ) ) );
}
if ( !GetOptions( 'delay=i'=>\$delay ) )
if ( !GetOptions( 'filter=s'=>\$filter_parm ) )
{
Usage();
}
@ -161,11 +162,21 @@ chdir( EVENT_PATH );
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );
Info( "Scanning for events\n" );
if ( $filter_parm )
{
Info( "Scanning for events using filter '$filter_parm'\n" );
}
else
{
Info( "Scanning for events\n" );
}
sleep( START_DELAY );
if ( !$filter_parm )
{
sleep( START_DELAY );
}
my $filters = getFilters();
my $filters;
my $last_action = 0;
while( 1 )
@ -174,7 +185,7 @@ while( 1 )
{
Debug( "Reloading filters\n" );
$last_action = time();
$filters = getFilters();
$filters = getFilters( $filter_parm );
}
foreach my $filter ( @$filters )
@ -182,6 +193,8 @@ while( 1 )
checkFilter( $filter );
}
last if ( $filter_parm );
Debug( "Sleeping for $delay seconds\n" );
sleep( $delay );
}
@ -212,10 +225,29 @@ sub getDiskBlocks
sub getFilters
{
my $filter_name = shift;
my @filters;
my $sql = "select * from Filters where (AutoArchive = 1 or AutoVideo = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1 or AutoExecute = 1 or AutoDelete = 1) order by Name";
my $sql = "select * from Filters where";
if ( $filter_name )
{
$sql .= " Name = ? and";
}
else
{
$sql .= " Background = 1 and";
}
$sql .= " (AutoArchive = 1 or AutoVideo = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1 or AutoExecute = 1 or AutoDelete = 1) order by Name";
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or Fatal( "Can't execute '$sql': ".$sth->errstr() );
my $res;
if ( $filter_name )
{
$res = $sth->execute( $filter_name ) or Fatal( "Can't execute '$sql': ".$sth->errstr() );
}
else
{
$res = $sth->execute() or Fatal( "Can't execute '$sql': ".$sth->errstr() );
}
FILTER: while( my $filter_data = $sth->fetchrow_hashref() )
{
Debug( "Found filter '$filter_data->{Name}'\n" );

View File

@ -169,9 +169,15 @@ if ( !empty($action) )
}
elseif ( $action == "filter" )
{
if ( $execute )
{
$temp_filter_name = "_TempFilter".time();
}
if ( $filter_name || $new_filter_name )
{
if ( $new_filter_name )
if ( $temp_filter_name )
$filter_name = $temp_filter_name;
elseif ( $new_filter_name )
$filter_name = $new_filter_name;
$filter_query = array();
$filter_query['trms'] = $trms;

View File

@ -195,7 +195,7 @@ $jws = array(
'eventdetail' => array( 'w'=>400, 'h'=>220 ),
'events' => array( 'w'=>720, 'h'=>480 ),
'export' => array( 'w'=>400, 'h'=>340 ),
'filter' => array( 'w'=>620, 'h'=>250 ),
'filter' => array( 'w'=>620, 'h'=>360 ),
'filtersave' => array( 'w'=>560, 'h'=>220 ),
'frames' => array( 'w'=>500, 'h'=>300 ),
'function' => array( 'w'=>248, 'h'=>92 ),

View File

@ -1052,6 +1052,16 @@ function createImage( $monitor, $scale )
return( $status );
}
function executeFilter( $filter )
{
$command = ZM_PATH_BIN."/zmfilter.pl --filter ".$filter;
$result = exec( $command, $output, $status );
$result = mysql_query( "delete from Filters where Name like '_TempFilter%'" );
if ( !$result )
die( mysql_error() );
return( $status );
}
function reScale( $dimension, $dummy )
{
for ( $i = 1; $i < func_num_args(); $i++ )

View File

@ -117,7 +117,7 @@ $jws = array(
'eventdetail' => array( 'w'=>400, 'h'=>220 ),
'events' => array( 'w'=>720, 'h'=>480 ),
'export' => array( 'w'=>400, 'h'=>340 ),
'filter' => array( 'w'=>620, 'h'=>250 ),
'filter' => array( 'w'=>620, 'h'=>360 ),
'filtersave' => array( 'w'=>560, 'h'=>220 ),
'frames' => array( 'w'=>500, 'h'=>300 ),
'function' => array( 'w'=>248, 'h'=>92 ),

View File

@ -18,12 +18,17 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
if ( !canView( 'Events' ) )
if ( !canView( 'Events' ) || ($execute && !canEdit('Events')) )
{
$view = "error";
return;
}
if ( $execute )
{
executeFilter( $temp_filter_name );
}
$count_sql = "select count(E.Id) as EventCount from Monitors as M inner join Events as E on (M.Id = E.MonitorId) where";
$events_sql = "select E.Id,E.MonitorId,M.Name As MonitorName,M.Width,M.Height,M.DefaultScale,E.Name,E.Cause,E.StartTime,E.Length,E.Frames,E.AlarmFrames,E.TotScore,E.AvgScore,E.MaxScore,E.Archived,E.LearnState from Monitors as M inner join Events as E on (M.Id = E.MonitorId) where";
if ( $user['MonitorIds'] )

View File

@ -31,6 +31,8 @@ if ( !$result )
while ( $row = mysql_fetch_assoc( $result ) )
{
$filter_names[$row['Name']] = $row['Name'];
if ( $row['Background'] )
$filter_names[$row['Name']] .= "*";
if ( !empty($reload) && isset($filter_name) && $filter_name == $row['Name'] )
{
$filter_data = $row;
@ -38,28 +40,11 @@ while ( $row = mysql_fetch_assoc( $result ) )
}
mysql_free_result( $result );
$auto_str = "";
$background_str = "";
if ( isset($filter_data) )
{
$auto_text = array();
if ( $filter_data['AutoArchive'] )
$auto_text[] = $zmSlangAutoArchiveAbbr;
if ( $filter_data['AutoVideo'] )
$auto_text[] = $zmSlangAutoVideoAbbr;
if ( $filter_data['AutoUpload'] )
$auto_text[] = $zmSlangAutoUploadAbbr;
if ( $filter_data['AutoEmail'] )
$auto_text[] = $zmSlangAutoEmailAbbr;
if ( $filter_data['AutoMessage'] )
$auto_text[] = $zmSlangAutoMessageAbbr;
if ( $filter_data['AutoExecute'] )
$auto_text[] = $zmSlangAutoExecuteAbbr;
if ( $filter_data['AutoDelete'] )
$auto_text[] = $zmSlangAutoDeleteAbbr;
if ( count($auto_text) )
{
$auto_str = '['.join( ',', $auto_text ).']';
}
if ( $filter_data['Background'] )
$background_str = '['.strtolower($zmSlangBackground).']';
foreach( split( '&', $filter_data['Query'] ) as $filter_parm )
{
@ -211,14 +196,22 @@ function submitToFilter( form, reload )
}
function submitToEvents( form )
{
var Url = '<?= $PHP_SELF ?>';
var Name = 'zmEvents';
var Width = <?= $jws['events']['w'] ?>;
var Height = <?= $jws['events']['h'] ?>;
var Options = 'resizable,scrollbars,width='+Width+',height='+Height;
form.target = Name;
form.view.value = 'events';
form.action.value = '';
form.execute.value = 0;
form.submit();
}
function executeFilter( form )
{
var Name = 'zmEvents';
form.target = Name;
form.view.value = 'events';
form.action.value = 'filter';
form.execute.value = 1;
form.submit();
}
function saveFilter( form )
@ -266,6 +259,7 @@ function delTerm( form, line )
<input type="hidden" name="view" value="filter">
<input type="hidden" name="page" value="<?= $page ?>">
<input type="hidden" name="reload" value="0">
<input type="hidden" name="execute" value="0">
<input type="hidden" name="action" value="">
<input type="hidden" name="subaction" value="">
<input type="hidden" name="fid" value="">
@ -274,7 +268,7 @@ function delTerm( form, line )
<tr>
<td valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td align="left" class="text"><?= $zmSlangUseFilter ?>:&nbsp;<?php if ( count($filter_names) > 1 ) { echo buildSelect( $select_name, $filter_names, "submitToFilter( document.filter_form, 1 );" ); } else { ?><select class="form" disabled><option><?= $zmSlangNoSavedFilters ?></option></select><?php } ?>&nbsp;&nbsp;<?= $auto_str ?></td>
<td align="left" class="text"><?= $zmSlangUseFilter ?>:&nbsp;<?php if ( count($filter_names) > 1 ) { echo buildSelect( $select_name, $filter_names, "submitToFilter( document.filter_form, 1 );" ); } else { ?><select class="form" disabled><option><?= $zmSlangNoSavedFilters ?></option></select><?php } ?>&nbsp;&nbsp;<?= $background_str ?></td>
<?php if ( canEdit( 'Events' ) ) { ?>
<td align="right" class="text"><a href="javascript: saveFilter( document.filter_form );"><?= $zmSlangSave ?></a></td>
<?php } else { ?>
@ -352,7 +346,67 @@ else
<td colspan="4" class="text"><hr width="100%"></td>
</tr>
<tr>
<td colspan="4" align="right"><input type="button" value="<?= $zmSlangReset ?>" class="form" onClick="submitToFilter( document.filter_form, 1 );">&nbsp;&nbsp;<input type="button" value="<?= $zmSlangSubmit ?>" class="form" onClick="if ( validateForm( document.filter_form ) ) submitToEvents( document.filter_form, 1 );"></td>
<td colspan="4" class="text"><table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="left" class="text"><?= $zmSlangAutoArchiveEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_archive" value="1"<?php if ( $filter_data['AutoArchive'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
if ( ZM_OPT_MPEG != "no" )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoVideoEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_video" value="1"<?php if ( $filter_data['AutoVideo'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_UPLOAD )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoUploadEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_upload" value="1"<?php if ( $filter_data['AutoUpload'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_EMAIL )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoEmailEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_email" value="1"<?php if ( $filter_data['AutoEmail'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_MESSAGE )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoMessageEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_message" value="1"<?php if ( $filter_data['AutoMessage'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoExecuteEvents ?>:&nbsp;</td>
<td align="left" class="text"><input type="checkbox" name="auto_execute" value="1"<?php if ( $filter_data['AutoExecute'] ) { echo " checked"; } ?> class="form-noborder"></td>
<td align="left" class="text"><input type="text" name="auto_execute_cmd" value="<?= $filter_data['AutoExecuteCmd'] ?>" size="32" maxlength="255" class="form"></td>
</tr>
<tr>
<td align="left" class="text"><?= $zmSlangAutoDeleteEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_delete" value="1"<?php if ( $filter_data['AutoDelete'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr></table></td>
<tr>
<td colspan="4" class="text"><hr width="100%"></td>
</tr>
<tr>
<td colspan="4" align="right">
<input type="button" value="<?= $zmSlangSubmit ?>" class="form" onClick="if ( validateForm( document.filter_form ) ) submitToEvents( document.filter_form );">&nbsp;&nbsp;
<input type="button" value="<?= $zmSlangExecute ?>" class="form" onClick="if ( validateForm( document.filter_form ) ) executeFilter( document.filter_form );">&nbsp;&nbsp;
<input type="button" value="<?= $zmSlangReset ?>" class="form" onClick="submitToFilter( document.filter_form, 1 );">
</td>
</tr>
</table>
</td>

View File

@ -100,55 +100,8 @@ mysql_free_result( $result );
<td align="right" colspan="3" class="text">&nbsp;</td>
</tr>
<tr>
<td align="left" class="text"><?= $zmSlangAutoArchiveEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_archive" value="1"<?php if ( $filter_data['AutoArchive'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
if ( ZM_OPT_MPEG != "no" )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoVideoEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_video" value="1"<?php if ( $filter_data['AutoVideo'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_UPLOAD )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoUploadEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_upload" value="1"<?php if ( $filter_data['AutoUpload'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_EMAIL )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoEmailEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_email" value="1"<?php if ( $filter_data['AutoEmail'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
if ( ZM_OPT_MESSAGE )
{
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoMessageEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_message" value="1"<?php if ( $filter_data['AutoMessage'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<?php
}
?>
<tr>
<td align="left" class="text"><?= $zmSlangAutoExecuteEvents ?>:&nbsp;</td>
<td align="left" class="text"><input type="checkbox" name="auto_execute" value="1"<?php if ( $filter_data['AutoExecute'] ) { echo " checked"; } ?> class="form-noborder"></td>
<td align="left" class="text"><input type="text" name="auto_execute_cmd" value="<?= $filter_data['AutoExecuteCmd'] ?>" size="32" maxlength="255" class="form"></td>
</tr>
<tr>
<td align="left" class="text"><?= $zmSlangAutoDeleteEvents ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="auto_delete" value="1"<?php if ( $filter_data['AutoDelete'] ) { echo " checked"; } ?> class="form-noborder"></td>
<td align="left" class="text"><?= $zmSlangBackgroundFilter ?>:&nbsp;</td>
<td align="left" class="text" colspan="2"><input type="checkbox" name="background" value="1"<?php if ( $filter_data['Background'] ) { echo " checked"; } ?> class="form-noborder"></td>
</tr>
<tr>
<td align="right" colspan="3" class="text">&nbsp;</td>

View File

@ -115,23 +115,18 @@ $zmSlangAttrNotes = 'Notes';
$zmSlangAttrTime = 'Time';
$zmSlangAttrTotalScore = 'Total Score';
$zmSlangAttrWeekday = 'Weekday';
$zmSlangAutoArchiveAbbr = 'Archive';
$zmSlangAutoArchiveEvents = 'Automatically archive all matches';
$zmSlangAuto = 'Auto';
$zmSlangAutoDeleteAbbr = 'Delete';
$zmSlangAutoArchiveEvents = 'Automatically archive all matches';
$zmSlangAutoDeleteEvents = 'Automatically delete all matches';
$zmSlangAutoEmailAbbr = 'Email';
$zmSlangAutoEmailEvents = 'Automatically email details of all matches';
$zmSlangAutoExecuteAbbr = 'Execute';
$zmSlangAutoExecuteEvents = 'Automatically execute command on all matches';
$zmSlangAutoMessageAbbr = 'Message';
$zmSlangAutoMessageEvents = 'Automatically message details of all matches';
$zmSlangAutoStopTimeout = 'Auto Stop Timeout';
$zmSlangAutoUploadAbbr = 'Upload';
$zmSlangAutoUploadEvents = 'Automatically upload all matches';
$zmSlangAutoVideoAbbr = 'Video';
$zmSlangAutoVideoEvents = 'Automatically create video for all matches';
$zmSlangAvgBrScore = 'Avg.<br/>Score';
$zmSlangBackground = 'Background';
$zmSlangBackgroundFilter = 'Run filter in background';
$zmSlangBadAlarmFrameCount = 'Alarm frame count must be an integer of one or more';
$zmSlangBadAlarmMaxFPS = 'Alarm Maximum FPS must be a positive integer or floating point value';
$zmSlangBadChannel = 'Channel must be set to an integer of zero or more';
@ -273,6 +268,7 @@ $zmSlangEventName = 'Event Name';
$zmSlangEventPrefix = 'Event Prefix';
$zmSlangEvents = 'Events';
$zmSlangExclude = 'Exclude';
$zmSlangExecute = 'Execute';
$zmSlangExportDetails = 'Export Event Details';
$zmSlangExport = 'Export';
$zmSlangExportFailed = 'Export Failed';