From: Bernhard Tittelbach Date: Tue, 8 Oct 2013 22:27:08 +0000 (+0000) Subject: listen_to_events X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=d317256ea5a947ec54106bab83309750bd9d8161;p=svn42.git listen_to_events --- 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)