GetCam.pl
上传用户:qdrechuli
上传日期:2022-08-01
资源大小:917k
文件大小:4k
- #!/usr/bin/perl -w
- use strict;
- use DBI;
- use LWP::UserAgent;
- use vars qw(%base);
- sub existf
- {
- my $existf = 1;
- open(FEXIST,$_[0]) or $existf = 0;
- close(FEXIST);
- return $existf;
- }
- sub GetImg
- {
- (my $url,my $user,my $pwd) = @_;
- my $ua = LWP::UserAgent->new;
- $ua->agent("$0/0.1 " . $ua->agent);
- my $req = HTTP::Request->new(GET => 'http://'.$url.'/cgi-bin/jpg/image.cgi');
- $req->authorization_basic($user,$pwd);
- my $res = $ua->request($req);
- if ($res->is_success)
- {
- return $res->content;
- }
- else
- {
- return 0;
- }
- }
- sub SaveImg
- {
- (my $CID,my $img,my $tranche,my $min,my $sec) = @_;
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- my $sqlreq = "SELECT Count(R_ID) FROM record ";
- $sqlreq = $sqlreq." WHERE R_Cam=".$CID." ";
- $sqlreq = $sqlreq." AND R_Day='".substr($tranche,0,8)."' ";
- $sqlreq = $sqlreq." AND R_Hour=".substr($tranche,8,2)." ";
- print "DEBUTn";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $index) = $sql->fetchrow();
- $sql->finish();
- print "FIN INDEX = ",$index,"n";
- $base{insert} = $db->prepare("INSERT INTO record VALUES ( ?,?,?,?,?,?,?,?,?)");
- $db->do("LOCK TABLE record LOW_PRIORITY WRITE");
- $base{insert}->execute('',$CID,$tranche.$min.$sec,$img,length($img),$index,0,substr($tranche,0,8),substr($tranche,8,2));
- $db->do("UNLOCK TABLE");
- $db->disconnect();
- }
- sub DoCam
- {
- (my $CID) = @_;
- while (1)
- {
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- my $sqlreq = "SELECT C_Name,C_Url,C_User,C_Pwd,C_Active,C_Freq FROM cam ";
- $sqlreq = $sqlreq." WHERE C_ID=".$CID;
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $CName,my $CUrl,my $CUser,my $CPwd,my $CActive,my $CFreq) = $sql->fetchrow();
- $sql->finish();
- if ($CActive)
- {
- print "Activen";
- (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst) = localtime(time);
- $year = $year+1900;
- $mon = $mon+1;
- my $smon;
- my $smday;
- if ($mon<10) { $smon = "0".$mon; }
- if ($mon>9) { $smon = $mon; }
- if ($mday<10) { $smday = "0".$mday; }
- if ($mday>9) { $smday = $mday; }
- if ($hour<10) { $hour= '0'.$hour; }
- if ($min<10) { $min= '0'.$min; }
- if ($sec<10) { $sec= '0'.$sec; }
- my $tranche = $year.$smon.$smday.$hour;
- print $tranche,"n";
- print $min," ",$sec,"n";
- my $img = GetImg($CUrl,$CUser,$CPwd);
- print length($img),"n";
- SaveImg($CID,$img,$tranche,$min,$sec);
- }
- sleep($CFreq);
- $db->disconnect();
- }
- }
- sub main
- {
- $SIG{CHLD} = 'IGNORE';
- # while (1)
- {
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- my $sqlreq = "SELECT C_ID,C_Name,C_Url,C_User,C_Pwd,C_Active,C_Freq FROM cam ";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- while ((my $CID,my $CName,my $CUrl,my $CUser,my $CPwd,my $CActive,my $CFreq) = $sql->fetchrow())
- {
- print "CAM ID = ",$CID,"n";
- if ($CActive)
- {
- local $SIG{INT}='IGNORE';
- my $pid = fork;
- if ($pid)
- {
- print "PEREn";
- my $sqlreq = "UPDATE cam SET C_Pid=".$pid." WHERE C_ID=".$CID;
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- $sql->finish();
- }
- else
- {
- print "FILSn";
- DoCam($CID,$CName,$CUrl,$CUser,$CPwd,$CActive,$CFreq);
- $sql->finish();
- $db->disconnect();
- exit(0);
- }
- }
- }
- $sql->finish();
- $db->disconnect();
- }
- }
- main();