//宏定义
#define LOCAL_VER_LEN 64 //局部变量长度
/*
*@author jylou
*@data 2022.12.10
*@para Json json 字符串 Ttype 值 OutValue 解析后的值
*
*/
static void wget_JsonNum(char* Json,char* Ttype,double* OutValue)
{
char *out=NULL,*end=NULL;
int type;
out = SJSON_ToObjValue(Json,Ttype,&type,&end);
if(!out)
{
//解析出错
}
if(type == SJSON_STRING)
{
out = SJSON_ParseNum(Json,OutValue);
}
}
/*
*@author jylou
*@data 2022.12.10
*@para Json JSON字符串 Value json 值 OutStr 解析后的String
*@return void
*@brief 解析JSON字符串
*/
static void wget_JsonStr(char* Json,char* Ttype,char* OutStr)
{
char *out=NULL,*end=NULL;
int type;
out = SJSON_ToObjValue(Json,Ttype,&type,&end);
if(!out)
{
//解析出错
}
if(type == SJSON_STRING)
{
out = SJSON_ParseString(out,OutStr,64);
}
}
/*
*@author jylou
*@data 2022.12.10
*@para Json JSON字符串 Value json 值 OutStr 解析后的String Ptype Json首字段 NextPtype Json次字段
*@return void
*@brief 解析JSON字符串
*/
static void wget_JsonNextStr(char* Json,char* Ptype,char* NextPtype,char* OutStr)
{
char *out=NULL,*end=NULL;
static int type;
out = SJSON_ToObjValue(Json,Ptype,&type,&end);
if(!out)
{
//解析出错
}
if(type == SJSON_OBJECT)
{
out = SJSON_ToObjValue(out,NextPtype,&type,&end);
out = SJSON_ParseString(out,OutStr,64);
}
}
/*
*@author jylou
*@data 2022.12.10
*@para Json 带解析JSON字符串 Ptype 首字段 NextPtype 次字段 OutNum
*/
static void wget_JsonNextNum(char* Json,char* Ptype,char* NextPtype,double* OutNum)
{
char *out=NULL,*end=NULL;
static int type;
out = SJSON_ToObjValue(Json,Ptype,&type,&end);
if(!out)
{
//解析出错
}
if(type == SJSON_OBJECT)
{
out = SJSON_ToObjValue(out,NextPtype,&type,&end);
out = SJSON_ParseNum(out,OutNum);
}
}
/*
*@author jylou
*@data 2022.12.10
*@brief Parse MQTTSer Para 解析Mqtt Server 参数数据
*/
void ParseMqttSerParaPro(uint8_t *RxBuf,uint16_t RxCount)
{
uint8_t i=0,j=0;
char TemBuf[LOCAL_VER_LEN]={0};
char TemVoltage[LOCAL_VER_LEN]={0};
int TempId =0;
double TemValue = 0;
if(SJSON_IsValid((char*)RxBuf)==0) //表示JSON不合法
return ;
wget_JsonStr((char*)RxBuf,"base_id",TemBuf);
TempId = atoi((const char*)TemBuf); //
if(TempId !=McuPara.ID)
return ; //说明不是自己ID
memset(TemBuf,0x00,LOCAL_VER_LEN);
wget_JsonStr((char*)RxBuf,"type",TemBuf); //获取Type 数据
if(strncmp("param",TemBuf,5)==0) //判断Type
{
memset(TemBuf,0x00,LOCAL_VER_LEN);
wget_JsonNextStr((char*)RxBuf,"data","cmd",TemBuf);
if(strncmp("Restart",TemBuf,7)==0)
{
SystemReset(); //终端模块重启
}
wget_JsonNextNum((char*)RxBuf,"data","gps-on(min)",&TemValue); //Gps 开启周期
McuPara.Gps_on =TemValue;
wget_JsonNextNum((char*)RxBuf,"data","4g-off(lt-mv)",&TemValue); //电压低于该值则不再开启GPS
McuPara.LTE_OFF_Voltage = TemValue;
wget_JsonNextNum((char*)RxBuf,"data","p-off(lt-mv)",&TemValue); //整机关机电压
McuPara.P_OFF_Voltage = TemValue;
memset(TemBuf,0x00,LOCAL_VER_LEN);
wget_JsonNextStr((char*)RxBuf,"data","4g-sleep(lt-mv,hour)",TemBuf);
for(i=0;i<(strlen(TemBuf));i++)
{
if(TemBuf[i] ==',')
{
i++;
break;
}
TemVoltage[i]= TemBuf[i];
}
j=0;
memset(TemVoltage,0x00,LOCAL_VER_LEN);
for(;i<(strlen(TemBuf));i++)
{
TemVoltage[j++]= TemBuf[i];
}
WriteParaToEeprom(PARA_START_ADDR,(uint8_t*)&McuPara,sizeof(McuParaStru));
}
}
CJson 库下载路径
https://download.youkuaiyun.com/download/qq_35257512/85963225?spm=1001.2014.3001.5503