From f80951fdc3f37653fca8e0c9a21cb53b4397039b Mon Sep 17 00:00:00 2001 From: cheliequan <liequanche@126.com> Date: 星期四, 12 一月 2023 21:08:35 +0800 Subject: [PATCH] 增加返回protobuf结构体是vector类型处理方法,还需要封装优化 --- util.cpp | 71 +++++++++++++++++++++-------------- 1 files changed, 43 insertions(+), 28 deletions(-) diff --git a/util.cpp b/util.cpp index 6f7b235..9eedab4 100644 --- a/util.cpp +++ b/util.cpp @@ -15,6 +15,7 @@ #include "3rdparty/bus_client/cbhomeclient.h" #include "3rdparty/bus_client/message.h" #include "proto/x86_64/sysset.pb.h" +#include <vector> using namespace protomsg; using namespace std; @@ -51,7 +52,7 @@ return json; } -extern "C" int bus_dbapi_get_json_data(const char * json, char ** pptr_data) +extern "C" int bus_dbapi_get_json_data(const char * json, char ** pptr_data, yyjson_type *ptr_type) { /*{ "success": true, @@ -118,7 +119,9 @@ // Get root["data"] //yyjson_val *msg = yyjson_obj_get(root, "data"); - yyjson_val *data = yyjson_obj_get(root, "data"); + yyjson_val *data = yyjson_obj_get(root, "data"); + + *ptr_type = yyjson_get_type(data); ptr_value = yyjson_val_write(data, YYJSON_WRITE_NOFLAG, &data_len); printf("data: %s\n", ptr_value); printf("len: %lu\n", data_len); @@ -133,42 +136,54 @@ return ret; } +bool bus_dbapi_get_topic_data(void *handle, const char* topic, crepmsg **pptr_repmsg, size_t len) +{ + const auto topicl = strlen(topic); + auto reqData = make_get_request(topic); + auto reqmsg = make_req_msg(topic, topicl, reqData, strlen(reqData)); + + if (bus_client_request(handle, reqmsg, pptr_repmsg)) { + printf("======>> bus_client_reqest [%s] get [%s]\n", topic, (*pptr_repmsg)->data); + } else { + return false; + } + + len = strlen((*pptr_repmsg)->data) + 1; + + free(reqData); +} + + bool bus_dbapi_get(void *handle, const char* topic, google::protobuf::Message& message) { - const auto topicl = strlen(topic); - - auto reqData = make_get_request(topic); - - auto reqmsg = make_req_msg(topic, topicl, reqData, strlen(reqData)); - - crepmsg *repmsg = NULL; - if (bus_client_request(handle, reqmsg, &repmsg)) { - printf("======>> bus_client_reqest [%s] get [%s]\n", topic, repmsg->data); - } else { - return false; - } + yyjson_type type = YYJSON_TYPE_OBJ; + crepmsg *repmsg = NULL; + size_t len = 0; + + bus_dbapi_get_topic_data(handle, topic, &repmsg, len); int msglen = strlen(repmsg->data); char* msgdata = (char *)malloc(msglen); memset(msgdata, 0 , msglen); - if(0 == bus_dbapi_get_json_data((const char *)repmsg->data, &msgdata)) + if(0 == bus_dbapi_get_json_data((const char *)repmsg->data, &msgdata, &type)) { - printf("======>> protomsg: %s\n", msgdata); - std::string jsonString = msgdata; - if (json_to_proto(jsonString, message)) - { - printf("======>> json_to_proto done\n"); - } - else - { - printf("======>> json_to_proto fail\n"); - return false; - } + printf("======>> protomsg: %s\n", msgdata); } + std::string jsonString = msgdata; + if (json_to_proto(jsonString, message)) + { + printf("======>> json_to_proto done\n"); + } + else + { + printf("======>> json_to_proto fail\n"); + return false; + } + free(msgdata); - free_reqmsg(reqmsg); - free(reqData); + free_reply_msg(repmsg); return true; } + -- Gitblit v1.8.0