zmcamtool.pl: readability

This commit is contained in:
Dmitry Smirnov 2015-04-11 00:34:46 +10:00
parent 90fa57788a
commit 53907eeba1
1 changed files with 57 additions and 31 deletions

View File

@ -155,8 +155,10 @@ sub selectQuery
my $sql = shift; my $sql = shift;
my $monitorid = shift; my $monitorid = shift;
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql )
my $res = $sth->execute($monitorid) or die( "Can't execute: ".$sth->errstr() ); or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($monitorid)
or die( "Can't execute: ".$sth->errstr() );
my @data = $sth->fetchrow_array(); my @data = $sth->fetchrow_array();
$sth->finish(); $sth->finish();
@ -169,8 +171,10 @@ sub runQuery
{ {
my $dbh = shift; my $dbh = shift;
my $sql = shift; my $sql = shift;
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql )
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute()
or die( "Can't execute: ".$sth->errstr() );
$sth->finish(); $sth->finish();
return $res; return $res;
@ -183,10 +187,13 @@ sub insertQuery
my $tablename = shift; my $tablename = shift;
my @data = @_; my @data = @_;
my $sql = "insert into $tablename values (NULL,".(join ", ", ("?") x @data).")"; # Add "?" for each array element my $sql = "INSERT INTO $tablename VALUES (NULL,"
.(join ", ", ("?") x @data).")"; # Add "?" for each array element
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql )
my $res = $sth->execute(@data) or die( "Can't execute: ".$sth->errstr() ); or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute(@data)
or die( "Can't execute: ".$sth->errstr() );
$sth->finish(); $sth->finish();
return $res; return $res;
@ -199,9 +206,11 @@ sub deleteQuery
my $sqltable = shift; my $sqltable = shift;
my $sqlname = shift; my $sqlname = shift;
my $sql = "delete from $sqltable where Name = ?"; my $sql = "DELETE FROM $sqltable WHERE Name = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql )
my $res = $sth->execute($sqlname) or die( "Can't execute: ".$sth->errstr() ); or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($sqlname)
or die( "Can't execute: ".$sth->errstr() );
$sth->finish(); $sth->finish();
return $res; return $res;
@ -215,9 +224,11 @@ sub checkExists
my $sqlname = shift; my $sqlname = shift;
my $result = 0; my $result = 0;
my $sql = "select count(*) from $sqltable where Name = ?"; my $sql = "SELECT count(*) FROM $sqltable WHERE Name = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql )
my $res = $sth->execute($sqlname) or die( "Can't execute: ".$sth->errstr() ); or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($sqlname)
or die( "Can't execute: ".$sth->errstr() );
my $rows = $sth->fetchrow_arrayref(); my $rows = $sth->fetchrow_arrayref();
$sth->finish(); $sth->finish();
@ -247,7 +258,8 @@ sub importsql
$sqlfile = $Config{ZM_PATH_DATA}.'/db/zm_create.sql'; $sqlfile = $Config{ZM_PATH_DATA}.'/db/zm_create.sql';
} }
open(my $SQLFILE,"<",$sqlfile) or die( "Can't Open file: $!\n" ); open(my $SQLFILE,"<",$sqlfile)
or die( "Can't Open file: $!\n" );
# Find and extract ptz control and monitor preset records # Find and extract ptz control and monitor preset records
while (<$SQLFILE>) { while (<$SQLFILE>) {
@ -264,21 +276,24 @@ sub importsql
die( "Error: No relevant data found in $sqlfile.\n" ); die( "Error: No relevant data found in $sqlfile.\n" );
} }
# Now that we've got what we were looking for, compare to what is already in the dB # Now that we've got what we were looking for,
# compare to what is already in the dB
my $dbh = zmDbConnect(); my $dbh = zmDbConnect();
foreach (keys %controls) { foreach (keys %controls) {
if (!checkExists($dbh,"Controls",$_)) { if (!checkExists($dbh,"Controls",$_)) {
# No existing Control was found. Add new control to dB. # No existing Control was found. Add new control to dB.
runQuery($dbh,$controls{$_}); runQuery($dbh,$controls{$_});
push @newcontrols, $_; push @newcontrols, $_;
} elsif ($overwrite) { } elsif ($overwrite) {
# An existing Control was found and the overwrite flag is set. Overwrite the control. # An existing Control was found and the overwrite flag is set.
# Overwrite the control.
deleteQuery($dbh,"Controls",$_); deleteQuery($dbh,"Controls",$_);
runQuery($dbh,$controls{$_}); runQuery($dbh,$controls{$_});
push @overwritecontrols, $_; push @overwritecontrols, $_;
} else { } else {
# An existing Control was found and the overwrite flag was not set. Do nothing. # An existing Control was found and the overwrite flag was not set.
# Do nothing.
push @skippedcontrols, $_; push @skippedcontrols, $_;
} }
} }
@ -289,34 +304,42 @@ sub importsql
runQuery($dbh,$monitorpresets{$_}); runQuery($dbh,$monitorpresets{$_});
push @newpresets, $_; push @newpresets, $_;
} elsif ($overwrite) { } elsif ($overwrite) {
# An existing MonitorPreset was found and the overwrite flag is set. Overwrite the MonitorPreset. # An existing MonitorPreset was found and the overwrite flag is set.
# Overwrite the MonitorPreset.
deleteQuery($dbh,"MonitorPresets",$_); deleteQuery($dbh,"MonitorPresets",$_);
runQuery($dbh,$monitorpresets{$_}); runQuery($dbh,$monitorpresets{$_});
push @overwritepresets, $_; push @overwritepresets, $_;
} else { } else {
# An existing MonitorPreset was found and the overwrite flag was not set. Do nothing. # An existing MonitorPreset was found and the overwrite flag was
# not set. Do nothing.
push @skippedpresets, $_; push @skippedpresets, $_;
} }
} }
if (@newcontrols) { if (@newcontrols) {
print "Number of ptz camera controls added: ".scalar(@newcontrols)."\n"; print "Number of ptz camera controls added: "
.scalar(@newcontrols)."\n";
} }
if (@overwritecontrols) { if (@overwritecontrols) {
print "Number of existing ptz camera controls overwritten: ".scalar(@overwritecontrols)."\n"; print "Number of existing ptz camera controls overwritten: "
.scalar(@overwritecontrols)."\n";
} }
if (@skippedcontrols) { if (@skippedcontrols) {
print "Number of existing ptz camera controls skipped: ".scalar(@skippedcontrols)."\n"; print "Number of existing ptz camera controls skipped: "
.scalar(@skippedcontrols)."\n";
} }
if (@newpresets) { if (@newpresets) {
print "Number of monitor presets added: ".scalar(@newpresets)."\n"; print "Number of monitor presets added: "
.scalar(@newpresets)."\n";
} }
if (@overwritepresets) { if (@overwritepresets) {
print "Number of existing monitor presets overwritten: ".scalar(@overwritepresets)."\n"; print "Number of existing monitor presets overwritten: "
.scalar(@overwritepresets)."\n";
} }
if (@skippedpresets) { if (@skippedpresets) {
print "Number of existing presets skipped: ".scalar(@skippedpresets)."\n"; print "Number of existing presets skipped: "
.scalar(@skippedpresets)."\n";
} }
} }
@ -361,7 +384,7 @@ sub toPreset
my $monitorid = $ARGV[0]; my $monitorid = $ARGV[0];
# Grap the following fields from the Monitors table # Grap the following fields from the Monitors table
my $sql = "select my $sql = "SELECT
Name, Name,
Type, Type,
Device, Device,
@ -383,14 +406,15 @@ sub toPreset
ControlAddress, ControlAddress,
DefaultRate, DefaultRate,
DefaultScale DefaultScale
from Monitors where Id = ?"; FROM Monitors WHERE Id = ?";
my @data = selectQuery($dbh,$sql,$monitorid); my @data = selectQuery($dbh,$sql,$monitorid);
if (!@data) { if (!@data) {
die( "Error: Monitor Id $monitorid does not appear to exist in the database.\n" ); die( "Error: Monitor Id $monitorid does not appear to exist in the database.\n" );
} }
# Attempt to search for and replace system specific values such as ip addresses, ports, usernames, etc. with generic placeholders # Attempt to search for and replace system specific values such as
# ip addresses, ports, usernames, etc. with generic placeholders
if (!$noregex) { if (!$noregex) {
foreach (@data) { foreach (@data) {
s/\b(?:\d{1,3}\.){3}\d{1,3}\b/<ip-address>/; # ip address s/\b(?:\d{1,3}\.){3}\d{1,3}\b/<ip-address>/; # ip address
@ -408,12 +432,14 @@ sub toPreset
print "Adding new preset: $data[0]\n"; print "Adding new preset: $data[0]\n";
insertQuery($dbh,"MonitorPresets",@data); insertQuery($dbh,"MonitorPresets",@data);
} elsif ($overwrite) { } elsif ($overwrite) {
# An existing Control was found and the overwrite flag is set. Overwrite the control. # An existing Control was found and the overwrite flag is set.
# Overwrite the control.
print "Existing preset $data[0] detected.\nOverwriting...\n"; print "Existing preset $data[0] detected.\nOverwriting...\n";
deleteQuery($dbh,"MonitorPresets",$data[0]); deleteQuery($dbh,"MonitorPresets",$data[0]);
insertQuery($dbh,"MonitorPresets",@data); insertQuery($dbh,"MonitorPresets",@data);
} else { } else {
# An existing Control was found and the overwrite flag was not set. Do nothing. # An existing Control was found and the overwrite flag was not set.
# Do nothing.
print "Existing preset $data[0] detected and overwrite flag not set.\nSkipping...\n"; print "Existing preset $data[0] detected and overwrite flag not set.\nSkipping...\n";
} }
} }