
#include "soft_version.h"
#include "soft_obstacle.h"
#include "soft_terrain.h"
#include "stm32f1xx_hal.h"
#include "soft_flow.h"
#include "soft_can.h"
#include "soft_p_2_c.h"
#include "config.h"
#include "soft_eft.h"
#include "string.h"
#include "soft_bms.h"
#include "soft_seed_device.h"
#include "soft_water_device.h"


dev_version_content dev_pmu;
dev_version_content dev_ter;
dev_version_content dev_obsf;
dev_version_content dev_obsb;
dev_version_content dev_bms1;
dev_version_content dev_bms2;
dev_version_content dev_spreader;
dev_version_content dev_weight;
dev_version_content dev_flowmeter;
dev_version_content dev_pump;
dev_version_content dev_arm;
dev_version_content dev_lack;
dev_version_content dev_centri;
dev_version_content dev_current;
dev_version_content dev_pump1,dev_pump2;
dev_version_content dev_nozzle1,dev_nozzle2,dev_nozzle3,dev_nozzle4;
dev_version_content dev_part_tradar;
dev_version_content dev_tradar_f;
dev_version_content dev_tradar_b;
dev_version_content dev_h_bmsA;
dev_version_content dev_h_bmsB;
dev_version_content dev_remoteid;
dev_version_content dev_tempSensor;

dev_version_content dev_null;

// void *dev_ptr[10] = {&temp,
                    
//                     &dev_pmu,
//                    &dev_ter,
//                    &dev_obsf,
//                    &dev_obsb,
//                    &dev_bms1,
//                    &dev_bms2,
//                    &dev_spreader,
//                    &dev_weight,
//                    &dev_flowmeter,
//                    &dev_arm,
//                   };
void *dev_ptr[] = {&dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_pmu,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_ter,
                   &dev_obsf,
                   &dev_obsb,
                   &dev_bms1,
                   &dev_bms2,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_null,
                   &dev_spreader,
                   &dev_weight,
                   &dev_pump,
                   &dev_centri,
                   &dev_flowmeter,
                   &dev_arm,
                   &dev_lack,
                   &dev_null,
                   &dev_current,
                   &dev_null,
                   &dev_pump1,
                   &dev_pump2,
                   &dev_null,
                   &dev_null,
                   &dev_nozzle1,
                   &dev_nozzle2,
                   &dev_nozzle3,
                   &dev_nozzle4,
                   &dev_part_tradar,
                   &dev_tradar_f,
                   &dev_tradar_b,
                   &dev_h_bmsA,
                   &dev_h_bmsB,
                   &dev_remoteid,
                   &dev_tempSensor,
                  };
short dev_num = sizeof( dev_ptr )/sizeof( void * );
/**
  * @file    get_radar_version_and_sn
  * @brief   获取雷达版本和SN号
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void get_radar_version_and_sn(void)
{
    uint8_t radar_can_buf[8] = {0};
    static uint8_t mimo_area_40flag = 10;

    //版本信息识别 mocib    mimo
    //新加同时获取SN号
    if(uavr11_info.Link.connect_status == COMP_NORMAL && uavr11_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 0x11; 
        can_send_msg_normal(radar_can_buf, 1, 0x00eeff11);
    }
    else if(uavr11_info.Link.connect_status == COMP_NORMAL && uavr11_info.get_radar_sn_flag == false)
    {
        radar_can_buf[0] = 0x85;    
        can_send_msg_normal(radar_can_buf, 1, 0x210);
    }
    if(uavr12_info.Link.connect_status == COMP_NORMAL && uavr12_info.get_radar_ver_flag == false)
    { 
        radar_can_buf[0] = 0x12;
        can_send_msg_normal(radar_can_buf, 1, 0x00eeff12);        
    }
    else if(uavr12_info.Link.connect_status == COMP_NORMAL && uavr12_info.get_radar_sn_flag == false)
    {
        radar_can_buf[0] = 0x85;    
        can_send_msg_normal(radar_can_buf, 1, 0x220);
    }
    if(uavr56_info.Link.connect_status == COMP_NORMAL && uavr56_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 0x0b;
        can_send_msg_normal(radar_can_buf, 1, 0x00eeff0b);
    }
    else if(uavr56_info.Link.connect_status == COMP_NORMAL && uavr56_info.get_radar_sn_flag == false)
    {
        radar_can_buf[0] = 0x85;    
        can_send_msg_normal(radar_can_buf, 1, 0x230);
    }
    if(mimo_f_info.Link.connect_status == COMP_NORMAL && mimo_f_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 0x55;
        radar_can_buf[1] = 0xAA;
        radar_can_buf[2] = 0x03;
        radar_can_buf[3] = 0x70;
        radar_can_buf[4] = 0x03;
        radar_can_buf[5] = 0x01;
        radar_can_buf[6] = 0x74;
        can_send_msg_normalstd(radar_can_buf, 7, 0xFA);

    }
    else if(mimo_b_info.Link.connect_status == COMP_NORMAL && mimo_b_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 0x55;
        radar_can_buf[1] = 0xAA;
        radar_can_buf[2] = 0x03;
        radar_can_buf[3] = 0x70;
        radar_can_buf[4] = 0x04;
        radar_can_buf[5] = 0x01;
        radar_can_buf[6] = 0x75;
        can_send_msg_normalstd(radar_can_buf, 7, 0xFA);
    }
    else if(mimo_ter_info.Link.connect_status == COMP_NORMAL && mimo_ter_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 0x55;
        radar_can_buf[1] = 0xAA;
        radar_can_buf[2] = 0x03;
        radar_can_buf[3] = 0x70;
        radar_can_buf[4] = 0x00;
        radar_can_buf[5] = 0x01;
        radar_can_buf[6] = 0x71;
        can_send_msg_normalstd(radar_can_buf, 7, 0xFA);
    }

    if(DM_ter_info.Link.connect_status == COMP_NORMAL && DM_ter_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 1;
        radar_can_buf[7] = 7;
        can_send_msg_normal(radar_can_buf, 8, 0x981300);
    }
    else if(DM_f_info.Link.connect_status == COMP_NORMAL && DM_f_info.get_radar_ver_flag == false)
    {
        radar_can_buf[0] = 1;
        radar_can_buf[7] = 7;
        can_send_msg_normal(radar_can_buf, 8, 0XA81300);
    }
    // else if(DM_B_info.Link.connect_status == COMP_NORMAL && DM_B_info.get_radar_ver_flag == false)
    // {
    //     radar_can_buf[0] = 1;
    //     radar_can_buf[7] = 7;
    //     can_send_msg_normal(radar_can_buf, 8, 0XB81300);
    // }



    //恩曌一批雷达盲区20cm有问题，盲区需要改成40CM
    if(mimo_ter_info.Link.connect_status == COMP_NORMAL 
        && memcmp(mimo_ter_info.version,"EB00190110",10) == 0 && mimo_area_40flag > 0)
    {
        radar_can_buf[0] = 0x4D;
        radar_can_buf[1] = 0x69;
        radar_can_buf[2] = 0x6E;
        radar_can_buf[3] = 0x44;
        radar_can_buf[4] = 0x69;
        radar_can_buf[5] = 0x73;
        radar_can_buf[6] = 0x28;
        radar_can_buf[7] = 0x34;
        can_send_msg_normalstd(radar_can_buf, 8, 0x500);

        radar_can_buf[0] = 0x30;
        radar_can_buf[1] = 0x29;
        radar_can_buf[2] = 0x0D;
        radar_can_buf[3] = 0x0A;;
        can_send_msg_normalstd(radar_can_buf, 4, 0x500);

        mimo_area_40flag--;
    }   

}


/**
  * @file    get_flowmeter_version_and_sn
  * @brief   获取流量计版本、SN号、K值
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void get_flowmeter_version_and_sn(void)
{
    static uint8_t send_interval = 0;
    if((Dev.Flow_Link1.connect_status == COMP_NORMAL || Dev.Flow_Link2.connect_status == COMP_NORMAL) && 
    (Dev.Flow.facid == FAC_MIMO_SIG || Dev.Flow.facid == FAC_MIMO_DOU))
    {
        uint8_t can_buf[8] = {0};

        //读取K值 和序列号
        if(flow_mimo1.get_k_count  < 5 && Dev.Flow_Link1.connect_status == COMP_NORMAL )
        {   
            //K值
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB1;
            can_buf[4] = 0xD1;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;

            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 0)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 1;
            }
            can_buf[7] = 0;
            
            //序列号
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xEB;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;

            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 1)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 2;
            }
            can_buf[7] = 0;

            //软件版本
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xEC;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;

            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 2)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 0;
            }
            can_buf[7] = 0;            

            if(send_interval == 0)
                flow_mimo1.get_k_count++;
        }
        else if(flow_mimo2.get_k_count  < 5 && Dev.Flow_Link2.connect_status == COMP_NORMAL)
        {   
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x01;
            can_buf[3] = 0xB1;
            can_buf[4] = 0xD1;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;
            
            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 0)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 1;
            }
            can_buf[7] = 0;

            //序列号
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xEB;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;

            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 1)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 2;
            }
            can_buf[7] = 0;

            //软件版本
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xEC;
            can_buf[5] = 0x00;
            can_buf[6] = 0x00;

            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            if(send_interval == 2)
            {
                can_send_msg_normalstd(can_buf, 8, 0xFA);
                send_interval = 0;
            }
            can_buf[7] = 0;  

            flow_mimo2.get_k_count++;
        }
        
        //设置流量计K
        if(flow_mimo1.send_k_count > 0 && Dev.Flow_Link1.connect_status == COMP_NORMAL )
        {
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB1;
            can_buf[4] = 0xC1;
            can_buf[5] = flow_mimo1.flow_calk >> 8;
            can_buf[6] = flow_mimo1.flow_calk;
            can_buf[7] = 0;
            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }

            flow_mimo1.send_k_count--;
            can_send_msg_normalstd(can_buf, 8, 0xFA);
            HAL_Delay(5);
        }
        else if(flow_mimo2.send_k_count > 0 && Dev.Flow_Link2.connect_status == COMP_NORMAL)
        {            
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x01;
            can_buf[3] = 0xB1;
            can_buf[4] = 0xC1;
            can_buf[5] = flow_mimo2.flow_calk >> 8;
            can_buf[6] = flow_mimo2.flow_calk;
            can_buf[7] = 0;
            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }

            flow_mimo2.send_k_count--;
            can_send_msg_normalstd(can_buf, 8, 0xFA);
        }

        if(flow_inf.ch1.clear_background == true)
        {
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x00;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xE2;
            can_buf[5] = 0;
            can_buf[6] = 0;
            can_buf[7] = 0;
            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            can_send_msg_normalstd(can_buf, 8, 0xFA);
        }
        else if(flow_inf.ch2.clear_background == true)
        {
            can_buf[0] = 0xfa;
            can_buf[1] = 0x10;
            can_buf[2] = 0x01;
            can_buf[3] = 0xB2;
            can_buf[4] = 0xE2;
            can_buf[5] = 0;
            can_buf[6] = 0;
            can_buf[7] = 0;
            for(uint8_t i = 1; i < 7; i++)
            {
                can_buf[7] += can_buf[i];
            }
            can_send_msg_normalstd(can_buf, 8, 0xFA);
        }
        
    }
}


/**
  * @file    get_seed_version_and_sn
  * @brief   获取播撒器版本和SN号
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void get_seed_version_and_sn(void)
{
    static uint8_t get_counts = 5;
    uint8_t can_buf[8] = {0}; 
    if(Dev.Weight_Link.connect_status == COMP_NORMAL && Dev.Weight.facid == FAC_VK && get_counts > 0)
    {
        can_buf[0] = 0xF6;
        can_buf[5] = 0xFE;
        can_buf[6] = 0xFE;

        can_send_msg_normal(&can_buf[0], 8, 0x81321);
        get_counts--;
    }

    if( dev_weight.regist.sn == false &&Dev.Weight_Link.connect_status == COMP_NORMAL && Dev.Weight.facid == FAC_LIFTWEIGHT)
    {
        put_date_to_can(can_buf,0xF3,0,0,0,0,0xF1,0xF3,0 );
        can_send_msg_normal(&can_buf[0], 8, 0X88BB);
    }
    else if(dev_weight.regist.soft == false && Dev.Weight_Link.connect_status == COMP_NORMAL && Dev.Weight.facid == FAC_LIFTWEIGHT)
    {
        put_date_to_can(can_buf,0xF4,0,0,0,0,0xF1,0xF3,0 );
        can_send_msg_normal(&can_buf[0], 8, 0X88BB);
    }
    else if(weight_runing_time == false && Dev.Weight_Link.connect_status == COMP_NORMAL && Dev.Weight.facid == FAC_LIFTWEIGHT)
    {
        put_date_to_can(can_buf,0xF5,0,0,0,0,0xF1,0xF3,0 );
        can_send_msg_normal(&can_buf[0], 8, 0X88BB);
    }
}



/**
  * @file    get_device_version_and_sn
  * @brief   版本号
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void get_device_version_and_sn(void)
{
    static uint32_t circu_time = 0;

    if(Check_Timer_Ready(&circu_time,_2_HZ_))
    {
        //获取雷达版本和SN号
        get_radar_version_and_sn();

        //获取流量计版本和SN号
        get_flowmeter_version_and_sn();

        //获取播撒器称重版本和SN号
        get_seed_version_and_sn();

    }

}


/**
  * @file    regist_dev_info
  * @brief   注册设备信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void regist_dev_info(dev_version_content *dev,uint8_t num,bool verify_flag,char *sn,uint8_t size1,
                        char *soft_serial,uint8_t size2,char *hard_serial,uint8_t size3,char *manu,uint8_t size4)
{   
    dev->num = num;
    dev->verify = verify_flag;

    if(dev->regist.dev == false)
    {
        dev->regist.dev = true;
        dev->send_times++;
    }
    if(sn != NULL && size1 <= 32 && dev->regist.sn == false)
    {
        memcpy(dev->sn,sn,size1);
        dev->regist.sn = true;
        dev->send_times++;
    }
    if(soft_serial != NULL && size2 <= 16 && dev->regist.soft == false)
    {
        memcpy(dev->soft_serial,soft_serial,size2);
        dev->regist.soft = true;
        dev->send_times++;
    }
    if(hard_serial != NULL && size3 <= 16 && dev->regist.hard == false)
    {
        memcpy(dev->hard_serial,hard_serial,size3);
        dev->regist.hard = true;
        dev->send_times++;
    }
    if(manu != NULL && size4 <= 16 && dev->regist.manu == false)
    {
        memcpy(dev->manu,manu,size4);
        dev->regist.manu = true;
        dev->send_times++;
    }
};

