# -*- coding: utf-8 -*-
import json
import time
import urllib2
import oss2
from datetime import datetime, timedelta

import logging

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

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

key1 = 'pjibot_delivery/'
key2 = 'data/'
key3 = 'data_merge/'
key4 = 'data_parse/'
path2 = 'data/'
path3 = 'data_merge/'
path4 = 'data_parse/'
url1_private = "http://10.14.86.147:9081/device/auth"
url2_private = "http://10.14.86.147:9081/device/data/callback"


def add_hour(date_string, hour_number):
    original_date = datetime.strptime(date_string, "%Y-%m-%d-%H-%M-%S")
    new_date = original_date + timedelta(hours=hour_number)
    return new_date.strftime("%Y-%m-%d-%H-%M-%S")


if __name__ == '__main__':
    auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
    endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
    bucket_name = 'pji-bucket1'
    bucket = oss2.Bucket(auth, endpoint, bucket_name)
    while True:
        logging.info("开始新一轮扫描")
        try:
            local_delete_list = []
            oss_delete_list = []
            upload_completed_prefix_list = []
            # 4 获取即将被合并的bag目录
            for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
                try:
                    if 'callback.json' in str(obj1.key):
                        prefix = '/'.join(str(obj1.key).split('/')[:-1])

                        file1 = False
                        file2 = False
                        file3 = False
                        file4 = False
                        file5 = False
                        file7 = False
                        file8 = False
                        file9 = False
                        for obj2 in oss2.ObjectIterator(bucket, prefix=prefix+ '/'):
                            if '/callback.json' in str(obj2.key):
                                file1 = True
                            if '/ego_pji.csv' in str(obj2.key):
                                file2 = True
                            if '/objects_pji.csv' in str(obj2.key):
                                file3 = True
                            if '/pcd_overlook.mp4' in str(obj2.key):
                                file4 = True
                            if '/pos_pji.csv' in str(obj2.key):
                                file5 = True
                            if '/scenario_orig.mp4' in str(obj2.key):
                                file7 = True
                            if '/simulation.xosc' in str(obj2.key) or '/scenario.xosc' in str(obj2.key):
                                file8 = True
                            if '/trajectory_pji.csv' in str(obj2.key):
                                file9 = True
                        if not file1 or not file2 or not file3 or not file4 or not file5 or not file7 or not file8 or not file9:
                            continue
                        time.sleep(1)
                        logging.info("发送：%s", prefix)
                        # 1 获取json内容
                        json_content = bucket.get_object(str(obj1.key)).read()
                        # 2 获取token
                        json_object = json.loads(json_content)
                        data1 = {
                            "equipmentNo": json_object['equipmentNo'],
                            "secretKey": json_object['secretKey']
                        }
                        json_data1 = json.dumps(data1)
                        logging.info("授权接口请求中： %s" % url1_private)
                        logging.info("授权发送参数为： %s" % str(data1))
                        request1 = urllib2.Request(url1_private, json_data1,
                                                   headers={'Content-Type': 'application/json'})
                        response1 = urllib2.urlopen(request1)
                        result_json1 = response1.read()
                        result_object1 = json.loads(result_json1)
                        logging.info("授权接口请求结果为： %s", result_object1)
                        access_token = result_object1.get('data').get('accessToken')
                        # 要发送的JSON参数
                        try:
                            # logging.info("bag文件为： %s", json_object['rosBagPath'])
                            old_date = json_object['dataName']
                            data_size = bucket.get_object_meta(json_object['rosBagPath']).content_length
                            equipment_no = json_object['equipmentNo']
                            old_file_path = json_object['filePath']
                            old_ros_bag_path = json_object['rosBagPath']
                            task_id = json_object['taskId']
                            trigger_id = json_object['triggerId']
                        except Exception as e:
                            logging.exception("callback报错：%s", str(e))
                            continue
                        if old_date is None:
                            old_date = ''
                        # 将时区统一（室外不需要需要加8，根据机器人终端的时区判断）
                        # new_date = add_hour(old_date, 8)
                        new_date = old_date
                        old_delete_list = []
                        for obj_old in oss2.ObjectIterator(bucket, prefix=old_file_path):
                            old_delete_list.append(str(obj_old.key))
                            if 'callback.json' in str(obj_old.key):
                                bucket.copy_object(bucket_name, str(obj_old.key),
                                                   str(obj_old.key).replace(old_date, new_date).replace(
                                                       'callback.json',
                                                       'callback_done.json'))
                                bucket.delete_object(str(obj_old.key))  # 删除 callback.json
                        # todo 时区不变也就不需要移动文件了
                        #     else:
                        #         bucket.copy_object(bucket_name, str(obj_old.key),
                        #                            str(obj_old.key).replace(old_date, new_date))
                        # bucket.copy_object(bucket_name, old_ros_bag_path, old_ros_bag_path.replace(old_date, new_date))
                        # bucket.delete_object(old_ros_bag_path)
                        # bucket.batch_delete_objects(old_delete_list)

                        if 'userId' in json_object:
                            logging.info("json_object 包含 'userId' 字段，值为:", json_object['userId'])
                            data2 = {
                                'userId': json_object['userId'],
                                "dataName": new_date,
                                "dataSize": data_size,
                                "equipmentNo": equipment_no,
                                "filePath": old_file_path.replace(old_date, new_date),
                                "rosBagPath": old_ros_bag_path.replace(old_date, new_date),
                                "taskId": task_id,
                                "triggerId": trigger_id
                            }
                        else:
                            logging.info("json_object 不包含 'userId' 字段")
                            data2 = {
                                "dataName": new_date,
                                "dataSize": data_size,
                                "equipmentNo": equipment_no,
                                "filePath": old_file_path.replace(old_date, new_date),
                                "rosBagPath": old_ros_bag_path.replace(old_date, new_date),
                                "taskId": task_id,
                                "triggerId": trigger_id
                            }
                        json_data2 = json.dumps(data2)
                        logging.info("回调接口请求中：%s" % url2_private)
                        request2 = urllib2.Request(url2_private, json_data2,
                                                   headers={'Content-Type': 'application/json',
                                                            'authorization': access_token})
                        response2 = urllib2.urlopen(request2)
                        result_json2 = response2.read()
                        result_object2 = json.loads(result_json2)
                        logging.info("回调接口请求结果为： %s", result_object2)
                except Exception as e:
                    logging.exception("局部异常处理: %s" % str(e))
            time.sleep(30)
        except Exception as e:
            logging.exception("全局错误处理: %s" % str(e))
