#include "soft_seed_weight.h"
#include "soft_device.h"
#include "common.h"
#include "string.h"
#include "soft_can.h"
#include "usart_data_handle.h"
#include "main_task.h"
#include "soft_update.h"
#include "soft_version.h"
#include "soft_water.h"
#include "config.h"

Spread_par spread_par;
weight weight_vkinfo;
/**
  * @file    Eft_Spread_And_Mimolack_recieved_hookfuction
  * @brief   eft播撒器带恩曌断料记
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
eft eft_info;
_mimo_lackloss mimo_lackloss;
void Eft_Spread_And_Mimolack_recieved_hookfuction(uint32_t CanID, uint8_t data[], uint8_t len)
{
    switch (CanID)
    {
    //播撒器
    case CAN_EFT_SPREAD:
        //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 CAN_EFT_VERSION:
        if(data[5] == 0xF1 && data[6] == 0xF3)
        {
            switch (data[0])
            {
            case 0XF8:
                for(uint8_t i = 0;i < 3;i++)
                {
                    dev_spreader.sn[2 * i] = ((data[2+i] >> 4) & 0xf) + '0';
                    dev_spreader.sn[2 * i + 1] = (data[2+i] & 0xf )+ '0';
                }
                dev_spreader.sn[6] = ((data[7] >> 4) & 0xf) + '0';
                dev_spreader.sn[7] = (data[7] & 0xf )+ '0';

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

                }
                regist_dev_info(&dev_spreader,DEVICE_SPREAD,false,NULL,0,dev_spreader.soft_serial,8,NULL,0,"eftspreader",12);
                break;
            case 0XFB:
                Dev.Seed.runtime = data[1] + (data[2] + data[3] * 256) * 60;
                spreader_runing_time = true;
                break;
            default:
                break;
            }
        }
        break;
    case CAN_MIMO_LACKLOSS:
        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;
    default:
        break;
    }
}

/**
  * @file    can_recv_eft_front_plate
  * @brief   eftz20前板
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
eft_fplate fplate;
Connect_check z20_fplate_link;
bool Z70_LiftingWeight_exist = false;
void can_recv_eft_front_plate(uint32_t CanID, uint8_t data[], uint8_t len)
{
    switch (CanID)
    {
    case CAN_EFT_FPLATE:
        if(drone_type == Drone_Z20) //20 70称重协议冲突
        {
            memcpy(&fplate,&data[0],sizeof(eft_fplate));

            Dev.Weight_Link.connect_status = COMP_NORMAL;
            Dev.Weight.facid = FAC_EFT;
            Dev.Weight_Link.recv_time = HAL_GetTick();
        }
        else if(drone_type == Drone_Z70)
        {
            memcpy(&fplate,&data[0],1);
            memcpy(&fplate.Rled_tempture,&data[3],4);
        }

        
        Dev.Arm_Link.connect_status = COMP_NORMAL;
        Dev.Arm.facid = FAC_EFT;
        Dev.Arm_Link.recv_time = HAL_GetTick();

        break;
    case CAN_EFT_FPLATE_ACK:
        switch (data[0])
        {
        //LED控制ACK指令
        case 0xE1:
            last_light_brightness = pmu_pin.aux_light;
            break;
        //版本信息
        case 0xE2:
            for (uint8_t i = 1; i < 5; i++)
            {
                if(data[i] < 10)
                {
                    dev_weight.soft_serial[2 * (i - 1)] = '0';
                    Int2String(data[i],&dev_weight.soft_serial[2 * (i - 1) + 1],1);
                }
                else
                {
                    Int2String(data[i],&dev_weight.soft_serial[ (i - 1) * 2],2);
                }

            }
            regist_dev_info(&dev_weight,DEVICE_WEIGHT,false,NULL,0,dev_weight.soft_serial,8,NULL,0,"eftweight",10);
            break;
        //序列号
        case 0xE3:
            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 0xE4:
            Dev.Weight.runtime = data[1] + (data[2] + data[3] * 256) * 60;
            weight_runing_time = true;
            break;
        //设置波特率ACK
        case 0xF4:
            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 0xEB:
            eft_dev_update_wait_ereseflash(data);
            break;
        //K值校准
        case 0xFC:
            eft_info.seed_k[weight_order.order_con1 - 1] = weight_order.order_con2;
            pmu_set_ack(_MSGID_SET,MSGID_SET_WEIGHT_K,0,0);
            break;
        //K值查询
        case 0xFD:
            memcpy(&eft_info.seed_k[0],&data[1],2);
            memcpy(&eft_info.seed_k[1],&data[3],2);
            memcpy(&eft_info.seed_k[2],&data[6],2);
            weight_init_eft.step.read_k_flag = 0;
            break;
        
        default:
            break;
        }
        break;
    default:
        break;
    }
    
}


/**
  * @file    seed_init_send_info
  * @brief   播撒器初始化
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
Dev_weight_init weight_init_eft = {.step.read_k_flag = 1};
Dev_weight_init weight_init_vk = {.step.dose_rate_flag = 1, 
                                    .step.weight_mode_flag = 1, 
                                    .step.read_k_flag = 1, 
                                    .step.fac_num_flag = 1};
void seed_init_send_info(uint8_t device_type,char *factory)
{
    uint8_t vk_canbuf[8] = {0,0,0,0,0,0XFE,0XFE,0};
    static uint8_t spread_init_count = 10;
    int vk_can_id = 0;

    if(strcmp(factory,"VK") == 0)
    {
        switch (device_type)
        {
        //称重
        case WEIGHT_DEVICE:
            vk_can_id = 0x81321;
            if(weight_init_vk.status != 0)
            {
                if(weight_init_vk.step.read_k_flag != 0)
                {
                    vk_canbuf[0] = 0xE1;
                }
                else if(weight_init_vk.step.fac_num_flag != 0 && weight_init_count > 0)
                {
                    vk_canbuf[0] = 0xF7;
                    weight_init_count--;
                    if(weight_init_count == 0)
                    {
                        weight_init_count = 10;
                        weight_init_vk.step.fac_num_flag = 0;
                    }
                }
                else if (weight_init_vk.step.weight_mode_flag != 0 && weight_init_count > 0)
                {
                    vk_canbuf[0] = 0xE3;
                    weight_init_count--;
                    if(weight_init_count == 0)
                    {
                        weight_init_count = 10;
                        weight_init_vk.step.weight_mode_flag = 0;
                    }
                }
                else if (weight_init_vk.step.dose_rate_flag != 0 && weight_init_count > 0)
                {
                    vk_canbuf[0] = 0xE5;
                    weight_init_count--;
                    if(weight_init_count == 0)
                    {
                        weight_init_count = 10;
                        weight_init_vk.step.dose_rate_flag = 0;
                    }
                }
                Can_Send_Msg_Func(CANID2, vk_canbuf, 8, vk_can_id, CAN_ID_EXT); 
            }
            break;
        //播撒
        case SEED_DEVICE:
            vk_can_id = 0x81324;
            if(spread_init_count > 0)
            {
                //获取厂家信息
                vk_canbuf[0] = 0xf7;
                spread_init_count--;
                Can_Send_Msg_Func(CANID2, vk_canbuf, 8, vk_can_id, CAN_ID_EXT); 
            }
            break;
        default:
            break;
        }
    }   
    else if (strcmp(factory,"EFT") == 0)
    {     
        vk_can_id = 0x8877;
        switch (device_type)
        {
        case WEIGHT_DEVICE:
            if(weight_init_eft.status != 0)
            {
                if(weight_init_eft.step.read_k_flag != 0)
                {
                    vk_canbuf[0] = 0xFD;
                    vk_canbuf[5] = 0xf1;
                    vk_canbuf[6] = 0xf3;
                }
                Can_Send_Msg_Func(CANID1, vk_canbuf, 8, vk_can_id, CAN_ID_EXT); 
            }
            break;
        case SEED_DEVICE:
            if(seed_init.status != 0)
            {
                if(seed_init.step.sn_flag != 0)
                {

                }
                else if(seed_init.step.version_flag != 0)
                {

                }
                //Can_Send_Msg_Func(CANID1, vk_canbuf, 8, vk_can_id, CAN_ID_EXT); 
            }

            break;
        default:
            break;
        }
    }
}

/**
  * @file    Set_Seed_Weight_Par
  * @brief   设置称重播撒参数
  * @param    1 称重
              2 播撒
  * @details 
  * @author  Zhang Sir 
 **/
seed seed_vkinfo;
void Set_Seed_Weight_Par(uint8_t device_type,char *factory)
{   
    uint8_t can_buf[8] = {0};
    uint32_t can_id = 0;
    if(strcmp(factory,"VK") == 0)
    {
        switch (device_type)
        {
        case WEIGHT_DEVICE:
            if(weight_order.type != 0)
            {
                can_buf[1] = 0x00;  //D1 - D4
                can_buf[2] = 0x00;
                can_buf[3] = 0x00;
                can_buf[4] = 0x00;
                can_buf[5] = 0xFE;
                can_buf[6] = 0xFE;
                can_buf[7] = 0x00;  //D5

                switch (weight_order.type)
                {
                //去皮校准
                case Weight_Peer:
                    can_buf[0] = 0xF1;
                    break;
                //重量校准
                case Weight_Kg:
                    can_buf[0] = 0xF2;
                    memcpy(&can_buf[1], &weight_order.order_con2,2);
                    break;
                case Weight_Reserve:
                    break;
                //恢复出厂设置
                case Weight_Bcak:
                    can_buf[0] = 0xF4;
                    break;
                //称重传感器K值
                case Weight_Set_K:
                    can_buf[0] = 0xE2;
                    can_buf[1] = weight_order.order_con2 & 0xff;
                    can_buf[2] = (weight_order.order_con2 >> 8) & 0xff;

                    can_buf[7] = weight_order.order_con1 - 1; //VK协议传感器编号是0-3
                    break;
                //称重传感器模式
                case Weight_Mode:
                    can_buf[0] = 0xE4;
                    can_buf[1] = weight_order.order_con1 & 0xff;
                    break;
                //最高下药率
                case Weight_Drug_Rate:
                    can_buf[0] = 0xE6;
                    can_buf[1] = weight_order.order_con1 & 0xff;
                    can_buf[2] = (weight_order.order_con1 >> 8) & 0xff;
                    break;
                default:
                    break;
                }
                Can_Send_Msg_Func(CANID2, can_buf, 8, 0x81321, CAN_ID_EXT);

                weight_order.type = 0;
            }    
            break;
        case SEED_DEVICE:
            //更换信号输出方式 0 PWM 1 CAN
            if((seed_vkinfo.output != spread_par.output_mode) && recv_fmu_seed_info == true)
            {
                can_buf[0] = 0xF2;
                can_buf[1] = spread_par.output_mode;
                can_buf[2] = 0x00;
                can_buf[3] = 0x00;
                can_buf[4] = 0x00;
                can_buf[5] = 0xFE;
                can_buf[6] = 0xFE;
                can_buf[7] = 0;

                Can_Send_Msg_Func(CANID2, can_buf, 8, 0x81324, CAN_ID_EXT);
            }
            break;
        default:
            break;
        }
    }
    else if (strcmp(factory,"EFT") == 0)
    {
        switch (device_type)
        {
        case SEED_DEVICE:
            //更换信号输出方式 0 PWM 1 CAN
            if((eft_info.can_enable != spread_par.output_mode) && recv_fmu_seed_info == true)
            {
                can_buf[0] = 0xF2;
                can_buf[1] = spread_par.output_mode;
                can_buf[2] = 0x00;
                can_buf[3] = 0x00;
                can_buf[4] = 0x00;
                can_buf[5] = 0xf1;
                can_buf[6] = 0xf3;
                can_buf[7] = 0;

                Can_Send_Msg_Func(CANID2, can_buf, 8, SEND_EFT_INFO, CAN_ID_EXT);
                HAL_Delay(10);
            }        
            break;
        case WEIGHT_DEVICE:
            if(weight_order.type != 0)   
            {
                if(drone_type == Drone_Z20)
                {
                    can_id = 0x8877;
                    can_buf[1] = 0x00;
                    can_buf[2] = 0x00;
                    can_buf[3] = 0x00;
                    can_buf[4] = 0x00;
                    can_buf[5] = 0xf1;
                    can_buf[6] = 0xf3;
                    can_buf[7] = 0;

                    switch (weight_order.type)
                    {
                    case Weight_Peer:
                        can_buf[0] = 0xF6;
                        break;
                    case Weight_Kg:
                        can_buf[0] = 0xF7;
                        can_buf[1] = (weight_order.order_con2 >> 8) & 0xff;
                        can_buf[2] = weight_order.order_con2 & 0xff;
                        break;
                    case Weight_Reserve:
                        break;
                    case Weight_Bcak:
                        can_buf[0] = 0xF5;
                        break;
                    case Weight_Set_K:
                        can_buf[0] = 0xFC;
                        can_buf[1] = eft_info.seed_k[0];
                        can_buf[2] = eft_info.seed_k[0] >> 8;
                        can_buf[3] = eft_info.seed_k[1];
                        can_buf[4] = eft_info.seed_k[1] >> 8;
                        can_buf[6] = eft_info.seed_k[2];
                        can_buf[7] = eft_info.seed_k[2] >> 8;

                        if(weight_order.order_con1 == 1)
                        {
                            can_buf[1] = weight_order.order_con2;
                            can_buf[2] = weight_order.order_con2 >> 8;
                        }
                        else if(weight_order.order_con1 == 2)
                        {
                            can_buf[3] = weight_order.order_con2;
                            can_buf[4] = weight_order.order_con2 >> 8; 
                        }
                        else if(weight_order.order_con1 == 3)
                        {
                            can_buf[6] = weight_order.order_con2;
                            can_buf[7] = weight_order.order_con2 >> 8; 
                        }
                        
                        break;
                    default:
                        break;
                    }
                    Can_Send_Msg_Func(CANID1, can_buf, 8, can_id, CAN_ID_EXT);
                }
                else if(drone_type == Drone_Z70)
                {   
                    if(Z70_LiftingWeight_exist == true)
                        can_id = 0x88BB;
                    else
                        can_id = 0x7011;
                    
                    can_buf[1] = 0x00;  //D1 - D4
                    can_buf[2] = 0x00;
                    can_buf[3] = 0x00;
                    can_buf[4] = 0x00;
                    can_buf[5] = 0xF1;
                    can_buf[6] = 0xF3;
                    can_buf[7] = 0x00;  //D5

                    switch (weight_order.type)
                    {
                    //去皮校准
                    case Weight_Peer:
                        can_buf[0] = 0xE1;
                        break;
                    //重量校准
                    case Weight_Kg:
                        can_buf[0] = 0xE2;
                        memcpy(&can_buf[1], &weight_order.order_con2,2);
                        break;
                    case Weight_Reserve:
                        break;
                    //恢复出厂设置
                    case Weight_Bcak:
                        can_buf[0] = 0xF4;
                        break;
                    //称重传感器K值 
                    case Weight_Set_K:
                        z70weight.buf[0] = 0xE3;
                        if(weight_order.order_con1 == 1)
                            z70weight.info.k1 = weight_order.order_con2;
                        else if(weight_order.order_con1 == 2)
                            z70weight.info.k2 = weight_order.order_con2;
                        else if(weight_order.order_con1 == 3)
                            z70weight.info.k3 = weight_order.order_con2;
                        else if(weight_order.order_con1 == 4)
                            z70weight.info.k4 = weight_order.order_con2;
                        
                        memcpy(&can_buf,&z70weight.buf[0],8);
                        break;
                    //称重传感器模式
                    case Weight_Mode:
                        can_buf[0] = 0xE4;
                        can_buf[1] = weight_order.order_con1 & 0xff;
                        break;
                    //最高下药率
                    case Weight_Drug_Rate:
                        can_buf[0] = 0xE6;
                        can_buf[1] = weight_order.order_con1 & 0xff;
                        can_buf[2] = (weight_order.order_con1 >> 8) & 0xff;
                        break;
                    default:
                        break;
                    }
                    Can_Send_Msg_Func(CANID2, can_buf, 8, 0x7011, CAN_ID_EXT);
                }
                HAL_Delay(10);
                weight_order.type = 0;
            }
            break;
        default:
            break;
        }
    }
}



/**
  * @file    can_sendmsg_eft
  * @brief   发送信息给EFT
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
uint16_t last_light_brightness = 0;
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 && drone_type != Drone_Z70)
    {
        seed_init_send_info(SEED_DEVICE,"EFT");
        Set_Seed_Weight_Par(SEED_DEVICE,"EFT");

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

            Can_Send_Msg_Func(CANID2, eft_canbuf, 8, SEND_EFT_INFO, CAN_ID_EXT); 
            eft_count++;

            if(eft_count >= 256)
            {
                eft_count = 0;
            }
        }
    }
    if((Dev.Weight_Link.connect_status == COMP_NORMAL && Dev.Weight.facid == FAC_EFT)) 
    {   
        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] = 0;//0xf3;
        eft_canbuf[7] = 0;

        Can_Send_Msg_Func(CANID1, eft_canbuf, 8, 0x8877, CAN_ID_EXT); 
    }
}


/**
  * @file    can_sendmsg_VK
  * @brief   播撒常发数据包
  * @param    
  * @details 
  * @author  Zhang Sir 
 **/
void can_sendmsg_VK(void)
{
    static uint8_t vk_count = 0;
    uint8_t vk_canbuf[8] = {0};

    if(Dev.Weight_Link.connect_status == COMP_NORMAL && (Dev.Weight.facid == FAC_VK ||
         Dev.Weight.facid == FAC_QIFEI) && HAL_GetTick() > 3000)
    {
        seed_init_send_info(WEIGHT_DEVICE,"VK");
        Set_Seed_Weight_Par(WEIGHT_DEVICE,"VK");
    }

    if(Dev.Seed_Link.connect_status == COMP_NORMAL && (Dev.Seed.facid == FAC_VK ||
         Dev.Seed.facid == FAC_QIFEI) && HAL_GetTick() > 2000)
    {

        seed_init_send_info(SEED_DEVICE,"VK");
        Set_Seed_Weight_Par(SEED_DEVICE,"VK");

        if(spread_par.output_mode == 1 )
        {
            if(HAL_GetTick() < 5000) //初始化自检
            {
                spread_par.pwm_turntable = 1000;
                spread_par.pwm_valve = 1000;
            }
            //发送转速 和阀门
            vk_canbuf[0] = 0xf1;
            vk_canbuf[1] = spread_par.pwm_turntable & 0xff;
            vk_canbuf[2] = (spread_par.pwm_turntable >> 8) & 0xff;
            vk_canbuf[3] = spread_par.pwm_valve & 0xff;
            vk_canbuf[4] = (spread_par.pwm_valve >> 8) & 0xff;
            vk_canbuf[5] = 0xfe;
            vk_canbuf[6] = 0xfe;
            vk_canbuf[7] = vk_count;

            vk_count++;
            Can_Send_Msg_Func(CANID2, vk_canbuf, 8, 0x81324, CAN_ID_EXT);
        }

        if(vk_count >= 255)
        {
            vk_count = 0;
        }
        
    }
}