Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
barcode/html/show.pl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
292 lines (228 sloc)
7.44 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# http://barcode/show.pl/3F5884C47698DB4A95E444878372B354/lastregex/10 | |
# echo "delete from codes where readerid=\"tuetenfurz\";" | sqlite3 scanner.sqlite3 | |
use strict; | |
use Data::Dumper; $Data::Dumper::Sortkeys=1; | |
use HTTP::Request; | |
use LWP::UserAgent; | |
use URI::Escape; | |
use CGI qw/:standard/; | |
use CGI::Carp qw(fatalsToBrowser); | |
use DBI; | |
use Barcode; | |
our $dbh; | |
my $q = CGI->new; | |
# $READERID ist das, was ueber URL reinkam | |
# $readerid ist das, was nachher der hardware entspricht. also die serial in der DB | |
my $PATH_INFO = $ENV{PATH_INFO}; $PATH_INFO =~ s{^/}{}; | |
my ($READERID,$FORMAT,$REST) = split('/',$PATH_INFO,3); | |
# | |
# print html header (needed early for debugging) | |
# | |
my $hmlheader_printed = 0; | |
sub htmlheader { | |
$hmlheader_printed and return; | |
print $q->header(-type => 'text/html',-charset=>'utf-8'); | |
print <<"__EOHTML__"; | |
<html> | |
<head> | |
<title>show/$READERID</title> | |
</head> | |
<body> | |
__EOHTML__ | |
$hmlheader_printed = 1; | |
} | |
#htmlheader(); # uncomment for debuging | |
# | |
# db stuff | |
# | |
my $sth = undef; | |
sub db_open { | |
my $f = shift; | |
$dbh=DBI->connect("dbi:SQLite:dbname=$f","","", | |
{ | |
PrintError=>0, | |
RaiseError=>1, | |
PrintWarn=>1, | |
} | |
); | |
$dbh or die "$DBI::errstr\n"; | |
} | |
db_open( $CONF{path}->{dbfile} ); | |
# select readerid from codes group by readerid; | |
$sth = $dbh->prepare("SELECT readerid FROM codes GROUP BY readerid ORDER BY readerid"); | |
$sth->execute(); | |
my $readerid_uniq = $sth->fetchall_arrayref( ); | |
# | |
# fix up alias of %CONF to 'aliases' as a x-ref | |
# | |
# 'alias' => { | |
# '3F5884C47698DB4A95E444878372B354' => 'SA', | |
# 'CDDCDCC34B72704F9374DD29325F75F1' => 'IT', | |
# 'E791F9C7D956FF49A1E283B49C62C209' => 'SB', | |
# }, | |
# | |
my %ALIASES; | |
for ( keys %{$CONF{alias}} ) { | |
$ALIASES{$CONF{alias}{$_}} = $_; | |
} | |
# my $readerid = exists($CONF{alias}->{$READERID}) ? $CONF{alias}->{$READERID} : $READERID; | |
my $readerid = exists($ALIASES{$READERID}) ? $ALIASES{$READERID} : $READERID; | |
my @READERIDs = map { @$_ } @$readerid_uniq; | |
# | |
# generiere eine regex aller codes | |
# | |
# http://barcode/show.pl/3F5884C47698DB4A95E444878372B354/lastregex/10 | |
# http://barcode/show.pl/3F5884C47698DB4A95E444878372B354/lastregex/50 | |
# http://barcode/show.pl/s4pm/lastregex | |
if ($FORMAT eq 'lastregex') { | |
#print "<xmp>" . Dumper(\@READERIDs) . "</xmp>"; | |
#print "<xmp>" . Dumper(\%ALIASES) . "</xmp>"; | |
#print "<xmp>" . Dumper(\%CONF) . "</xmp>"; | |
my $limit = 5; | |
if ( $REST =~ m{^(\d+)} ) { | |
$limit = $1; | |
} | |
$sth = $dbh->prepare("SELECT rowid, * FROM codes WHERE readerid=? ORDER BY rowid DESC LIMIT ?"); | |
$sth->execute( $readerid, $limit ); | |
my $row = $sth->fetchall_hashref( 'rowid' ); | |
print join('|', map {$row->{$_}->{code}} sort { $b <=> $a } keys %{$row} ); | |
exit; | |
} | |
my $myself = $q->url; | |
htmlheader(); # debugging; | |
print $q->a({-href=>"$myself"},"<tt> all </tt>");print '| '; | |
for my $readerid ( @READERIDs ) { | |
my $alias = exists($CONF{alias}->{$readerid}) ? $CONF{alias}->{$readerid} : $readerid; | |
print $q->a({-href=>"$myself/$readerid"},"<tt>\"$alias\"</tt>"); | |
print '| '; | |
} | |
print button(-name=>'button_reload', | |
-value=>'refresh', | |
-onClick=>"location.reload(true)"); | |
print '<div id=number> </div>'; | |
#print $q->h1("$READERID,$readerid,$FORMAT,$REST"); | |
if ( $READERID ) { | |
@READERIDs = ( $readerid ); | |
} | |
else { | |
print $q->h1("All scanners"); | |
} | |
sub repl { | |
my $find = shift; | |
my $replace = shift; | |
my $var = shift; | |
my @items = ( $var =~ $find ); | |
$var =~ s/$find/$replace/; | |
for( reverse 0 .. $#items ){ | |
my $n = $_ + 1; | |
$var =~ s/\$$n/${items[$_]}/g ; | |
} | |
return $var; | |
}; | |
sub code2url { | |
my $code = shift; | |
my $type = 'unknown'; | |
my $url = ''; | |
for my $fr ( keys %{$CONF{re2url}} ) { | |
if ($code =~ m/$fr/) { | |
my $to = $CONF{re2url}->{$fr}; | |
if ( $url = repl( $fr, $to, $code ) ) { | |
last; | |
} | |
} | |
} | |
if ($url =~ /^http:/) { | |
} | |
return ($type,$url); | |
}; | |
sub code2lnk { | |
my $code = shift; | |
my $type = 'unknown'; | |
my $url = ''; | |
for my $fr ( keys %{$CONF{re2lnk}} ) { | |
if ($code =~ m/$fr/) { | |
my $to = $CONF{re2lnk}->{$fr}; | |
if ( $url = repl( $fr, $to, $code ) ) { | |
$type = $CONF{retype}->{$fr}; | |
last; | |
} | |
} | |
} | |
if ($url =~ /^http:/) { | |
} | |
return ($type,$url); | |
}; | |
my $code = ''; | |
# | |
# batchlinks | |
# | |
# setze revers type/variablen und biete entsprechende links | |
# | |
if ($FORMAT eq 'invreg') { | |
# select time from (select rowid as r,* from codes where rowid<2500 order by rowid desc limit 10) order by r; | |
# select r, * from (select rowid as r,* from codes order by rowid desc limit 10) order by r; | |
# $sth = $dbh->prepare("SELECT rowid as r,* from codes WHERE readerid=? ORDER BY rowid DESC LIMIT 70"); | |
# $sth = $dbh->prepare("SELECT * FROM ( SELECT rowid as r,* from codes WHERE readerid=? ORDER BY rowid DESC LIMIT 70 ) ORDER BY code"); | |
$sth = $dbh->prepare("SELECT rowid, * FROM codes WHERE readerid=? ORDER BY rowid DESC LIMIT 50"); | |
for my $readerid ( @READERIDs ) { | |
my $alias = exists($CONF{alias}->{$readerid}) ? $CONF{alias}->{$readerid} : $readerid; | |
$sth->execute( $readerid ); | |
my $row = $sth->fetchall_hashref( 'rowid' ); | |
print $q->h1("Scanner \"$alias\" $readerid"); | |
print "<div> </div>\n"; | |
for my $r ( sort { $a <=> $b } keys %{$row} ) { | |
if ( !exists($row->{$_}->{location}) ) { | |
$row->{$_}->{location} = 'loc'; | |
} | |
} | |
print $q->table({-border=>undef}, | |
Tr({-align=>'CENTER',-valign=>'TOP'}, | |
[ | |
th(['date', 'code',,'type','link','url']), | |
map { td([YYYYMMDDHHMMSS($row->{$_}->{time}), | |
$row->{$_}->{remote}, | |
'<tt>' . $row->{$_}->{code} . '</tt>', | |
(code2lnk($row->{$_}->{code}))[0], | |
(code2lnk($row->{$_}->{code}))[1], | |
'<tt>' . (code2url($row->{$_}->{code}))[1] . '</tt>' , | |
]) } sort { $a <=> $b } keys %{$row}, | |
] | |
) | |
); | |
# print "<xmp>" . Dumper(\$row) . "</xmp>"; | |
} | |
} | |
else { # standard barcode table | |
# index auf readderid | |
# select readerid from codes group by readerid; | |
$sth = $dbh->prepare("SELECT rowid, * FROM codes WHERE readerid=? ORDER BY rowid DESC"); | |
for my $readerid ( @READERIDs ) { | |
my $alias = exists($CONF{alias}->{$readerid}) ? $CONF{alias}->{$readerid} : $readerid; | |
$sth->execute( $readerid ); | |
my $row = $sth->fetchall_hashref( 'rowid' ); | |
print $q->h1("Scanner \"$alias\" $readerid"); | |
print "<div> </div>\n"; | |
#print "<xmp>" . Dumper(\$row) . "</xmp>"; | |
print $q->table({-border=>undef}, | |
Tr({-align=>'CENTER',-valign=>'TOP'}, | |
[ | |
th(['Date', 'remote','code',,'type','link','url']), | |
map { td([YYYYMMDDHHMMSS($row->{$_}->{time}), | |
$row->{$_}->{remote}, | |
'<tt>' . $row->{$_}->{code} . '</tt>', | |
(code2lnk($row->{$_}->{code}))[0], | |
(code2lnk($row->{$_}->{code}))[1], | |
'<tt>' . (code2url($row->{$_}->{code}))[1] . '</tt>' , | |
]) } sort { $b <=> $a } keys %{$row}, | |
] | |
) | |
); | |
# print "<xmp>" . Dumper(\$row) . "</xmp>"; | |
} | |
} | |
print "\n<!-- <xmp>" . Dumper(\%CONF) . "</xmp> -->"; | |
print <<"__EOHTML__"; | |
</xmp> | |
</body> | |
__EOHTML__ | |