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,23 +34,25 @@ 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
|
||||||
|
|
@ -56,7 +60,7 @@ def on_message(client, userdata, 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:]
|
||||||
|
|
@ -80,10 +84,6 @@ def on_message(client, userdata, message):
|
||||||
|
|
||||||
# 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