1 Star 0 Fork 0

qinxude/rk3568ui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
jsonanalysis.cpp 3.58 KB
一键复制 编辑 原始数据 按行查看 历史
qinxude 提交于 2024-09-03 09:38 . 3568ui base
#include "jsonanalysis.h"
JsonAnalysis::JsonAnalysis()
{
}
QByteArray JsonAnalysis::Analysis(JsonProType_t T,QByteArray Data)
{
QByteArray Result;
if(T == BB_CLOUD_CMD)
{
Result = BBCloudCmdAnalysis(Data);
}
return Result;
}
QByteArray JsonAnalysis::BBCloudCmdAnalysis(QByteArray Data)
{
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(Data,&err);
if(err.error != QJsonParseError::NoError){
qDebug() << err.errorString();
}
QJsonDocument ResJson;
QJsonObject Object;
if(doc.isNull())
{
BBCloudResult(Object,0,1,"Json format error");
}
else
{
//int cmd = doc["cmd"].toInt();
int cmd = GetCmd(doc.object());
if(cmd == 0) // read request
{
QJsonArray obj;
QString err;
int code = 0;
if(GetData(doc.object(),obj,err)){
code = BBCloudCmdRead(obj,err);
if(code == obj.count()){
code = 1;
}else if(code != 0){
code = 2;
}
}else {
code = 1;
}
BBCloudResult(Object,cmd,code,err);
BBCloudAddDataResult(Object,obj);
}
else if(cmd == 1) // write request
{
QJsonArray obj = doc.object().value("data").toArray();
QString err;
int code = 0;
if(GetData(doc.object(),obj,err)){
code = BBCloudCmdWrite(obj,err);
if(code == obj.count()){
code = 1;
}else if(code != 0){
code = 2;
}
}else {
code = 1;
}
BBCloudResult(Object,cmd,code,err);
BBCloudAddDataResult(Object,obj);
}
else if(cmd == -1)
{
BBCloudResult(Object,cmd,1,"command lose");
}
else
{
BBCloudResult(Object,cmd,1,"Unsupported command");
}
}
ResJson.setObject(Object);
return ResJson.toJson(QJsonDocument::Compact);
}
int JsonAnalysis::BBCloudCmdRead(QJsonArray &Data,QString &errMsg)
{
int Result = 0;
for(int i = 0;i < Data.count();i++)
{
QJsonObject ob = Data[i].toObject();
QString err;
if(m_JsonKeyToSys.Read(ob,err) == false)
{
errMsg += err;
Result++;
}
Data[i] = ob;
}
return Result;
}
int JsonAnalysis::BBCloudCmdWrite(QJsonArray Data,QString &errMsg)
{
int Result = 0;
for(QJsonArray::Iterator it = Data.begin(); it != Data.end(); it++)
{
QJsonObject ob = it[0].toObject();
QString err;
if(m_JsonKeyToSys.Write(ob,err) == false)
{
errMsg += err;
Result++;
}
}
m_JsonKeyToSys.WriteReset();
return Result;
}
void JsonAnalysis::BBCloudResult(QJsonObject &Ob,int cmd,int code,QString Msg)
{
Ob.insert("cmd",cmd);
Ob.insert("code",code);
Ob.insert("msg",Msg);
}
void JsonAnalysis::BBCloudAddDataResult(QJsonObject &Ob,QJsonArray Arr)
{
Ob.insert("data",Arr);
}
int JsonAnalysis::GetCmd(QJsonObject Ob)
{
if(Ob.contains("cmd") && Ob.value("cmd").isDouble())
{
return Ob.value("cmd").toInt();
}
return -1;
}
bool JsonAnalysis::GetData(QJsonObject Ob,QJsonArray &Arr,QString &err)
{
if(Ob.contains("data") && Ob.value("data").isArray())
{
QJsonValue V = Ob.take("data");
Arr = V.toArray();
return true;
}
err = "data array format error";
return false;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/qinxude/rk3568ui.git
[email protected]:qinxude/rk3568ui.git
qinxude
rk3568ui
rk3568ui
master

搜索帮助