From d317256ea5a947ec54106bab83309750bd9d8161 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Tue, 8 Oct 2013 22:27:08 +0000 Subject: [PATCH] listen_to_events --- listen_to_events.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 listen_to_events.py diff --git a/listen_to_events.py b/listen_to_events.py new file mode 100755 index 0000000..8be98e0 --- /dev/null +++ b/listen_to_events.py @@ -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) -- 1.7.10.4