#include "stdint.h"
#include "stddef.h"
#include "dbug.h"
#include "djyos.h"
#include <math.h>
#include "Iboot_info.h"
#include <uartctrl.h>
#include "cJSON.h"
#include "stdlib.h"
#include "mongoose.h"
#include "OtherMessage.h"
#include "BMSDaRen.h"
char errlog_BMSDaRen[100]={0};
/*char* Wrong (char* json_data,int code)//指令不存在或者参数不对
{
    if(json_data == NULL&&code == 0)
    {
        printf(" -------Wrong code NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong code NULL");
        return json_data;
    }

    cJSON* json = cJSON_Parse(json_data);
    if (json==NULL)
    {
        printf(" -------Wrong json NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong json NULL");
        return json_data;
    }
    cJSON* msgId = cJSON_GetObjectItem(json, "msgId");
    if (msgId==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Wrong msgId NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong msgId NULL");
        return json_data;
    }
    cJSON* payload_1 = cJSON_GetObjectItem(json, "payload");
    if (payload_1==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Wrong payload_1 NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong payload_1 NULL");
        return json_data;
    }
    cJSON* deviceId = cJSON_GetObjectItem(payload_1, "deviceId");
    if (deviceId==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Wrong deviceId NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong deviceId NULL");
        return json_data;
    }
    cJSON_Delete(json);
    cJSON *QProduct_information = cJSON_CreateObject();
    if (QProduct_information==NULL)
    {
        json_data = cJSON_Print(QProduct_information);
        cJSON_Delete(QProduct_information);


        printf(" -------Wrong QProduct_information NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong QProduct_information NULL");
        return json_data;
    }
    char* message;
    if(msgId->valueint!=NULL)
    {
       cJSON_AddNumberToObject(QProduct_information,"msgId",msgId->valueint);
    }
    if(code==5)
    {
        message="device not support cmd";
    }else if(code==2)
    {
        message="param error";
    }
    cJSON *payload  = cJSON_CreateObject();
    if (payload==NULL)
    {
        cJSON_AddItemToObject(QProduct_information,"payload",payload);
        json_data = cJSON_Print(QProduct_information);
        cJSON_Delete(QProduct_information);


        printf(" -------Wrong payload NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong payload NULL");
        return json_data;
    }
    cJSON_AddNumberToObject(payload,"code",code);
    cJSON_AddStringToObject(payload,"message",message);
    cJSON_AddStringToObject(payload,"deviceId",deviceId->valuestring);
    cJSON *data  = cJSON_CreateObject();
    if (data==NULL)
    {

        cJSON_Delete(QProduct_information);


        printf(" -------Wrong data NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong data NULL");
        return json_data;
    }
    cJSON_AddItemToObject(payload,"data",data);
    cJSON_AddItemToObject(QProduct_information,"payload",payload);
    json_data = cJSON_Print(QProduct_information);
     if (json_data==NULL)
    {
        cJSON_Delete(QProduct_information);

        printf(" -------Wrong json_data NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Wrong json_data NULL");

        return json_data;
    }
    cJSON_Delete(QProduct_information);

    return json_data;
}*/
char *Qproduct_information  (char *json_data)//查询产品信息
{
    cJSON* json = cJSON_Parse(json_data);
    cJSON* msgId = cJSON_GetObjectItem(json, "msgId");
    cJSON* payload_1 = cJSON_GetObjectItem(json, "payload");
    cJSON* deviceId = cJSON_GetObjectItem(payload_1, "deviceId");
    char *jsondeviceId=deviceId->valuestring;
    int jsonmsgId=msgId->valueint;


    char str[10] ;
    strncpy(str, deviceId->valuestring,sizeof(str) - 1);
    str[sizeof(str) - 1] = '\0';
    char result[10][20];
    int delimiter = '_';
    char *token = strtok(str, &delimiter);
    int i = 0;
    while (token != NULL) {
        strncpy(result[i++], token,sizeof(result[i++])-1);
        result[i][sizeof(result[i]) - 1] = '\0';
        token = strtok(NULL, &delimiter);
    }
    int count=0,address=0;
    for(int i=0;i<20;i++)
    {
        if(result[2][i]!=NULL)
        {
            count++;
        }else{
            break;
        }
    }
    for(int i=count-1;i>=0;i--)
    {
        int power=0,digit=0;
        digit=result[2][count-i-1]-'0';
        power=(int)pow(10,i)*digit;
        address=address+power;
    }

    char* message=NULL;
    int code=0;
    char data_time[50],data_factory[300],data_protocolVersion[50],data_address[50];
    char * LENID="00";
    char * CID2Time="4D";
    char * CID2SMI="51";
    char * CID2SPI="4F";
    char * CID2address="50";
    char * SOI="7E";
    char * VER="22";
    char * CID1="4A";
    char * EOI="0D";
    char *DaRen[]={SOI,VER,CID1,EOI};//达人高科
    char *GetTimestate=NULL,*GetFactorystate=NULL,*GetProtocolVersionstate=NULL,*GetAddress=NULL;
    bool_t timestatic,factorystatic,protocolVersionstatic,addressstatic;

    memset(data_time, 0, sizeof(data_time));
    memset(data_factory, 0, sizeof(data_factory));
    memset(data_protocolVersion, 0, sizeof(data_protocolVersion));
    memset(data_address, 0, sizeof(data_address));



    for (u32 i = 0; i < 3; i++)
    {
        DJY_EventDelay(10*1000);
        GetTimestate=BMSMasterRead(address,CID2Time,LENID,DaRen,NULL);//获取时间
        timestatic=BMSMasterGET(data_time);
        if (i>0&&strcmp(GetTimestate, "Success") == 0)
        {
            break;
        }
    }
    char *normal="Success";
    if (strcmp(GetTimestate, normal) == 0)
    {
        for (u32 i = 0; i < 2; i++)
        {
            DJY_EventDelay(10*1000);
            GetFactorystate=BMSMasterRead(address,CID2SMI,LENID,DaRen,NULL);//获取厂家信息
            factorystatic=BMSMasterGET(data_factory);
            if (i>0&&strcmp(GetFactorystate, "Success") == 0)
            {
                break;
            }

        }



        for (u32 i = 0; i < 2; i++)
        {
            DJY_EventDelay(10*1000);
            GetProtocolVersionstate=BMSMasterRead(address,CID2SPI,LENID,DaRen,NULL);//获取协议版本号
            protocolVersionstatic=BMSMasterGET(data_protocolVersion);
            if (i>0&&strcmp(GetProtocolVersionstate, "Success") == 0)
            {
                break;
            }
        }



        for (u32 i = 0; i < 2; i++)
        {
            DJY_EventDelay(10*1000);
            GetAddress=BMSMasterRead(address,CID2address,LENID,DaRen,NULL);//获取设备地址
            addressstatic=BMSMasterGET(data_protocolVersion);
            if (i>0&&strcmp(GetAddress, "Success") == 0)
            {
                break;
            }

        }        
    }
    

    if((strcmp(GetTimestate, "Success") == 0)&&(strcmp(GetFactorystate, "Success") == 0)&&(strcmp(GetAddress, "Success") == 0)&&(strcmp(GetProtocolVersionstate, "Success") == 0))
    {
        message="NoErr";
        code=0;
    }else
    {
        message="Timeout";
        code=8;
    }


    char data_year[10],data_month[10],data_day[10],data_hour[10],data_min[10],data_second[10];
    char *year;
    char *month;
    char *day;
    char *hour;
    char *min;
    char *second;
    for (int i = 0; i < 4; i++)//分别取出年月日时分秒对应的数据
    {
        data_year[i]=data_time[13+i];
        switch (i)
        {
            case 0:
            case 1:
                data_month[i]=data_time[17+i];
                data_day[i]=data_time[19+i];
                data_hour[i]=data_time[21+i];
                data_min[i]=data_time[23+i];
                data_second[i]=data_time[25+i];
                break;
            case 2://'\0'是终止符
                data_month[i]='\0';
                data_day[i]='\0';
                data_hour[i]='\0';
                data_min[i]='\0';
                data_second[i]='\0';
                break;
        }
    }
    data_year[4]='\0';
    year=data_year;
    month=data_month;
    day=data_day;
    hour=data_hour;
    min=data_min;
    second=data_second;

    int Time_year,Time_month,Time_day,Time_hour,Time_min,Time_second;
    Time_year =(int) strtol(year, NULL, 16);//把取出来的数据从16进制转成10进制
    Time_month =(int) strtol(month, NULL, 16);
    Time_day =(int) strtol(day, NULL, 16);
    Time_hour =(int) strtol(hour, NULL, 16);
    Time_min =(int) strtol(min, NULL, 16);
    Time_second =(int) strtol(second, NULL, 16);

    static char currentTime[50];//把十进制数据按照2023-08-01 08:03:01这个格式拼接起来

    char temp_lastyear[5];
    sprintf(temp_lastyear, "%d", Time_year);
    strcat(currentTime, temp_lastyear);
    strcat(currentTime, "-");

    char temp_lastmouth[5];
    if (Time_month<10)
    {
        sprintf(temp_lastmouth, "%02d", Time_month);
    }else{
        sprintf(temp_lastmouth, "%d", Time_month);
    }

    strcat(currentTime, temp_lastmouth);
    strcat(currentTime, "-");

    char temp_lastday[5];

    if (Time_day<10)
    {
        sprintf(temp_lastday, "%02d", Time_day);
    }else{
        sprintf(temp_lastday, "%d", Time_day);
    }

    strcat(currentTime, temp_lastday);
    strcat(currentTime, " ");

    char temp_lasthour[5];

    if (Time_hour<10)
    {
        sprintf(temp_lasthour, "%02d", Time_hour);
    }else
    {
        sprintf(temp_lasthour, "%d",Time_hour);
    }

    strcat(currentTime, temp_lasthour);
    strcat(currentTime, ":");

    char temp_lastmin[5];
    if (Time_min<10)
    {
        sprintf(temp_lastmin, "%02d", Time_min);
    }else{
        sprintf(temp_lastmin, "%d",Time_min);
    }
    strcat(currentTime, temp_lastmin);
    strcat(currentTime, ":");

    char temp_lastsecond[5];
    if (Time_second<10)
    {
        sprintf(temp_lastsecond, "%02d", Time_second);
    }else{
        sprintf(temp_lastsecond, "%d", Time_second);
    }

    strcat(currentTime, temp_lastsecond);



    char arr[50],protocolVersion[10];
    arr[0]=data_protocolVersion[1];
    arr[1]=data_protocolVersion[2];
    s32 version,part;
    char arr_version[5],arr_part[5];
    version=data_protocolVersion[1]-'0';
    part=data_protocolVersion[2]-'0';
    if (version>=10)
    {
        version=data_protocolVersion[1]-'0'-7;
    }
    if (part>=10)
    {
        part=data_protocolVersion[2]-'0'-7;
    }
    sprintf(arr_version,"%d",version);
    sprintf(arr_part,"%d",part);
    strncpy(protocolVersion, arr_version,sizeof(protocolVersion)-1);
    strcat(protocolVersion, ".");
    strcat(protocolVersion, arr_part);






    char hardwareType[20],productModel[20],projectCode[20],softwareVersion[10],bootVersion[10];

    char container[10];
    int numA,tens,ones;
    int numB,j=0;
    memset(container,0,10);
    memset(hardwareType,0,20);
    memset(productModel,0,20);
    memset(projectCode,0,20);
    for (s32 i = 0; i < 20;)
    {
        container[0]=data_factory[i+13];
        container[1]=data_factory[i+14];
        if (container[0]=='2'&&container[1]=='0')
        {

        }else{
            numB = strtol(container, NULL, 16); // 将16进制整数转换为10进制
            hardwareType[j]= (char)numB; // 将10进制整数转换为ASCII码
        }

        memset(container,0,10);

        container[0]=data_factory[i+33];
        container[1]=data_factory[i+34];

        if (container[0]=='2'&&container[1]=='0')
        {

        }else{
            numB = strtol(container, NULL, 16); // 将16进制整数转换为10进制
            productModel[j]= (char)numB; // 将10进制整数转换为ASCII码
        }

        memset(container,0,10);

        container[0]=data_factory[i+53];
        container[1]=data_factory[i+54];

        if (container[0]=='2'&&container[1]=='0')
        {

        }else{
            numB = strtol(container, NULL, 16); // 将16进制整数转换为10进制
            projectCode[j]= (char)numB; // 将10进制整数转换为ASCII码
        }

        memset(container,0,10);
         i=i+2;
         j++;
    }
    container[0]=data_factory[73];
    container[1]=data_factory[74];
    container[2]=data_factory[75];
    container[3]=data_factory[76];
    container[4]=data_factory[77];
    container[5]=data_factory[78];
    tens=container[0]-'0';
    ones=container[1]-'0';
    if (tens>=10)
    {
        tens=(container[0]-'0')-7;
    }
    if (ones>=10)
    {
        ones=(container[1]-'0')-7;
    }
    numA=tens*10+ones;

    memset(str,0,10);
    sprintf(str,"%d",numA);
    strncpy(softwareVersion, str,sizeof(softwareVersion)-1);
    strcat(softwareVersion, ".");
    memset(str,0,10);

    tens=container[2]-'0';
    ones=container[3]-'0';
    if (tens>=10)
    {
        tens=(container[2]-'0')-7;
    }
    if (ones>=10)
    {
        ones=(container[3]-'0')-7;
    }
    numA=tens*10+ones;

    sprintf(str,"%d",numA);
    strcat(softwareVersion, str);
    strcat(softwareVersion, ".");
    memset(str,0,10);

    tens=container[4]-'0';
    ones=container[5]-'0';
    if (tens>=10)
    {
        tens=(container[4]-'0')-7;
    }
    if (ones>=10)
    {
        ones=(container[5]-'0')-7;
    }
    numA=tens*10+ones;

    sprintf(str,"%d",numA);
    strcat(softwareVersion, str);
    memset(str,0,10);
    memset(container,0,10);

    container[0]=data_factory[79];
    container[1]=data_factory[80];
    container[2]=data_factory[81];
    container[3]=data_factory[82];

    tens=container[0]-'0';
    ones=container[1]-'0';
    if (tens>=10)
    {
        tens=(container[0]-'0')-7;
    }
    if (ones>=10)
    {
        ones=(container[1]-'0')-7;
    }
    numA=tens*10+ones;

    sprintf(str,"%d",numA);
    strncpy(bootVersion, str,sizeof(bootVersion)-1);
    strcat(bootVersion, ".");
    memset(str,0,10);

    tens=container[2]-'0';
    ones=container[3]-'0';
    if (tens>=10)
    {
        tens=(container[2]-'0')-7;
    }
    if (ones>=10)
    {
        ones=(container[3]-'0')-7;
    }
    numA=tens*10+ones;

    sprintf(str,"%d",numA);
    strcat(bootVersion, str);




    char *address_arr,container_address[5];
    int address_data;
    container_address[0]=data_protocolVersion[3];
    container_address[1]=data_protocolVersion[4];
    container_address[2]='\0';
    address_arr=container_address;
    address_data =(int) strtol(address_arr, NULL, 16);

    
    cJSON *Qproduct_information = cJSON_CreateObject();

    
    cJSON_AddStringToObject(Qproduct_information,"cmd","query_BMS_ProductInfo");

    if(msgId->valueint!=NULL)
    {
        cJSON_AddNumberToObject(Qproduct_information,"msgId",jsonmsgId);
    }
    cJSON *payload  = cJSON_CreateObject();
   
    cJSON_AddNumberToObject(payload,"code",code);
    cJSON_AddStringToObject(payload,"message",message);
    cJSON_AddStringToObject(payload,"deviceId",jsondeviceId);//设备id

    if (code==0)
    {
        cJSON *data  = cJSON_CreateObject();
        cJSON_AddNumberToObject(data,"productType",1);//区分是逆变器还是BMS
        //cJSON_AddStringToObject(data,"packManufacturerInfo","");//电池生产商信息
        //cJSON_AddStringToObject(data,"packModel","");//电池型号
        //cJSON_AddStringToObject(data,"manufacturer","");//生产厂家
        //cJSON_AddStringToObject(data,"model","");//型号
        //cJSON_AddStringToObject(data,"commSoftVersion","");//通信软件版本
        //cJSON_AddStringToObject(data,"productSN","");//电池编码（ID），当作序列号
        cJSON_AddStringToObject(data,"hardwareType",hardwareType); //硬件类型
        cJSON_AddStringToObject(data,"productModel",productModel);//产品型号
        cJSON_AddStringToObject(data,"projectCode",projectCode); //项目代号
        cJSON_AddStringToObject(data,"softwareVersion",softwareVersion);//软件版本
        cJSON_AddStringToObject(data,"bootVersion",bootVersion); // BOOT 版本
        cJSON_AddStringToObject(data,"protocolVersion",protocolVersion);//对应达人协议版本号（达人协议文档9.3指令）DTU根据规则解析出的
        cJSON_AddStringToObject(data,"currentTime",currentTime);//BMS上的时间(9.1指令)
        cJSON_AddNumberToObject(data,"address",address_data);//DTU根据协议（9.4指令）读取到的信息
        cJSON_AddItemToObject(payload,"data",data);
    }


    cJSON_AddItemToObject(Qproduct_information,"payload",payload);
    json_data = cJSON_Print(Qproduct_information );  
    cJSON_Delete(Qproduct_information);
    cJSON_Delete(json);
    memset(currentTime,0,sizeof(currentTime));
    return json_data;
}

char *Obtain_battery_pack_information(char *json_data)//获取电池组信息
{
    cJSON* json = cJSON_Parse(json_data);
    cJSON* msgId = cJSON_GetObjectItem(json, "msgId");
    cJSON* payload_1 = cJSON_GetObjectItem(json, "payload");
    cJSON* deviceId = cJSON_GetObjectItem(payload_1, "deviceId");

    char str[10] ;
    strncpy(str, deviceId->valuestring,sizeof(str) - 1);
    str[sizeof(str) - 1] = '\0';
    char result[10][20];
    int delimiter = '_';
    char *token = strtok(str, &delimiter);
    int i = 0;
    while (token != NULL) {
        strncpy(result[i++], token,sizeof(result[i++])-1);
        result[i][sizeof(result[i]) - 1] = '\0';
        token = strtok(NULL, &delimiter);
    }


    int count=0,address=0;
    for(int i=0;i<20;i++)
    {
        if(result[2][i]!=NULL)
        {
            count++;
        }else{
            break;
        }
    }
    for(int i=count-1;i>=0;i--)
    {
        int power=0,digit=0;
        digit=result[2][count-i-1]-'0';
        power=(int)pow(10,i)*digit;
        address=address+power;
    }

    char* message;
    int code;

    char *DaRen[]={"7E","22","4A","0D"};//达人高科
    char * CID2="42";
    char * LENID="02";
    char * COMMANDINFO[]={"FF"};
    char buff[600];
    memset(buff, 0, sizeof(buff));
    bool_t isget;

    for (int i = 0; i < 2; i++)
    {
        DJY_EventDelay(100*1000);
        message=BMSMasterRead(address,CID2,LENID,DaRen,COMMANDINFO);
        isget=BMSMasterGET(buff);
        if (strcmp(message, "Success") == 0)
        {
            break;
        }

    }


    if (strcmp(message, "Success") == 0&&isget==true)
    {
        message="NoErr";
        code=0;
    }else
    {
        message="Timeout";
        code=7;
    }



    char arr[50];
    int num;
    num=strlen(buff);

    char *temp =(char*)malloc(num*2);
    *temp='\0';
    int j=0;
    char *symbol="0x";

    for (int i = 13; i < num;)
    {
        memset(arr,0,50);
        arr[0]=buff[i];
        arr[1]=buff[i+1];
        if (i==13)
        {
            strncpy(temp,symbol,sizeof(temp)-1);

        }else{
            strcat(temp,symbol);
        }
        strcat(temp,arr);
        i=i+2;
    }
    cJSON *Obtain_battery_pack_information = cJSON_CreateObject();

    cJSON_AddStringToObject(Obtain_battery_pack_information,"cmd","query_BMS_PackInfo");

    if(msgId->valueint!=NULL)
    {
        cJSON_AddNumberToObject(Obtain_battery_pack_information,"msgId",msgId->valueint);
    }
    cJSON *payload  = cJSON_CreateObject();

    cJSON_AddNumberToObject(payload,"code",code);
    cJSON_AddStringToObject(payload,"message",message);
    cJSON_AddStringToObject(payload,"deviceId",deviceId->valuestring);

    cJSON_AddNumberToObject(payload,"productType",1);
    if (code==0)
    {
        cJSON_AddStringToObject(payload,"data",temp);
    }

    cJSON_AddItemToObject(Obtain_battery_pack_information,"payload",payload);
    json_data = cJSON_Print(Obtain_battery_pack_information );

    cJSON_Delete(Obtain_battery_pack_information);
    cJSON_Delete(json);
    free(temp);
    return json_data;
}
char *Set_time  (char *json_data)//设置时间
{
    char err_arr[100];
    char *errbackmessage;
    char *errmessage;
    memset(err_arr, 0, sizeof(err_arr));
    if (json_data==NULL)
    {
        printf(" -------Set_time json_data NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time json_data NULL");
        return json_data;
    }
    
    cJSON* json = cJSON_Parse(json_data);
    if (json==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Set_time json NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time json NULL");
        return json_data;
    }
    
    cJSON* msgId = cJSON_GetObjectItem(json, "msgId");
    if (msgId==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Set_time msgId NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time msgId NULL");
        return json_data;
    }
    
    cJSON* json_payload = cJSON_GetObjectItem(json, "payload");
    if (json_payload==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Set_time json_payload NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time json_payload NULL");
        return json_data;
    }
    
    cJSON* currentTime = cJSON_GetObjectItem(json_payload, "currentTime");
    if (currentTime==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Set_time currentTime NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time currentTime NULL");
        return json_data;
    }
    
    cJSON* deviceId = cJSON_GetObjectItem(json_payload, "deviceId");
    if (deviceId==NULL)
    {
        cJSON_Delete(json);
        printf(" -------Set_time deviceId NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time deviceId NULL");
        return json_data;
    }
    
    char time_str[40];
    strncpy(time_str, currentTime->valuestring,sizeof(time_str)-1);
    int century,year, month, day, hour, minute, second;
    int convert[6];
    memset(convert,0,sizeof(convert));
    convert[0]=time_str[0]-'0';
    convert[1]=time_str[1]-'0';
    convert[2]=time_str[2]-'0';
    convert[3]=time_str[3]-'0';
    year=1000*convert[0]+100*convert[1]+10*convert[2]+convert[3];
    //year=10*convert[2]+convert[3];
    memset(convert,0,sizeof(convert));

    convert[0]=time_str[5]-'0';
    convert[1]=time_str[6]-'0';
    month=10*convert[0]+convert[1];
    memset(convert,0,sizeof(convert));

    convert[0]=time_str[8]-'0';
    convert[1]=time_str[9]-'0';
    day=10*convert[0]+convert[1];
    memset(convert,0,sizeof(convert));

    convert[0]=time_str[11]-'0';
    convert[1]=time_str[12]-'0';
    hour=10*convert[0]+convert[1];
    memset(convert,0,sizeof(convert));

    convert[0]=time_str[14]-'0';
    convert[1]=time_str[15]-'0';
    minute=10*convert[0]+convert[1];
    memset(convert,0,sizeof(convert));

    convert[0]=time_str[17]-'0';
    convert[1]=time_str[18]-'0';
    second=10*convert[0]+convert[1];
    memset(convert,0,sizeof(convert));

    char hex_year[6];
    char hex_month[6];
    char hex_day[6];
    char hex_hour[6];
    char hex_minute[6];
    char hex_second[6];

    sprintf(hex_year, "%X", year);

    if (month<16)
    {
        sprintf(hex_month, "0%X", month);
    }else{
        sprintf(hex_month, "%X", month);
    }
    if (day<16)
    {
        sprintf(hex_day, "0%X", day);
    }else{
        sprintf(hex_day, "%X", day);
    }
    if (hour<16)
    {
        sprintf(hex_hour, "0%X", hour);
    }else{
        sprintf(hex_hour, "%X", hour);
    }
    if (minute<16)
    {
        sprintf(hex_minute, "0%X", minute);
    }else
    {
        sprintf(hex_minute, "%X", minute);
    }
    if (second<16)
    {
        sprintf(hex_second, "0%X", second);
    }else
    {
        sprintf(hex_second, "%X", second);
    }

    char * COMMANDINFO[6];
    char temp[3];
    memset(COMMANDINFO,0,6);
     bool_t state=false;
    if (hex_year[3]!=NULL)
    {
        temp[0]=hex_year[0];
        temp[1]=hex_year[1];
        temp[2]='\0';
        state=true;
    }else{
        temp[0]='0';
        temp[1]=hex_year[0];
        temp[2]='\0';
    }
    COMMANDINFO[0] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[0],0,1);
    strncpy(COMMANDINFO[0], temp,sizeof(COMMANDINFO[0])-1);

    if (state==true)
    {
        temp[0]=hex_year[2];
        temp[1]=hex_year[3];
        temp[2]='\0';
    }else{
        temp[0]=hex_year[1];
        temp[1]=hex_year[2];
        temp[2]='\0';
    }
    COMMANDINFO[1] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[1],0,1);
    strcat(COMMANDINFO[1], temp);

    memset(temp,0,2);
    temp[0]=hex_month[0];
    temp[1]=hex_month[1];
    temp[2]='\0';
    COMMANDINFO[2] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[2],0,1);
    strcat(COMMANDINFO[2],temp);

    memset(temp,0,2);
    temp[0]=hex_day[0];
    temp[1]=hex_day[1];
    temp[2]='\0';
    COMMANDINFO[3] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[3],0,1);
    strcat(COMMANDINFO[3],temp);

    memset(temp,0,2);
    temp[0]=hex_hour[0];
    temp[1]=hex_hour[1];
    temp[2]='\0';
    COMMANDINFO[4] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[4],0,1);
    strcat(COMMANDINFO[4],temp);


    memset(temp,0,2);
    temp[0]=hex_minute[0];
    temp[1]=hex_minute[1];
    temp[2]='\0';
    COMMANDINFO[5] = (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[5],0,1);
    strcat(COMMANDINFO[5],temp);


    memset(temp,0,2);
    temp[0]=hex_second[0];
    temp[1]=hex_second[1];
    temp[2]='\0';
    COMMANDINFO[6] =  (char*)malloc(sizeof(char) * 3);
    memset(COMMANDINFO[6],0,1);
    strcat(COMMANDINFO[6],temp);

    char str[10] ;
    strncpy(str, deviceId->valuestring,sizeof(str) - 1);
    str[sizeof(str) - 1] = '\0';
    char result[10][20];
    int delimiter = '_';
    char *token = strtok(str, &delimiter);
    int i = 0;
    while (token != NULL) {
        strncpy(result[i++], token,sizeof(result[i++])-1);
        result[i][sizeof(result[i]) - 1] = '\0';
        token = strtok(NULL, &delimiter);
    }
    int count=0,address=0;
    for(int i=0;i<20;i++)
    {
        if(result[2][i]!=NULL)
        {
            count++;
        }else{
            break;
        }
    }
    for(int i=count-1;i>=0;i--)
    {
        int power=0,digit=0;
        digit=result[2][count-i-1]-'0';
        power=(int)pow(10,i)*digit;
        address=address+power;
    }

    char * LENID="0E";
    char * CID2="4E";
    char * SOI="7E";
    char * VER="22";
    char * CID1="4A";
    char * EOI="0D";
    char *DaRen[]={SOI,VER,CID1,EOI};//达人高科
    char *SetTime=NULL;

    DJY_EventDelay(10*1000);
    for (u32 i = 0; i < 2; i++)
    {
        DJY_EventDelay(10*1000);
        SetTime=BMSMasterRead(address,CID2,LENID,DaRen,COMMANDINFO);
        if (strcmp(SetTime, "Success") == 0)
        {
            break;
        }

    }
    char *message=NULL;
    int code=0;
    if(strcmp(SetTime, "Success") == 0)
    {
        message="NoErr";
        code=0;
    }else
    {
        message="Timeout";
        code=8;
    }
     free(COMMANDINFO[0]);
    free(COMMANDINFO[1]);
    free(COMMANDINFO[2]);
    free(COMMANDINFO[3]);
    free(COMMANDINFO[4]);
    free(COMMANDINFO[5]);
    free(COMMANDINFO[6]);
    
    cJSON *Set_time = cJSON_CreateObject();
    if (Set_time==NULL)
    {
        cJSON_Delete(Set_time);
        cJSON_Delete(json);
        printf(" -------Set_time Set_time NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time Set_time NULL");
        
        return json_data;
    }
    cJSON_AddStringToObject(Set_time,"cmd","ctrl_setTime");

    if(msgId->valueint!=NULL)
    {
        cJSON_AddNumberToObject(Set_time,"msgId",msgId->valueint);
    }
    cJSON *payload  = cJSON_CreateObject();
    if (payload==NULL)
    {
        cJSON_Delete(Set_time);
        cJSON_Delete(json);
        printf(" -------Set_time payload NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time payload NULL");
        return json_data;
    }
    cJSON_AddNumberToObject(payload,"code",code);
    cJSON_AddStringToObject(payload,"message",message);
    cJSON_AddStringToObject(payload,"deviceId",deviceId->valuestring);

    cJSON_AddItemToObject(Set_time,"payload",payload);
    json_data = cJSON_Print(Set_time );
    if (json_data==NULL)
    {
        cJSON_Delete(Set_time);
        cJSON_Delete(json);
        printf(" -------Set_time cJSON_Print json_data NULL -------\r\n");
        sprintf(errlog_BMSDaRen," Set_time cJSON_Print json_data NULL");
        
        
        return json_data;
    }
    cJSON_Delete(Set_time);
    cJSON_Delete(json);
    return json_data;
}
const  char *QYDT_1363_group[]={"reg","query_BMS_ProductInfo","query_BMS_PackInfo","ctrl_setTime"};

char *QYDT_1363(char *data)
{
    char* json_data=NULL;
    if (data==NULL)
    {
        printf("------- QYDT_1363 data NULL -------\r\n");
        sprintf(errlog_BMSDaRen," QYDT_1363 data NULL");
        return json_data;
    }

    cJSON* json = cJSON_Parse(data);
    if (json==NULL)
    {
        printf("------- QYDT_1363 json NULL -------\r\n");
        sprintf(errlog_BMSDaRen," QYDT_1363 json NULL");
        return json_data;
    }
    cJSON* cmd = cJSON_GetObjectItem(json, "cmd");
    if (cmd==NULL)
    {
        cJSON_Delete(json);
        printf("-------  QYDT_1363 cmd NULL -------\r\n");
        sprintf(errlog_BMSDaRen," QYDT_1363 cmd NULL");
        return json_data;
    }

    char *command=cmd->valuestring;
    if (cmd->valuestring==NULL)
    {
        cJSON_Delete(json);
        printf("-------  QYDT_1363 cmd->valuestring NULL -------\r\n");
        sprintf(errlog_BMSDaRen," QYDT_1363 cmd->valuestring NULL");
        return json_data;
    }
    
    u16 lenght=sizeof(QYDT_1363_group);
    
    if(strcmp(command, QYDT_1363_group[0]) == 0)
    {

    }else if(strcmp(command, QYDT_1363_group[1]) == 0)
    {
        json_data=Qproduct_information(data);
    }else if(strcmp(command, QYDT_1363_group[2]) == 0)
    {
        json_data=Obtain_battery_pack_information(data);
    }else if(strcmp(command, QYDT_1363_group[3]) == 0)
    {
        json_data=Set_time(data);
    }else
    {
/*        //extern char errlog_othermessage[100];
        json_data=Wrong(data,5);
        if (errlog_othermessage[0]!=0)
        {
            memcpy(errlog_BMSDaRen,errlog_othermessage,sizeof(errlog_BMSDaRen)-1);
        }
        */

    }
    cJSON_Delete(json);
    return json_data;


}
