#!/usr/bin/perl -w use DBI; use Fcntl; use Socket; use Time::HiRes; use Switch; if (!$ARGV[0]) { print "Usage: ./database.pl \n"; exit(-1); } $SIG{INT} = \&tsktsk; $in_msg_num = 11; $out_msg_num = 12; $in_event1_msg_num = 14; $in_event2_msg_num = 15; $in_msg_type = chr($in_msg_num); ##define message type which will send to node.c to indicate new event $out_msg_type = chr($out_msg_num); $in_event1_msg_type = chr($in_event1_msg_num); $in_event2_msg_type = chr($in_event2_msg_num); my $host = "localhost"; my $port = "9877"; my $proto = getprotobyname("tcp"); my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); ## mysql user database name $db ="zm"; ## mysql database user name $user = "root"; ## mysql database password $pass = "1"; ## user hostname : This should be "localhost" but it can be diffrent too $dbhost="localhost"; ## get monitor id from passing command line argument $id = $ARGV[0]; print "$id\n"; ## SQL query $db_query = "SELECT COUNT(*) FROM Events WHERE MonitorId = $id"; ## connect SQL database $dbh = DBI->connect("DBI:mysql:$db:$dbhost", $user, $pass); ## connect socket for send msg to node.c socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket:$!"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; select(SOCKET); $| = 1; select(STDOUT); connect( SOCKET, $paddr ) or die "connect: $!"; print SOCKET $in_event1_msg_type; print SOCKET "\n"; ## clean all data entry in Event tables $db_reset_query = "TRUNCATE TABLE Events"; $sqlQuery = $dbh->prepare($db_reset_query); $sqlQuery->execute(); $sqlQuery->finish(); #reset $id = 0 $id = 0; ## interupt function to catch Ctrl+C sub tsktsk { $SIG{INT} = \&tsktsk; $sqlQuery->finish(); $dbh->disconnect(); close SOCKET or die "close:$!"; print "\nCTRL+C caught. close socket and db connection\n"; exit(0); } { in_loop: $sqlQuery = $dbh->prepare($db_query) or die "Can't prepare $sqlQuery: $dbh->errstr\n"; $sqlQuery->execute() or die "can't execute the query: $sqlQuery->errstr"; #get event count before motion detection $count_bef_trigger = $sqlQuery->fetchrow_array(); $sqlQuery->finish(); in_loop_1: $sqlQuery->execute() or die "can't execute the query: $sqlQuery->errstr"; $num_count = $sqlQuery->fetchrow_array(); #event count will increase two counts, #one for motion triggered by camera panning, #the other for motion triggered by train moving into zone which is expected if ($id eq 0) #init run { #in init run only one event count increase, no dummy motion triggered by camera panning if ($count_bef_trigger eq $num_count) { $sqlQuery->finish(); #print "$count_bef_trigger\n"; goto in_loop_1; } else { $sqlQuery->finish(); $count_bef_trigger = $num_count; print "$count_bef_trigger\n"; goto in_end; } } else #normal run { #in normal run two event triggers, one dummy and one train trigger if ($num_count - $count_bef_trigger eq 2) #camera panning event and train motion event { $sqlQuery->finish(); $count_bef_trigger = $num_count; print "$count_bef_trigger\n"; goto in_end; } else #no event trigger or just one event triggered by camera panning { $sqlQuery->finish(); #print "$count_bef_trigger\n"; goto in_loop_1; } } in_end: if ($id eq 0) #initial run { $msg_type = $in_msg_type; $id = 2; #move to middle pos waiting for in_event1 trigger } elsif ($id eq 1) #normal run { $msg_type = $in_msg_type; $id = 2; #move to middle pos waiting for in_event1 trigger } elsif ($id eq 2) { $msg_type = $in_event1_msg_type; $id = 4; #jump to leftmost pos waiting for out event trigger } elsif ($id eq 3) { $msg_type = $in_event2_msg_type; } elsif ($id eq 4) { $msg_type = $out_msg_type; $id = 1; #back to rightmost pos waiting for in event trigger } print SOCKET $msg_type; goto in_loop_1; }