From 35f34ea01e12293c9f987cdd69e582d0c88473d7 Mon Sep 17 00:00:00 2001 From: realraum Date: Tue, 28 Apr 2009 19:58:28 +0000 Subject: [PATCH] neuer Tuer Daemon (open problem: status read von ttyUSB0) --- checkcard.pl | 85 ++++++++++++++++++++++++++++++++++++ door_daemon.pl | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ reader.pl | 85 ------------------------------------ 3 files changed, 215 insertions(+), 85 deletions(-) create mode 100755 checkcard.pl create mode 100755 door_daemon.pl delete mode 100755 reader.pl diff --git a/checkcard.pl b/checkcard.pl new file mode 100755 index 0000000..71c1966 --- /dev/null +++ b/checkcard.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl -w + +use strict; +my $fh; +my $fifofile = "/tmp/door_cmd.fifo"; + +my $keys; +my %good; + +open $keys,'/flash/realraum/keys'; +while (<$keys>) +{ + if ($_ =~ /([0-9A-Fa-f]{8})\s(\S+)/) + { + $good{$1}=$2; + } +} + +sub send_to_fifo +{ + if( -p $fifofile) + { + open(my $fifo,"> $fifofile"); + print $fifo shift(@_)."\n"; + close($fifo); + } +} + +while (sleep 1) +{ + open $fh,'/flash/realraum/mifare-read 0 2>&1 |'; + while (<$fh>) + { + next unless /UID/; + my ($id) = /UID=(\S+)\s+/; + if ($good{$id}) + { + send_to_fifo("toggle Card ".$good{$id}); + } else { + send_to_fifo("log InvalidCard $id"); + } + } +} + +############################################################### +# mifare-read +# +# writes: +# UID=<4 byte in hex, upper-case> +# +# +############################################################### +# /dev/ttyUSB0: door key printer +# +# +# Ok +# Ok, closing now +# Already closed +# Already opened +# close forced manually\nOk +# open forced manually\nOk +# Error: .* +# .* be: unknown command +# Operation in progress +# open/close took too long! +# last open/close operation took to long! +# +# commands: +# c ... close +# response: "Ok", "Already closed", "Error: .*" +# o ... open +# response: "Ok", "Already opened", "Error: .*" +# s ... status +# response: "Status: closed|opened|<->, opening|waiting|closing|idle" +# or "Error: .*" +# r ... reset +# "Ok, closing now" or "Error: .*" +# +# open/close will only be accepted if Status: ..., idle +# Reset overrules all other operations in progress +# s will always be accepted +# +############################################################### +# +# diff --git a/door_daemon.pl b/door_daemon.pl new file mode 100755 index 0000000..6d69894 --- /dev/null +++ b/door_daemon.pl @@ -0,0 +1,130 @@ +#!/usr/bin/perl -w +use IO::Handle; +use IO::Select; +use Date::Format; +use strict; + +my $url_door_open = "https://www.realraum.at/"; +my $url_door_closed = "https://www.realraum.at/"; + +my $door_ttyS = "/dev/ttyUSB0"; +my $fifofile = "/tmp/door_cmd.fifo"; +unless( -p $fifofile) +{ + unlink $fifofile; + system("mkfifo -m 600 $fifofile") && die "Can't mkfifo $fifofile: $!"; + system("setfacl -m u:realraum:rw $fifofile"); + system("setfacl -m u:asterisk:rw $fifofile"); +} + +my $logfile; +open($logfile,'>>/var/log/tuer.log'); +$logfile->autoflush(1); +sub door_log +{ + print $logfile Date::Format::time2str("%Y-%m-%d %T: ",time()).shift()."\n"; +} +door_log("Door Daemon started"); + + +$main::door_open=1; +$main::door_closed=0; +$main::tuer_status=$main::door_closed; +#system('wget --no-check-certificate -q -O /dev/null '.$url_door_closed.' &>/dev/null &'); + + +my $fifo=undef; +my $ttyS=undef; +sub handler +{ + #local($sig) = @_; + door_log("Door Daemon stopped"); + close $logfile; + close $fifo if (defined $fifo); + close $ttyS if (defined $ttyS); + exit(0); +} +$SIG{'INT'} = 'handler'; +$SIG{'QUIT'} = 'handler'; +$SIG{'KILL'} = 'handler'; + +while (1) +{ + unless (defined $fifo) { open($fifo,"< $fifofile"); } + unless (defined $ttyS) { open($ttyS,"< $door_ttyS"); } + my $read_set = new IO::Select(); + $read_set->add($fifo); + $read_set->add($ttyS); + + print $ttyS "s\n"; + + do + { + my ($rh_set) = IO::Select->select($read_set, undef, undef); + foreach my $fh (@$rh_set) + { + if ($fh == $fifo) + { + my $fifo_msg = <$fh>; + if ($fifo_msg =~ /^(\w+)\s*(.*)/) + { + handle_cmd($1,$2); + } + } + elsif ($fh == $ttyS) + { + my $ttyS_msg = <$fh>; + #last unless ($ttyS_msg); + print($ttyS_msg); + door_log($door_ttyS.": ".$ttyS_msg); + my $tuer=$main::tuer_status; + $tuer=$main::door_open if $ttyS_msg =~ /open/; + $tuer=$main::door_closed if $ttyS_msg =~ /close/; + if ($tuer != $main::tuer_status) + { + $main::tuer_status=$tuer; + if ($tuer == $main::door_open) + { + system('wget --no-check-certificate -q -O /dev/null '.$url_door_open.' &>/dev/null &'); + } + else + { + system('wget --no-check-certificate -q -O /dev/null '.$url_door_closed.' &>/dev/null &'); + } + } + } + } + } until (eof $fifo or eof $ttyS); + if (eof $fifo) {close($fifo); $fifo=undef;} + if (eof $ttyS) {close($ttyS); $ttyS=undef;} +} + +sub handle_cmd +{ + my $cmd = shift; + my $who = shift; + print "c:'$cmd' w:'$who'\n"; + my $tuer=$main::tuer_status; + if ($cmd eq "open") { $tuer=$main::door_open; } + elsif ($cmd eq "close") {$tuer=$main::door_closed; } + elsif ($cmd eq "toggle") {$tuer=!$tuer;} + elsif ($cmd eq "log") {door_log($who)} + else {door_log("Invalid Command: $cmd $who")} + + if ($tuer != $main::tuer_status) + { + if ($tuer == $main::door_open) + { + door_log("Door opened by $who"); + print $ttyS "o\n"; + system('wget --no-check-certificate -q -O /dev/null '.$url_door_open.' &>/dev/null &'); + } + else + { + door_log("Door closed by $who"); + print $ttyS "c\n"; + system('wget --no-check-certificate -q -O /dev/null '.$url_door_closed.' &>/dev/null &'); + } + + } +} diff --git a/reader.pl b/reader.pl deleted file mode 100755 index 48e9df5..0000000 --- a/reader.pl +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/perl - -use strict; -my $fh; - -my $keys; -my %good; -my $status = 'c'; - -open $keys,'/flash/realraum/keys'; -while (<$keys>) -{ - my ($code,$comment) = split /\s/,$_,2; - $good{$code}=$comment; -} - - -while (sleep 1) -{ - open $fh,'/flash/realraum/mifare-read 0 2>&1 |'; - while (<$fh>) - { - next unless /UID/; - my ($id) = /UID=(\S+)\s+/; - if ($good{$id}) - { - my $newstat; - if ($status eq 'o') - { - $newstat='c'; - - } else { - $newstat = 'o'; - } - print "$newstat: $good{$id}"; - system ( "echo -n $newstat > /dev/ttyUSB0"); - $status = $newstat; - } else { - print "boese\n"; - } - } - -} - -############################################################### -# mifare-read -# -# writes: -# UID=<4 byte in hex, upper-case> -# -# -############################################################### -# /dev/ttyUSB0: door key printer -# -# -# Ok -# Ok, closing now -# Already closed -# Already opened -# close forced manually\nOk -# open forced manually\nOk -# Error: .* -# .* be: unknown command -# Operation in progress -# open/close took too long! -# last open/close operation took to long! -# -# commands: -# c ... close -# response: "Ok", "Already closed", "Error: .*" -# o ... open -# response: "Ok", "Already opened", "Error: .*" -# s ... status -# response: "Status: closed|opened|<->, opening|waiting|closing|idle" -# or "Error: .*" -# r ... reset -# "Ok, closing now" or "Error: .*" -# -# open/close will only be accepted if Status: ..., idle -# Reset overrules all other operations in progress -# s will always be accepted -# -############################################################### -# -# \ No newline at end of file -- 1.7.10.4