X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=door_daemon.py;h=60d0cb21dd922b2212595ff86c9a66217f9ebd23;hb=a3b81ec5089fbd974fff0f711c093c2033b1615a;hp=8ff8435962d65ff2e039c8a8ec0ba1e65a7e5ccb;hpb=cd2f45daa92bf1390ff69a60729e45318ff8b9d6;p=svn42.git diff --git a/door_daemon.py b/door_daemon.py index 8ff8435..60d0cb2 100755 --- a/door_daemon.py +++ b/door_daemon.py @@ -14,15 +14,15 @@ logging.basicConfig(level=logging.INFO,filename='/var/log/tuer.log',format="%(as 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%3Ch3%3E%3Ccenter%3ETuer%20ist%20Offen%3C/center%3E%3C/h3%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%3Ch3%3E%3Ccenter%3ETuer%20ist%20Geschlossen%3C/center%3E%3C/h3%3E%3C/body%3E%3C/html%3E'; + 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 - print "accessing %s\n" % self.last_status_set + #print "accessing %s\n" % self.last_status_set f = urllib.urlopen(self.last_status_set) f.read() f.close() @@ -30,7 +30,7 @@ class StatusDisplay(): def display_closed(self): if self.last_status_set != self.url_closed: self.last_status_set=self.url_closed - print "accessing %s\n" % self.last_status_set + #print "accessing %s\n" % self.last_status_set f = urllib.urlopen(self.last_status_set) f.read() f.close() @@ -39,18 +39,20 @@ class StatusDisplay(): class ArduinoUSBThread ( threading.Thread ): def __init__(self, file_dev_ttyusb): self.re_isidle = re.compile(r'idle') - self.re_isopen = re.compile(r'open') - self.re_isclosed = re.compile(r'close|closing') + 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.fh = open(self.file_dev_ttyusb,"w+") - self.fh = os.fdopen(os.open(self.file_dev_ttyusb, os.O_RDWR | os.O_NONBLOCK ),"r+") + self.fh = os.fdopen(os.open(self.file_dev_ttyusb, os.O_RDWR | os.O_NONBLOCK),"r+") + #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) @@ -62,37 +64,48 @@ class ArduinoUSBThread ( threading.Thread ): def send_open(self): self.send_statusrequest() + self.cv_updatestatus.acquire() if self.re_isidle.search(self.lastline): logging.info("Opening Door") print("\nSending o..") self.fh.write("o"); print("done\n") + 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") print("\nSending c..") self.fh.write("c"); print("done\n") + self.cv_updatestatus.release() def send_toggle(self): self.send_statusrequest() + self.cv_updatestatus.acquire() if self.last_status == "open": + self.cv_updatestatus.release() self.send_close() elif self.last_status == "closed": + self.cv_updatestatus.release() self.send_open() + else: + self.cv_updatestatus.release() def send_reset(self): - logging.info("Resetting Door") - print("\nSending r..") + self.shortsleep = 20 + logging.info("Resetting Door") + #print("\nSending r..") self.fh.write("r"); - print("done\n") + #print("done\n") def send_statusrequest(self): - print("\nSending s..") + self.shortsleep = 20 + #print("\nSending s..") self.fh.write("s"); - print("done\n") + #print("done\n") self.cv_updatestatus.acquire() self.cv_updatestatus.wait(5.0) self.cv_updatestatus.release() @@ -102,18 +115,21 @@ class ArduinoUSBThread ( threading.Thread ): try: line = self.fh.readline(); except IOError, e: - time.sleep(1.0) + if self.shortsleep > 0: + time.sleep(0.05) + self.shortsleep -= 1 + else: + time.sleep(0.5) continue - print "l" self.cv_updatestatus.acquire() - self.lastline=line + 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_open() + self.statusdisplay.display_closed() elif self.re_isopen.search(self.lastline): self.last_status="open" - self.statusdisplay.display_closed() + 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: @@ -193,13 +209,13 @@ class ControlFIFOThread ( threading.Thread ): fifofile = "/tmp/door_cmd.socket" -logging.info("Door Daemon started") - 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") @@ -208,6 +224,11 @@ def exit_handler(signum, frame): arduino.stop() sys.exit(0) -signal.signal(signal.SIGTERM, exit_handler) +#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()