simulation-pjibot_patrol.py 6.7 KB

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