decoder.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class GPS:
  2. def __init__(self, data):
  3. data = data[0]
  4. self.data = data
  5. self.packet_id = data[2:4]
  6. self.avl_packet_id = data[5:6]
  7. self.codec_id = data[23:24]
  8. self.number_of_data = data[24:25]
  9. self.gps_data = data[33:200]
  10. def decode_gps_data(self):
  11. longitude = self.gps_data[1:5]
  12. latitude = self.gps_data[5:9]
  13. altitude = self.gps_data[9:13]
  14. angle = self.gps_data[11:13]
  15. speed = self.gps_data[14:16]
  16. decoded_data = {
  17. "Longitude": str(int(longitude.hex(),32)),
  18. "Latitude": str(int(latitude.hex(),32)),
  19. "Altitude": str(int(altitude.hex(),32)),
  20. "Angle": str(int(angle.hex(),16)),
  21. "Speed": str(int(speed.hex(),16))
  22. }
  23. return decoded_data
  24. def print_gps_data(self):
  25. gps_file = open("gps_data.txt","a")
  26. decoded_data = self.decode_gps_data()
  27. for key, value in decoded_data.items():
  28. if str(key) == "Latitude" or str(key) == "Longitude":
  29. key,value = str(key),str(value)
  30. value = value[0:2]+"."+value[2:len(value)]
  31. else:
  32. key,value = str(key),str(value)
  33. gps_file.write(key+" "+value+"\n")
  34. print(key, value)
  35. gps_file.close()
  36. def create_gpx_file(self):
  37. gpx_data = f"""<?xml version="1.0" encoding="UTF-8"?>
  38. <gpx version="1.1" xmlns="">"""
  39. with open("gps_data.gpx", "w", encoding="utf-8") as gpx_file:gpx_file.write(gpx_data)
  40. def add_waypoint(self):
  41. data = self.decode_gps_data()
  42. latitude = data['Latitude'][0:2]+"."+data['Latitude'][2:len(data['Latitude'])]
  43. longitude = data['Longitude'][0:2]+"."+data['Longitude'][2:len(data['Longitude'])]
  44. altitude = data['Altitude']
  45. angle = data['Angle']
  46. speed = data['Speed']
  47. waypoint = f"""
  48. <wpt lat="{latitude}" lon="{longitude}">
  49. <ele>{altitude}</ele>
  50. <magvar>{angle}</magvar>
  51. <speed>{speed}</speed>
  52. </wpt>
  53. """
  54. with open("gps_data.gpx", "a", encoding="utf-8") as gpx_file:gpx_file.write(waypoint)
  55. def close_gpx(self):
  56. close = f"""</gpx>"""
  57. with open("gps_data.gpx", "a", encoding="utf-8") as gpx_file:gpx_file.write(close)