Remove debug messages and sigint_handler. Create user_data_set to set the redis client. #180
This commit is contained in:
parent
e61d77ad8b
commit
2c8dad9164
1 changed files with 39 additions and 39 deletions
|
|
@ -12,6 +12,8 @@ import json
|
||||||
class RiderLocationCache(object):
|
class RiderLocationCache(object):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
client = mqtt.Client()
|
||||||
|
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
# client.on_publish = on_publish
|
# client.on_publish = on_publish
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
|
|
@ -32,58 +34,56 @@ class RiderLocationCache(object):
|
||||||
#signal.signal(signal.SIGINT, sigint_handler)
|
#signal.signal(signal.SIGINT, sigint_handler)
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
|
|
||||||
client = mqtt.Client()
|
|
||||||
redis_conn = redis.StrictRedis(host='localhost', port=6379, db=0)
|
|
||||||
|
|
||||||
def init_subscriptions(client):
|
def init_subscriptions(client):
|
||||||
print "subscribing to wildcard"
|
#print "subscribing to wildcard"
|
||||||
client.subscribe('#')
|
client.subscribe('#')
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
init_subscriptions(client)
|
init_subscriptions(client)
|
||||||
print("Connected with result code "+str(rc))
|
#print("Connected with result code "+str(rc))
|
||||||
# client.subscribe("$SYS/#")
|
# 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):
|
def on_publish(client, userdata, mid):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
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_':
|
if message.topic[0:10] != 'motorider_':
|
||||||
return
|
return
|
||||||
#print repr(message)
|
#print repr(message)
|
||||||
|
|
||||||
# check if json decodable
|
# check if json decodable
|
||||||
res = json.loads(message.payload)
|
res = json.loads(message.payload)
|
||||||
print res
|
#print res
|
||||||
|
|
||||||
# get rider session id
|
# get rider session id
|
||||||
sess_id = message.topic[10:]
|
sess_id = message.topic[10:]
|
||||||
|
|
||||||
# check if it has event
|
# check if it has event
|
||||||
if 'event' not in res:
|
if 'event' not in res:
|
||||||
return
|
return
|
||||||
|
|
||||||
# check if event is driver_location
|
# check if event is driver_location
|
||||||
if res['event'] != 'driver_location':
|
if res['event'] != 'driver_location':
|
||||||
return
|
return
|
||||||
|
|
||||||
# save the longitude and latitude
|
# save the longitude and latitude
|
||||||
# get the rider id from sess_id
|
# get the rider id from sess_id
|
||||||
rider_key = "rider.location.%s" % sess_id
|
rider_key = "rider.location.%s" % sess_id
|
||||||
rider_long = str(res['longitude'])
|
rider_long = str(res['longitude'])
|
||||||
rider_lat = str(res['latitude'])
|
rider_lat = str(res['latitude'])
|
||||||
|
|
||||||
# set the location
|
# set the location
|
||||||
redis_conn.hmset(rider_key, {'longitude': rider_long, 'latitude': rider_lat})
|
redis_conn.hmset(rider_key, {'longitude': rider_long, 'latitude': rider_lat})
|
||||||
|
|
||||||
# update our redis key
|
# update our redis key
|
||||||
key = 'location_%s' % sess_id
|
key = 'location_%s' % sess_id
|
||||||
print "setting %s" % key
|
#print "setting %s" % key
|
||||||
redis_conn.setex(key, 1600, message.payload)
|
redis_conn.setex(key, 1600, message.payload)
|
||||||
|
|
||||||
def sigint_handler(signal, frame):
|
|
||||||
print 'Interrupted'
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue