From 79c845835feb3ebd75042cd6c222b4c6556e6337 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Fri, 20 Oct 2017 16:55:07 +0200 Subject: [PATCH] Added zrank for coord --- templates/index.html | 2 ++ zmq_subscriber.py | 70 +++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 40 deletions(-) diff --git a/templates/index.html b/templates/index.html index 9e797fd..fac1f53 100644 --- a/templates/index.html +++ b/templates/index.html @@ -49,9 +49,11 @@ table { margin-right:auto; } +/* .panel-body { padding: 0px; } +*/ /* .panel-default { diff --git a/zmq_subscriber.py b/zmq_subscriber.py index 4b80161..679ae86 100755 --- a/zmq_subscriber.py +++ b/zmq_subscriber.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3.5 -import time +import time, datetime import zmq import redis import random @@ -46,9 +46,13 @@ def get_ip(data): def ip_to_coord(ip): resp = reader.city(ip) - lat = resp.location.latitude - lon = resp.location.longitude - return {'lat': lat, 'lon': lon} + lat = float(resp.location.latitude) + lon = float(resp.location.longitude) + # 0.0001 correspond to ~10m + # Cast the float so that it has the correct float format + lat_corrected = float("{:.4f}".format(lat)) + lon_corrected = float("{:.4f}".format(lon)) + return {'lat': lat_corrected, 'lon': lon_corrected} def default_log(jsonevent): print('sending', 'log') @@ -72,29 +76,37 @@ def default_event(jsonevent): to_send = { 'name': 'Event', 'log': json.dumps(to_push) } redis_server.publish(channel, json.dumps(to_send)) -def default_attribute(jsonevent): +def default_attribute(jsonattr): print('sending', 'attribute') - jsonevent = jsonevent['Attribute'] + jsonattr = jsonattr['Attribute'] to_push = [ - jsonevent['id'], - jsonevent['category'], - jsonevent['type'], - jsonevent['value'], + jsonattr['id'], + jsonattr['category'], + jsonattr['type'], + jsonattr['value'], ] #try to get coord - if jsonevent['category'] == "Network activity": - try: - coord = ip_to_coord(jsonevent['value']) - to_send = {'lat': float(coord['lat']), 'lon': float(coord['lon'])} - serv_coord.publish(channel_proc, json.dumps(to_send)) - print('coord sent') - except ValueError: - print("can't resolve ip") + if jsonattr['category'] == "Network activity": + handleCoord(jsonattr['value']) to_send = { 'name': 'Attribute', 'log': json.dumps(to_push) } redis_server.publish(channel, json.dumps(to_send)) +def handleCoord(value): + try: + coord = ip_to_coord(value) + coord_dic = {'lat': coord['lat'], 'lon': coord['lon']} + coord_list = [coord['lat'], coord['lon']] + print(coord_list) + now = datetime.datetime.now() + today_str = str(now.year)+str(now.month)+str(now.day) + keyname = 'GEO_' + today_str + #serv_coord.zincrby(keyname, coord_list) + serv_coord.publish(channel_disp, json.dumps({ "coord": coord })) + print('coord sent') + except ValueError: + print("can't resolve ip") def process_log(event): event = event.decode('utf8') @@ -144,25 +156,3 @@ if __name__ == "__main__": main() reader.close() - - -# server side -pubsub = redis_server.pubsub(ignore_subscribe_messages=True) - -#while True: -# rdm = random.randint(1,10) -# time.sleep(float(rdm)) -# #lux -# lon = random.uniform(5.7373, 6.4823) -# lat = random.uniform(49.4061,49.7449) -# #central eur -# lon = random.uniform(3.936, 9.890) -# lat = random.uniform(47.957, 50.999) -# content = ["rdm "+str(rdm)] -# content = [lat,lon] -# jsonContent = json.dumps(content) -# to_send = { 'name': 'feeder'+str(rdm), 'log': jsonContent } -# redis_server.publish(channel, json.dumps(to_send)) -# serv_coord.publish(channel_proc, json.dumps({'lat': float(lat), 'lon': float(lon)})) - -