/*
 * @Description:
 * @Author: yangsw
 * @Date: 2022-06-24 16:54:32
 * @LastEditTime: 2022-08-19 14:51:26
 * @LastEditors: 002389 yangsw@lierda.com
 * @Reference:
 */
#include "svc_pulse.h"
#include "../../log/svc/svc_log.h"
#include "../../com/svc/svc_com.h"
#include "svc_pulse_protocol.h"
#include "../../util/util_data.h"
#include "../../lcd/svc/svc_lcd.h"
#include "../../flash/svc/svc_flash.h"
#include "../../cloud/svc/svc_cloud_protocol.h"
#include "../../cloud/svc/svc_cloud.h"
static S_Pulse s_pulse;

void Svc_Pulse_Init(void)
{
    gw_u16 u16Baud;
    S_Flash_Data *ps_flash_data;
    ps_flash_data = Svc_Flash_Get_Data();
    u16Baud = ps_flash_data->s_security.u16PulseBaud;
    Svc_Pulse_Uart_Init(u16Baud);
}

S_Pulse *Svc_Pulse_Get_Data(void)
{
    return &s_pulse;
}

gw_u16 Svc_Pulse_Get_WaterTemp(void)
{
    return s_pulse.s16WaterTemp;
}

void Svc_Pulse_Timer_1s_Callback(void)
{
    if (s_pulse.u8TimeMeasureCnt > 0)
    {
        s_pulse.u8TimeMeasureCnt--;
    }
}

/**
 * @brief: 流量检查
 * @return {void}
 * @note:
 * @see:
 * @param {gw_s32} s32Flow 流速，单位：mL/h
 * @param {gw_u8} u8Time    持续时间，单位：s
 */
void Svc_Pulse_Flow_Check(S_Pulse *ps_pulse_data)
{
    gw_s32 s32Flow;
    S_Flash_Data *ps_flash;
    ps_flash = Svc_Flash_Get_Data();
    S_Cloud *s_cloud;
    s_cloud = Svc_Cloud_Get_Data();
    s32Flow = ps_pulse_data->s32Flow_mL / 1000;
    if (ps_pulse_data->s8FlowDir == -1) // 反流
    {
        s32Flow = 0 - s32Flow;
        ps_pulse_data->s_err_flow.u8LargeCnt++;
        ps_pulse_data->s_err_flow.u8LargeCnt++;
        if ((ps_pulse_data->s_err_flow.u8LargeCnt >= 3) || (ps_pulse_data->s_err_flow.u8SmallCnt >= 3))
        { /*清过流、小流报警*/
            ps_pulse_data->s_err_flow.s32LargeFlow = 0;
            ps_pulse_data->s_err_flow.u8LargeCnt = 0;
            ps_pulse_data->s_err_flow.s32SmallFlow = 0;
            ps_pulse_data->s_err_flow.u8SmallCnt = 0;
            Svc_Flash_Clear_Device_Alarm(b17Large_Flow);
            Svc_Flash_Clear_Device_Alarm(b18Small_Flow);
        }
        if (s32Flow >= ps_flash->s_para.u16ReverseFlowThreshold) // 流速超过反流警告阈值(L/h)
        {
            ps_pulse_data->s_err_flow.s32NegFlow += s32Flow;
            ps_pulse_data->s_err_flow.u8NegCnt = 0;
            if (ps_pulse_data->s_err_flow.s32NegFlow > ps_flash->s_para.u16ReverseFlowDuration) // 持续反流超出预设值
            {
                if (Svc_Flash_IsDevice_Alarm(b13Reverse_Flow) == 0) // 原来没有反流报警
                {
                    Svc_Flash_Set_Device_Alarm(b13Reverse_Flow);
                    memcpy(ps_flash->s_security.s_state.pu8ReverseFlowStartTime, s_cloud->s_manage.pu8CurTime, 6); // 记录反流开始时间
                    Svc_Flash_Save_Meter_Event(E_Event_Reverse_Flow);                                              // 事件存储
                }
                else if (Svc_Flash_IsDevice_Alarm(b13Reverse_Flow) == 1) // 原来已经反流报警
                {
                    if (s32Flow >= ps_flash->s_security.s_state.u16MaxReverseFlow)
                    {
                        ps_flash->s_security.s_state.u16MaxReverseFlow = s32Flow; // 记录反流最大流量
                    }
                }
            }
        }
        return;
    }
    else // 正流或静水
    {
        if (s32Flow > ps_flash->s_para.u16LargeFlowThreshold) // 流速超过过流告警阈值（L/h）
        {
            ps_pulse_data->s_err_flow.u8SmallCnt++;
            if (ps_pulse_data->s_err_flow.u8SmallCnt >= 3)
            { /*清小流报警*/
                ps_pulse_data->s_err_flow.u8SmallCnt = 0;
                ps_pulse_data->s_err_flow.s32SmallFlow = 0;
                Svc_Flash_Clear_Device_Alarm(b18Small_Flow);
            }
            ps_pulse_data->s_err_flow.s32LargeFlow += s32Flow;
            ps_pulse_data->s_err_flow.u8LargeCnt = 0;
            if (ps_pulse_data->s_err_flow.s32LargeFlow > ps_flash->s_para.u16LargeFlowContinue) // 持续过流超出预设值
            {
                if (Svc_Flash_IsDevice_Alarm(b17Large_Flow) == 0) // 原来没有过流报警
                {
                    Svc_Flash_Set_Device_Alarm(b17Large_Flow);
                    memcpy(ps_flash->s_security.s_state.pu8LargeFlowStartTime, s_cloud->s_manage.pu8CurTime, 6); // 记录过流开始时间
                    Svc_Flash_Save_Meter_Event(E_Event_Reverse_Flow);                                            // 事件存储
                }
                else if (Svc_Flash_IsDevice_Alarm(b17Large_Flow) == 1) // 原来已经过流报警
                {
                    if (s32Flow >= ps_flash->s_security.s_state.u16MaxLargeFlow)
                    {
                        ps_flash->s_security.s_state.u16MaxLargeFlow = s32Flow;                      // 记录过流最大流量
                        ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u16DayMaxFlow = s32Flow; // 记录日最大流量
                        memcpy(ps_flash->s_usage_info.s_day_usage.s_realtime_usage.pu8DayMaxFlowTime, s_cloud->s_manage.pu8CurTime, 6);
                    }
                }
            }
            return;
        }

        if (s32Flow <= ps_flash->s_para.u16SmallFlowThreshold) // 流速小于告警阈值（L/h）
        {
            ps_pulse_data->s_err_flow.u8LargeCnt++;
            if (ps_pulse_data->s_err_flow.u8LargeCnt >= 3)
            { /*清过流报警*/
                ps_pulse_data->s_err_flow.u8LargeCnt = 0;
                ps_pulse_data->s_err_flow.s32LargeFlow = 0;
                Svc_Flash_Clear_Device_Alarm(b17Large_Flow);
            }
            ps_pulse_data->s_err_flow.s32SmallFlow += s32Flow;
            ps_pulse_data->s_err_flow.u8SmallCnt = 0;
            if (ps_pulse_data->s_err_flow.s32SmallFlow > ps_flash->s_para.u16SmallFlowContinue) // 持续小流超出预设值
            {
                if (Svc_Flash_IsDevice_Alarm(b18Small_Flow) == 0) // 原来没有小流报警
                {
                    Svc_Flash_Set_Device_Alarm(b18Small_Flow);
                    memcpy(ps_flash->s_security.s_state.pu8SmallFlowStartTime, s_cloud->s_manage.pu8CurTime, 6); // 记录小流开始时间
                    Svc_Flash_Save_Meter_Event(E_Event_Reverse_Flow);                                            // 事件存储
                }
                else if (Svc_Flash_IsDevice_Alarm(b18Small_Flow) == 1) // 原来已经小流报警
                {
                    if (s32Flow <= ps_flash->s_security.s_state.u16MinSmallFlow)
                    {
                        ps_flash->s_security.s_state.u16MinSmallFlow = s32Flow; // 记录小流最小流量
                    }
                }
            }
            return;
        }
        ps_pulse_data->s_err_flow.u8LargeCnt++;
        ps_pulse_data->s_err_flow.u8NegCnt++;
        ps_pulse_data->s_err_flow.u8SmallCnt++;
        if (ps_pulse_data->s_err_flow.u8LargeCnt >= 3)
        {
            ps_pulse_data->s_err_flow.u8LargeCnt = 0;
            ps_pulse_data->s_err_flow.s32LargeFlow = 0;
            Svc_Flash_Clear_Device_Alarm(b17Large_Flow);
        }
        if (ps_pulse_data->s_err_flow.u8NegCnt >= 3)
        {
            ps_pulse_data->s_err_flow.u8NegCnt = 0;
            ps_pulse_data->s_err_flow.s32NegFlow = 0;
            Svc_Flash_Clear_Device_Alarm(b13Reverse_Flow);
        }
        if (ps_pulse_data->s_err_flow.u8SmallCnt >= 3)
        {
            ps_pulse_data->s_err_flow.u8SmallCnt = 0;
            ps_pulse_data->s_err_flow.s32SmallFlow = 0;
            Svc_Flash_Clear_Device_Alarm(b18Small_Flow);
        }

        if (ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u16DayMaxFlow > s32Flow)
        {
            ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u16DayMaxFlow = s32Flow; // 记录日最大流量
            memcpy(ps_flash->s_usage_info.s_day_usage.s_realtime_usage.pu8DayMaxFlowTime, s_cloud->s_manage.pu8CurTime, 6);
        }
    }
}

/**
 * @brief: 用量记录
 * @return {void}
 * @note:
 * @see:
 * @param {gw_s32} s32Usage 流量，单位：L
 *
 */
void Svc_Pulse_Usage_Record(S_Pulse *ps_pulse_data)
{
    S_Flash_Data *ps_flash;
    ps_flash = Svc_Flash_Get_Data();
    ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u32CurTotalUsage = ps_pulse_data->s32Usage_mL;
    ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u32ReverseTotalUsage = ps_pulse_data->u32UsageRe;
    ps_flash->s_usage_info.s_day_usage.s_realtime_usage.u32ForwardTotalUsage = ps_pulse_data->s32Flow_mL;
}

/**
 * @brief: 处理单个报警
 * @return {void}
 * @note:
 * @see:
 * @param
 *
 */
static void Svc_Pulse_HandleAlarmBit(gw_u32 device, E_Flash_Meter_Event event, gw_u8 bitIndex)
{
    if (s_pulse.u8PulseAlarm & (1 << bitIndex))
    {
        if (!Svc_Flash_IsDevice_Alarm(device))
        {
            Svc_Flash_Set_Device_Alarm(device);
            Svc_Flash_Save_Meter_Event(event);
        }
    }
    else
    {
        Svc_Flash_Clear_Device_Alarm(device);
    }
}

/**
 * @brief: 报警记录
 * @return {void}
 * @note:
 * @see:
 * @param
 *
 */
void Svc_Pulse_Alarm_Record(S_Pulse *ps_pulse_data)
{
    /*-------------空管------------*/
    if (ps_pulse_data->u8PulseAlarm & (1 << 0))
    {
        s_pulse.s_err_flow.u8EmptyCnt++;
        s_pulse.s_err_flow.u8UnEmptyCnt = 0;
        if(s_pulse.s_err_flow.u8EmptyCnt >= 30) //异常次数大于30次
        {
            if (!Svc_Flash_IsDevice_Alarm(b26Blank_Pipe)) //原本没有这个报警
            {
                s_pulse.s_err_flow.u8EmptyCnt = 0;
                Svc_Flash_Set_Device_Alarm(b26Blank_Pipe); //设置报警
                Svc_Flash_Save_Meter_Event(E_Event_Blank_Pipe); //事件记录
            }
        } 
    }
    else 
    {
        s_pulse.s_err_flow.u8UnEmptyCnt++;
        s_pulse.s_err_flow.u8EmptyCnt = 0;
        if(s_pulse.s_err_flow.u8UnEmptyCnt >= 30) //非异常次数大于30
        {
            s_pulse.s_err_flow.u8UnEmptyCnt = 0;
            Svc_Flash_Clear_Device_Alarm(b26Blank_Pipe); //清除报警
        }
    }
    /*-------------温度传感器------------*/
    Svc_Pulse_HandleAlarmBit(b30Temp_Sensor, E_Event_Temp_Sensor, 1);
    /*-------------SPI 通信异常------------*/
    Svc_Pulse_HandleAlarmBit(b29Time_Chip, E_Event_Time_Chip, 3);
}

/**
 * @brief: 温度检查
 * @return {void}
 * @note:
 * @see:
 * @param
 *
 */
void Svc_Pulse_Temp_Check(S_Pulse *ps_pulse_data)
{
    if (ps_pulse_data->s16WaterTemp > HIGHT_TEMP_THRESHOLD)
    {
        if (Svc_Flash_IsDevice_Alarm(b20Temp_Hig) == 0) // 原来没有报警
        {
            Svc_Flash_Set_Device_Alarm(b20Temp_Hig); // 设置报警
            Svc_Flash_Save_Meter_Event(E_Event_Temp_Hight);
        }
    }
    else if (ps_pulse_data->s16WaterTemp < LOW_TEMP_THRESHOLD)
    {
        if (Svc_Flash_IsDevice_Alarm(b21Temp_Low) == 0) // 原来没有报警
        {
            Svc_Flash_Set_Device_Alarm(b21Temp_Low); // 设置报警
            Svc_Flash_Save_Meter_Event(E_Event_Temp_Low);
        }
    }
    else
    {
        if (Svc_Flash_IsDevice_Alarm(b20Temp_Hig) == 1)
        {
            Svc_Flash_Clear_Device_Alarm(b20Temp_Hig);
        }
        if (Svc_Flash_IsDevice_Alarm(b21Temp_Low) == 1)
        {
            Svc_Flash_Clear_Device_Alarm(b21Temp_Low);
        }
    }
}
