#include "soft_flow.h"
#include "soft_device.h"
#include "usart_data_handle.h"
#include "main_task.h"
#include "string.h"
#include "soft_can.h"
#include "main.h"


mimo_flow flow_mimo1;
mimo_flow flow_mimo2;
flow_hl flow_dev1;
flow_hl flow_dev2;
/**
  * @file    can_recv_mimor_flow_function
  * @brief   恩曌流量计检测
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void can_recv_mimor_flow_function(uint32_t CanID, uint8_t data[], uint8_t len)
{

    Dev.Flow.facid = FAC_MIMO_SIG;
    Dev.Flow_Link1.connect_status = COMP_NORMAL;
    Dev.Flow_Link1.recv_time = HAL_GetTick();

    if(Dev.Flow_Link2.connect_status == COMP_NORMAL)
    {
        Dev.Flow.facid = FAC_MIMO_DOU;
    }
    if(CanID == CAN_MIMO_FLOW)
    {
        switch (data[0])
        {
        case 0:

            memcpy(&flow_mimo1.ch,&data[0],8);

            if(flow_mimo1.last_total_ml > flow_mimo1.total_ml)
            {
                flow_mimo1.overturn_count++;
            }

            flow_mimo1.last_total_ml = flow_mimo1.total_ml;

            break;
        case 1:
            Dev.Flow_Link2.connect_status = COMP_NORMAL;
            Dev.Flow_Link2.recv_time = HAL_GetTick();

            memcpy(&flow_mimo2.ch,&data[0],8);
            
            if(flow_mimo2.last_total_ml > flow_mimo2.total_ml)
            {
                flow_mimo2.overturn_count++;
            }

            flow_mimo2.last_total_ml = flow_mimo2.total_ml;
            break;
        default:
            break;
        }
    }
    else if(CanID == CAN_MIMO_FLOW_K)
    {
        if(data[1] == 0xFB && data[2] == 0x01)
        switch (data[2])
        {
        case 0:
            if(data[4] == 0XD1)
            {
               flow_mimo1.flow_k = data[5] * 256 + data[6]; 
            }
            break;
        case 1:
            if(data[4] == 0XD1)
            {
                flow_mimo2.flow_k = data[5] * 256 + data[6];
            }
            break;
        default:
            break;
        }
    }
}


/**
  * @file    Flow_recieved_hookfuction
  * @brief   VK协议流量计解析
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
flow flow_inf;
void Flow_recieved_hookfuction(uint32_t cellCanID,uint8_t data[], uint8_t len)
{
    
    switch (cellCanID)
    {
        case 0x2012C0:
            flow_inf.ch = data[0];
            switch (flow_inf.ch)
            {
            case 1:
                memcpy(&flow_inf.ch1.speed,&data[1],5);

                if(flow_inf.ch1.last_ml  > flow_inf.ch1.ml)
                {
                    flow_inf.ch1.overturn++;
                }
                flow_inf.ch1.last_ml = flow_inf.ch1.ml;

                Dev.Flow_Link1.connect_status = COMP_NORMAL;
                Dev.Flow_Link1.recv_time = HAL_GetTick();
                break;
            case 2:
                memcpy(&flow_inf.ch2.speed,&data[1],5);

                if(flow_inf.ch2.last_ml  > flow_inf.ch2.ml)
                {
                    flow_inf.ch2.overturn++;
                }
                flow_inf.ch2.last_ml = flow_inf.ch2.ml;

                Dev.Flow_Link2.connect_status = COMP_NORMAL;
                Dev.Flow_Link2.recv_time = HAL_GetTick();
                break;
            default:
                break;
            }
        break;
        case 0x2812C0:
            switch (data[0])
            {
            case 0xF1:
                if(data[1]== 0x01)
                    flow_inf.ch1.k = data[2] + ((data[3] << 8) & 0xff00);
                if(data[4]== 0x02)
                    flow_inf.ch2.k = data[5] + ((data[6] << 8) & 0xff00);
                pmu_set_ack(_MSGID_SET,MSGID_SET_MIMO_FLOW,flow_inf.ch1.k,flow_inf.ch2.k);
                flow_inf.ch1.set_k = false;
                break;
            case 0XF2:
                if(data[1]== 0x01)
                    flow_inf.ch1.k = data[2] + ((data[3] << 8) & 0xff00);
                if(data[4]== 0x02)
                    flow_inf.ch2.k = data[5] + ((data[6] << 8) & 0xff00);

                if(flow_inf.ch2.k != 0) //多判断一次，CAN通讯质量不高可能获取不到厂家信息
                {
                    Dev.Flow.facid = FAC_QIFEI_DOU;
                }
                flow_inf.ch1.get_k = true;
                break;
            case 0XF3:
                flow_inf.ch1.k_multiple = data[1] + ((data[2] << 8) & 0xff00);
                flow_inf.ch2.k_multiple = data[1] + ((data[2] << 8) & 0xff00);
                break;
            case 0XF7:
                if(data[1] == 'Q' && data[2] == 'F')
                {
                    Dev.Flow.facid = FAC_QIFEI_SIG;
                    if(Dev.Flow_Link2.connect_status == COMP_NORMAL)
                    {
                        Dev.Flow.facid = FAC_QIFEI_DOU;
                    }
                }
                break;
            default:
                break;
            }
            break;
        default:
            break;
    }

}


/**
  * @file    can_sendmsg_flow
  * @brief   发送信息到流量计
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void can_sendmsg_flow(void)
{   
    uint8_t can_buf[8] = {0};
    
    //获取厂商ID
    if(Dev.Flow_Link1.connect_status == COMP_NORMAL && Dev.Flow.facid == 0xff)
    {
        can_buf[0] = 0XF7;
        can_buf[7] = 0XFE;
        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x216020, CAN_ID_EXT);   
    }
    //上电读取流量计K值 和 系数单位
    else if(Dev.Flow_Link1.connect_status == COMP_NORMAL && (Dev.Flow.facid == FAC_VK || 
        Dev.Flow.facid == FAC_QIFEI_SIG ||Dev.Flow.facid == FAC_QIFEI_DOU) && flow_inf.ch1.get_k == false)
    {
        can_buf[0] = 0XF2;
        can_buf[7] = 0XFE;
        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x216020, CAN_ID_EXT);   

        HAL_Delay(100);

        can_buf[0] = 0XF3;
        can_buf[7] = 0XFE;
        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x216020, CAN_ID_EXT);  

    }
    else if (Dev.Flow_Link1.connect_status == COMP_NORMAL && (Dev.Flow.facid == FAC_VK || 
        Dev.Flow.facid == FAC_QIFEI_SIG ||Dev.Flow.facid == FAC_QIFEI_DOU) &&
         flow_inf.ch1.set_k == true)
    {
        can_buf[0] = 0XF1;
        can_buf[1] = 0x1;
        can_buf[5] = flow_inf.ch1.cal_k;
        can_buf[6] = (flow_inf.ch1.cal_k >> 8) & 0xff; //12做个调换
        can_buf[4] = 0x2;
        can_buf[2] = flow_inf.ch2.cal_k;
        can_buf[3] = (flow_inf.ch2.cal_k >> 8) & 0xff;
        can_buf[7] = 0XFE;

        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x216020, CAN_ID_EXT);
    }

    
}



/**
  * @file    L1L2_GPIO_check
  * @brief   34IO端口检测
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
GPIO_PinState L3_status = GPIO_PIN_SET;
GPIO_PinState L4_status = GPIO_PIN_SET;
void L1L2_GPIO_check()
{
    static uint32_t l1l2_time = 0;

    if(Check_Timer_Ready(&l1l2_time,_1_HZ_))
    {
        L3_status = HAL_GPIO_ReadPin( GPIOA, A8_LIQUID_Pin );

    }
}