I've edited the Perl script we can find in the FAQ, to light on up to 8 leds when there
is an alarm.
I can't program, but it works for me.
I hope you can help me to make it better!!
Code: Select all
#!/usr/bin/perl -w
# i build a parallel cable with 8 leds following this guide:
# http://www.epanorama.net/circuits/parallel_output.html#linuxprogramming
# this is the schema to create it http://www.epanorama.net/circuits/lptleds.gif
# the software I use to turn on the led is lptout.
# http://www.epanorama.net/circuits/lptout.c
# well, with LPT port I can control 8 leds, so
# in the DB, in table Monitors, I've added a column (int) called LED.
# In this field I set the position of the led I want to light on.
# 1 for the 1st led, 2 for the 2nd...
# So I've changed the query to select only the monitors that corresponds to a led.
# my idea is to create a string variable containing the binary number I want to sent to the port,
# with lptout I can send only decimal values, so after that I convert it to decimal.
# 00000000 all leds off
# 11111111 all leds on
# 00000001 led 1 on
# 00000101 led 1, 3 on
# 10000101 led 1, 3, 8 on
# ......
# I use this function to convert binary to decimal
# $decimale= ord(pack('B8', $binario));
# after that I send the decimal number to the port
# system("sudo ./lptout " . $decimale);
use strict;
use ZoneMinder;
$| = 1;
zmDbgInit( "myscript", level=>0, to_log=>0, to_syslog=>0, to_term=>1 );
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );
# the new SQL QUERY
my $sql = "select M.*, max(E.Id) as LastEventId from Monitors as M left join Events as E on M.Id = E.MonitorId where M.Function != 'None' and M.Function != 'Monitor' and E.Id != '' and LED!='' group by (E.MonitorId) order by LED";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my @monitors;
while ( my $monitor = $sth->fetchrow_hashref() )
{
push( @monitors, $monitor );
}
my $binario= "";
my $decimale= 0;
my $prima= ""; # I need this to manipolate _
my $dopo= ""; # _ the sting $binario
my $led= 0; # I have 1 to 8 leds. I use this variable to set the position of the led I want to light
my $evento= 0;
while( 1 )
{
system("sudo ./lptout 0"); # I send to lpt port number 0 to turn off all the leds
$binario = "00000000";
$decimale= 0;
$prima = "";
$dopo = "";
$evento= 0;
foreach my $monitor ( @monitors )
{
# next if ( !zmShmVerify( $monitor ) ); IT GIVES ME ERROR... I DON'T KNOW WHY
if ( my $last_event_id = zmHasAlarmed( $monitor, $monitor->{LastEventId} ) )
{
$monitor->{LastEventId} = $last_event_id;
$evento= 1;
$led = $monitor->{LED};
$prima = substr($binario, 0, 8 - $led );
$dopo = substr($binario, 8 - $led +1);
$binario = $prima . "1" . $dopo;
}
}
if ($evento == 1)
{
#converts binary to decimal
$decimale= ord(pack('B8', $binario));
print( "binary " . $binario. " ->> decimal ". $decimale . "\n" );
system("sudo ./lptout " . $decimale);
}
sleep( 1 );
}