catcher.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import socket
  2. import sys
  3. import time
  4. from test_decoder.decoder import GPS
  5. import ast
  6. imei = 35209308062032
  7. def start_server():
  8. try:
  9. hostname, port = '', 350
  10. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. server.bind((hostname, port))
  12. server.listen(1)
  13. print('Подключение к teltonika')
  14. connection, client_address = server.accept()
  15. data = connection.recv(16)
  16. new_imei = data.decode()[2:]
  17. print(new_imei)
  18. if new_imei == imei:
  19. answer = '\x01'.encode('utf-8')
  20. connection.sendall(answer)
  21. print(str(answer))
  22. print('Подключено')
  23. catch(connection)
  24. else:
  25. print('Неправильный imei')
  26. except Exception as e:
  27. print("Ошибка")
  28. print(e)
  29. def catch(connection):
  30. try:
  31. while True:
  32. data = connection.recv(8192)
  33. data = data.decode()
  34. print(f'Получено: {data}')
  35. except:
  36. print("Ошибка при получении")
  37. connection.close()
  38. print(data)
  39. def start_server_UDP():
  40. hostname, port = '', 350
  41. sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  42. sock.bind((hostname, port))
  43. numb_data = 0
  44. GPS(sock.recvfrom(1024)).create_gpx_file()
  45. while True:
  46. data = sock.recvfrom(1024)
  47. print(data)
  48. gps_decoder = GPS(data)
  49. numb_data=gps_decoder.packet_id
  50. gps_to_txt(gps_decoder)
  51. # send_answer(gps_decoder)
  52. GPS(sock.recvfrom(1024)).close_gpx()
  53. def gps_to_txt(gps_decoder):
  54. gps_decoder.print_gps_data()
  55. gps_decoder.add_waypoint()
  56. def send_answer(gps_decoder):
  57. hostname, port = '', 350
  58. data = b"\x00\x05"+gps_decoder.packet_id+b"\x01"+gps_decoder.avl_packet_id+gps_decoder.number_of_data
  59. udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  60. udp_socket.sendto((data), (hostname, port))
  61. udp_socket.close()
  62. #порты для дверей
  63. if __name__ == "__main__":
  64. start_server_UDP()