PERL Script to detect daylight and start/stop cameras
Posted: Tue Sep 18, 2012 2:20 pm
I have 2 cameras monitoring my grandmothers house which do not have night capabilities, after receiving multiple events for cars driving by and wasting a large amount of hard disk space, I wrote the following using perl to calculate the sunrise and sunset and to enable/disable the cameras accordingly. This was a quick write, so please constructive criticism only:
You will need the perl module DateTime::Event::Sunrise
The following is the perl script, please modify parameters as defined below
You will need to create the file "/var/log/camstopper.log" with write permissions of the user that will run the script. I set a cron job to run every 5 minutes.
You will need the perl module DateTime::Event::Sunrise
Code: Select all
sudo cpan
install DateTime::Event::Sunrise
Code: Select all
#! /usr/bin/perl
use strict;
use warnings;
use DateTime;
use DateTime::Event::Sunrise;
my @cams = qw(1 5); # space delimited monitor id's
my $username = "admin"; # administrative username
my $password = "password"; #admin password
my $latt = 'xx.xxxx'; # Lattitude from google
my $long = 'xx.xxxx'; # Longitude from google
my $alt = '-6';
my $tz = 'America/Chicago'; # Timezone can be found (in ubuntu) by 'cat /etc/timezone'
my $state_directory = "/opt/camera_scripts/"; # working directory for the state files
sub Log{
my $message = $_[0];
my $timestamp = `date +[\%Y-\%m-\%d\\ \%H:\%M:\%S]`;
chomp($timestamp);
open(LOG, ">> /var/log/camstopper.log");
print LOG $timestamp." ".$message."\n";
close(LOG);
}
my $today = `date +\%Y,\%m,\%d`;
chomp($today);
my @date = split(",", $today);
my $dt = DateTime->new( year => $date[0], month => $date[1], day => $date[2], time_zone => $tz);
my $sunrise = DateTime::Event::Sunrise->sunrise(longitude => $long, latitude => $latt, altitude => $alt, iteration => 1);
my $sunset = DateTime::Event::Sunrise->sunset(longitude => $long, latitude => $latt, altitude => $alt, iteration => 1);
my $rise = $sunrise->next($dt);
my $set = $sunset->next($dt);
my @rise_parts = split("T", $rise);
my @set_parts = split("T", $set);
my $rise_time = $rise_parts[1];
my $set_time = $set_parts[1];
my $cur_time = `date +\%H:\%M:\%S`;
chomp($cur_time);
foreach my $cam(@cams){
my $cam_file = $state_directory.$cam;
if($cur_time gt $rise_time && $cur_time lt $set_time){
# After sunrise
unless(-e $cam_file){
Log("Cam file not found. Sunrise at ".$rise_time."! Setting camera ".$cam." to enable");
open(CAMFILE, "> $cam_file");
print CAMFILE 1;
`zmu -m $cam -E -U $username -P $password`;
close(CAMFILE);
}else{
open(CAMFILE, "< $cam_file");
my @file = <CAMFILE>;
close(CAMFILE);
unless($file[0] == 1){
Log("Sunrise detected (Rise time at ".$rise_time.")! Setting cam ".$cam." to enable");
`zmu -m $cam -E -U $username -P $password`;
open(CAMFILE, "> $cam_file");
print CAMFILE 1;
close(CAMFILE);
}
}
}else{
# After sunset
unless(-e $cam_file){
Log("Cam file not found. Sunset at ".$set_time."! Setting camera ".$cam." to disable");
open(CAMFILE, "> $cam_file");
print CAMFILE 0;
`zmu -m $cam -D -U $username -P $password`;
close(CAMFILE);
}else{
open(CAMFILE, "< $cam_file");
my @file = <CAMFILE>;
close(CAMFILE);
unless($file[0] == 0){
Log("Sunset detected (Set time at ".$set_time.")! Setting cam ".$cam." to disable");
`zmu -m $cam -D -U $username -P $password`;
open(CAMFILE, "> $cam_file");
print CAMFILE 0;
close(CAMFILE);
}
}
}
}