zwischenstand
authorrealraum <realraum@realraum.at>
Tue, 28 Apr 2009 21:48:54 +0000 (21:48 +0000)
committerrealraum <realraum@realraum.at>
Tue, 28 Apr 2009 21:48:54 +0000 (21:48 +0000)
door_daemon.pl

index 6d69894..fba8a5d 100755 (executable)
@@ -1,13 +1,15 @@
 #!/usr/bin/perl -w
+use POSIX qw();
 use IO::Handle;
 use IO::Select; 
 use Date::Format;
+use Fcntl; 
 use strict;
 
 my $url_door_open = "https://www.realraum.at/";
 my $url_door_closed = "https://www.realraum.at/";
 
-my $door_ttyS = "/dev/ttyUSB0";
+my $door_ttyusb_dev = "/dev/ttyUSB0";
 my $fifofile = "/tmp/door_cmd.fifo";
 unless( -p $fifofile) 
 {
@@ -34,14 +36,14 @@ $main::tuer_status=$main::door_closed;
 
 
 my $fifo=undef;
-my $ttyS=undef;
+my $ttyusb=undef;
 sub handler
 {
   #local($sig) = @_;
   door_log("Door Daemon stopped");
   close $logfile;
   close $fifo if (defined $fifo);
-  close $ttyS if (defined $ttyS);
+  close $ttyusb if (defined $ttyusb);
   exit(0);
 }
 $SIG{'INT'} = 'handler';
@@ -50,53 +52,68 @@ $SIG{'KILL'} = 'handler';
 
 while (1)
 {
-       unless (defined $fifo) { open($fifo,"< $fifofile"); }
-       unless (defined $ttyS) { open($ttyS,"< $door_ttyS"); }
+       unless (defined $ttyusb) {
+               print "open usb\n";
+               sysopen($ttyusb, $door_ttyusb_dev, O_RDWR | O_NONBLOCK); 
+               $ttyusb->autoflush(1);
+               my $termios = POSIX::Termios->new;
+               $termios->getattr(fileno $ttyusb);
+               $termios->setispeed( &POSIX::B9600 );
+               $termios->setospeed( &POSIX::B9600 );
+               #$termios->setcflag( $termios->getcflag & ~(&POSIX::PARENB | &POSIX::PARODD) & (~&POSIX::CSIZE | &POSIX::CS8));
+               $termios->setattr(fileno $ttyusb);
+               print "x\n";
+       }
+       unless (defined $fifo) {print "open fifo\n"; sysopen($fifo,$fifofile, O_RDONLY | O_NONBLOCK); print "x\n";}
        my $read_set = new IO::Select();
         $read_set->add($fifo); 
-        $read_set->add($ttyS); 
-        
-       print $ttyS "s\n";
+        $read_set->add($ttyusb); 
+       print $ttyusb "s";
         
        do
        {
+               print $main::tuer_status,"\n";
                my ($rh_set) = IO::Select->select($read_set, undef, undef);
                foreach my $fh (@$rh_set)
                {
                        if ($fh == $fifo)
                        {
                                my $fifo_msg = <$fh>;
+                               last unless ($fifo_msg);
                                if ($fifo_msg =~ /^(\w+)\s*(.*)/) 
                                {
                                        handle_cmd($1,$2);
                                }
                        }
-                       elsif ($fh == $ttyS)
+                       elsif ($fh == $ttyusb)
                        {
-                               my $ttyS_msg = <$fh>;
-                               #last unless ($ttyS_msg);
-                               print($ttyS_msg);
-                               door_log($door_ttyS.": ".$ttyS_msg);
+                               my $ttyusb_msg = <$fh>;
+                               last unless ($ttyusb_msg);
+                               print($ttyusb_msg);
+                               door_log($door_ttyusb_dev.": ".$ttyusb_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)
+                               $tuer=$main::door_open if $ttyusb_msg =~ /open/;
+                               $tuer=$main::door_closed if $ttyusb_msg =~ /close/;
+                               if (not $tuer == $main::tuer_status)
                                {
                                        $main::tuer_status=$tuer;
                                        if ($tuer == $main::door_open)
                                        {
+                                               print "change to open\n";
                                                system('wget --no-check-certificate -q -O /dev/null '.$url_door_open.' &>/dev/null &');
                                        }
                                        else
                                        {
+                                               print "change to closed\n";
                                                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;}
+       } until(0); #until (eof $fifo or eof $ttyusb);
+       print("eof\n");
+       if (eof $fifo) {print "eof fifo\n"; close($fifo); $fifo=undef; print "closed fifo\n";}
+       if (eof $ttyusb) {print "eof ttyusb\n"; close($ttyusb); $ttyusb=undef; print "closed ttyusb\n";}
 }
 
 sub handle_cmd
@@ -107,22 +124,22 @@ sub handle_cmd
        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 "toggle") {$tuer= !$tuer;}
        elsif ($cmd eq "log") {door_log($who)}
        else {door_log("Invalid Command: $cmd $who")}
        
-       if ($tuer != $main::tuer_status)
+       if (not $tuer == $main::tuer_status)
        {
                if ($tuer == $main::door_open)
                {
                        door_log("Door opened by $who");
-                       print $ttyS "o\n";
+                       print $ttyusb "o";
                        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";
+                       print $ttyusb "c";
                        system('wget --no-check-certificate -q -O /dev/null '.$url_door_closed.' &>/dev/null &');
                }