12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import socket
- import sys
- import time
- from test_decoder.decoder import GPS
- import ast
- 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()
|