PresenceTracker line write-through, xmpp-status announces presence
authorBernhard Tittelbach <xro@realraum.at>
Fri, 5 Mar 2010 12:52:30 +0000 (12:52 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Fri, 5 Mar 2010 12:52:30 +0000 (12:52 +0000)
track-presence.py
update-web-status.py
update-xmpp-status.py

index 0d76030..ddf3f2e 100644 (file)
@@ -138,7 +138,7 @@ class UWSConfig:
 
 ######## Status Listener Threads ############
 
-def trackSensorStatusThread(uwscfg,status_tracker):
+def trackSensorStatusThread(uwscfg,status_tracker,connection_listener):
   #RE_TEMP = re.compile(r'temp\d: (\d+\.\d+)')
   RE_PHOTO = re.compile(r'photo\d: (\d+\.\d+)')
   RE_MOVEMENT = re.compile(r'movement|button\d?')
@@ -161,6 +161,8 @@ def trackSensorStatusThread(uwscfg,status_tracker):
           raise Exception("EOF on Subprocess, daemon seems to have quit")
         if not sshp.poll() is None:
           raise Exception("trackSensorStatusThread: subprocess %d finished, returncode: %d" % (sshp.pid,sshp.returncode))
+          
+        connection_listener.distributeData(line)
         m = RE_MOVEMENT.match(line)
         if not m is None:
           status_tracker.movementDetected()
@@ -189,7 +191,7 @@ def trackSensorStatusThread(uwscfg,status_tracker):
       time.sleep(5)  
   
 
-def trackDoorStatusThread(uwscfg, status_tracker):
+def trackDoorStatusThread(uwscfg, status_tracker,connection_listener):
   #socket.setdefaulttimeout(10.0) #affects all new Socket Connections (urllib as well)
   RE_STATUS = re.compile(r'Status: (\w+), idle')
   RE_REQUEST = re.compile(r'Request: (\w+) (?:Card )?(.+)')
@@ -213,6 +215,7 @@ def trackDoorStatusThread(uwscfg, status_tracker):
         if line == "":
           raise Exception("EOF on Socket, daemon seems to have quit")
         
+        connection_listener.distributeData(line)
         m = RE_STATUS.match(line)
         if not m is None:
           (status,who) = m.group(1,2)
@@ -319,15 +322,17 @@ class ConnectionListener:
   
   def statusString(self,somebody_present):
     if somebody_present:
-      return "Status: people present" + "\n"
+      return "Presence: yes" + "\n"
     else:
-      return "Status: room empty" + "\n"
+      return "Presence: no" + "\n"
   
   def updateStatus(self,somebody_present):
-    presence_status_data = self.statusString(somebody_present)
+    self.distributeData(self.statusString(somebody_present))
+    
+  def distributeData(self,data):
     with self.lock:
       for socket_to_send_to in self.client_sockets:
-        socket_to_send_to.send(presence_status_data)
+        socket_to_send_to.send(data)
     
   def serve(self):
     self.server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -342,8 +347,7 @@ class ConnectionListener:
       for socket_to_read in ready_to_read:
         if socket_to_read == self.server_socket:
           newsocketconn, addr = self.server_socket.accept()
-          presence_status_data = self.statusString(self.status_tracker.somebodyPresent())
-          newsocketconn.send(presence_status_data)
+          newsocketconn.send(self.statusString(self.status_tracker.somebodyPresent()))
           with self.lock:
             self.client_sockets.append(newsocketconn)
         else:
@@ -378,12 +382,14 @@ else:
 
 #Status Tracker keeps track of stuff and derives peoples presence from current state
 status_tracker = StatusTracker(uwscfg)
+#ConnectionListener servers incoming socket connections and distributes status update
+connection_listener = ConnectionListener(uwscfg, status_tracker)
 #Thread listening for door status changes
-track_doorstatus_thread = threading.Thread(target=trackDoorStatusThread,args=(uwscfg,status_tracker),name="trackDoorStatusThread")
+track_doorstatus_thread = threading.Thread(target=trackDoorStatusThread,args=(uwscfg,status_tracker,connection_listener),name="trackDoorStatusThread")
 track_doorstatus_thread.start()
 #Thread listening for movement
-track_sensorstatus_thread = threading.Thread(target=trackSensorStatusThread,args=(uwscfg,status_tracker),name="trackSensorStatusThread")
+track_sensorstatus_thread = threading.Thread(target=trackSensorStatusThread,args=(uwscfg,status_tracker,connection_listener),name="trackSensorStatusThread")
 track_sensorstatus_thread.start()
-#ConnectionListener servers incoming socket connections and distributes status update
-connection_listener = ConnectionListener(uwscfg, status_tracker)
+
+#main routine: serve connections
 connection_listener.serve()
\ No newline at end of file
index ef2535e..1011461 100755 (executable)
@@ -191,7 +191,8 @@ else:
   uwscfg = UWSConfig()
 
 #socket.setdefaulttimeout(10.0) #affects all new Socket Connections (urllib as well)
-RE_STATUS = re.compile(r'Status: (people present|room empty)')
+#RE_STATUS = re.compile(r'Status: (\w+), idle')
+RE_PRESENCE = re.compile(r'Presence: (yes|no)')
 while True:
   try:
     if not os.path.exists(uwscfg.tracker_socket):
@@ -212,12 +213,12 @@ while True:
       if line == "":
         raise Exception("EOF on Socket, daemon seems to have quit")
       
-      m = RE_STATUS.match(line)
+      m = RE_PRESENCE.match(line)
       if not m is None:
         status = m.group(1)
-        if status == "people present":
+        if status == "yes":
           displayOpen()
-        if status == "room empty":
+        else:
           displayClosed()
   except Exception, ex:
     logging.error("main: "+str(ex)) 
index 61615e6..14c8007 100755 (executable)
@@ -41,6 +41,8 @@ class UWSConfig:
     self.config_parser.set('msg','comment_msg',"\s(${comment})")
     self.config_parser.set('msg','status_still_opened_msg',"Door remains closed")
     self.config_parser.set('msg','status_still_closed_msg',"Door remains open")
+    self.config_parser.add_section('tracker')
+    self.config_parser.set('tracker','socket',"/var/run/tuer/presence.socket")        
     self.config_parser.add_section('debug')
     self.config_parser.set('debug','enabled',"False")
     self.config_mtime=0
@@ -187,7 +189,13 @@ def distributeXmppMsg(msg,high_priority=False,debug=False):
     sendXmppMsg(uwscfg.xmpp_recipients_nooffline, msg, noofflinemsg=(not high_priority))
   else:
     sendXmppMsg(uwscfg.xmpp_recipients_debug, "D: " + msg)
-  
+
+def formatAndDistributePresence(presence):
+  if presence == "yes":
+    distributeXmppMsg("Somebody is present right now")
+  else:
+    distributeXmppMsg("Nobody is here, everybody left")
+
 current_status = (None, None, None) 
 def filterAndFormatMessage(new_status):
   global current_status
@@ -250,32 +258,28 @@ signal.signal(signal.SIGINT, exitHandler)
 signal.signal(signal.SIGQUIT, exitHandler)
 
 logging.info("Update-Xmpp-Status started")
-
 if len(sys.argv) > 1:
-  socketfile = sys.argv[1]
-else:
-  socketfile = "/var/run/tuer/door_cmd.socket"
-  
-if len(sys.argv) > 2:
-  uwscfg = UWSConfig(sys.argv[2])
+  uwscfg = UWSConfig(sys.argv[1])
 else:
   uwscfg = UWSConfig()
 
 distributeXmppMsg("update-xmpp-status.py started", debug=True)
 RE_STATUS = re.compile(r'Status: (\w+), idle')
 RE_REQUEST = re.compile(r'Request: (\w+) (?:Card )?(.+)')
+RE_PRESENCE = re.compile(r'Presence: (yes|no)')
 RE_ERROR = re.compile(r'Error: (.+)')
 while True:
   try:
-    if not os.path.exists(socketfile):
-      logging.debug("Socketfile '%s' not found, waiting 5 secs" % socketfile)
+    if not os.path.exists(uwscfg.tracker_socket):
+      logging.debug("Socketfile '%s' not found, waiting 5 secs" % uwscfg.tracker_socket)
       time.sleep(5)
       continue
     sockhandle = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-    sockhandle.connect(socketfile)
+    sockhandle.connect(uwscfg.tracker_socket)
     conn = os.fdopen(sockhandle.fileno())
-    sockhandle.send("listen\n")
-    sockhandle.send("status\n")
+    #sockhandle.send("listen\n")
+    #sockhandle.send("status\n")
     last_request = (None, None)
     while True:
       line = conn.readline()
@@ -286,23 +290,30 @@ while True:
       if line == "":
         raise Exception("EOF on Socket, daemon seems to have quit")      
       
+      m = RE_PRESENCE.match(line)
+      if not m is None:
+        formatAndDistributePresence(m.group(1))
+        continue
       m = RE_STATUS.match(line)
       if not m is None:
         status = m.group(1)
         filterAndFormatMessage((status,) + last_request)
+        last_request = (None, None)
+        continue
       m = RE_REQUEST.match(line)
       if not m is None:  
         last_request = m.group(1,2)
-      else:
-        last_request = (None, None)
+        continue
       m = RE_ERROR.match(line)
       if not m is None:
         errorstr = m.group(1)
         if "too long!" in errorstr:
           filterAndFormatMessage(("error",) + last_request)
+          last_request = (None, None)
         else:
           logging.error("Recieved Error: "+errorstr)
           distributeXmppMsg("Error: "+errorstr, debug=True)
+          last_request = (None, None)
   except Exception, ex:
     logging.error("main: "+str(ex)) 
     try: