#!/usr/local/bin/perl # Name: backyard/get-vfc Author: Joe Smith 26-Oct-00 # Purpose: Captures images from each VFC video port on the SPARCstation. $verbose = $ARGV[0] eq "-v" ? shift(@ARGV) : 0; $vfc_host = "jomis"; # SPARC workstation with VFC SBus card $pic_dir = "backyard"; # Relative to $ENV{HOME} $vfc_snap = "/usr/local/bin/vfc-snap"; # Program on $vfc_host $font = "/usr/local/lib/stamp.fnt"; # Used by /usr/local/bin/stamp $ENV{PATH} .= ":/usr/local/bin"; @camera = ("Chez Inwap", "Backyard Web Cam", "Indoor Web Cam", "S-Video Feed"); # 0, 1, 2, 3 @minutes_between_snaps = (0, 1, 10, 5); # In order to avoid problems with browsers caching images, give each # captured image a different file name. # To keep files from accumulating, keep only one *$mm.gif and *$mm.jpg. ($_,$min,$hour,$day,$mon) = localtime(); $file = sprintf("%02d%02d%02d%02d",$mon+1,$day,$hour,$min); $mm = sprintf("%02d",$min); $| = 1; foreach $port (1,2,3) { $dir = "$pic_dir/.video/$port"; &remove_old_files($dir,$mm); next unless ($min % $minutes_between_snaps[$port]) == 0; $tmp = "/tmp/vfc$port-$$.jpg"; $jpg = "$dir/$file.jpg"; $gif = "$dir/$file.gif"; # Connect to the SPARCclassic workstation via rsh and execute the command # that captures a frame from a video port, asking for a JPG image $_ = "rsh $vfc_host $vfc_snap ${port}j"; # Video capture, jpeg format print "$_\n" if $verbose; $_ = `$_ 2>&1 >$tmp`; # JPG file to $tmp file, STDERR to $_ print $_ if $verbose; if (-s $tmp) { # Zero bytes long if no video signal $top = "$camera[0] $camera[$port]"; # "The Amazing Chez Inwap ... Web Cam"; chop($bottom = `date`); # Add captions to the top and the bottom of the image $_ = "stamp -i $tmp -o $jpg.tmp -t '$top' -b '$bottom' -y $font"; print $_ if $verbose; $_ = `$_ 2>&1` || "\n"; print "$? $_" if $verbose or $?; if (-s "$jpg.tmp") { # Create an 80x60 GIF from the 640x480 JPG file system("djpeg -scale 1/8 -gif <$jpg.tmp >$gif.tmp"); rename("$gif.tmp",$gif); rename("$jpg.tmp",$jpg); system("ls -l --no-group $jpg $gif") if $verbose; } else { warn("Failed: ", `ls -l $jpg.tmp $tmp`); } } unlink($tmp) unless $verbose; # Keep temp file when debugging sleep(4) if $verbose; # Allow time for Control-C between captures } exit(0); # Done sub remove_old_files { local($dir,$minute) = @_; opendir(DIR,$dir) and @files=readdir(DIR) and closedir(DIR); shift(@files) if $files[0] eq "."; shift(@files) if $files[0] eq ".."; foreach $_ (@files) { $_ = "$dir/$_" }; print scalar(@files)," files in $dir\n" if $verbose; if (@files > 3*2) { # Keep at least three jpg+gif pairs foreach $_ (@files) { next if -l $_; # Leave symlinks alone # At 15 minutes past the hour, get rid if *15.jpg and *15.gif unlink $_ if /$minute\.(gif|jpg)$/ || -M $_ > 1.0; # And day-old files } } }