eingestampft wenn auch noch funktional
authorrealraum <realraum@realraum.at>
Tue, 1 Mar 2011 00:12:03 +0000 (00:12 +0000)
committerrealraum <realraum@realraum.at>
Tue, 1 Mar 2011 00:12:03 +0000 (00:12 +0000)
door_daemon.pl [deleted file]
door_daemon.py [deleted file]

diff --git a/door_daemon.pl b/door_daemon.pl
deleted file mode 100755 (executable)
index ce14364..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-#!/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/cgi/status.cgi?pass=jako16&set=%3Chtml%3E%3Cbody%20bgcolor=%22lime%22%3E%3Ch3%3E%3Ccenter%3ETuer%20ist%20Offen%3C/center%3E%3C/h3%3E%3C/body%3E%3C/html%3E';
-my $url_door_closed = 'https://www.realraum.at/cgi/status.cgi?pass=jako16&set=%3Chtml%3E%3Cbody%20bgcolor=%22red%22%3E%3Ch3%3E%3Ccenter%3ETuer%20ist%20Geschlossen%3C/center%3E%3C/h3%3E%3C/body%3E%3C/html%3E';
-
-my $door_ttyusb_dev = "/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
-{
-  my $msg=shift;
-  chomp($msg);
-  print $logfile Date::Format::time2str("%Y-%m-%d %T: ",time()).$msg."\n";
-}
-door_log("Door Daemon started");
-
-
-$main::door_open=1;
-$main::door_closed=0;
-$main::tuer_status=$main::door_closed;
-$main::tuer_future_status=$main::tuer_status;
-#system('wget --no-check-certificate -q -O /dev/null '.$url_door_closed.' &>/dev/null &');
-
-
-my $fifo=undef;
-my $ttyusb=undef;
-sub handler
-{
-  #local($sig) = @_;
-  print $ttyusb "c" if (defined $ttyusb);
-  door_log("Door Daemon stopped");
-  close $logfile;
-  close $fifo if (defined $fifo);
-  close $ttyusb if (defined $ttyusb);
-  exit(0);
-}
-$SIG{'INT'} = 'handler';
-$SIG{'QUIT'} = 'handler';
-$SIG{'KILL'} = 'handler';
-
-my $read_set = new IO::Select();
-
-sub open_fifo
-{
-       #print "open fifo\n"; 
-       sysopen($fifo,$fifofile, O_RDONLY | O_NONBLOCK); 
-       #print "x\n";
-        $read_set->add($fifo); 
-}
-
-sub open_usb
-{
-       #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";
-       $read_set->add($ttyusb); 
-}
-
-sub close_fifo
-{
-       $read_set->remove($fifo);
-       close($fifo);
-}
-
-sub close_usb
-{
-       $read_set->remove($ttyusb);
-       close($ttyusb);
-}
-
-&open_usb;
-&open_fifo;
-
-print $ttyusb "s";
-        
-while(1)
-{
-       my ($rh_set) = IO::Select->select($read_set, undef, undef);
-       #print "tuer_status_start: ".$main::tuer_status,"\n";
-       sleep(1); #give other end time to finish writing... (bad fix)
-       foreach my $fh (@$rh_set)
-       {
-               if ($fh == $fifo)
-               {
-                       my $fifo_msg = readline($fh);
-                       unless ($fifo_msg)
-                       {
-                               close_fifo();
-                               #sleep(0.1);
-                               open_fifo();
-                               last;
-                       }
-                       if ($fifo_msg =~ /^(\w+)\s*(.*)/) 
-                       {
-                               handle_cmd($1,$2);
-                       }
-               }
-               elsif ($fh == $ttyusb)
-               {
-                       my $ttyusb_msg = readline($fh);
-                       last unless ($ttyusb_msg);
-                       #print($ttyusb_msg);
-                       door_log($door_ttyusb_dev.": ".$ttyusb_msg);
-                       if ($ttyusb_msg =~ /took too long!/)
-                       {
-                               door_log("Got '".$ttyusb_msg."'.  Sending Reset..");
-                               print $ttyusb "r";
-                               $main::tuer_status=$main::door_closed;
-                               $main::tuer_future_status=$main::tuer_status;
-                               last;
-                       }                       
-                       $main::tuer_status = $main::tuer_future_status if $ttyusb_msg =~ /^Ok/;
-                       my $tuer=$main::tuer_status;
-                       $tuer=$main::door_open if $ttyusb_msg =~ /open/;
-                       $tuer=$main::door_closed if $ttyusb_msg =~ /close|closing/;
-                       door_log("$tuer");
-                       if (not ($tuer == $main::tuer_status))
-                       {
-                               $main::tuer_status=$tuer;
-                               if ($tuer == $main::door_open)
-                               {
-                                       door_log("change to opened");
-                                       #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";
-                                       door_log("change to closed");
-                                       system('wget --no-check-certificate -q -O /dev/null "'.$url_door_closed.'" &>/dev/null &');
-                               }
-                       }
-               }
-       }
-       #print "tuer_status_end: ".$main::tuer_status,"\n------------\n";
-}
-
-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 (not $tuer == $main::tuer_status)
-       {
-               $main::tuer_future_status=$tuer;
-               if ($tuer == $main::door_open)
-               {
-                       door_log("Door opened by $who");
-                       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 $ttyusb "c";
-                       system('wget --no-check-certificate -q -O /dev/null "'.$url_door_closed.'" &>/dev/null &');
-               }
-               
-       }       
-}
diff --git a/door_daemon.py b/door_daemon.py
deleted file mode 100755 (executable)
index f17083b..0000000
+++ /dev/null
@@ -1,219 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-import os
-import sys
-import threading
-import logging
-import urllib
-import time
-import signal
-import re
-import socket
-import serial
-
-logging.basicConfig(level=logging.INFO,filename='/var/log/tmp/tuer.log',format="%(asctime)s %(message)s",datefmt="%Y-%m-%d %H:%M")
-
-class StatusDisplay():
-  def __init__(self):
-    self.url_open = 'https://www.realraum.at/cgi/status.cgi?pass=jako16&set=%3Chtml%3E%3Cbody%20bgcolor=%22lime%22%3E%3Ccenter%3E%3Cb%3ET%26uuml%3Br%20ist%20Offen%3C/b%3E%3C/center%3E%3C/body%3E%3C/html%3E'
-    self.url_closed = 'https://www.realraum.at/cgi/status.cgi?pass=jako16&set=%3Chtml%3E%3Cbody%20bgcolor=%22red%22%3E%3Cb%3E%3Ccenter%3ET%26uuml%3Br%20ist%20Geschlossen%3C/center%3E%3C/b%3E%3C/body%3E%3C/html%3E'
-    self.last_status_set=""
-    #object.__init__(self)
-    
-  def display_open(self):
-    if self.last_status_set != self.url_open:
-      self.last_status_set=self.url_open
-      try:
-        #print "accessing %s\n" % self.last_status_set
-        f = urllib.urlopen(self.last_status_set)
-        f.read()
-        f.close()
-      except:
-        pass
-
-  def display_closed(self):
-    if self.last_status_set != self.url_closed:
-      self.last_status_set=self.url_closed
-      try:
-        #print "accessing %s\n" % self.last_status_set
-        f = urllib.urlopen(self.last_status_set)
-        f.read()
-        f.close()
-      except:
-        pass
-
-class ArduinoUSBThread ( threading.Thread ):
-  def __init__(self, file_dev_ttyusb):
-    self.re_isidle = re.compile(r'idle')
-    self.re_isopen = re.compile(r'Status: opened, idle')
-    self.re_isclosed = re.compile(r'Status: closed, idle')
-    self.re_toolong = re.compile(r'took too long!')
-    self.min_seconds_between_reset=10;
-    self.timestamp_send_reset=0;
-    self.running=True
-    self.lastline=""
-    self.last_status=None
-    self.shortsleep = 0
-    self.cv_updatestatus = threading.Condition(); #lock ist automatically created withing condition
-    self.file_dev_ttyusb=file_dev_ttyusb
-    self.dev = serial.Serial(port=file_dev_ttyusb, baudrate=9600,timeout=60)
-    #pythons sucks just like perl: we need nonblock or we can't write to FileHandle while we block reading on same filehandle
-    self.statusdisplay = StatusDisplay()
-    threading.Thread.__init__(self)
-    
-  def stop(self):
-    self.running=False
-    if (self.dev.isOpen()):
-      self.dev.close()
-
-  def send_open(self):
-    self.send_statusrequest()
-    self.cv_updatestatus.acquire()
-    if self.re_isidle.search(self.lastline):
-      logging.info("Opening Door")
-      self.dev.write("o");
-    self.cv_updatestatus.release()
-    
-  def send_close(self):
-    self.send_statusrequest()
-    self.cv_updatestatus.acquire()
-    if self.re_isidle.search(self.lastline):
-      logging.info("Closing Door")
-      self.dev.write("c");
-    self.cv_updatestatus.release()
-      
-  def send_toggle(self):
-    self.send_statusrequest()
-    self.cv_updatestatus.acquire()
-    if self.re_isidle.search(self.lastline):
-      logging.info("Toggling Door")
-      self.dev.write("t");
-    self.cv_updatestatus.release()
-    
-  def send_reset(self):
-    self.shortsleep = 20
-    logging.info("Resetting Door")    
-    self.dev.write("r");
-
-  def send_statusrequest(self):
-    self.shortsleep = 20
-    self.dev.write("s");
-    self.cv_updatestatus.acquire()
-    self.cv_updatestatus.wait(5.0)
-    self.cv_updatestatus.release()        
-
-  def run (self):
-    if not self.dev.isOpen():
-      self.dev.open()
-    while (self.running and self.dev.isOpen()):
-      line = self.dev.readline()
-      if len(line) == 0:
-        continue
-      self.cv_updatestatus.acquire()
-      self.lastline=line.strip()
-      logging.info(self.file_dev_ttyusb+": "+self.lastline)
-      if self.re_isclosed.search(self.lastline):
-        self.last_status="closed"
-        self.statusdisplay.display_closed()
-      elif self.re_isopen.search(self.lastline):
-        self.last_status="open"
-        self.statusdisplay.display_open()
-      elif self.re_toolong.search(self.lastline):
-        self.last_status="error"
-        if (time.time() - self.timestamp_send_reset) > self.min_seconds_between_reset:
-          self.timestamp_send_reset=time.time()
-          self.send_reset()
-      self.cv_updatestatus.notifyAll()
-      self.cv_updatestatus.release()
-    if self.dev.isOpen():
-      self.dev.close()
-
-class ControlFIFOThread ( threading.Thread ):
-  def __init__(self, file_fifo, arduino):
-    self.running=True
-    self.file_fifo=file_fifo
-    self.arduino = arduino
-    self.re_cmd = re.compile(r'^(\w+)\s*(.*)')
-    threading.Thread.__init__(self)
-  
-  def stop(self):
-    self.running=False
-    self.dev.close()
-  
-  def run (self):
-    self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-    try:
-        os.remove(self.file_fifo)
-    except OSError:
-        pass
-    self.socket.bind(self.file_fifo)
-    self.socket.listen(1)
-    while (self.running):
-      self.socketconn, addr = self.socket.accept()
-      self.conn = os.fdopen(self.socketconn.fileno())
-      while 1:
-        #~ line=""
-        #~ while 1:
-          #~ print "d"
-          #~ data = self.conn.recv(1024)
-          #~ if not data: 
-            #~ break
-          #~ else:
-            #~ line+= data
-        line=self.conn.readline()
-        if not line:
-          break
-        m = self.re_cmd.match(line)
-        if not m is None:
-          (cmd,who) = m.group(1,2)
-          if cmd == "open":
-            logging.info("Open Requested by %s" % who)
-            arduino.send_open()
-          elif cmd == "close":
-            logging.info("Close Requested by %s" % who)
-            arduino.send_close()
-          elif cmd == "toggle":
-            logging.info("Toggle Requested by %s" % who)
-            arduino.send_toggle()
-          elif cmd == "reset":
-            logging.info("Reset Requested by %s" % who)
-            arduino.send_reset()
-          elif cmd == "status":
-            arduino.send_statusrequest()
-            
-          elif cmd == "log":
-            logging.info(who)
-          else:
-            logging.info("Invalid Command %s %s" % (cmd,who))
-      self.conn.close()
-      self.socketconn.close()
-    if self.socket:
-      self.socket.shutdown(socket.SHUT_RDWR)
-
-
-
-fifofile = "/tmp/door_cmd.socket"
-
-arduino = ArduinoUSBThread("/dev/ttyUSB0")
-arduino.start()
-ctrlfifo = ControlFIFOThread(fifofile,arduino)
-ctrlfifo.start()
-
-arduino.send_statusrequest()
-
-def exit_handler(signum, frame):
-  global arduino, ctrlfifo
-  logging.info("Door Daemon stopping")
-  arduino.send_close()
-  ctrlfifo.stop()
-  arduino.stop()
-  sys.exit(0)
-  
-#signals proapbly don't work because of readline
-#signal.signal(signal.SIGTERM, exit_handler)
-signal.signal(signal.SIGINT, exit_handler)
-signal.signal(signal.SIGQUIT, exit_handler)
-
-logging.info("Door Daemon started")
-arduino.join()
-ctrlfifo.join()