# -*- coding: utf-8 -*-
import os
import time
import oss2

from resource import parse_pji_image

import logging

path1 = '/mnt/disk001/dcl_data_process/src/python2/pjibot/'

logging.basicConfig(filename=path1 + 'log/camera.log', level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

key1 = 'pjibot/'
sleep_time = 2  # 每多少秒扫描一次


def parse_to_mp4(merged_bag_file_path, parse_prefix, local_parse_dir, local_delete_list):
    try:
        local_mp4_dir1 = parse_pji_image.parse1('/ob_camera_01/color/image_raw', merged_bag_file_path,
                                                local_parse_dir + '/camera1/')
        mp4_file_name1 = 'camera'
        local_mp4_file_path1 = local_mp4_dir1 + '/' + mp4_file_name1 + '.mp4'
        local_delete_list.append(local_mp4_file_path1)
        oss_csv_object_key1 = parse_prefix + mp4_file_name1 + '.mp4'
        bucket.put_object_from_file(oss_csv_object_key1, local_mp4_file_path1)

        local_mp4_dir2 = parse_pji_image.parse2('/ob_camera_02/color/image_raw', merged_bag_file_path,
                                                local_parse_dir + '/camera2/')
        mp4_file_name2 = 'camera2'
        local_mp4_file_path2 = local_mp4_dir2 + '/' + mp4_file_name2 + '.mp4'
        local_delete_list.append(local_mp4_file_path2)
        oss_csv_object_key2 = parse_prefix + mp4_file_name2 + '.mp4'
        bucket.put_object_from_file(oss_csv_object_key2, local_mp4_file_path2)

    except Exception as e2:
        # 当出现异常时执行的代码
        logging.exception("生成摄像头视频报错: %s", e2)
        # logging.exception("生成摄像头视频报错，删除脏数据: %s", e2)
        # for obj in oss2.ObjectIterator(bucket, prefix=parse_prefix):
        #     bucket.delete_object(obj.key)


# ------- 获取合并之后的bag包，解析出csv -------
if __name__ == '__main__':
    # 1 创建阿里云对象
    auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
    # cname = 'http://open-bucket.oss.icvdc.com'
    # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
    endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
    bucket = oss2.Bucket(auth, endpoint, 'pji-bucket1')

    while True:
        logging.info("开始新一轮扫描")
        try:
            local_delete_list = []
            oss_delete_list = []
            prefix_list = []
            # 2 获取已经上传完成的所有目录并分组
            for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
                # 获取合并后的包
                merged_bag_object_key = str(obj1.key)
                # print(f'判断1{merged_bag_object_key}')
                if 'data_merge' in str(obj1.key) and str(obj1.key).endswith('.bag'):
                    merged_bag_object_key_split = merged_bag_object_key.split('/')
                    merged_prefix = '/'.join(merged_bag_object_key_split[:-1])
                    parse_prefix = merged_prefix.replace('data_merge', 'data_parse')
                    parse_prefix_full = merged_bag_object_key.replace('data_merge', 'data_parse')[:-4] + '/'
                    camera_done = False
                    camera2_done = False
                    callback_done = False
                    for obj2 in oss2.ObjectIterator(bucket, prefix=parse_prefix_full):
                        if '/callback.json' in str(obj2.key):
                            callback_done = True
                        if '/camera.mp4' in str(obj2.key):
                            # logging.info("存在摄像头视频: %s" % str(obj2.key))
                            camera_done = True
                        if '/camera2.mp4' in str(obj2.key):
                            # logging.info("存在摄像头视频: %s" % str(obj2.key))
                            camera2_done = True
                    if camera_done and camera2_done:
                        # logging.info("存在摄像头视频: %s" % str(parse_prefix_full))
                        continue
                    if not callback_done:  # 不存在存在callback.json的就不处理了
                        # logging.info("数据已回调完成，不需要处理: %s" % str(parse_prefix_full))
                        continue

                    local_merged_bag_path = path1 + 'camera/' + merged_bag_object_key
                    local_merged_dir = '/'.join(local_merged_bag_path.split('/')[:-1])
                    local_parse_dir = local_merged_dir.replace('data_merge', 'data_parse')
                    if not os.path.exists(local_merged_dir):
                        os.makedirs(local_merged_dir)
                    if not os.path.exists(local_parse_dir):
                        os.makedirs(local_parse_dir)
                    merged_bag_full_name = merged_bag_object_key_split[-1]
                    merged_bag_name = merged_bag_full_name.split('.')[0]
                    try:
                        bucket.get_object_to_file(merged_bag_object_key, local_merged_bag_path)
                    except Exception as e:
                        logging.exception("下载合并后的bag包失败: %s" % str(e))

                    local_delete_list.append(local_merged_bag_path)
                    # 2 生成 pos_orig.csv 和 pos_hmi.csv
                    logging.info("生成视频: %s" % str(obj1.key))
                    parse_to_mp4(local_merged_bag_path, parse_prefix_full, local_parse_dir, local_delete_list)

            # 删除本地临时文件
            if len(local_delete_list) > 0:
                for local_delete in local_delete_list:
                    try:
                        os.remove(local_delete)
                    except Exception as e:
                        pass
                        # logging.exception("捕获到一个异常: %s" % str(e))
            time.sleep(sleep_time)
        except Exception as e:
            logging.exception("全局错误处理: %s" % str(e))
