/*
 * @Description:
 * @Author: yangsw
 * @Date: 2022-06-24 16:54:32
 * @LastEditTime: 2024-01-12 18:40:34
 * @LastEditors: LiDa
 * @Reference:
 */
#include "svc_pulse.h"
#include "../../log/svc/svc_log.h"
#include "../../com/svc/svc_com.h"
#include "../../timer/svc/svc_timer.h"
#include "../../rtc/svc/svc_rtc.h"
#include "../../util/util_time.h"
#include "param/svc/svc_flash.h"
#include "../../lptimer/svc/svc_lptimer.h"
#include "../../cloud/svc/svc_cloud_protocol.h"
#include "../../cloud/svc/svc_cloud.h"
//#include "sr_app_data_collect.h"

S_Pulse s_pulse;
static S_Pulse_ErrorCheck s_pulse_errorcheck;

/**
 * @brief: 计量模块协议子模块初始化
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
S_Pulse_ErrorCheck *Svc_Pulse_ErrorCheck_GetData(void)
{
    return &s_pulse_errorcheck;
}

/**
 * @brief: 计量模块协议子模块初始化
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Pulse_Protocol_Init(void)
{
    memset(&s_pulse, 0, sizeof(s_pulse));
}
/**
 * @brief:
 * @return
 * @note:
 * @see:
 * @param  void
 */
void Svc_Pulse_Timer_Callback(void)
{
    if (s_pulse.u8PulseLowTime < 10)
    { //
        s_pulse.u8PulseLowTime++;
    }
    Svc_Pulse_Flow_Timer_Callback();
}

// 检测到低电平,脉冲计时开始
void Svc_Pulse_Low_Start_Time(void)
{
    s_pulse.u8PulseLowTime = 0;
}

// 返回脉冲时间
gw_u8 Svc_Pulse_Get_Low_Time(void)
{
    return s_pulse.u8PulseLowTime;
}

// 返回脉冲周期时间
gw_u32 Svc_Pulse_Get_Cycle_Time(void)
{
    return s_pulse.u32PulseCycleTime;
}

// 清脉冲周期时间
void Svc_Pulse_Clear_Cycle_Time(void)
{
    s_pulse.u32PulseCycleTime = 0;
}

// 返流时间开始
void Svc_Pulse_Start_Neg_FLow_Time(void)
{
    s_pulse.u8StartNegFLowTimeEnable = 1;
}

// 返回时间结束
void Svc_Pulse_Stop_Neg_FLow_Time(void)
{
    s_pulse.u8StartNegFLowTimeEnable = 0;
    s_pulse.u32NegFLowTime = 0;
}

// 过流时间开始
void Svc_Pulse_Start_Large_FLow_Time(void)
{
    s_pulse.u8StartLargeFLowTimeEnable = 1;
}

// 过流时间结束
void Svc_Pulse_Stop_Large_FLow_Time(void)
{
    s_pulse.u8StartLargeFLowTimeEnable = 0;
    s_pulse.u32LargeFLowTime = 0;
}

/**
 * @brief: 获取计量相关信息
 * @param  {void}
 * @return {S_Pulse} 计量相关信息
 * @note:
 * @see:
 */
S_Pulse Svc_Pulse_Get_Info(void)
{
    return s_pulse;
}
/**
 * @brief: 脉冲时间更新 ms
 * @return
 * @note:
 * @see:
 * @param  gw_u32
 */
gw_u32 Svc_Pulse_Refresh_TimeMs(void)
{
    RTC_TimeType RTC_TimeStructure;
    RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
    s_pulse.u8second = RTC_TimeStructure.Seconds;
    s_pulse.u32subsecondbuf = RTC_GetSubSecond();
    s_pulse.u32subsecond = 1000 - (float)1000 / 256 * s_pulse.u32subsecondbuf;
    if ((s_pulse.u8second == 0) && (s_pulse.u8lastsecond == 59))
    {
        s_pulse.u8lastsecond = s_pulse.u8second;
        return ((s_pulse.u8second + 60) * 1000 + s_pulse.u32subsecond);
    }
    s_pulse.u8lastsecond = s_pulse.u8second;
    return (s_pulse.u8second * 1000 + s_pulse.u32subsecond);
}

/**
 * @brief: 计量模块初始化
 * @return {void}
 * @note:
 * @see:
 * @param {void}
 */
void Svc_Pulse_Init(void)
{
#if LOG_FUNC_ENABLE
    Svc_Log_Printf(LOG_DEBUG, "%s \r\n", __FUNCTION__);
#endif
    Bsp_Pulse_Init();
    // Svc_Pulse_Set_Cur_State(E_CHECK_FAIL);
    Svc_Pulse_Set_Error_State(E_PULSE_NO_ERR);
    // Svc_Pulse_Protocol_Init();
}

void Svc_Pulse_Var_Init(void)
{
}

/**
 * @brief: 将一个脉冲数据放入接收队列
 * @return {char} 1：队列满，0：队列未满
 * @note:
 * @see:
 * @param {char} data 待放入数据
 */
void Svc_Pluse_Put_Data_Quene(SR_PULSE_FLOW_T FlowData)
{
    s_pulse.Pulse_Flow[s_pulse.fornt] = FlowData;
    s_pulse.fornt = (s_pulse.fornt + 1) % Pulse_Flow_Max_Numb;
}

/**
 * @brief: 从脉冲接收队列中取出一组脉冲数据
 * @param  {char*} data 取出的数据
 * @return {char} 1：队列中没有数据，0：正常
 * @note:
 * @see:
 */
char Svc_Pluse_Out_data_Quene(SR_PULSE_FLOW_T *FlowData)
{
    if (s_pulse.rear == s_pulse.fornt)
    {
        return 1;
    }
    *FlowData = s_pulse.Pulse_Flow[s_pulse.rear];
    s_pulse.rear = (s_pulse.rear + 1) % Pulse_Flow_Max_Numb;
    return 0;
}

/**
 * @name: 脉冲检测
 * @return {*}
 * @note:
 */
gw_u8 Svc_Pluse_Det(void)
{ // 脉冲检测
    S_Flash_Data *ps_flash;
    SR_PULSE_FLOW_T PluseDetFlowData;
    ps_flash = Svc_Flash_Get_Data();
    // 下降沿时间判断和数据边缘更新
    if (FALSE == GPIO_ReadInputDataBit(PULSE_IN_GPIO_PORT, PULSE_IN_GPIO_PIN))
    {
        if (s_pulse.u8Edge_First == RESET)
        { // 下降沿
            // 开启定时器标志位,检测高电平时间
            s_pulse.u8Edge_First = SET;
            s_pulse.u16Fallingms = Svc_Pulse_Refresh_TimeMs(); // 更新下降沿检测时间
            // if(s_pulse.u16Fallingms == 60003){
            //    s_pulse_errorcheck.u16perror_subcount++;
            //    if(s_pulse_errorcheck.u16perror_subcount >= MAX_ERRORCNT){
            //        s_pulse_errorcheck.u16perror_subcount = MAX_ERRORCNT;
            //    }
            //   s_pulse_errorcheck.u16perror_subsecond[s_pulse_errorcheck.u16perror_subcount-1] = s_pulse.u32subsecondbuf;
            //   s_pulse_errorcheck.u16perror_second[s_pulse_errorcheck.u16perror_subcount-1] = s_pulse.u8second;
            // }
            s_pulse_errorcheck.u16error_FallingTime = s_pulse.u16Fallingms;
            // Svc_Pulse_Low_Start_Time ();  //开始计时脉冲宽度
        }
        else
        { // 缺少上升沿
            Svc_Pulse_Set_Error_State(E_PULSE_LOST_Rising_ERR);
        }
    }
    // 上升沿时间判断和数据边缘更新
    else if (TRUE == GPIO_ReadInputDataBit(PULSE_IN_GPIO_PORT, PULSE_IN_GPIO_PIN))
    {
        if (s_pulse.u8Edge_First == SET)
        {
            s_pulse.u8Edge_First = RESET;
            s_pulse.u16Risingms = Svc_Pulse_Refresh_TimeMs(); // 更新上升沿检测时间
            s_pulse_errorcheck.u16error_risingTime = s_pulse.u16Risingms;
            if (s_pulse.u16Fallingms > 60000)
            {
                PluseDetFlowData.PulseWidth = s_pulse.u16Risingms - (s_pulse.u16Fallingms - 60000);
            }
            else
            {
                PluseDetFlowData.PulseWidth = s_pulse.u16Risingms - s_pulse.u16Fallingms;
                if (s_pulse.u16Risingms < s_pulse.u16Fallingms)
                {
                    PluseDetFlowData.PulseWidth = s_pulse.u16Risingms + 1000 - s_pulse.u16Fallingms;
                }
                if (s_pulse.u16Risingms < s_pulse.u16Fallingms)
                {
                    s_pulse_errorcheck.u16perror_subcount++;
                    if (s_pulse_errorcheck.u16perror_subcount >= MAX_ERRORCNT)
                    {
                        s_pulse_errorcheck.u16perror_subcount = MAX_ERRORCNT;
                    }
                    s_pulse_errorcheck.u16perror_subsecond[s_pulse_errorcheck.u16perror_subcount - 1] = s_pulse.u32subsecondbuf;
                    s_pulse_errorcheck.u16perror_second[s_pulse_errorcheck.u16perror_subcount - 1] = s_pulse.u8second;
                }
            }
            s_pulse.u16Risingms = s_pulse.u32subsecond;
            // 计算差值
            //  PluseDetFlowData.PulseWidth=Svc_Pulse_Get_Low_Time(); //低电平秒时间
            //  PluseDetFlowData.PulseWidth=s_pulse.u16Risingms-s_pulse.u16Fallingms;   		//计算脉宽
            PluseDetFlowData.PulseEqu = ps_flash->s_para.u16PluseEqu;
            // 判断时间差值所在范围 低电平时间为55ms~65ms，典型值：60ms
            if (((PluseDetFlowData.PulseWidth >= PLUSE_LOW_TIME_MIN) && (PluseDetFlowData.PulseWidth <= PLUSE_LOW_TIME_MAX)) || ((PluseDetFlowData.PulseWidth >= 1040) && (PluseDetFlowData.PulseWidth <= 1080)))
            { //
                // Svc_Pulse_Stop_Neg_FLow_Time();
                PluseDetFlowData.u8FLowDir = PLUSE_FLOW_ROTATE_FORWARD;
                s_pulse.u16LastTimeMs = s_pulse.u16NowTimeMs;
                s_pulse.u16NowTimeMs = s_pulse.u16Risingms;
                PluseDetFlowData.u32QusmLH = (gw_u32)PluseDetFlowData.PulseEqu * 3600 * 1000 / (Svc_Pulse_Get_Cycle_Time() * 1000 + s_pulse.u16NowTimeMs - s_pulse.u16LastTimeMs); // 计算流速   单位L/H
                Svc_Pulse_Clear_Cycle_Time();
                Svc_Pluse_Put_Data_Quene(PluseDetFlowData);
            }
            // 低电平时间为115ms~125ms，典型值：120ms
            else if (((PluseDetFlowData.PulseWidth >= PLUSE_NEG_LOW_TIME_MIN) && (PluseDetFlowData.PulseWidth <= PLUSE_NEG_LOW_TIME_MAX)) || ((PluseDetFlowData.PulseWidth >= 1110) && (PluseDetFlowData.PulseWidth <= 1140)))
            {   //
                // Svc_Pulse_Stop_Large_FLow_Time();
                PluseDetFlowData.u8FLowDir = PLUSE_FLOW_ROTATE_REVERSAL;
                s_pulse.u16LastTimeMs = s_pulse.u16NowTimeMs; // 更新上一个脉冲起始时间
                s_pulse.u16NowTimeMs = s_pulse.u16Risingms;
                PluseDetFlowData.u32QusmLH = (gw_u32)PluseDetFlowData.PulseEqu * 3600 * 1000 / (Svc_Pulse_Get_Cycle_Time() * 1000 + s_pulse.u16NowTimeMs - s_pulse.u16LastTimeMs); // 计算流速  单位L/H
                Svc_Pulse_Clear_Cycle_Time();
                Svc_Pluse_Put_Data_Quene(PluseDetFlowData);
            }
            else
            {
                s_pulse.PulseWidthErr = PluseDetFlowData.PulseWidth;
                Svc_Pulse_Set_Error_State(E_PULSE_WIDTH_ERR);
                s_pulse_errorcheck.u16perror_pulsecount++;
                if (s_pulse_errorcheck.u16perror_pulsecount >= MAX_ERRORCNT)
                {
                    s_pulse_errorcheck.u16perror_pulsecount = MAX_ERRORCNT;
                }
                s_pulse_errorcheck.u16perror_width[s_pulse_errorcheck.u16perror_pulsecount - 1] = s_pulse.PulseWidthErr;
                s_pulse_errorcheck.u16perror_FallTime[s_pulse_errorcheck.u16perror_pulsecount - 1] = s_pulse_errorcheck.u16error_FallingTime;
                s_pulse_errorcheck.u16perror_RiseTime[s_pulse_errorcheck.u16perror_pulsecount - 1] = s_pulse_errorcheck.u16error_risingTime;
                s_pulse_errorcheck.u16perror_subcount++;
                if (s_pulse_errorcheck.u16perror_subcount >= MAX_ERRORCNT)
                {
                    s_pulse_errorcheck.u16perror_subcount = MAX_ERRORCNT;
                }
                s_pulse_errorcheck.u16perror_subsecond[s_pulse_errorcheck.u16perror_subcount - 1] = s_pulse.u32subsecondbuf;
                s_pulse_errorcheck.u16perror_second[s_pulse_errorcheck.u16perror_subcount - 1] = s_pulse.u8second;
            }
        }
    }
    else
    {
        Svc_Pulse_Set_Error_State(E_PULSE_LOST_Falling_ERR);
        s_pulse_errorcheck.u16perror_count++;
        if (s_pulse_errorcheck.u16perror_count >= 255)
        {
            s_pulse_errorcheck.u16perror_count = 0;
        }
    }
    return TRUE;
}

void Svc_Pulse_Flow_Timer_Callback(void)
{
    S_Flash_Data *ps_flash;
    S_Flash_Device_Para *ps_device_para;
    ps_flash = Svc_Flash_Get_Data();
    ps_device_para = &ps_flash->s_para;
    // if(Svc_Exti10_15_Check_Flag_State() != 1)
    // {
    //     return;
    // }
    // 计算不过流最大脉冲周期 额外加2秒 以确保不过流
    s_pulse.u16LargePulseMinCycleTime = (3600 / ps_device_para->u32LargeFlowCutPoint) + 2;
    s_pulse.u16NegPulseMinCycleTime = (3600 / ps_device_para->u32NegFlowCutPoint) + 2;

    if (s_pulse.u8StartLargeFLowTimeEnable)
    { // 过流时间累加
        if (s_pulse.u32LargeFLowTime < 86400)
        { // 60*60*24
            s_pulse.u32LargeFLowTime++;
        }
    }
    else
    {
        s_pulse.u32LargeFLowTime = 0;
    }
    if (s_pulse.u8StartNegFLowTimeEnable)
    { // 反流时间累加
        if (s_pulse.u32NegFLowTime < 86400)
        { // 60*60*24
            s_pulse.u32NegFLowTime++;
        }
    }
    else
    {
        s_pulse.u32NegFLowTime = 0;
    }
    s_pulse.u32PulseCycleTime++; // 脉冲周期计数
    // 超过不过流最大脉冲周期即判断不过流,不用等待下个脉冲到来再判断是否过流
    if (s_pulse.u32PulseCycleTime > s_pulse.u16LargePulseMinCycleTime)
    {
        s_pulse.u16LargeFLowMax = 0;       // 清过流最大值
        s_pulse.u32LargeFLowStartTime = 0; // 清过流开始时间
        Svc_Pulse_Stop_Large_FLow_Time();  // 清过流计时值
        // if(s_pulse.u32LargeFLowTime == 0)
        // {
        //     Svc_Exti10_15_Clear_Flag();
        // }
    }
    if (s_pulse.u32PulseCycleTime > s_pulse.u16NegPulseMinCycleTime)
    {
        s_pulse.u16NegFLowMax = 0;       // 清返流最大值
        s_pulse.u32NegFLowStartTime = 0; // 清返流开始时间
        Svc_Pulse_Stop_Neg_FLow_Time();  // 清返流计时值
        // if(s_pulse.u32LargeFLowTime == 0)
        // {
        //     Svc_Exti10_15_Clear_Flag();
        // }
    }
}

/**
 * @brief: 流量检查
 * @return {void}
 * @note:
 * @see:
 * @param
 */
static void Svc_Pulse_Flow_Check(void)
{ // 是否达到过流告警检测
    S_Flash_Data *ps_flash;
    S_Cloud_Data *ps_cloud;
    S_Flash_Device_Para *ps_device_para;
    S_Flash_Usage_Alarm *ps_usage_alarm;

    ps_cloud = Svc_Cloud_Get_Data();
    ps_flash = Svc_Flash_Get_Data();

    ps_device_para = &ps_flash->s_para;
    ps_usage_alarm = &ps_flash->s_usage_info.s_usage_alarm;

    if (Svc_Cloud_Time_Check((char *)__FUNCTION__) == E_CHECK_FAIL)
    {
        return;
    }
    if ((s_pulse.u32LargeFLowTime > ps_device_para->u8LargeFlowDuration * 60) && // 计算过流
        (s_pulse.u32LargeFLowStartTime != 0) &&
        ((s_pulse.u32LargeFLowStartTime + ps_device_para->u8LargeFlowDuration * 60) < ps_cloud->u32CurTime))
    { // 防止时间倒走时时间对不上问题
        ps_usage_alarm->u16LargeFlowUsage = s_pulse.u16LargeFLowMax;
        if (ps_usage_alarm->u8LargeFlowAlarm == 0)
        {
            ps_usage_alarm->u8LargeFlowAlarm = 1;
            Svc_Log_Printf(LOG_WARNING, "%s->u8LargeFlowAlarm!!", __FUNCTION__); //<<<<<<<<<测试用
            Util_Time_Timestamp_To_Time(s_pulse.u32LargeFLowStartTime, ps_usage_alarm->u8LargeFlowStartTime);
        }
    }
    if ((s_pulse.u32NegFLowTime > ps_flash->s_para.u8NegFlowDuration * 60) && // 计算返流
        (s_pulse.u32NegFLowStartTime != 0) &&
        ((s_pulse.u32NegFLowStartTime + ps_flash->s_para.u8LargeFlowDuration * 60) < ps_cloud->u32CurTime))
    {
        ps_usage_alarm->u16NegFlowUsage = s_pulse.u16NegFLowMax;
        if (ps_usage_alarm->u8NegFlowAlarm == 0)
        {
            ps_usage_alarm->u8NegFlowAlarm = 1;
            Svc_Log_Printf(LOG_WARNING, "%s->u8NegFlowAlarm!!", __FUNCTION__); //<<<<<<<<<测试用
            Util_Time_Timestamp_To_Time(s_pulse.u32NegFLowStartTime, ps_usage_alarm->u8NegFlowStartTime);
        }
    }
}
/**
 * @brief: 流量计算
 * @return {void}
 * @note:
 * @see:
 * @param
 */
static void Svc_Pulse_Update_Flow_Usage(SR_PULSE_FLOW_T *FlowData)
{

    S_Flash_Data *ps_flash;
    S_Cloud_Data *ps_cloud;
    S_Flash_Real_Usage *ps_real_usage;
    S_Flash_Device_Para *ps_device_para;

    ps_cloud = Svc_Cloud_Get_Data();
    ps_flash = Svc_Flash_Get_Data();
    ps_device_para = &ps_flash->s_para;
    ps_real_usage = &ps_flash->s_usage_info.s_day_usage.s_real_usage;

    if (FlowData->u32QusmLH > PLUSE_LARGE_ALARM_FLOW)
    { // 流速过大进行限制
        FlowData->u32QusmLH = PLUSE_LARGE_ALARM_FLOW;
    }
    if (FlowData->u8FLowDir == PLUSE_FLOW_ROTATE_FORWARD)
    { // 计算正流
        Svc_Pulse_Stop_Neg_FLow_Time();
        if (s_pulse.u8FLowDirLast == PLUSE_FLOW_ROTATE_REVERSAL)
        {
            Svc_Pulse_Stop_Large_FLow_Time();
        }
        if ((FlowData->u32QusmLH) > ps_real_usage->u16MaxUsage)
        { // 更新实时最大值
            if (Svc_Cloud_Time_Check((char *)__FUNCTION__) == E_CHECK_OK)
            {
                ps_real_usage->u16MaxUsage = FlowData->u32QusmLH;
                memcpy(ps_real_usage->u8MaxUseTime, ps_cloud->pu8CurTime, 6);
            }
        }
        if (FlowData->u32QusmLH > ps_device_para->u32LargeFlowCutPoint)
        {
            if (Svc_Cloud_Time_Check((char *)__FUNCTION__) == E_CHECK_OK)
            {
                Svc_Pulse_Start_Large_FLow_Time();
                if (FlowData->u32QusmLH > s_pulse.u16LargeFLowMax)
                { // 更新告警最大流量
                    s_pulse.u16LargeFLowMax = FlowData->u32QusmLH;
                }
                if (s_pulse.u32LargeFLowStartTime == 0)
                { // 更新告警开始时间
                    s_pulse.u32LargeFLowStartTime = ps_cloud->u32CurTime;
                }
            }
        }
        else
        { // 清告警值
            s_pulse.u16LargeFLowMax = 0;
            s_pulse.u32LargeFLowStartTime = 0;
            Svc_Pulse_Stop_Large_FLow_Time();
        }
    }
    else
    { // 计算返流
        Svc_Pulse_Stop_Large_FLow_Time();
        if (s_pulse.u8FLowDirLast == PLUSE_FLOW_ROTATE_FORWARD)
        {
            Svc_Pulse_Stop_Neg_FLow_Time();
        }
        if (FlowData->u32QusmLH > ps_device_para->u32NegFlowCutPoint)
        {
            if (Svc_Cloud_Time_Check((char *)__FUNCTION__) == E_CHECK_OK)
            {
                Svc_Pulse_Start_Neg_FLow_Time();
                if (FlowData->u32QusmLH > s_pulse.u16NegFLowMax)
                {
                    s_pulse.u16NegFLowMax = FlowData->u32QusmLH;
                }
                if (s_pulse.u32NegFLowStartTime == 0)
                {
                    s_pulse.u32NegFLowStartTime = ps_cloud->u32CurTime;
                }
            }
        }
        else
        {
            s_pulse.u32NegFLowStartTime = 0;
            s_pulse.u16NegFLowMax = 0;
            Svc_Pulse_Stop_Neg_FLow_Time();
        }
    }
    s_pulse.u8FLowDirLast = FlowData->u8FLowDir;
}
/**
 * @brief: Pulse错误状态打印
 * @return {void}
 * @note:
 * @see:
 * @param
 */
void Svc_Pulse_Error_State_Printf(void)
{

    if (s_pulse.PulseErrState == E_PULSE_NO_ERR)
    {
        return;
    }
    Svc_Log_Printf(LOG_INFO, "----------------PULSE  ERROR----------------\r\n");
    Svc_Cloud_SysTime_Printf();
    switch (s_pulse.PulseErrState)
    {
    case E_PULSE_WIDTH_ERR:
        Svc_Log_Printf(LOG_INFO, "PulseWidth error%ld rise %ld fall%ld\r\n", s_pulse.PulseWidthErr, s_pulse_errorcheck.u16error_risingTime, s_pulse_errorcheck.u16error_FallingTime);
        break;
    case E_PULSE_LOST_Falling_ERR:
        Svc_Log_Printf(LOG_INFO, "MSR CH Lost Falling\r\n");
        break;
    case E_PULSE_LOST_Rising_ERR:
        Svc_Log_Printf(LOG_INFO, "MSR CH Lost Rising\r\n");
        break;
    default:
        break;
    }
    Svc_Pulse_Set_Error_State(E_PULSE_NO_ERR);
}
/**
 * @brief: Pulse用量信息打印
 * @return {void}
 * @note:
 * @see:
 * @param
 */
void Svc_Pulse_Info_Printf(SR_PULSE_FLOW_T *FlowData)
{
    gw_u16 PLUSE_EQU;
    S_Flash_Data *ps_flash;
    S_Flash_Usage_Info *ps_usage_info;
    ps_flash = Svc_Flash_Get_Data();
    ps_usage_info = &ps_flash->s_usage_info;
    PLUSE_EQU = ps_flash->s_para.u16PluseEqu;                 // 脉冲当量
    Svc_Cloud_SysTime_Printf();                               // 打印时间
    Svc_Log_Print("PulseWidth %d\r\n", FlowData->PulseWidth); // 打印脉宽
    if (FlowData->u8FLowDir == PLUSE_FLOW_ROTATE_FORWARD)
    { // 正走
        Svc_Log_Print("FORWARD\r\n");
    }
    else
    { // 反走
        Svc_Log_Print("s_pulse.u8FLowDir= REVERSAL\r\n");
    }
    if (FlowData->u32QusmLH >= PLUSE_LARGE_ALARM_FLOW)
    { // 流速过大进行限制
        Svc_Log_Print("Pulse Flow Too Max %d\r\n", FlowData->u32QusmLH);
    }
    else
    {
        Svc_Log_Print("Pulse Flow = %d \r\n", FlowData->u32QusmLH); // 打印流速
    }
    Svc_Log_Print("ps_usage_info->TotalUsagePos = %d \r\n", ps_usage_info->u32TotalUsagePos);                       // 打印总正流量
    Svc_Log_Print("ps_usage_info->u32TotalUsageNeg = %d \r\n", ps_usage_info->u32TotalUsageNeg);                    // 打印总负流量
    Svc_Log_Print("ps_usage_info->u32TotalUsage = %d \r\n", ps_usage_info->u32TotalUsage);                          // 打印总流量
    Svc_Log_Print("ps_real_usage->u32TotalUsage = %d \r\n", ps_usage_info->s_day_usage.s_real_usage.u32TotalUsage); // 打印总流量
}

void Svc_Pulse_Deal(void)
{
    SR_PULSE_FLOW_T FlowData;
#if LOG_FUNC_ENABLE
    Svc_Pulse_Error_State_Printf();
#endif
    //		if(Svc_Pulse_Is_Busy()==E_CHECK_FAIL){
    //        return ;
    //      }
    Svc_Pulse_Flow_Check(); // 计算是否过流报警
    if (Svc_Pluse_Out_data_Quene(&FlowData))
    {
        return;
    }
    Svc_Flash_Update_Usage(&FlowData);      // 更新用量
    Svc_Pulse_Update_Flow_Usage(&FlowData); // 计算流量
// Svc_Pulse_Set_Cur_State(E_CHECK_FAIL);
#if LOG_FUNC_ENABLE
    Svc_Pulse_Info_Printf(&FlowData);
#endif
}

/**
 * @brief: 计量模块是否忙
 * @param  {void}
 * @return {E_Result_Check} E_CHECK_OK:正忙，E_CHECK_FAIL:空闲
 * @note:
 * @see:
 */
E_Result_Check Svc_Pulse_Is_Busy(void)
{

    if (s_pulse.rear == s_pulse.fornt)
    {
        return E_CHECK_FAIL;
    }
    return E_CHECK_OK;
}

/**
 * @brief: Pulse设置状态
 * @return {void}
 * @note:
 * @see:
 * @param {E_Result_Check} pulse_state 状态
 */
void Svc_Pulse_Set_Cur_State(E_Result_Check pulse_state)
{
    s_pulse.PulseDetState = pulse_state;
}

/**
 * @brief: Pulse设置错误状态
 * @return {void}
 * @note:
 * @see:
 * @param {E_Result_Check} pulse_state IR状态
 */
void Svc_Pulse_Set_Error_State(E_Pulse_Err_State pulse_state)
{
    s_pulse.PulseErrState = pulse_state;
}

/**
 * @brief: Pulse硬件复位
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Pulse_Core_Reset(void)
{

    Bsp_Pulse_Power_Off();
    Svc_Timer_Delay_Ms(1000);
    Bsp_Pulse_Power_On();
}

/**
 * @brief: Pulse读协议
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Pulse_GetN(void)
{

    Pulse_Uart_Init();
    Bsp_Pulse_GetN_On();
}

/**
 * @brief: Pulse读协议
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Pulse_Clear_Run_State(void)
{

    Pulse_Uart_Init();
    Bsp_Pulse_Clear_Run_State();
}

/**
 * @brief: Pulse读协议
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Pulse_Clear_Calibrate_State(void)
{

    Pulse_Uart_Init();
    Bsp_Pulse_Clear_Calibrate_State();
}

void Svc_Exti10_15_Set_Flag(void)
{
    s_pulse.exti_wakeFlag = 1;
}

gw_u8 Svc_Exti10_15_Check_Flag_State(void)
{
    return s_pulse.exti_wakeFlag;
}

void Svc_Exti10_15_Clear_Flag(void)
{
    s_pulse.exti_wakeFlag = 0;
}
