From 2c8dad91649b4015cfe92cb30389946bef8ab714 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 29 Apr 2019 09:04:37 +0000 Subject: [PATCH] Remove debug messages and sigint_handler. Create user_data_set to set the redis client. #180 --- .../rider_location_cache.py | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/utils/rider_location_cache/rider_location_cache.py b/utils/rider_location_cache/rider_location_cache.py index cbd1bd4b..504884e3 100644 --- a/utils/rider_location_cache/rider_location_cache.py +++ b/utils/rider_location_cache/rider_location_cache.py @@ -12,6 +12,8 @@ import json class RiderLocationCache(object): def run(self): + client = mqtt.Client() + client.on_connect = on_connect # client.on_publish = on_publish client.on_message = on_message @@ -32,58 +34,56 @@ class RiderLocationCache(object): #signal.signal(signal.SIGINT, sigint_handler) client.loop_forever() -client = mqtt.Client() -redis_conn = redis.StrictRedis(host='localhost', port=6379, db=0) - def init_subscriptions(client): - print "subscribing to wildcard" - client.subscribe('#') + #print "subscribing to wildcard" + client.subscribe('#') def on_connect(client, userdata, flags, rc): - init_subscriptions(client) - print("Connected with result code "+str(rc)) - # client.subscribe("$SYS/#") + init_subscriptions(client) + #print("Connected with result code "+str(rc)) + # client.subscribe("$SYS/#") + +def user_data_set(userdata): + conn = redis.StrictRedis(host='localhost', port=6379, db=0) + return conn def on_publish(client, userdata, mid): - pass + pass def on_message(client, userdata, message): - print("message topic=",message.topic[0:10]) + redis_conn = user_data_set(userdata) + #print("message topic=", message.topic[0:10]) - if message.topic[0:10] != 'motorider_': - return - #print repr(message) + if message.topic[0:10] != 'motorider_': + return + #print repr(message) - # check if json decodable - res = json.loads(message.payload) - print res + # check if json decodable + res = json.loads(message.payload) + #print res - # get rider session id - sess_id = message.topic[10:] + # get rider session id + sess_id = message.topic[10:] - # check if it has event - if 'event' not in res: - return + # check if it has event + if 'event' not in res: + return - # check if event is driver_location - if res['event'] != 'driver_location': - return + # check if event is driver_location + if res['event'] != 'driver_location': + return - # save the longitude and latitude - # get the rider id from sess_id - rider_key = "rider.location.%s" % sess_id - rider_long = str(res['longitude']) - rider_lat = str(res['latitude']) + # save the longitude and latitude + # get the rider id from sess_id + rider_key = "rider.location.%s" % sess_id + rider_long = str(res['longitude']) + rider_lat = str(res['latitude']) - # set the location - redis_conn.hmset(rider_key, {'longitude': rider_long, 'latitude': rider_lat}) + # set the location + redis_conn.hmset(rider_key, {'longitude': rider_long, 'latitude': rider_lat}) - # update our redis key - key = 'location_%s' % sess_id - print "setting %s" % key - redis_conn.setex(key, 1600, message.payload) - -def sigint_handler(signal, frame): - print 'Interrupted' - sys.exit(0) + # update our redis key + key = 'location_%s' % sess_id + #print "setting %s" % key + redis_conn.setex(key, 1600, message.payload)