
#include "fault_manager.h"
#include "dido_manager.h"
#include "flash_manager.h"
#include "signal_manager.h"
#include "eeprom_manager.h"
#include "logic_ctrl.h"
#include "hv_adc_manager.h"

#include "kit_time.h"

typedef struct
{
    uint16_t step;
    uint16_t dly;
}DoParamItem;

DoParamItem do_param_item[APP_CFG_RELAY_NUM];

//制冷
void logic_do_cool1_ctrl(uint32_t base_time, uint8_t idx)
{
	if(bms_get_signal(kSignalIdx_Cool) == kSignalStatus_High)
	{
		bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_On, RELAY_CTRL_NO_DLY);
	}
	else
	{
		bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_Off, RELAY_CTRL_NO_DLY);
	}
}


//加热
void logic_do_heat1_ctrl(uint32_t base_time, uint8_t idx)
{    
    if(bms_get_signal(kSignalIdx_Heat) == kSignalStatus_High)
	{
		bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_On, RELAY_CTRL_NO_DLY);
	}
	else
	{
		bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_Off, RELAY_CTRL_NO_DLY);
	}
}

//运行灯-回路闭合亮
void logic_do_run1_led_ctrl(uint32_t base_time, uint8_t idx)
{
    DoCtrlStatus do_status = kDoCtrlStatus_Off;
	if(bms_get_circuit_status(kCircuitType_Main) == kCircuitStatus_On)
    {
		do_status = kDoCtrlStatus_On;
    }
    bms_crtl_do_status_by_idx(idx, do_status, RELAY_CTRL_NO_DLY);
}

//告警灯-一级故障常亮（严重）  二级故障1s间隔闪烁（中等）  三级故障2s间隔闪烁（轻微） 无故障不亮
void logic_do_alarm1_led_ctrl(uint32_t base_time, uint8_t idx)
{
    uint16_t dly_beep;
    FaultLevel max_level;
    DoCtrlStatus do_status;
    if(idx < APP_CFG_RELAY_NUM)
    {
        dly_beep = do_param_item[idx].dly;
        max_level = bms_get_max_fault_level();
        do_status = kDoCtrlStatus_Off;
        
        if(1)        
        {
            dly_beep += base_time;
            if(max_level == kFaultLevel_Third)
            {
                do_status = kDoCtrlStatus_On;
            }
            else if(max_level == kFaultLevel_Second)
            {
                if(dly_beep <= 1000)
                {
                    do_status = kDoCtrlStatus_Off;
                }
                else if(dly_beep <= 2000)
                {
                    do_status = kDoCtrlStatus_On;
                }
                else
                {
                    dly_beep = base_time;
                }
            }		
            else if(max_level == kFaultLevel_First)
            {
                if(dly_beep <= 2000)
                {
                    do_status = kDoCtrlStatus_Off;
                }
                else if(dly_beep <= 4000)
                {
                    do_status = kDoCtrlStatus_On;
                }
                else
                {
                    dly_beep = base_time;
                }
            }
            else
            {
                dly_beep = 0;
            }
        }
        else
        {
            dly_beep = 0;
        }
        
        do_param_item[idx].dly = dly_beep;
        bms_crtl_do_status_by_idx(idx, do_status, RELAY_CTRL_NO_DLY);
    }
}


typedef enum
{
    kTripStep_Normal,
    kTripStep_Delay,
    kTripStep_NoDelay,
    kTripStep_Trigger,
    kTripStep_Wait,
    kTripStep_End,
}TripStep;

//BCU的分励断路器
void logic_do_trip1_ctrl(uint32_t base_time, uint8_t idx)
{
    if(idx < APP_CFG_RELAY_NUM)
    {
        switch(do_param_item[idx].step)
        {
            case kTripStep_Normal:
                if((bms_get_fault_level(kFaultCode_Stop) >= kFaultLevel_Third)
                || (bms_get_fault_level(kFaultCode_Fire) >= kFaultLevel_Third))
                {
                    do_param_item[idx].step = kTripStep_NoDelay;
                }            
                else if(((bms_is_bsu_fault() == true) || (bms_get_fault_relay_off_bit() != 0))
                && (KIT_ABS(bms_get_current() / 10) >= get_eeprom_data(kEep_CutOffCurrent, kEepromDataType_Full))) //满足禁充且禁放且有电流 分辨率1A
                {
                    if(get_eeprom_data(kEep_SwitchOffDly_BcuShuntTrip, kEepromDataType_Low) > 0) 
                    {
                        do_param_item[idx].step = kTripStep_Delay;
                    }
                    else
                    {
                        do_param_item[idx].step = kTripStep_Wait;
                    }                            
                }
                break;
                
            case kTripStep_NoDelay:
                do_param_item[idx].step = kTripStep_Trigger;
                bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_On, RELAY_CTRL_NO_DLY);                   
                break;
            
             case kTripStep_Delay:
                do_param_item[idx].dly += base_time;
                if(do_param_item[idx].dly >= get_eeprom_data(kEep_SwitchOffDly_BcuShuntTrip, kEepromDataType_Low) * 100) 
                {
                    do_param_item[idx].step = kTripStep_Trigger;
                    do_param_item[idx].dly = 0;
                    bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_On, RELAY_CTRL_NO_DLY);
                }
                break;               
                
            case kTripStep_Trigger:
                do_param_item[idx].dly += base_time;
                if(do_param_item[idx].dly >= get_eeprom_data(kEep_TripPulse_DataSavePeriod, kEepromDataType_High) * 100)
                {
                    do_param_item[idx].step = kTripStep_Wait;
                    bms_crtl_do_status_by_idx(idx, kDoCtrlStatus_Off, RELAY_CTRL_NO_DLY);
                }
                break;
                
            case kTripStep_Wait:
                if((bms_get_fault_level(kFaultCode_Stop) < kFaultLevel_Third)
                && (bms_get_fault_level(kFaultCode_Fire) < kFaultLevel_Third)
                && (bms_is_bsu_fault() == false) 
                && (bms_get_fault_relay_off_bit() == 0))
                {
                    do_param_item[idx].step = kTripStep_Normal;
                }
                break;
                
            default:
                break;
        }
    }
}

