diff --git a/main.py b/main.py index f538066..10fe1b9 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ import threading import sys import os import logging -#import time +import time import socket import struct import zlib @@ -18,6 +18,18 @@ SOCK_BUFF_SIZE = 100000 global dictionary dictionary = {} +helper = {} + +# This is dictionary to be able to convert OBUID to unitID +#TODO: This needs to be updated regularly + +with open('/var/www/html/certificates.princip.cz/mapa/data/unitinfo.csv', 'r') as file: + reader = csv.reader(file) + for row in reader: + print(row[0]) + print(row[1]) + helper[int(row[0])] = {'obuid': row[1]} + STA = namedtuple('STA', ['time', 'lat', 'lon', 'velocity', 'heading', 'nav', 'nsat', 'alt', 'age', 'active', 'pwr', 'csq']) @@ -61,76 +73,85 @@ def convert_STA(sta_txt): alt, age, active, pwr, csq) def generateMap(): - print(f"generateMap func initialized") - - #Gather all needed data - for key in dictionary.keys(): - # Get the actual OBUID - if not key in helper: - obuid = "N/A" - - if key in helper: - obuid = helper[unitid]['obuid'] - - msgtype = dictionary[key]['msgtype'] - payload = dictionary[key]['payload'] - - datasta = convert_STA(payload) - - # Parse unittime - timefromsta = datasta[0] - timefromsta = datetime.fromtimestamp(timefromsta, tz) - timefromsta = timefromsta.strftime("%Y-%m-%d %H:%M:%S") - - lat = datasta[1] - lon = datasta[2] - velocity = datasta[3] - heading = datasta[4] - nav = datasta[5] - nsat = datasta[6] - alt = datasta[7] - age = datasta[8] - active = datasta[9] - pwr = datasta[10] - csq = datasta[11] - - # Real time we got the STA - rtime = datetime.now(tz) - rtime = rtime.strftime("%Y-%m-%d %H:%M:%S") - - # Get the state from location - coordinates = (lat, lon) - if datasta[1] and datasta[2] is not None: - results = rg.search(coordinates, mode=1) - country = results[0]['cc'] - - # And finally just feed the file - with open('/var/www/html/maps.princip.cz/map3/data/csv_list.csv', 'w') as f: + time.sleep(30) + while True: + start_time = time.time() + with open('/home/jakub/csv_list.csv', 'w') as f: f.write("lat|lng|Name|SN|Unit time|STA received|Lat|Lon|Country|Velocity|Heading|Nav|Nsat|Age|Active|Pwr|Csq|sta\n") - for key in dictionary.keys(): - if lat and lon is not None: - wr = f"{lat}|{lon}|{key}|{obuid}|{timefromsta}|{rtime}|{lat}|{lon}|{country}|{velocity}|{heading}|{nav}|{nsat}|{age}|{active}|{pwr}|{csq}|{sta}\n" + print(f"generateMap func initialized") + print("Count of units: ", len(dictionary.keys())) + + + # Copy of dict, the original one is modified during the loop + dictionary_print = dictionary.copy() + for key in dictionary_print.keys(): + # Gather all needed data + + # Get the actual OBUID from helper + + print(helper[1]['obuid']) + if key in helper: + obuid = helper[key]['obuid'] + + print("in helper") + else: + obuid = "N/A" + + print("not in helper") + + + msgtype = dictionary[key]['msgtype'] + payload = dictionary[key]['payload'] + + datasta = convert_STA(payload) + + # Parse unittime + timefromsta = datasta[0] + tz = pytz.timezone("Europe/Prague") + timefromsta = datetime.fromtimestamp(timefromsta, tz) + timefromsta = timefromsta.strftime("%Y-%m-%d %H:%M:%S") + + lat = datasta[1] + lon = datasta[2] + velocity = datasta[3] + heading = datasta[4] + nav = datasta[5] + nsat = datasta[6] + alt = datasta[7] + age = datasta[8] + active = datasta[9] + pwr = datasta[10] + csq = datasta[11] + + # Real time we got the STA + rtime = datetime.now(tz) + rtime = rtime.strftime("%Y-%m-%d %H:%M:%S") + + # Get the state from location + coordinates = (lat, lon) + if datasta[1] and datasta[2] is not None: + results = rg.search(coordinates, mode=1) + country = results[0]['cc'] + # And finally just feed the file + + print("lat: ", lat) + f.write(f"{lat}|{lon}|{obuid}|{msgtype}|{timefromsta}|{rtime}|{lat}|{lon}|{country}|{velocity}|{heading}|{nav}|{nsat}|{alt}|{age}|{active}|{pwr}|{csq}|{payload}\n") + + + # TODO: needs rewrite to trigger this function on request + print("--- %s seconds ---" % (time.time() - start_time)) + time.sleep(60) + - # TODO: needs rewrite to trigger this function on request - time.sleep(200) -# This is dictionary to be able to convert OBUID to unitID -#TODO: This function needs to update regularly -def helper(): - helper = {} - with open('/var/www/html/certificates.princip.cz/mapa/data/unitinfo.csv', 'r') as file: - reader = csv.reader(file) - for row in reader: - print(row[0]) - print(row[1]) - helper[int(row[0])] = {'obuid': row[1]} def main(): + from argparse import ArgumentParser class YArgumentParser(ArgumentParser): @@ -140,9 +161,9 @@ def main(): parser = YArgumentParser(usage="", epilog="") - parser.add_argument("-a", "--ip-addr", type=str, default="localhost", + parser.add_argument("-a", "--ip-addr", type=str, default="192.168.8.11", help="") - parser.add_argument("-p", "--ip-port", type=int, default=54337, + parser.add_argument("-p", "--ip-port", type=int, default=5001, help="") parser.add_argument("-l", "--log-level", type=int, default=20, help="Logging level") @@ -173,8 +194,7 @@ def main(): payload = pack[:size] pack = pack[size:] - print("obuid:{} >{}{}".format( - obuid, chr(msg_type), binascii.hexlify(payload))) + #print("obuid:{} >{}{}".format(obuid, chr(msg_type), binascii.hexlify(payload))) tz = pytz.timezone("Europe/Prague") @@ -183,11 +203,11 @@ def main(): if obuid not in dictionary: - print(f"Creating dictionary for {obuid}") + #print(f"Creating dictionary for {obuid}") dictionary[obuid] = {'msgtype': chr(msg_type), 'payload': binascii.hexlify(payload), 'rtime': rtime} else: - print(f"we already have this unit, just update the data") + #print(f"we already have this unit, just update the data") dictionary[obuid]["msgtype"] = chr(msg_type) dictionary[obuid]["payload"] = binascii.hexlify(payload) dictionary[obuid]["rtime"] = rtime @@ -200,4 +220,5 @@ def main(): if __name__ == '__main__': + main() \ No newline at end of file