listen_to_events
authorBernhard Tittelbach <xro@realraum.at>
Tue, 8 Oct 2013 22:27:08 +0000 (22:27 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 8 Oct 2013 22:27:08 +0000 (22:27 +0000)
listen_to_events.py [new file with mode: 0755]

diff --git a/listen_to_events.py b/listen_to_events.py
new file mode 100755 (executable)
index 0000000..8be98e0
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import os
+import os.path
+import sys
+import signal
+import zmq.utils.jsonapi as json
+import zmq
+########################
+
+def decodeR3Message(multipart_msg):
+    try:
+        return (multipart_msg[0], json.loads(multipart_msg[1]))
+    except Exception, e:
+        logging.debug("decodeR3Message:"+str(e))
+        return ("",{})
+
+def exitHandler(signum, frame):
+  try:
+    zmqsub.close()
+    zmqctx.destroy()
+  except:
+    pass
+  sys.exit(0)
+
+signal.signal(signal.SIGINT, exitHandler)
+signal.signal(signal.SIGQUIT, exitHandler)
+
+while True:
+  try:
+    #Start zmq connection to publish / forward sensor data
+    zmqctx = zmq.Context()
+    zmqctx.linger = 0
+    zmqsub = zmqctx.socket(zmq.SUB)
+    zmqsub.setsockopt(zmq.SUBSCRIBE, "")
+    zmqsub.connect("tcp://wuzzler.realraum.at:4244")
+
+    while True:
+
+      data = zmqsub.recv_multipart()
+      (structname, dictdata) = decodeR3Message(data)
+      print "Got data: " + structname + ":"+ str(dictdata)
+
+  except Exception, ex:
+    logging.error("main: "+str(ex))
+    traceback.print_exc(file=sys.stdout)
+    try:
+      zmqsub.close()
+      zmqctx.destroy()
+    except:
+      pass
+    time.sleep(5)