callback.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import time
  4. import urllib2
  5. import oss2
  6. from datetime import datetime, timedelta
  7. import logging
  8. path1 = '/mnt/disk001/cicv-data-closedloop/python2-pjisuv-module/'
  9. logging.basicConfig(filename=path1 + 'log/callback.log', level=logging.INFO,
  10. format='%(asctime)s - %(levelname)s - %(message)s')
  11. key1 = 'pjisuv/'
  12. sleep_time = 60 # 每多少秒扫描一次
  13. url1_public = "http://1.202.169.139:9081/device/auth"
  14. url1_private = "http://10.14.86.127:9081/device/auth"
  15. url2_public = " http://1.202.169.139:9081/device/data/callback"
  16. url2_private = "http://10.14.86.127:9081/device/data/callback"
  17. def add_hour(date_string, hour_number):
  18. original_date = datetime.strptime(date_string, "%Y-%m-%d-%H-%M-%S")
  19. new_date = original_date + timedelta(hours=hour_number)
  20. return new_date.strftime("%Y-%m-%d-%H-%M-%S")
  21. '''
  22. cname:http://open-bucket.oss.icvdc.com
  23. 内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
  24. oss桶名: open-bucket
  25. keyid:n8glvFGS25MrLY7j
  26. secret:xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
  27. '''
  28. if __name__ == '__main__':
  29. # 1 登录验证 。
  30. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  31. # 2 填写自定义域名,例如example.com。获取桶。
  32. # 3 填写Bucket名称,并设置is_cname=True来开启CNAME。CNAME是指将自定义域名绑定到存储空间。
  33. # cname = 'http://open-bucket.oss.icvdc.com'
  34. # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
  35. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  36. bucket_name = 'open-bucket'
  37. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  38. while True:
  39. logging.info("开始新一轮扫描")
  40. local_delete_list = []
  41. oss_delete_list = []
  42. upload_completed_prefix_list = []
  43. # 4 获取即将被合并的bag目录
  44. logging.info("开始扫描目录: %s" % str(key1))
  45. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  46. if 'callback.json' in str(obj1.key):
  47. time.sleep(1)
  48. prefix = '/'.join(str(obj1.key).split('/')[:-1])
  49. pos_orig_csv = False
  50. pos_hmi_csv = False
  51. drive_csv = False
  52. camera_mp4 = False
  53. pcd_forwardlook_mp4 = False
  54. pcd_overlook_mp4 = False
  55. scenario_orig_xosc = False
  56. scenario_hmi_xosc = False
  57. scenario_orig_mp4 = False
  58. scenario_hmi_mp4 = False
  59. camera_bag = False
  60. fusion_bag = False
  61. plan_bag = False
  62. control_bag = False
  63. callback_json = False
  64. for obj2 in oss2.ObjectIterator(bucket, prefix=prefix):
  65. if 'callback.json' in str(obj2.key):
  66. callback_json = True
  67. if 'camera.mp4' in str(obj2.key):
  68. camera_mp4 = True
  69. if 'drive.csv' in str(obj2.key):
  70. drive_csv = True
  71. if 'pcd_forwardlook.mp4' in str(obj2.key):
  72. pcd_forwardlook_mp4 = True
  73. if 'pcd_overlook.mp4' in str(obj2.key):
  74. pcd_overlook_mp4 = True
  75. if 'pos_orig.csv' in str(obj2.key):
  76. pos_orig_csv = True
  77. if 'scenario_orig.mp4' in str(obj2.key):
  78. scenario_orig_mp4 = True
  79. if 'scenario_orig.xosc' in str(obj2.key):
  80. scenario_orig_xosc = True
  81. if not callback_json or not camera_mp4 or not drive_csv or not pcd_forwardlook_mp4 or not pcd_overlook_mp4 or not pos_orig_csv or not scenario_orig_mp4 or not scenario_orig_xosc:
  82. continue
  83. time.sleep(2)
  84. logging.info("发送: %s" % str(obj1.key))
  85. # 1 获取json内容
  86. json_content = bucket.get_object(str(obj1.key)).read()
  87. # 2 获取token
  88. json_object = json.loads(json_content)
  89. data1 = {
  90. "equipmentNo": json_object['equipmentNo'],
  91. "secretKey": json_object['secretKey']
  92. }
  93. json_data1 = json.dumps(data1)
  94. logging.info("授权接口请求中: %s" % url1_private)
  95. request1 = urllib2.Request(url1_private, json_data1,
  96. headers={'Content-Type': 'application/json'})
  97. response1 = urllib2.urlopen(request1)
  98. result_json1 = response1.read()
  99. result_object1 = json.loads(result_json1)
  100. logging.info("授权接口请求结果为: %s", result_object1)
  101. try:
  102. access_token = result_object1.get('data').get('accessToken')
  103. logging.info("bag文件为: %s" % str(json_object['rosBagPath']))
  104. old_date = json_object['dataName']
  105. data_size = bucket.get_object_meta(json_object['rosBagPath']).content_length
  106. equipment_no = json_object['equipmentNo']
  107. old_file_path = json_object['filePath']
  108. old_ros_bag_path = json_object['rosBagPath']
  109. task_id = json_object['taskId']
  110. trigger_id = json_object['triggerId']
  111. except Exception as e:
  112. logging.exception("callback报错: %s" % str(e))
  113. continue
  114. # 将时区统一
  115. new_date = add_hour(old_date, 8)
  116. old_delete_list = []
  117. for obj_old in oss2.ObjectIterator(bucket, prefix=old_file_path):
  118. old_delete_list.append(str(obj_old.key))
  119. if 'callback.json' in str(obj_old.key):
  120. bucket.copy_object(bucket_name, str(obj_old.key),
  121. str(obj_old.key).replace(old_date, new_date).replace('callback.json',
  122. 'callback_done.json'))
  123. else:
  124. bucket.copy_object(bucket_name, str(obj_old.key), str(obj_old.key).replace(old_date, new_date))
  125. bucket.copy_object(bucket_name, old_ros_bag_path, old_ros_bag_path.replace(old_date, new_date))
  126. bucket.delete_object(old_ros_bag_path)
  127. bucket.batch_delete_objects(old_delete_list)
  128. triggerId = json_object['triggerId']
  129. data2 = {
  130. "dataName": new_date,
  131. "dataSize": data_size,
  132. "equipmentNo": equipment_no,
  133. "filePath": old_file_path.replace(old_date, new_date),
  134. "rosBagPath": old_ros_bag_path.replace(old_date, new_date),
  135. "taskId": task_id,
  136. # "triggerId": ["1744180835122155522", "1744178775815360514"]
  137. "triggerId": triggerId
  138. }
  139. json_data2 = json.dumps(data2)
  140. logging.info("回调接口请求中:%s" % url2_private)
  141. request2 = urllib2.Request(url2_private, json_data2,
  142. headers={'Content-Type': 'application/json',
  143. 'authorization': access_token})
  144. response2 = urllib2.urlopen(request2)
  145. result_json2 = response2.read()
  146. result_object2 = json.loads(result_json2)
  147. logging.info("回调接口请求结果为: %s", result_object2)
  148. time.sleep(sleep_time)