catcher.py 2.1 KB

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