#!/usr/local/bin/perl # Name: backyard/send-vfc Author: Joe Smith 23-Jan-2001 # Purpose: Captures image from VFC card and sends it to best.com # Created 13-Dec-98 after the previous webcam moved to Chez TyeDye. $main_dir = "public_html/backyard"; $pic_dir = "$main_dir/current-hour"; $make_thumbs = "$main_dir/thumbs.pl $pic_dir"; $ENV{TZ} = "US/Pacific"; require "$main_dir/sunrise.pl"; # Get definition of suns() routine $now = time; ($sunrise,$sunset) = suns($now); # Sun times for Fremont, California $daylight_start = $sunrise - (30*60); # Start 30 minutes before sunrise $daylight_end = $sunset + (30*60); # End 30 minutes after sunset $verbose = $ARGV[0] eq "-v" ? shift(@ARGV) : 0; umask(0022); # writable by group # Naming convention for captured images: MMDDhhmm.jpg (month, day, hour, min) ($sec,$min,$hour,$day,$mon,$year) = localtime(); $min = 5 * int($min/5); $file = sprintf "%02d%02d%02d%02d",$mon+1,$day,$hour,$min; $jpg = "$pic_dir/$file.jpg"; # 640x480 main picture $gif = "$pic_dir/$file.gif"; # 80x60 thumbnail $txt = "$pic_dir/capture.txt"; # File describing the latest capture # Cron runs backyard/get-vfc once a minute or so. During daylight hours, # use the capture from the backyard cam. At night, use the indoor cam. # # This is for two reasons: 1) the indoor cam (pointing at a flicker light) is # more interesting than a completely dark back yard, and 2) when the kitchen # lights are on, the backyard cam picks up a reflection of anyone walking # around inside (whether they are wearing a bathrobe or not). $_ = sprintf "%02d:%02d",$hour,$min; $port = 1; # Backyard cam (8mm camcorder) $port = 2 if ($now < $daylight_start or $now > $daylight_end); # Indoor cam $snap_dir = "$main_dir/.video/$port"; # backyard/.video/{1,2,3} $snap = "$snap_dir/$file.jpg"; foreach $_ (0 .. ($verbose ? 5 : 59)) { last if -s $snap; # Wait for backyard/get-vfc to complete print "Waiting for $snap\n" if $verbose && $_ == 2; sleep 1; # It takes from 28 to 45 seconds to run } exit 1 unless -s $snap; # Give up if no image link($snap,$jpg) or die("Cannot get to image $snap: $!\n"); ($_ = $snap) =~ s/.jpg$/.gif/; link($_,$gif); # Use existing thumbnail if any # Create "capture.txt" with information about the image file ($size,$mtime) = (stat $jpg)[7,10]; ($sec,$min,$hour,$day,$mon,$year) = localtime $mtime; # Write capture.txt with some of the same data that SnapCAP did on Win95 # SnapCAP wrote "11291706.jpg 19870 11/29/98 17:07:11 286 320x240" (no "\n") # name bytes date time ??? resolution $_ = sprintf "%s.jpg %d %02d/%02d/%d %02d:%02d:%02d $ENV{TZ} 640x480\n", $file, $size, $mon+1, $day, $year+1900, $hour, $min, $sec; @time = localtime $daylight_start; $_ .= sprintf "light=%02d:%02d ",$time[2],$time[1]; @time = localtime $sunrise; $_ .= sprintf "sunrise=%02d:%02d ",$time[2],$time[1]; @time = localtime $sunset; $_ .= sprintf "sunset=%02d:%02d ",$time[2],$time[1]; @time = localtime $daylight_end; $_ .= sprintf "dark=%02d:%02d\n",$time[2],$time[1]; print $_ if $verbose; open(OUT,">$txt") || die("Cannot write $txt: $!\n"); print OUT $_; close(OUT) || warn("Problems writing $txt: $!\n"); system($make_thumbs); # Create $pic_dir/$file.gif # Starting the morning of 9-Mar-99, 'rcp' to shell3.ba.best.com would hang. # shell3: -r-sr-xr-x 1 root bin 192512 Mar 9 03:20 /bin/rcp @jobs = grep(/rcp -p $pic_dir/,`ps ax`); if (scalar(@jobs) >= 2) { print @jobs if $verbose; system("killall rcp"); # Get rid of hung rcp jobs @jobs = grep(/rcp -p $pic_dir/,`ps ax`); print @jobs if $verbose; exit(1) if scalar(@jobs) > 2; # Abort if previous 2 rcp jobs still running } # Send image file to web server(s) $mode = 'rsync'; foreach $remote_host (@ARGV) { if ($mode eq 'rcp') { $_ = "rcp -p $jpg $gif $txt $remote_host:$pic_dir"; local $SIG{ALRM} = sub { kill(1,-$$); kill(9,-$$) }; # Kill rcp child and self alarm(180); } elsif ($mode eq 'rsync') { $_ = "rsync -au $verbose --timeout=180 " . "$main_dir/capture.gif $pic_dir $remote_host:$main_dir"; chmod 0755,$main_dir,$pic_dir; # Server demands 'chmod g-w' for *.cgi } else { die "What mode? '$mode'"; } print "$_\n" if $verbose; system($_) || die "system($_): $! $?"; } print "\tsent to @ARGV\n"; # Done