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

path1 = '/mnt/disk001/dcl_data_process/src/python2/pjibot_delivery/'
path2 = '/mnt/disk001/dcl_data_process/src/python3/pjibot_outdoor/'

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

key1 = 'pjibot_delivery/'
sleep_time = 60  # 每多少秒扫描一次


def generate_xosc(parse_prefix, local_parse_dir, local_delete_list):
    try:
        os.chdir(path2)
        command2 = 'python3 jiqiren_outdoor.py ' + local_parse_dir[:-1] + ' 0'  # 配送机器人0 巡检机器人1
        logging.info("调用命令2: %s" % str(command2))
        os.system(command2)
        local_xosc_path2 = local_parse_dir + 'simulation/xosc/openx_outdoor0.xosc'
        bucket.put_object_from_file(parse_prefix + 'scenario.xosc', local_xosc_path2)
        bucket.put_object_from_file(parse_prefix + 'scenario_hmi.xosc', local_xosc_path2)
        logging.info("上传 simulation.xosc(scenario.xosc) 成功: %s" % str(parse_prefix + 'scenario.xosc'))
        local_delete_list.append(local_xosc_path2)
    except Exception as e:
        logging.exception("生成xosc报错: %s" % str(e))


# ------- 获取合并之后的bag包，解析出csv -------
if __name__ == '__main__':
    # 1 创建阿里云对象
    auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
    endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
    bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
    while True:
        try:
            logging.info("开始新一轮扫描")
            local_delete_list = []
            oss_delete_list = []
            prefix_list = []
            # 2 获取已经上传完成的所有目录并分组
            for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
                try:
                    # 获取合并后的包
                    merged_bag_object_key = str(obj1.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] + '/'
                        xosc_done = False
                        csv1_done = False
                        csv2_done = False
                        for obj3 in oss2.ObjectIterator(bucket, prefix=str(parse_prefix_full)):
                            if '/simulation.xosc' in str(obj3.key) or '/scenario.xosc' in str(obj3.key):
                                xosc_done = True
                            if '/objects_pji.csv' in str(obj3.key):
                                csv1_done = True
                            if '/pos_pji.csv' in str(obj3.key):
                                csv2_done = True
                        if xosc_done:
                            logging.info("存在 simulation.xosc(scenario.xosc): %s" % str(parse_prefix_full))
                            continue
                        if not csv1_done:
                            logging.info("不存在 /objects_pji.csv: %s" % str(parse_prefix_full))
                            continue
                        if not csv2_done:
                            logging.info("不存在 /pos_pji.csv: %s" % str(parse_prefix_full))
                            continue
                        logging.info("需要生成 simulation.xosc: %s" % str(parse_prefix_full))
                        local_dir_full = path1 + parse_prefix_full
                        if not os.path.exists(local_dir_full):
                            os.makedirs(local_dir_full)
                        bucket.get_object_to_file(parse_prefix_full + 'objects_pji.csv', local_dir_full + 'objects_pji.csv')
                        bucket.get_object_to_file(parse_prefix_full+'pos_pji.csv', local_dir_full+'pos_pji.csv')
                        generate_xosc(parse_prefix_full, local_dir_full, local_delete_list)
                except Exception as e:
                    logging.exception("局部异常处理: %s", str(e))
            # 删除本地临时文件
            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))
            time.sleep(sleep_time)
        except Exception as e:
            logging.exception("全局异常处理: %s", str(e))
