simulation-pjibot_delivery.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. import oss2
  5. import xml.etree.ElementTree as ET
  6. import shutil
  7. import docker
  8. import logging
  9. key1 = 'pjibot_delivery/'
  10. path1 = '/scenarios1/'
  11. path2 = '/mnt/disk001/dcl_data_process/src/python2/pjibot_delivery/'
  12. path3 = '/mnt/disk001/simulation_outdoor/'
  13. # xodr = 'thq_1116.xodr'
  14. # osgb = 'thq_1116.opt.osgb'
  15. xodr = 'thq_20240706.xodr'
  16. osgb = 'thq_20230710.osgb'
  17. vehicle_name = 'PuJin_distribution' # 配送 PuJin_distribution 巡检 PuJin_patrol_robot
  18. xoscName = 'scenario.xosc'
  19. logging.basicConfig(filename=path2 + 'log/simulation.log', level=logging.INFO,
  20. format='%(asctime)s - %(levelname)s - %(message)s')
  21. sleep_time = 60 # 每多少秒扫描一次
  22. def move_xosc_before_simulation(root_path):
  23. try:
  24. xosc_path = root_path + 'scenario_orig.xosc'
  25. logging.info('仿真还原需要的 xosc 路径为:%s' % xosc_path)
  26. tree1 = ET.parse(xosc_path)
  27. root1 = tree1.getroot()
  28. for node0 in root1:
  29. if node0.tag == 'RoadNetwork':
  30. for node1 in node0:
  31. # 打印根元素的标签名
  32. src_file_prefix = path3
  33. if 'LogicFile' == node1.tag:
  34. if xodr in node1.get('filepath'):
  35. dst_file = root_path + xodr
  36. shutil.copy(src_file_prefix + xodr, dst_file)
  37. node1.set('filepath', dst_file)
  38. if xodr in node1.get('filepath'):
  39. dst_file = root_path + xodr
  40. shutil.copy(src_file_prefix + xodr, dst_file)
  41. node1.set('filepath', dst_file)
  42. if xodr in node1.get('filepath'):
  43. dst_file = root_path + xodr
  44. shutil.copy(src_file_prefix + xodr, dst_file)
  45. node1.set('filepath', dst_file)
  46. if 'SceneGraphFile' == node1.tag:
  47. if osgb in node1.get('filepath'):
  48. dst_file = root_path + osgb
  49. shutil.copy(src_file_prefix + osgb, dst_file)
  50. node1.set('filepath', dst_file)
  51. if osgb in node1.get('filepath'):
  52. dst_file = root_path + osgb
  53. shutil.copy(src_file_prefix + osgb, dst_file)
  54. node1.set('filepath', dst_file)
  55. if osgb in node1.get('filepath'):
  56. dst_file = root_path + osgb
  57. shutil.copy(src_file_prefix + osgb, dst_file)
  58. node1.set('filepath', dst_file)
  59. if node0.tag == 'Entities':
  60. for node1 in node0:
  61. if node1.get("name") == 'Ego':
  62. for node2 in node1:
  63. if node2.tag == 'Vehicle':
  64. node2.set('name', vehicle_name)
  65. tree1.write(root_path + 'openx0.xosc')
  66. except Exception as e:
  67. logging.exception("修改xosc报错: %s" % str(e))
  68. def upload_simulation(parse_prefix, mp41):
  69. try:
  70. # 上传两次吧,对应上
  71. bucket.put_object_from_file(parse_prefix + 'scenario_orig.mp4', mp41)
  72. bucket.put_object_from_file(parse_prefix + 'scenario_hmi.mp4', mp41)
  73. logging.info('上传仿真视频到 %s' % parse_prefix + 'scenario_orig.mp4')
  74. shutil.rmtree(path1) # 仿真完就删除
  75. except Exception as e:
  76. logging.exception("上传视频报错 %s" % str(e))
  77. def simulation(parse_prefix, mp41):
  78. try:
  79. os.system("docker start vtd1")
  80. # 实例化Docker客户端
  81. client = docker.from_env()
  82. while True:
  83. time.sleep(5)
  84. # 获取容器列表
  85. containers = client.containers.list()
  86. run = False
  87. # 打印容器列表
  88. for container in containers:
  89. if 'vtd1' == container.name:
  90. run = True
  91. break
  92. if not run:
  93. break
  94. time.sleep(5)
  95. upload_simulation(parse_prefix, mp41)
  96. except Exception as e:
  97. logging.exception("生成仿真视频报错 %s" % str(e))
  98. def is_upload_completed(bucket, prefix):
  99. target_number = str(prefix).split('_')[-1][:-1]
  100. count = 0
  101. for obj in oss2.ObjectIterator(bucket, prefix=prefix):
  102. if obj.key != prefix:
  103. count += 1
  104. return int(count) == int(target_number)
  105. '''
  106. cname:http://open-bucket.oss.icvdc.com
  107. keyid:n8glvFGS25MrLY7j
  108. secret:xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
  109. oss桶名: oss://open-bucket
  110. oss桶名: open-bucket
  111. 内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
  112. '''
  113. # ------- 获取合并之后的bag包,解析出csv -------
  114. if __name__ == '__main__':
  115. # 1 创建阿里云对象
  116. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  117. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  118. bucket = oss2.Bucket(auth, endpoint, 'pji-bucket1')
  119. while True:
  120. try:
  121. logging.info("开始新一轮扫描")
  122. local_delete_list = []
  123. oss_delete_list = []
  124. prefix_list = []
  125. # 2 获取已经上传完成的所有目录并分组
  126. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  127. try:
  128. # 获取合并后的包
  129. merged_bag_object_key = str(obj1.key)
  130. if 'data_merge' in str(obj1.key) and str(obj1.key).endswith('.bag'):
  131. merged_bag_object_key_split = merged_bag_object_key.split('/')
  132. merged_prefix = '/'.join(merged_bag_object_key_split[:-1])
  133. parse_prefix = merged_prefix.replace('data_merge', 'data_parse')
  134. parse_prefix_full = merged_bag_object_key.replace('data_merge', 'data_parse')[:-4] + '/'
  135. xosc_done = False
  136. mp4_done = False
  137. for obj3 in oss2.ObjectIterator(bucket, prefix=str(parse_prefix_full)):
  138. if xoscName in str(obj3.key):
  139. xosc_done = True
  140. if '/scenario_orig.mp4' in str(obj3.key):
  141. mp4_done = True
  142. if not xosc_done:
  143. continue
  144. if mp4_done:
  145. continue
  146. logging.info("需要生成仿真视频: %s" % str(parse_prefix_full))
  147. parse_prefix_full = str(parse_prefix_full)
  148. local_dir_full = path1 + parse_prefix_full
  149. if not os.path.exists(local_dir_full):
  150. os.makedirs(local_dir_full)
  151. # 下载两个csv
  152. root_path1 = local_dir_full + 'orig/'
  153. if not os.path.exists(root_path1):
  154. os.makedirs(root_path1)
  155. bucket.get_object_to_file(parse_prefix_full + xoscName, root_path1 + 'scenario_orig.xosc')
  156. local_delete_list.append(root_path1 + 'scenario_orig.xosc')
  157. move_xosc_before_simulation(root_path1)
  158. simulation(parse_prefix_full, root_path1 + 'simulation.mp4')
  159. except Exception as e:
  160. logging.exception("局部异常处理: %s", str(e))
  161. # 删除本地临时文件
  162. if len(local_delete_list) > 0:
  163. for local_delete in local_delete_list:
  164. try:
  165. os.remove(local_delete)
  166. except Exception as e:
  167. pass
  168. time.sleep(sleep_time)
  169. except Exception as e:
  170. logging.exception("全局异常处理: %s", str(e))