Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Cam Check

by djw (Vicar)
on May 04, 2001 at 19:45 UTC ( [id://77998]=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info djw
Description: This code helps me manage my cam on gibfest.org.

I have a cam at work, and a cam at home - they aren't on at the same time, I have them set on a schedule. I could use just one cam image for both cams but that isn't any fun - I also wanted my page to report to the visitor where the cam image was coming from.

Anyhow, this script checks my two cam files for creation date (File::Stat) to see which is newer. It aslo checks to see if I have updated my cam image less than 10 minutes ago, if not, its offline.

For example, lets say the newest file is work.jpg, and its creation time is less than 10 minutes ago - the script changes 3 text files in my web directory (used in Server-Side Includes) to reflect the fact that I'm at work. If the newest file (in this case work.jpg) is older than 10 minutes, then the cam is offline, and it reports that using the text files and SSI.

I have this script run on my linux box on a schedule using cron.
#!/usr/bin/perl -w
use strict;
use File::stat;

my(%files, $create_date, $file, $current_date, $code);
my($hometime, $log, $worktime, $location, $url, $set_time);
my($home_image, $work_image, $code_color);

# ------------------------------------------------
# Script created by David Wakeman, 2001.
#
# I have this setup to use the /var/www/images dir
# for my cam images, and the /var/www dir to use
# for the text files in my server-side includes.
#
# Test to see if the most recent image of your 
# specified images (%files hash) is newer than 
# your specified amount of time ($set_time).  If
# it is, that is the current image displayed on
# your cam, if not, the camera is offline.
#
# If you don't have SSI enabled in your Apache
# configs, go to apache.org and read their Docs
# to learn how it works, and how to enable it.
# ------------------------------------------------


# -----------------------------------------------
#             START OF CONFIG AREA
# -----------------------------------------------

# $code is used for the top part of my cam where
# you see: $cam = ( work | home | offline );
# My index page has this for that line:
# $cam = ( <!--#include virtual="code.txt"--> );
# It basically highlights the current location
# in whatever you put for $code_color below.

$code = "/var/www/code.txt";


# Put the highlight color of your choice here.
# Default = orange.

$code_color = "#F07800";


# $url is used for the java applet running the
# cam refresh on my page.  The java applet code's
# param name="url" value="" uses SSI as above to
# include the correct URL of The Moment (tm).

$url = "/var/www/url.txt";


# $cam is used for the blurb under my cam that
# says ".. camera image is from $location ..".
# Again, this uses SSI from the index page.

$file = "/var/www/cam.txt";


# $log = logfile - duh.

$log = "/home/iago/camlog";


# Here is the location of your work and image
# files.

$home_image = "/var/www/images/home.jpg";
$work_image = "/var/www/images/work.jpg";


# Here is the amount of time you want to test 
# your most current file against.  Value is 
# in seconds.

$set_time = 600;

# -----------------------------------------------
#              END OF CONFIG AREA
# -----------------------------------------------

$current_date = time;    # epoch time
$create_date = 0;
$worktime = 0;
$hometime = 0;
$location = "";

%files =
(
    home    =>    "$home_image",
    work    =>    "$work_image",
);


$hometime = stat($files{home})->ctime;        # This checks each file 
+for current
$worktime = stat($files{work})->ctime;        # epoch time.


# This figures out if the newest file is newer than
# our $set_time, if not - cam is off.
# --------------------------------------------------

if ($worktime > $hometime && $current_date - $worktime < $set_time)   
+ 
{
    $location = "work";        # dood, i'm at work. blah
    adjust_cam($location);
}
elsif ($hometime > $worktime && $current_date - $hometime < $set_time)
{
    $location = "home";        # dood! i'm at home! weee
    adjust_cam($location);
}
else
{
    $location = "offline";    # dood, i'm hiding...
    adjust_cam($location);
}


# This sub changes all our SSI txt files needed
# for the cam applet.
# -----------------------------------------------

sub adjust_cam
{
    open(FILE, "+>$file") || die "can't open file: ($!)\n";
    if ($location eq "home" || $location eq "work")
    {
        print FILE ".. current image is from <b>$location</b> ..";
    }
    else
    {
        print FILE ".. camera is currently offline ..";
    }
    open(URL, "+>$url") || die "can't open file: ($!)\n";
    print URL "http://www.gibfest.org/images/$location.jpg";
    open(CODE, "+>$code") || die "can't open file: ($!)\n";
    if ($location eq "home")
    {
        print CODE "work | <font color=\"$code_color\">$location</font
+> | offline";
    }
    elsif ($location eq "work")
    {
        print CODE "<font color=\"$code_color\">$location</font> | hom
+e | offline";
    }
    else
    {
        print CODE "work | home | <font color=\"$code_color\">$locatio
+n</font>";
    }
}

close(FILE);
close(URL);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://77998]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-16 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found