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

from resource import bagtocsv_rebot

import logging

path1 = '/mnt/disk001/dcl_data_process/src/python2/pjibot/'
path2 = '/mnt/disk001/run' # 生成评价报告pdf的可执行文件路径
path3 = '/mnt/disk001/dcl_data_process/src/python3/pjibot_indoor'
path4 = '/mnt/disk001/dcl_data_process/src/python3/pjibot_indoor/filter_pos.py'
pgm_path = '/mnt/disk001/dcl_data_process/src/python3/pjibot_indoor/resource/shiyanshi_newpgm_20240416.pgm'

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

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


def parse_csv(costmap_bag, data_bag, parse_prefix, local_parse_dir, local_delete_list):
    try:
        bagtocsv_rebot.parse(costmap_bag, data_bag, local_parse_dir + '/csv/')
        bagname = data_bag.split('/')[-1].split('.')[0]
        local_csv_dir = os.path.join(local_parse_dir + '/csv/', bagname)
        csv_file_name1 = 'pos_pji'
        local_csv_file_path1 = str(local_csv_dir) + '/' + str(csv_file_name1) + '.csv'
        local_delete_list.append(local_csv_file_path1)
        oss_csv_object_key1 = parse_prefix + csv_file_name1 + '.csv'
        bucket.put_object_from_file(oss_csv_object_key1, local_csv_file_path1)

        csv_file_name2 = 'EgoState_pji'
        local_csv_file_path2 = str(local_csv_dir) + '/' + str(csv_file_name2) + '.csv'
        local_delete_list.append(local_csv_file_path2)
        oss_csv_object_key2 = parse_prefix + csv_file_name2 + '.csv'
        bucket.put_object_from_file(oss_csv_object_key2, local_csv_file_path2)

        # 下载 track.png
        track_png_key = parse_prefix + 'track.png'
        bucket.get_object_to_file(track_png_key, str(local_csv_dir) + '/track1.png')
        # 生成pdf 报告
        os.chdir(path2)
        command1 = './pji_single ' + str(local_csv_dir) + '/ ' + str(local_csv_dir) + '/ ' + str(
            local_csv_dir) + '/track1.png ' + data_bag.split('/')[-1].split('.')[0]
        logging.info("调用生成pdf 报告命令: %s" % str(command1))
        os.system(command1)
        bucket.put_object_from_file(parse_prefix + 'report.pdf',
                                    str(local_csv_dir) + '/report.pdf')
        local_delete_list.append(str(local_csv_dir) + '/report.pdf')

        # ---------------------------------------------------
        csv_file_name3 = 'merged_obstacles'
        local_csv_file_path3 = str(local_csv_dir) + '/' + str(csv_file_name3) + '.csv'
        oss_csv_object_key3 = parse_prefix + csv_file_name3 + '.csv'
        bucket.put_object_from_file(oss_csv_object_key3, local_csv_file_path3)

        # 根据 merged_obstacles 和 机器人指定的pgm文件 生成新的 merged_obstacles 文件
        os.chdir(path3)
        command2 = 'python3 ' + path4 + ' ' + pgm_path + ' ' + local_csv_file_path3
        logging.info("调用命令merged_obstacles_new.csv生成命令: %s" % str(command2))
        os.system(command2)
        time.sleep(2)
        bucket.put_object_from_file(oss_csv_object_key3.replace('merged_obstacles', 'merged_obstacles_new'),
                                    local_csv_file_path3)  # 因为生成的文件是同名覆盖的
        # todo 暂时不删除 排查问题
        local_delete_list.append(local_csv_file_path3)

        # 这里不要删除了，不然无法复现pdf
        # bucket.delete_object(track_png_key)
    except Exception as e2:
        # 当出现异常时执行的代码
        logging.exception("生成csv报错: %s", e2)


# ------- 获取合并之后的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("开始新一轮扫描")
        time.sleep(sleep_time)
        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] + '/'
                    csv1_done = False
                    csv2_done = False
                    csv3_done = False
                    callback_undone = False
                    for obj2 in oss2.ObjectIterator(bucket, prefix=parse_prefix_full):
                        if '/callback.json' in str(obj2.key):
                            callback_undone = True
                        if '/pos_pji.csv' in str(obj2.key):
                            csv1_done = True
                        if '/EgoState_pji.csv' in str(obj2.key):
                            csv2_done = True
                        if '/merged_obstacles.csv' in str(obj2.key):
                            csv3_done = True
                    if not callback_undone:  # 没有 callback.json
                        # logging.info("数据已回调完成，不需要处理: %s" % str(parse_prefix_full))
                        continue
                    if csv1_done and csv2_done and csv3_done:
                        # logging.info("存在场景还原csv: %s" % str(parse_prefix_full))
                        continue

                    logging.info("开始生成场景还原csv: %s" % str(obj1.key))
                    # print(f'需要解析{merged_bag_object_key}')
                    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)
                    # 下载costmap.bag
                    costmap_key = merged_prefix.replace('data_merge', '') + 'costmap.bag'
                    logging.info("costmap.bag包的key为: %s" % str(costmap_key))
                    bucket.get_object_to_file(costmap_key, local_merged_dir + 'costmap.bag')
                    # 2 生成 pos_orig.csv 和 pos_hmi.csv
                    parse_csv(local_merged_dir + 'costmap.bag', 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:
                        logging.exception("捕获到一个异常: %s" % str(e))
        except Exception as e:
            logging.exception("全局错误处理: %s" % str(e))
