xosc-pjisuv.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. import oss2
  5. import logging
  6. import json
  7. path1 = '/mnt/disk001/dcl_data_process/src/python2/pjisuv/'
  8. path2 = '/mnt/disk001/dcl_data_process/src/python3/pjisuv/'
  9. logging.basicConfig(filename=path1 + 'log/xosc.log', level=logging.INFO,
  10. format='%(asctime)s - %(levelname)s - %(message)s')
  11. key1 = 'pjisuv/'
  12. sleep_time = 60 # 每多少秒扫描一次
  13. error_bag_json = "/mnt/disk001/dcl_data_process/src/python2/pjisuv/errorBag.json"
  14. def parse_json_to_string_array(file_path):
  15. try:
  16. # 打开并读取JSON文件
  17. with open(file_path, 'r', encoding='utf-8') as file:
  18. # 解析JSON内容
  19. data = json.load(file)
  20. # 检查数据是否是一个列表,并且列表中的元素是否是字符串
  21. if isinstance(data, list):
  22. for item in data:
  23. if not isinstance(item, str):
  24. raise ValueError("JSON数组中的元素不是字符串")
  25. return data
  26. else:
  27. return []
  28. except Exception as e:
  29. return []
  30. def list_to_json_file(data, file_path):
  31. """
  32. 将列表转换为JSON格式并写入指定的文件路径。
  33. 如果文件已存在,则覆盖它。
  34. 参数:
  35. data (list): 要转换为JSON的列表。
  36. file_path (str): 要写入JSON数据的文件路径。
  37. """
  38. # 将列表转换为JSON格式的字符串
  39. json_data = json.dumps(data, ensure_ascii=False, indent=4)
  40. # 以写入模式打开文件,如果文件已存在则覆盖
  41. with open(file_path, 'w', encoding='utf-8') as file:
  42. # 将JSON字符串写入文件
  43. file.write(json_data)
  44. def generate_xosc(parse_prefix, local_parse_dir, local_delete_list):
  45. try:
  46. os.chdir(path2)
  47. # todo simulation_hmi 实际生成的 orig,因为多功能车没有 hmi
  48. command2 = 'python3 simulation_hmi.py ' + local_parse_dir[:-1] # 生成orig
  49. logging.info("调用命令2: %s" % str(command2))
  50. os.system(command2)
  51. local_xosc_path2 = local_parse_dir + 'simulation_orig/xosc/openx0.xosc'
  52. bucket.put_object_from_file(parse_prefix + 'scenario_orig.xosc', local_xosc_path2)
  53. bucket.put_object_from_file(parse_prefix + 'scenario_hmi.xosc',
  54. local_xosc_path2) # todo 将orig也上传成hmi,因为多功能车暂时没有hmi
  55. logging.info("上传 scenario_orig.xosc 成功: %s" % str(parse_prefix + 'scenario_orig.xosc'))
  56. local_delete_list.append(local_xosc_path2)
  57. except Exception as e:
  58. error_bag_list = parse_json_to_string_array(error_bag_json)
  59. error_bag_list.append(parse_prefix)
  60. list_to_json_file(error_bag_list, error_bag_json)
  61. logging.exception(parse_prefix, "生成 xosc 报错,将其添加到错误bag列表。", str(e))
  62. # ------- 获取合并之后的bag包,解析出csv -------
  63. if __name__ == '__main__':
  64. # 1 创建阿里云对象
  65. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  66. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  67. bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
  68. while True:
  69. try:
  70. logging.info("开始新一轮扫描")
  71. prefix_list = []
  72. # 2 获取已经上传完成的所有目录并分组
  73. all_prefix_set = set()
  74. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  75. # 获取csv
  76. if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
  77. all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1]) + '/')
  78. for obj2_key in all_prefix_set:
  79. try:
  80. local_delete_list = []
  81. oss_delete_list = []
  82. xosc1_done = False
  83. xosc2_done = False
  84. csv1_done = False
  85. csv2_done = False
  86. for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
  87. if '/scenario_orig.xosc' in str(obj3.key):
  88. xosc1_done = True
  89. # if '/scenario_hmi.xosc' in str(obj3.key):
  90. # xosc2_done = True
  91. if '/pos_orig.csv' in str(obj3.key):
  92. csv1_done = True
  93. # if '/pos_hmi.csv' in str(obj3.key):
  94. # csv2_done = True
  95. if xosc1_done or not csv1_done:
  96. continue
  97. error_bag_list = parse_json_to_string_array(error_bag_json)
  98. if str(obj2_key) in error_bag_list:
  99. continue
  100. logging.info("需要生成xosc: %s" % obj2_key)
  101. local_dir_full = path1 + obj2_key
  102. if not os.path.exists(local_dir_full):
  103. os.makedirs(local_dir_full)
  104. # todo 下载单个csv生成两个视频?
  105. bucket.get_object_to_file(obj2_key + 'pos_orig.csv', local_dir_full + 'pos_orig.csv')
  106. # bucket.get_object_to_file(obj2_key+'pos_hmi.csv', local_dir_full+'pos_hmi.csv')
  107. # local_delete_list.append(local_dir_full + 'pos_orig.csv') # todo 不删除了 做测试用
  108. # local_delete_list.append(local_dir_full+'pos_hmi.csv')
  109. generate_xosc(obj2_key, local_dir_full, local_delete_list)
  110. # 删除本地临时文件
  111. if len(local_delete_list) > 0:
  112. for local_delete in local_delete_list:
  113. try:
  114. os.remove(local_delete)
  115. except Exception as e:
  116. logging.exception("删除本地临时文件报错: %s" % str(e))
  117. except Exception as e:
  118. logging.exception("局部异常处理: %s", str(e))
  119. time.sleep(sleep_time)
  120. except Exception as e:
  121. logging.exception("全局异常处理: %s", str(e))