#include "soft_eft.h"
#include "common.h"
#include "soft_p_2_c.h"
#include "string.h"
#include "soft_seed_device.h"
#include "soft_version.h"


uint8_t seed_output_mode = 1;
eft eft_info;

short tppwm_value = 1000;
short sow_rotate_value = 1000;

/**
  * @file    EftCanRecvHookFunction
  * @brief   EFT信息接收
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
_Temp_sensor Temp_sensor;
_mimo_lackloss mimo_lackloss;
_mimo_lackloss DM_lackloss;
water_dev Lpump1;
water_dev Lpump2;
weight70_dev z70weight;
bool weight_runing_time = false;
bool eft_sparyDev_priority = false;//EFT飞机存在两种水泵、称重ID一起发送
bool eft_weightDev_priority = false;
uint16_t LiftingWeight_warning = 0;
void EftCanRecvHookFunction(uint32_t cellCanID, uint8_t data[], uint8_t len)
{
    switch (cellCanID)
    {
    case REVE_EFT_INFO_1:
        //eft是大端 
        eft_info.valve_angel = data[0];
        eft_info.warn_status = data[1];
        eft_info.can_enable = data[2];
        eft_info.speed_rpm = data[5];

        Dev.Seed_Link.connect_status = COMP_NORMAL;
        Dev.Seed.facid = FAC_EFT;
        Dev.Seed_Link.recv_time = HAL_GetTick();
        break;
    case REVE_EFT_INFO:
        eft_info.enginearm_lock = data[0];
        
        if(eft_weightDev_priority != true)
        {
            eft_info.weight = data[1] * 256 + data[2];
            Dev.Weight_Link.connect_status = COMP_NORMAL;
            Dev.Weight.facid = FAC_EFT;
            Dev.Weight_Link.recv_time = HAL_GetTick();
        }

        eft_info.pump1_rpm = data[4];
        eft_info.pump2_rpm = data[3];
        eft_info.cent1_rpm = data[5];
        eft_info.cent2_rpm = data[6];
        eft_info.watering_warn_status = data[7];

        

        if(eft_sparyDev_priority == false)
        {
            Dev.Pump_Link.connect_status = COMP_NORMAL;
            Dev.Pump.facid = FAC_EFT;
            Dev.Pump_Link.recv_time = HAL_GetTick();
        }

        Dev.Nozzle_Link.connect_status = COMP_NORMAL;
        Dev.Nozzle.facid = FAC_EFT;
        Dev.Nozzle_Link.recv_time = HAL_GetTick();

        Dev.Arm_Link.connect_status = COMP_NORMAL;
        Dev.Arm.facid = FAC_EFT;
        Dev.Arm_Link.recv_time = HAL_GetTick();
        break;
    case REVE_EFT_LOW:
        if(data[0] == 0x03)
        {
            Dev.Lackloss_Link.connect_status = COMP_NORMAL;
            Dev.Lackloss_Link.recv_time = HAL_GetTick();
            mimo_lackloss.distance = data[2] + data[1] * 256;
            mimo_lackloss.strength = data[4] + data[3] * 256;
            mimo_lackloss.status = data[7];
        }
        break;
    case REVE_EFT_ACK:
        switch (data[0])
        {
        case 0xFA:
            break;
        case 0xF5:
            pmu_set_ack(_MSGID_SET,25,Cal_Seed_Back,0); 
            break;
        case 0xF6:
            pmu_set_ack(_MSGID_SET,25,Cal_Remove_Peel,0); 
            break;
        case 0xF7:
            pmu_set_ack(_MSGID_SET,25,Cal_Weight,0);
            break;
        case 0xf8:
            for(uint8_t i = 0;i < 4;i++)
            {
                eft_info.sn[i*2] = ((data[i+1] >> 4) & 0xf) + '0';
                eft_info.sn[i*2+1] = (data[i+1] & 0xf) + '0';
            }
            eft_info.sn[8] = ((data[7] >> 4) & 0xf) + '0';
            eft_info.sn[9] = (data[7] & 0xf) + '0';
            //Dev_info.dev_connect.dev_flag.seed = true;
            break;
        case 0xF9:
            eft_info.version[0] = data[1] + '0';
            eft_info.version[1] = data[2] + '0';
            //Dev_info.dev_connect.dev_flag.seed = true;
            break;
        case 0xFC:
            eft_info.seed_k[weight_order.order_con1 - 1] = weight_order.order_con2 & 0xff;
            pmu_set_ack(_MSGID_SET,MSGID_SET_WEIGHT_K,0,0);
            break;
        case 0xFD:
            memcpy(&eft_info.seed_k[0],&data[1],3);
            weight_init_eft.step.read_k_flag = 0;
            break;
        default:
            break;
        }
        break;
    case BMS_LINE_TEMP:
        Temp_sensor.Positive_pole = data[0] * 256 + data[1];
        Temp_sensor.Negative_pole = data[2] * 256 + data[3];

        Dev.Temp_Sensor_Link.connect_status = COMP_NORMAL;
        Dev.Temp_sensor.facid = FAC_EFT;
        Dev.Temp_Sensor_Link.recv_time = HAL_GetTick();
        regist_dev_info(&dev_tempSensor,DEVICE_TEMP_SENSOR,false,NULL,0,NULL,0,NULL,0,"EFT",4);
        break;
    case LPump1:
        eft_sparyDev_priority = true;
        memcpy(&Lpump1,&data[0],sizeof(water_dev));

        Dev.L_pump1_Link.connect_status = COMP_NORMAL;
        Dev.L_pump1.facid = FAC_EFT;
        Dev.L_pump1_Link.recv_time = HAL_GetTick();
        Dev.Flow.facid = FAC_LPUMP;
        break;
    case LPump2:
        eft_sparyDev_priority = true;
        memcpy(&Lpump2,&data[0],sizeof(water_dev));

        Dev.L_pump2_Link.connect_status = COMP_NORMAL;
        Dev.L_pump2.facid = FAC_EFT;
        Dev.L_pump2_Link.recv_time = HAL_GetTick();
        Dev.Flow.facid = FAC_LPUMP;
        break;
    case CAN_EFT70_WEIGHT:
        eft_weightDev_priority = true;
        eft_info.weight = (data[0] + data[1] * 256);
        LiftingWeight_warning = data[2];

        Dev.Weight_Link.connect_status = COMP_NORMAL;
        Dev.Weight.facid = FAC_LIFTWEIGHT;
        Dev.Weight_Link.recv_time = HAL_GetTick();
        break;
    case CAN_EFT70_WEIGHT_ACK:
        //称重回馈
        switch (data[0])
        {
        case 0xe1:
            pmu_set_ack(_MSGID_SET,25,Cal_Remove_Peel,0); 
            break;
        case 0xe2:
            pmu_set_ack(_MSGID_SET,25,Cal_Weight,0);
            break;
        case 0xe3:
            pmu_set_ack(_MSGID_SET,MSGID_SET_WEIGHT_K,0,0);
            break;
        case 0xe4:
            z70weight.info.k1 = data[1] + ((data[2] & 0x3f) << 8);
            z70weight.info.k2 = (data[2] >> 6) + (data[3] << 2) + ((data[4] & 0xf) << 10);
            z70weight.info.k3 = (data[4] >> 4) + (data[5] << 4)  + ((data[6] & 0x3) << 12);
            z70weight.info.k4 = (data[6] >> 2) + (data[7] << 6);
            break;
        case 0XF3:
            for(uint8_t i = 0;i < 3;i++)
            {
                dev_weight.sn[2 * i] = ((data[2+i] >> 4) & 0xf) + '0';
                dev_weight.sn[2 * i + 1] = (data[2+i] & 0xf )+ '0';
            }
            dev_weight.sn[6] = ((data[7] >> 4) & 0xf) + '0';
            dev_weight.sn[7] = (data[7] & 0xf )+ '0';

            regist_dev_info(&dev_weight,DEVICE_WEIGHT,false,dev_weight.sn,8,NULL,0,NULL,0,"eftweight",10);
            break;
        case 0xF4:
            for (uint8_t i = 2; i < 5; i++)
            {
                if(data[i] < 10)
                {
                    dev_weight.soft_serial[2 * (i - 2)] = '0';
                    Int2String(data[i],&dev_weight.soft_serial[2 * (i - 2) + 1],1);
                }
                else
                {
                    Int2String(data[i],&dev_weight.soft_serial[ (i - 2) * 2],2);
                }

            }
            if(data[7] < 10)
            {
                dev_weight.soft_serial[2 * (4 - 1)] = '0';
                Int2String(data[7],&dev_weight.soft_serial[2 * (4 - 1) + 1],1);
            }
            else
            {
                Int2String(data[7],&dev_weight.soft_serial[ (4 - 1) * 2],2);
            }

            regist_dev_info(&dev_weight,DEVICE_WEIGHT,false,NULL,0,dev_weight.soft_serial,8,NULL,0,"eftweight",10);
            break;
        case 0xF5:
            Dev.Weight.run_time = data[2] + (data[3] + data[4] * 256) * 60;
            weight_runing_time = true;
            break;
        case 0xF7:
            pmu_set_ack(_MSGID_SET,MSGID_SET_RESIWIRE_BLOWN,data[1],0);
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
}

/**
  * @file    can_sendmsg_eft
  * @brief   发送信息给EFT
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void can_sendmsg_eft(void)
{
    static int eft_count = 0;
    uint8_t eft_canbuf[8] = {0};

    if(Dev.Seed_Link.connect_status == COMP_NORMAL && Dev.Seed.facid == FAC_EFT)
    {
        seed_init_send_info(SEED_DEVICE,"EFT");
        Set_Seed_Weight_Par(SEED_DEVICE,"EFT");

        if(seed_output_mode == 1)
        {   
            if(HAL_GetTick() < 5000) //初始化自检
            {
                tppwm_value = 1000;
                sow_rotate_value = 1000;
            }
            //发送转速 和阀门
            eft_canbuf[0] = 0xf1;
            eft_canbuf[1] = (tppwm_value >> 8) & 0xff;
            eft_canbuf[2] = tppwm_value & 0xff;
            eft_canbuf[3] = (sow_rotate_value >> 8) & 0xff;
            eft_canbuf[4] = sow_rotate_value & 0xff;
            eft_canbuf[5] = 0xf1;
            eft_canbuf[6] = 0xf3;
            eft_canbuf[7] = eft_count;

            can_send_msg_normal((unsigned char *)&eft_canbuf, 8, SEND_EFT_INFO);
            eft_count++;

            if(eft_count >= 256)
            {
                eft_count = 0;
            }
        }
    }
    if(Dev.Weight_Link.connect_status == COMP_NORMAL && (Dev.Weight.facid == FAC_EFT || Dev.Weight.facid == FAC_LIFTWEIGHT)) 
    {   
        seed_init_send_info(WEIGHT_DEVICE,"EFT");
        Set_Seed_Weight_Par(WEIGHT_DEVICE,"EFT");
    }

    if(planep.UAV_type == AG_EFTZ)
    {
        eft_canbuf[0] = 0xE1;
        eft_canbuf[1] = (pmu_pin.aux_light & 0x1) > 0? (planep.lock_status > 3? (pmu_pin.aux_light >> 2 & 0x7f) : 20) : 0;
        eft_canbuf[2] = (pmu_pin.aux_light & 0x2) > 0? (planep.lock_status > 3? (pmu_pin.aux_light >> 9 & 0x7f) : 20) : 0;
        eft_canbuf[3] = 0;
        eft_canbuf[4] = 0;
        eft_canbuf[5] = 0xf1;
        eft_canbuf[6] = 0xf3;
        eft_canbuf[7] = 0;

        can_send_msg_normal((unsigned char *)&eft_canbuf, 8, SEND_EFT_INFO);
    }
}
void DMlacklossCanRecvFunction(uint32_t cellCanID, uint8_t data[], uint8_t len)//电目断料计接收函数
{
    /*if(cellCanID==CAN_MSGID_DM_LACKLOSS)
    {
        uint32_t DMLackloss_recvtime=0;
        Dev.DM_Lackloss_Link.connect_status=COMP_NORMAL;
        
        Dev.DM_Lackloss_Link.recv_time = HAL_GetTick();

        DMLackloss_recvtime=Dev.DM_Lackloss_Link.recv_time;

        DM_lackloss.status=(data[0] ^ 1);
        DM_lackloss.distance=DMLackloss_recvtime & 0xFFFF;
        DM_lackloss.strength=(DMLackloss_recvtime >> 16) & 0xFFFF;
    }*/
    switch (data[0])
    {
    case 0XA0:
        Dev.Checklow.facid = FAC_DM_CHECKLOW;
        Dev.Lackloss_Link.connect_status= COMP_NORMAL;
        Dev.Lackloss_Link.recv_time = HAL_GetTick();
        DM_lackloss.status = 0;
        break;
    case 0XA1:
        Dev.Checklow.facid = FAC_DM_CHECKLOW;
        Dev.Lackloss_Link.connect_status = COMP_NORMAL;
        Dev.Lackloss_Link.recv_time = HAL_GetTick();
        DM_lackloss.status = 1;
        break;
    case 0XB0:
        pmu_set_ack(_MSGID_SET,MSGID_SET_LACKLOSS_CAL,0x01,0x00);//成功
        break;
    case 0XB1 ... 0XB2:
        pmu_set_ack(_MSGID_SET,MSGID_SET_LACKLOSS_CAL,0x02,0x00);//失败
        break;
    default:
        break;
    }
}

