41 lines
1 KiB
Python
41 lines
1 KiB
Python
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 on_publish(client, userdata, mid):
|
|
pass
|
|
|
|
def on_message(client, userdata, message):
|
|
redis_conn = userdata['redis']
|
|
|
|
topic_split = message.topic.split('/')
|
|
if topic_split[0] != 'rider':
|
|
return;
|
|
payload_split = message.payload.split(':')
|
|
|
|
rider_long = str(payload_split[1])
|
|
rider_lat = str(payload_split[0])
|
|
|
|
# set the location
|
|
redis_conn.geoadd('loc_rider_active', rider_long, rider_lat, topic_split[1])
|