GetCam.pl
上传用户:qdrechuli
上传日期:2022-08-01
资源大小:917k
文件大小:6k
- #!/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 T_Index FROM tranche ";
- $sqlreq = $sqlreq." WHERE T_Cam=".$CID." ";
- $sqlreq = $sqlreq." AND T_Day='".substr($tranche,0,8)."' ";
- $sqlreq = $sqlreq." AND T_Hour=".substr($tranche,8,2)." ";
- print "CHILD(",$CID,") : DEBUT INDEXn";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $index) = $sql->fetchrow();
- $sql->finish();
- if ($index eq '') { $index = 0; }
- $index++;
- # my $index = $min*60+$sec;
- print "CHILD(",$CID,") : 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");
- # my $sqlreq = "SELECT Count(T_ID) FROM tranche ";
- # $sqlreq = $sqlreq." WHERE T_Cam=".$CID." ";
- # $sqlreq = $sqlreq." AND T_Day='".substr($tranche,0,8)."' ";
- # $sqlreq = $sqlreq." AND T_Hour=".substr($tranche,8,2)." ";
- # my $sql = $db->prepare($sqlreq);
- # $sql->execute();
- # (my $NbT) = $sql->fetchrow();
- # $sql->finish();
- if ($index eq 1)
- {
- $base{insert} = $db->prepare("INSERT INTO tranche VALUES ( ?,?,?,?,?)");
- $db->do("LOCK TABLE tranche LOW_PRIORITY WRITE");
- $base{insert}->execute('',$CID,$index,substr($tranche,0,8),substr($tranche,8,2));
- $db->do("UNLOCK TABLE");
- }
- else
- {
- $base{update} = $db->prepare("UPDATE tranche set T_Index=? WHERE T_Cam=? and T_Day=? and T_Hour=?");
- $db->do("LOCK TABLE tranche LOW_PRIORITY WRITE");
- $base{update}->execute($index,$CID,substr($tranche,0,8),substr($tranche,8,2));
- $db->do("UNLOCK TABLE");
- }
- $db->disconnect();
- }
- sub DoCam
- {
- (my $CID,my $CName,my $CUrl,my $CUser,my $CPwd,my $CActive,my $CFreq) = @_;
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- while (1)
- {
- my $sqlreq = "SELECT V_Value FROM vars WHERE V_Var='GetCamState'";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $NewState) = $sql->fetchrow();
- $sql->finish();
- if ($NewState eq 'STOP')
- {
- $db->disconnect();
- return;
- }
- # $sqlreq = "SELECT C_Name,C_Url,C_User,C_Pwd,C_Active,C_Freq FROM cam ";
- # $sqlreq = $sqlreq." WHERE C_ID=".$CID;
- # $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 "CHILD(",$CID,") : 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 "CHILD(",$CID,") : ",$tranche,"n";
- print "CHILD(",$CID,") : ",$min," ",$sec,"n";
- my $img = GetImg($CUrl,$CUser,$CPwd);
- print "CHILD(",$CID,") : ",length($img),"n";
- SaveImg($CID,$img,$tranche,$min,$sec);
- # }
- sleep($CFreq);
- }
- }
- sub SetState
- {
- (my $State) = @_;
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- my $sqlreq = "UPDATE vars SET V_Value='".$State."' WHERE V_Var='GetCamState'";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- $sql->finish();
- $db->disconnect();
- }
- sub Stop
- {
- SetState('STOP');
- }
- sub Start
- {
- SetState('START');
- 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)
- {
- $SIG{CHLD}='IGNORE';
- my $pid = fork;
- if ($pid)
- {
- print "PARENT : STARTING CHILD ",$pid,"n";
- my $sqlreq = "UPDATE cam SET C_Pid=".$pid." WHERE C_ID=".$CID;
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- $sql->finish();
- }
- else
- {
- print "CHILD(",$CID,") : STARTINGn";
- DoCam($CID,$CName,$CUrl,$CUser,$CPwd,$CActive,$CFreq);
- $sql->finish();
- $db->disconnect();
- print "CHILD(",$CID,") : ENDINGn";
- exit(0);
- }
- }
- }
- $sql->finish();
- $db->disconnect();
- }
- sub main
- {
- $SIG{CHLD}='IGNORE';
- my $pid = fork;
- if ($pid)
- {
- print "GETCAM : STARTING PARENT ",$pid,"n";
- exit(0);
- }
- Start();
- my $CurState = "START";
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- while (1)
- {
- print "PARENT : READING STATEn";
- my $sqlreq = "SELECT V_Value FROM vars WHERE V_Var='GetCamState'";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $NewState) = $sql->fetchrow();
- $sql->finish();
- print "PARENT : CurSTATE = ",$CurState,"n";
- print "PARENT : NewSTATE = ",$NewState,"n";
- if ($NewState eq 'STOP' and $CurState eq "START")
- {
- Stop();
- $CurState = 'STOP';
- }
- if ($NewState eq 'START' and $CurState eq "STOP")
- {
- Start();
- $CurState = 'START';
- }
- if ($NewState eq 'RESTART')
- {
- Stop();
- sleep(10);
- Start();
- $CurState = 'START';
- }
- sleep(10);
- }
- $db->disconnect();
- }
- main();