Create new mqtt_rider utility script for rider location caching #299
This commit is contained in:
parent
f350b3ebb8
commit
a5a95a45d0
5 changed files with 87 additions and 3 deletions
49
utils/mqtt_rider/rider_location_cache.py
Normal file
49
utils/mqtt_rider/rider_location_cache.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import ssl
|
||||||
|
import redis
|
||||||
|
import time
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
class RiderLocationCache(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 rider/+/location"
|
||||||
|
client.subscribe('rider/+/location')
|
||||||
|
|
||||||
|
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):
|
||||||
|
# TODO: persist redis connection
|
||||||
|
redis_conn = user_data_set(userdata)
|
||||||
|
#print("message topic=", message.topic[0:10])
|
||||||
|
|
||||||
|
topic_split = message.topic.split('/')
|
||||||
|
if topic_split[0] != 'rider':
|
||||||
|
return;
|
||||||
|
payload_split = message.payload.split(':')
|
||||||
|
|
||||||
|
#print repr(message)
|
||||||
|
rider_long = str(res['longitude'])
|
||||||
|
rider_lat = str(res['latitude'])
|
||||||
|
|
||||||
|
# set the location
|
||||||
|
redis_conn.geoadd('rider_active', rider_long, rider_lat, topic_split[1])
|
||||||
|
|
||||||
BIN
utils/mqtt_rider/rider_location_cache.pyc
Normal file
BIN
utils/mqtt_rider/rider_location_cache.pyc
Normal file
Binary file not shown.
23
utils/mqtt_rider/riderloc.py
Normal file
23
utils/mqtt_rider/riderloc.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import rider_location_cache as rlc
|
||||||
|
import ssl
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_connect = rlc.on_connect
|
||||||
|
# client.on_publish = on_publish
|
||||||
|
client.on_message = rlc.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", 1883, 60)
|
||||||
|
|
||||||
|
|
||||||
|
rider_location = rlc.RiderLocationCache()
|
||||||
|
rider_location.run(client)
|
||||||
12
utils/mqtt_rider/riderloc.service
Normal file
12
utils/mqtt_rider/riderloc.service
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Rider Location Cache Service
|
||||||
|
After=mosquitto.service redis.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/usr/bin/python /root/www/resq/utils/rider_location_cache/riderloc.py
|
||||||
|
StandardInput=tty-force
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
|
@ -12,9 +12,9 @@ client.on_message = rlc.on_message
|
||||||
#client.tls_set(
|
#client.tls_set(
|
||||||
# "/etc/letsencrypt/live/resqaws.jankstudio.com/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
# "/etc/letsencrypt/live/resqaws.jankstudio.com/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
||||||
# tls_version=ssl.PROTOCOL_TLSv1)
|
# tls_version=ssl.PROTOCOL_TLSv1)
|
||||||
client.tls_set(
|
#client.tls_set(
|
||||||
"/root/aws_ssl_keys/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
# "/root/aws_ssl_keys/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
||||||
tls_version=ssl.PROTOCOL_TLSv1)
|
# tls_version=ssl.PROTOCOL_TLSv1)
|
||||||
#client.connect("resqaws.jankstudio.com", 8883, 60)
|
#client.connect("resqaws.jankstudio.com", 8883, 60)
|
||||||
client.connect("localhost", 8883, 60)
|
client.connect("localhost", 8883, 60)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue