import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore", category=FutureWarning, module='pandas')
pd.set_option('future.no_silent_downcasting', True)

SHUT_ACC_STATUS = 0
ACC_STATUS = 3

EGO_ID = 1
OBJ_ID = 2

class ACC_Trigger():
    def __init__(self, df_vehState):
        self.df_vehState = df_vehState

    def find_start_end_time(self, change_speed_time_list):
        start_end_time_list = []
        # 初始化一个变量来跟踪当前序列的开始值
        # 和一个变量来跟踪当前序列的长度
        start_of_sequence = None
        sequence_length = 0

        # 遍历sim_times列表（从第二个元素开始比较）
        for i in range(1, len(change_speed_time_list)):
            # 检查当前元素与上一个元素的差是否接近0.04（考虑浮点数精度）
            if abs(change_speed_time_list[i] - change_speed_time_list[i - 1] - 0.01) < 0.04:
                # 如果这是序列的开始，则记录开始值
                if start_of_sequence is None:
                    start_of_sequence = change_speed_time_list[i - 1]
                    # 增加序列长度
                sequence_length += 1
            else:
                # 如果序列中断且长度足够（例如，至少为2），则添加序列到结果列表中
                if start_of_sequence is not None and sequence_length >= 0:
                    start_end_time_list.append([start_of_sequence, change_speed_time_list[i - 1]])
                    # 重置开始值和序列长度
                start_of_sequence = None
                sequence_length = 0

            # 处理最后一个序列（如果存在且长度足够）
        if start_of_sequence is not None and sequence_length >= 2:
            start_end_time_list.append([start_of_sequence, change_speed_time_list[-1]])
        return start_end_time_list

    def ACC_active_time_statistics(self):
        start_end_time_dict = {}
        ACC_time = self.df_vehState[self.df_vehState['ACC_status'] == ACC_STATUS]['simTime'].tolist()
        ACC_start_end_time_list = self.find_start_end_time(ACC_time)
        start_end_time_dict['ACC_status_active_time'] = ACC_start_end_time_list
        return start_end_time_dict

if __name__ == "__main__":
    df_vehState = pd.read_csv(r"D:\Cicv\Lantu\数据\ACC_04_Straight_Front_vehicle_biased_driving\data\VehState.csv")
    ACC = ACC_Trigger(df_vehState)
    start_end_time_dict = ACC.ACC_active_time_statistics()
    print(start_end_time_dict)