#include "cJSON.h"
#include "stdint.h"
#include "stddef.h"
#include "dbug.h"
#include "djyos.h"
#include <math.h>
#include "Iboot_info.h"
#include "Modbus_PCS.h"
#include "mb.h"
#include "mb_m.h"
#include "OtherMessage.h"
char errlog_othermessage[100]={0};
char* Wrong (char* json_data,int code)//指令不存在或者参数不对
{
    if(json_data == NULL&&code == 0)
    {
        printf(" -------Wrong code NULL -------\r\n");
        sprintf(errlog_othermessage," Wrong code NULL");
        return json_data;
    }
    
    cJSON* json = cJSON_Parse(json_data);
    if (json==NULL)
    {
        printf(" -------Wrong json NULL -------\r\n");
        sprintf(errlog_othermessage," 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_othermessage," 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_othermessage," 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_othermessage," 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_othermessage," 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_othermessage," 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_othermessage," 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_othermessage," Wrong json_data NULL");
        
        return json_data;
    }  
    cJSON_Delete(QProduct_information);
    
    return json_data;
}
