def main(): try: config = ConfigParser.SafeConfigParser({"accountId":None,"deviceId":None,"dmtp_url":'dmtp://54.84.59.135:21000'}) config.read("/home/pi/attack_on_titan/src/settings.cfg") print config.get("dmtp","dmtp_url") print config.get("dmtp","accountId") print config.get("dmtp","deviceId") logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) ioloop = tornado.ioloop.IOLoop() client = SimpleAsyncDmtpClient(ioloop) def on_response(response): print(response) if response.error: print("errror sending packet") else: print("tag in range sent") @gen.coroutine def on_message(body,consumer,basic_deliver): try: print("received packet") dmtp_packet = pickle.loads(body) dmtp_request = DMTPRequest(\ config.get("dmtp","dmtp_url"),\ config.get("dmtp","accountId"),\ config.get("dmtp","deviceId"),\ packets=[dmtp_packet]) #yield DMTPResponse Object response = yield client.fetch(dmtp_request) print('sent data and got here') if not(response.code == 200): consumer.reject_message(basic_deliver.delivery_tag) raise Exception("Error sending data") consumer.acknowledge_message(basic_deliver.delivery_tag) return except: import sys print sys.exc_info() consumer = Consumer('amqp://*****:*****@localhost:5672/%2F',ioloop,None,on_ack_message=on_message,durable=True) consumer.QUEUE = 'dmtp' consumer.EXCHANGE = 'upstream' consumer.run() ioloop.start() except KeyboardInterrupt: consumer.stop() ioloop.stop()
def main(): try: config = ConfigParser.SafeConfigParser({"accountId":None,"deviceId":None,"dmtp_url":'dmtp://54.84.59.135:21000'}) config.read("/home/pi/attack_on_titan/src/settings.cfg") print config.get("dmtp","dmtp_url") print config.get("dmtp","accountId") print config.get("dmtp","deviceId") logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) ioloop = tornado.ioloop.IOLoop() #tag_dictionary = dict() processed = set() client = None #SimpleAsyncDmtpClient(ioloop) consumer = None producer = None convert = ['rssi','cust_num','flags','battery','tag_num','tag_seq','recv_bytes','switch_count'] def on_message(body): #pp = ProtracPacket(**json.loads(body)) #print("on message") #print(body) try: d = json.loads(body) tag_dictionary[d['tag_num']] = d except: logging.error("exception in asynchronous operation",exc_info=True) logging.info("finished processing message") #raise "Dont want to remove off queue" def on_response(response): print(response) if response.error: print("errror sending packet") else: print("tag in range sent") def on_timer(): print("running timer") global client global tag_dictionary global producer for k,d in tag_dictionary.iteritems(): print("processing packet %s"%k) for prop in convert: d[prop] = int(d[prop],16) print(d) try: d['rssi'] = d['rssi']-128 dmtp_packet = DMTPTagInRangePacket(**d) dmtp_request = DMTPRequest(\ config.get("dmtp","dmtp_url"),\ config.get("dmtp","accountId"),\ config.get("dmtp","deviceId"),\ packets=[dmtp_packet]) #client.fetch(dmtp_request,on_response) producer.send_message(pickle.dumps(dmtp_packet)) except: logging.error("exception in asynchronous operation",exc_info=True) tag_dictionary = dict() print("clear dictionary") return consumer = Consumer('amqp://*****:*****@localhost:5672/%2F',ioloop,on_message) #producer = Producer('amqp://*****:*****@localhost:5672/%2F',ioloop) #producer.QUEUE = "dmtp" #producer.EXCHANGE = "upstream" #consumer.run() #producer.run() print("Start timer") def run_loop(on_timer): thread_ioloop = tornado.ioloop.IOLoop() global client global producer producer = Producer('amqp://*****:*****@localhost:5672/%2F',thread_ioloop,durable=True) producer.QUEUE = "dmtp" producer.EXCHANGE = "upstream" client = SimpleAsyncDmtpClient(thread_ioloop) periodic_callback = tornado.ioloop.PeriodicCallback(on_timer,\ config.getint("dmtp","timer"),io_loop=thread_ioloop) periodic_callback.start() producer.run() thread_ioloop.start() thread = Thread(target = run_loop,args=(on_timer,)) thread.daemon = True thread.start() #periodic_callback = tornado.ioloop.PeriodicCallback(on_timer, 10000,io_loop=ioloop) #periodic_callback.start() consumer.run() ioloop.start() except KeyboardInterrupt: consumer.stop() #producer.stop() ioloop.stop()