Add a rider message converter script. #535
This commit is contained in:
parent
cdcb7d0b46
commit
eba1c015b2
3 changed files with 93 additions and 0 deletions
70
utils/mqtt_rider_convert/mqtt_rider_convert.py
Normal file
70
utils/mqtt_rider_convert/mqtt_rider_convert.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import paho.mqtt.client as mqtt
|
||||
import ssl
|
||||
import redis
|
||||
import time
|
||||
import signal
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
class MQTTRiderConvert(object):
|
||||
|
||||
def run(self, client):
|
||||
print "running loop..."
|
||||
client.loop_forever()
|
||||
|
||||
# TODO: fix this and put these guys back under the class
|
||||
def init_subscriptions(client):
|
||||
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/#")
|
||||
|
||||
def user_data_set(userdata):
|
||||
conn = redis.StrictRedis(host='localhost', port=6379, db=0)
|
||||
return conn
|
||||
|
||||
def on_publish(client, userdata, mid):
|
||||
pass
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
redis_conn = user_data_set(userdata)
|
||||
#print("message topic=", message.topic[0:10])
|
||||
|
||||
if message.topic[0:10] != 'motorider_':
|
||||
return
|
||||
#print repr(message)
|
||||
|
||||
# check if json decodable
|
||||
res = json.loads(message.payload)
|
||||
#print res
|
||||
|
||||
# get rider session id
|
||||
sess_id = message.topic[10:]
|
||||
|
||||
# check if it has event
|
||||
if 'event' not in res:
|
||||
return
|
||||
|
||||
# check if event is driver_location
|
||||
if res['event'] != 'driver_location':
|
||||
return
|
||||
|
||||
# save the longitude and latitude
|
||||
rider_long = str(res['longitude'])
|
||||
rider_lat = str(res['latitude'])
|
||||
|
||||
# get the rider id from redis using rider.id.<session id>
|
||||
session_rider_key = 'rider.id.%s' % sess_id
|
||||
rider_id = redis_conn.get(session_rider_key)
|
||||
|
||||
channel = 'rider/%s/location' % rider_id
|
||||
payload = rider_lat + ':' + rider_long
|
||||
|
||||
print channel
|
||||
print payload
|
||||
|
||||
client.publish(channel, payload)
|
||||
BIN
utils/mqtt_rider_convert/mqtt_rider_convert.pyc
Normal file
BIN
utils/mqtt_rider_convert/mqtt_rider_convert.pyc
Normal file
Binary file not shown.
23
utils/mqtt_rider_convert/riderconvert.py
Normal file
23
utils/mqtt_rider_convert/riderconvert.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import paho.mqtt.client as mqtt
|
||||
import mqtt_rider_convert as rconvert
|
||||
import ssl
|
||||
import logging
|
||||
|
||||
|
||||
client = mqtt.Client()
|
||||
client.on_connect = rconvert.on_connect
|
||||
# client.on_publish = on_publish
|
||||
client.on_message = rconvert.on_message
|
||||
|
||||
#client.tls_set(
|
||||
# "/etc/letsencrypt/live/resqaws.jankstudio.com/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
||||
# tls_version=ssl.PROTOCOL_TLSv1)
|
||||
#client.tls_set(
|
||||
# "/root/aws_ssl_keys/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
||||
# tls_version=ssl.PROTOCOL_TLSv1)
|
||||
#client.connect("resqaws.jankstudio.com", 8883, 60)
|
||||
client.connect("localhost", 8883, 60)
|
||||
|
||||
|
||||
rider_convert = rconvert.MQTTRiderConvert()
|
||||
rider_convert.run(client)
|
||||
Loading…
Reference in a new issue