# -*- coding: utf-8 -*-
import os
import time
import oss2
import xml.etree.ElementTree as ET
from rosbag import Bag, Compression
import shutil
import docker
import bagtocsv_orig
import bagtocsv_hmi
import get_drive
import parse_jinlong_image
import pcdtovideo_jilong_overlook
import pcdtovideo_jilong_forwardlook

key1 = 'kinglong/'
path1 = '/root/'
sleep_time = 5  # 每多少秒扫描一次



def generate_xosc(parse_prefix, local_parse_dir, local_delete_list):
    try:
        # 切换到指定目录
        os.chdir('/root/rosmerge/python3/')

        # 使用subprocess.run()执行命令，注意输入的目录不以斜杠结尾
        os.system('python3 simulation_new.py ' + local_parse_dir[:-1])

        # 输出命令执行结果
        local_xosc_path1 = local_parse_dir + 'simulation_orig/xosc/openx0.xosc'
        local_xosc_path2 = local_parse_dir + 'simulation_hmi/xosc/openx0.xosc'
        bucket.put_object_from_file(parse_prefix + 'scenario_orig.xosc', local_xosc_path1)
        bucket.put_object_from_file(parse_prefix + 'scenario_hmi.xosc', local_xosc_path2)
        local_delete_list.append(local_xosc_path1)
        local_delete_list.append(local_xosc_path2)
        return local_xosc_path1, local_xosc_path2
    except Exception as e:
        print "生成xosc报错: %s" % str(e)
        # print(f'生成xosc报错{e}')


'''
cname：http://open-bucket.oss.icvdc.com
内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
oss桶名： open-bucket
keyid：n8glvFGS25MrLY7j
secret：xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
'''
# ------- 获取合并之后的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, 'open-bucket')
    while True:
        local_delete_list = []
        oss_delete_list = []
        prefix_list = []
        # 2 获取已经上传完成的所有目录并分组
        all_prefix_set = set()
        for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
            # 获取csv
            if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
                all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1])+'/')
        for obj2_key in all_prefix_set:
            xosc1_done = False
            xosc2_done = False
            csv1_done = False
            csv2_done = False
            for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
                if '/scenario_orig.xosc' in str(obj3.key):
                    xosc1_done = True
                if '/scenario_hmi.xosc' in str(obj3.key):
                    xosc2_done = True
                if '/pos_orig.csv' in str(obj3.key):
                    csv1_done = True
                if '/pos_hmi.csv' in str(obj3.key):
                    csv2_done = True
            if xosc1_done and xosc2_done:
                continue
            if not csv1_done and not csv2_done:
                continue
            # print(f'需要生成xosc：{obj2_key}')
            print "需要生成xosc: %s" % obj2_key
            parse_prefix_full = obj2_key
            local_dir_full = path1 + parse_prefix_full
            if not os.path.exists(local_dir_full):
                os.makedirs(local_dir_full)
            # 下载两个csv
            bucket.get_object_to_file(parse_prefix_full+'pos_orig.csv', local_dir_full+'pos_orig.csv')
            bucket.get_object_to_file(parse_prefix_full+'pos_hmi.csv', local_dir_full+'pos_hmi.csv')
            local_delete_list.append(local_dir_full+'pos_orig.csv')
            local_delete_list.append(local_dir_full+'pos_orig.csv')
            generate_xosc(parse_prefix_full, local_dir_full, 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:
                    print "删除本地临时文件报错: %s" % str(e)
                    # print(f'删除本地临时文件报错{e}')


        time.sleep(sleep_time)
