Browse Source

new file: catcher.py
new file: data.txt
new file: decoder.py

stuckedS 1 month ago
commit
afd02133ca
3 changed files with 141 additions and 0 deletions
  1. 71 0
      catcher.py
  2. 0 0
      data.txt
  3. 70 0
      decoder.py

+ 71 - 0
catcher.py

@@ -0,0 +1,71 @@
+import socket
+import sys
+import time
+from test_decoder.decoder import GPS
+import ast
+
+imei = 35209308062032
+
+def start_server():
+    try:
+        hostname, port = '', 350
+        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        server.bind((hostname, port))
+        server.listen(1)
+        print('Подключение к teltonika') 
+        connection, client_address = server.accept()
+        data = connection.recv(16)
+        new_imei = data.decode()[2:] 
+        print(new_imei)
+        if new_imei == imei:
+            answer  = '\x01'.encode('utf-8')
+            connection.sendall(answer)
+            print(str(answer))
+            print('Подключено') 
+            catch(connection)
+        else:
+            print('Неправильный imei')    
+    except Exception as e:
+        print("Ошибка")
+        print(e)
+        
+def catch(connection): 
+    try:
+        while True:
+            data = connection.recv(8192)
+            data = data.decode()
+            print(f'Получено: {data}')        
+    except:
+        print("Ошибка при получении")
+        connection.close()
+    print(data)
+    
+def start_server_UDP():
+    hostname, port = '', 350
+    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
+    sock.bind((hostname, port))
+    numb_data = 0
+    GPS(sock.recvfrom(1024)).create_gpx_file() 
+    while True:
+        data = sock.recvfrom(1024)
+        print(data)
+        gps_decoder = GPS(data)
+        numb_data=gps_decoder.packet_id
+        gps_to_txt(gps_decoder)
+        # send_answer(gps_decoder)
+    GPS(sock.recvfrom(1024)).close_gpx()    
+                
+def gps_to_txt(gps_decoder):
+    gps_decoder.print_gps_data()
+    gps_decoder.add_waypoint()
+    
+def send_answer(gps_decoder):
+    hostname, port = '', 350
+    data = b"\x00\x05"+gps_decoder.packet_id+b"\x01"+gps_decoder.avl_packet_id+gps_decoder.number_of_data
+    udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
+    udp_socket.sendto((data), (hostname, port))
+    udp_socket.close()
+    
+#порты для дверей
+if __name__ == "__main__":  
+    start_server_UDP()

File diff suppressed because it is too large
+ 0 - 0
data.txt


+ 70 - 0
decoder.py

@@ -0,0 +1,70 @@
+class GPS:
+    def __init__(self, data):
+        data = data[0]
+        self.data = data
+        self.packet_id = data[2:4]
+        self.avl_packet_id = data[5:6]
+        self.codec_id = data[23:24]
+        self.number_of_data = data[24:25]
+        self.gps_data = data[33:200]
+
+    def decode_gps_data(self):
+        longitude = self.gps_data[1:5]
+        latitude = self.gps_data[5:9]
+        altitude = self.gps_data[9:13]
+        angle = self.gps_data[11:13]
+        speed = self.gps_data[14:16]
+        decoded_data = {
+            "Longitude": str(int(longitude.hex(),32)),
+            "Latitude": str(int(latitude.hex(),32)),
+            "Altitude": str(int(altitude.hex(),32)),
+            "Angle": str(int(angle.hex(),16)),
+            "Speed": str(int(speed.hex(),16))
+        }
+        
+        return decoded_data
+    
+    def print_gps_data(self):
+        gps_file = open("gps_data.txt","a")
+        decoded_data = self.decode_gps_data()
+        for key, value in decoded_data.items():
+            if str(key) == "Latitude" or str(key) == "Longitude":
+                key,value = str(key),str(value)
+                value = value[0:2]+"."+value[2:len(value)]
+            else:
+                key,value = str(key),str(value)
+            gps_file.write(key+" "+value+"\n")
+            print(key, value)
+        gps_file.close()
+        
+        
+    def create_gpx_file(self):
+        gpx_data = f"""<?xml version="1.0" encoding="UTF-8"?>
+        <gpx version="1.1" xmlns="">""" 
+        
+        with open("gps_data.gpx", "w", encoding="utf-8") as gpx_file:gpx_file.write(gpx_data)
+        
+        
+    def add_waypoint(self):
+        data = self.decode_gps_data()
+        
+        latitude = data['Latitude'][0:2]+"."+data['Latitude'][2:len(data['Latitude'])]
+        longitude = data['Longitude'][0:2]+"."+data['Longitude'][2:len(data['Longitude'])]
+        altitude = data['Altitude']
+        angle = data['Angle']
+        speed = data['Speed']
+        
+        waypoint = f"""
+        <wpt lat="{latitude}" lon="{longitude}">
+            <ele>{altitude}</ele>
+            <magvar>{angle}</magvar>
+            <speed>{speed}</speed>
+        </wpt>
+        """
+        with open("gps_data.gpx", "a", encoding="utf-8") as gpx_file:gpx_file.write(waypoint)
+        
+    def close_gpx(self):
+        close = f"""</gpx>"""
+        with open("gps_data.gpx", "a", encoding="utf-8") as gpx_file:gpx_file.write(close)
+
+

Some files were not shown because too many files changed in this diff