OptCam.pl
上传用户:qdrechuli
上传日期:2022-08-01
资源大小:917k
文件大小:3k
- #!/usr/bin/perl -w
- use strict;
- use DBI;
- use vars qw(%base);
- sub SetState
- {
- (my $State) = @_;
- my $db = DBI->connect("DBI:mysql:netcam","root","");
- my $sqlreq = "UPDATE vars SET V_Value='".$State."' WHERE V_Var='OptCamState'";
- 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 FROM cam";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- while ((my $CID) = $sql->fetchrow())
- {
- $SIG{CHLD} = 'IGNORE';
- my $pid = fork;
- if ($pid==0)
- {
- $sql->finish();
- print "CHILD(",$CID,") : STARTINGn";
- while (1)
- {
- my $sqlreq = "SELECT V_Value FROM vars WHERE V_Var='OptCamState'";
- my $sql = $db->prepare($sqlreq);
- $sql->execute();
- (my $NewState) = $sql->fetchrow();
- $sql->finish();
- if ($NewState eq 'STOP')
- {
- $db->disconnect();
- print "CHILD(",$CID,") : ENDINGn";
- exit(0);
- }
- $sqlreq = "SELECT R_ID,R_Img FROM record WHERE ";
- $sqlreq .= "R_Opt=0 and R_CAM=".$CID." ";
- $sqlreq .= "LIMIT 0,1";
- $sql = $db->prepare($sqlreq);
- $sql->execute();
- while ((my $RID,my $Img) = $sql->fetchrow())
- {
- print "CHILD(",$CID,") : ",$RID,"n";
- print "CHILD(",$CID,") : ",length($Img),"n";
- open(IMAGE,">/opt/NetAdmin/Net-Cam/data/tmp_".$CID.".jpg");
- print IMAGE $Img;
- close(IMAGE);
- my @args = ("/opt/NetAdmin/Net-Cam/bin/jdo.o","/opt/NetAdmin/Net-Cam/data/tmp_".$CID.".jpg");
- system(@args);
- open(IMAGE,"/opt/NetAdmin/Net-Cam/data/tmp_".$CID.".jpg");
- read IMAGE,$Img,length($Img)*2;
- close(IMAGE);
- print "CHILD(",$CID,") : ",length($Img),"n";
- $base{update} = $db->prepare("UPDATE record SET R_Size=?,R_Img=?,R_Opt=? WHERE R_ID=?");
- $db->do("LOCK TABLE record LOW_PRIORITY WRITE");
- $base{update}->execute(length($Img),$Img,1,$RID);
- $db->do("UNLOCK TABLE");
- }
- $sql->finish();
- }
- }
- }
- $sql->finish();
- $db->disconnect();
- }
- sub main
- {
- $SIG{CHLD}='IGNORE';
- my $pid = fork;
- if ($pid)
- {
- print "OPTCAM : 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='OptCamState'";
- 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();
- exit(0);