zhangzengfei
2023-01-16 71156fd4aaa5f18ba9f95f9f08bc930a321e257c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <string.h>
 
#include "google/protobuf/descriptor.h"
#include "google/protobuf/util/json_util.h"
#include "google/protobuf/util/type_resolver.h"
#include "google/protobuf/util/type_resolver_util.h"
 
#include "google/protobuf/compiler/importer.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
 
#include "3rdparty/bus_client/cbhomeclient.h"
#include "3rdparty/bus_client/message.h"
#include "3rdparty/yyjson/yyjson.h"
#include "proto/x86_64/sysset.pb.h"
#include <vector>
 
using namespace protomsg;
using namespace std;
using google::protobuf::util::JsonStringToMessage;
 
const char *CONTENT_TYPE_JSON = "application/json";
 
bool json_to_proto(const std::string &json, google::protobuf::Message &message) {
    google::protobuf::util::JsonParseOptions options;
    options.ignore_unknown_fields = true;
 
    return JsonStringToMessage(json, &message, options).ok();
}
 
bool proto_to_json(const google::protobuf::Message &message, std::string &json) {
    google::protobuf::util::JsonPrintOptions options;
    options.add_whitespace = true;
    options.always_print_primitive_fields = true;
    options.preserve_proto_field_names = true;
 
    return MessageToJsonString(message, &json, options).ok();
}
 
char *make_get_request(const char *topic) {
    auto doc = yyjson_mut_doc_new(NULL);
    auto root = yyjson_mut_obj(doc);
    yyjson_mut_obj_add_strn(doc, root, "path", topic, strlen(topic));
    yyjson_mut_obj_add_strn(doc, root, "method", "GET", 3);
    yyjson_mut_obj_add_strn(doc, root, "contentType", CONTENT_TYPE_JSON, strlen(CONTENT_TYPE_JSON));
 
    size_t jsonl = 0;
    char *json = yyjson_mut_val_write(root, 0, &jsonl);
 
    yyjson_mut_doc_free(doc);
 
    return json;
}
 
char *make_get_request(const char *topic, std::map<string, string> &query_params) {
    yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
    yyjson_mut_val *root = yyjson_mut_obj(doc);
    yyjson_mut_obj_add_strn(doc, root, "path", topic, strlen(topic));
    yyjson_mut_obj_add_strn(doc, root, "method", "GET", 3);
 
    // 填充请求参数
    if (!query_params.empty()) {
        yyjson_mut_val *query_map = yyjson_mut_obj(doc);
 
        // std::map<string, string> t_map = *query_params;
        std::map<string, string>::iterator iter;
        for (iter = query_params.begin(); iter != query_params.end(); iter++) {
            yyjson_mut_val *_key = yyjson_mut_str(doc, iter->first.c_str());
 
            yyjson_mut_val *param = yyjson_mut_arr(doc);
            yyjson_mut_val *_val = yyjson_mut_str(doc, iter->second.c_str());
            yyjson_mut_arr_append(param, _val);
 
            yyjson_mut_obj_add(query_map, _key, param);
        }
 
        yyjson_mut_val *query_map_key = yyjson_mut_str(doc, "queryMap");
        yyjson_mut_obj_add(root, query_map_key, query_map);
    }
 
    yyjson_mut_doc_set_root(doc, root);
 
    size_t jsonl = 0;
    char *json = yyjson_mut_val_write(root, 0, &jsonl);
 
    yyjson_mut_doc_free(doc);
 
    return json;
}
 
extern "C" int bus_dbapi_get_json_data_by_key(const char *json, const char *flag_key, const char *data_key, char **pptr_data, yyjson_type *ptr_type) {
    int ret = -1;
    size_t data_len = 0;
    const char *ptr_value = NULL;
    const char *success_name = flag_key;
    const char *data_name = data_key;
    const char *msg_name = "msg";
 
    if (NULL == *pptr_data) {
        return -1;
    }
 
    // Read JSON and get root
    yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
    yyjson_val *root = yyjson_doc_get_root(doc);
 
    if (NULL != success_name) {
        // Get root["success"]
        yyjson_val *success = yyjson_obj_get(root, success_name);
 
        int success_val = yyjson_get_bool(success);
        printf("success: %d\n", success_val);
        if (!success_val) {
            yyjson_doc_free(doc);
            return -1;
        }
    }
 
    // Get root["msg"]
    //yyjson_val *msg = yyjson_obj_get(root, "msg");
    yyjson_val *msg = yyjson_obj_get(root, "msg");
    printf("msg: %s\n", yyjson_get_str(msg));
 
    // Get root["data"]
    //yyjson_val *msg = yyjson_obj_get(root, "data");
    yyjson_val *data = yyjson_obj_get(root, data_name);
 
    *ptr_type = yyjson_get_type(data);
    ptr_value = yyjson_val_write(data, YYJSON_WRITE_NOFLAG, &data_len);
    printf("%s: %s\n", data_name, ptr_value);
    printf("len: %lu\n", data_len);
 
    memcpy(*pptr_data, ptr_value, data_len);
    ret = 0;
 
    // Free the doc
    yyjson_doc_free(doc);
 
    // All functions accept NULL input, and return NULL on error.
    return ret;
}
 
extern "C" int bus_dbapi_get_json_data(const char *json, char **pptr_data, yyjson_type *ptr_type) {
    /*{
      "success": true,
      "msg": "",
      "data": {
          "alarm_ip": "192.168.20.189",
          "alarm_port": 9200,
      }
  }*/
 
    /*{
"code": 200,
"data": [
  {
      "id": "295F36D6-7489-30AC-3263-335B4660AA79",
      "name": "全天",
      "time_rule":
"[{\"day\":1,\"time_range\":[{\"start\":\"05:31\",\"end\":\"21:07\"}]},{\"day\":2,\"time_range\":[{\"start\":\"01:42\",\"end\":\"23:59\"}]},{\"day\":3,\"time_range\":[{\"start\":\"00:00\",\"end\":\"23:59\"}]},{\"day\":4,\"time_range\":[{\"start\":\"00:00\",\"end\":\"23:59\"}]},{\"day\":5,\"time_range\":[{\"start\":\"00:00\",\"end\":\"23:59\"}]},{\"day\":6,\"time_range\":[{\"start\":\"00:00\",\"end\":\"23:59\"}]},{\"day\":7,\"time_range\":[{\"start\":\"00:00\",\"end\":\"23:59\"}]}]"
  },
  {
      "id": "21FA488C-2858-F366-4587-CE0661BFAD60",
      "name": "SAAS",
      "time_rule":
"[{\"day\":1,\"time_range\":[{\"start\":\"00:00\",\"end\":\"17:04\"}]},{\"day\":2,\"time_range\":[{\"start\":\"00:00\",\"end\":\"17:25\"}]},{\"day\":3,\"time_range\":[{\"start\":\"00:00\",\"end\":\"17:22\"}]},{\"day\":4,\"time_range\":[{\"start\":\"00:00\",\"end\":\"15:33\"}]},{\"day\":5,\"time_range\":[{\"start\":\"00:00\",\"end\":\"17:40\"}]},{\"day\":6,\"time_range\":[{\"start\":\"00:00\",\"end\":\"18:04\"}]},{\"day\":7,\"time_range\":[{\"start\":\"00:00\",\"end\":\"13:51\"}]}]"
  },
  {
      "id": "37B82C8F-3BDB-A247-3683-F045190D2991",
      "name": "116",
      "time_rule":
"[{\"day\":1,\"time_range\":[{\"start\":\"03:28\",\"end\":\"23:59\"}]},{\"day\":2,\"time_range\":[{\"start\":\"04:04\",\"end\":\"23:59\"}]},{\"day\":3,\"time_range\":[{\"start\":\"04:40\",\"end\":\"23:59\"}]},{\"day\":4,\"time_range\":[{\"start\":\"04:46\",\"end\":\"23:59\"}]},{\"day\":5,\"time_range\":[{\"start\":\"04:58\",\"end\":\"23:59\"}]},{\"day\":6,\"time_range\":[{\"start\":\"06:16\",\"end\":\"23:59\"}]},{\"day\":7,\"time_range\":[{\"start\":\"05:52\",\"end\":\"23:59\"}]}]"
  }
],
"msg": "",
"success": true
}*/
    const char *success_name = "success";
    const char *data_name = "data";
 
    if (NULL == *pptr_data) {
        return -1;
    }
 
    // All functions accept NULL input, and return NULL on error.
    return bus_dbapi_get_json_data_by_key(json, success_name, data_name, pptr_data, ptr_type);
}
 
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 {
        free(reqData);
        return false;
    }
 
    len = strlen((*pptr_repmsg)->data) + 1;
 
    free(reqData);
    return true;
}
 
bool bus_dbapi_get_topic_data(void *handle, const char *topic, crepmsg **pptr_repmsg, size_t len, std::map<string, string> &params) {
    const auto topicl = strlen(topic);
    auto reqData = make_get_request(topic, params);
    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 {
        free(reqData);
        return false;
    }
 
    len = strlen((*pptr_repmsg)->data) + 1;
 
    free(reqData);
    return true;
}
 
bool bus_dbapi_get(void *handle, const char *topic, google::protobuf::Message &message) {
    yyjson_type type = YYJSON_TYPE_OBJ;
    crepmsg *repmsg = NULL;
    size_t len = 0;
    bool bRet = false;
 
    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, &type)) {
        printf("======>> protomsg: %s\n", msgdata);
    }
 
    std::string jsonString = msgdata;
    bRet = json_to_proto(jsonString, message);
    if (bRet) {
        printf("======>> json_to_proto done\n");
    } else {
        printf("======>> json_to_proto fail\n");
    }
 
    free(msgdata);
    free_reply_msg(repmsg);
 
    return bRet;
}
 
/*****************************************************************************
 函 数 名  : bus_dbapi_get_str
 功能描述  : 获取订阅主题消息字符串,返回的内存调用者需要手工调用free函数释放
 输入参数  : void *handle
             const char* topic
             char **pptr_str
 输出参数  : 无
 返 回 值  :
 调用函数  :
 被调函数  :
 
 修改历史      :
  1.日    期   : 2023年1月13日
    作    者   : cheliequan
    修改内容   : 新生成函数
 
*****************************************************************************/
bool bus_dbapi_get_str(void *handle, const char *topic, char **pptr_str) {
    yyjson_type type = YYJSON_TYPE_OBJ;
    crepmsg *repmsg = NULL;
    size_t len = 0;
    int iRet = 0;
    bool bRet = false;
 
    bus_dbapi_get_topic_data(handle, topic, &repmsg, len);
 
    int msglen = strlen(repmsg->data);
    char *msgdata = (char *)malloc(msglen);
    memset(msgdata, 0, msglen);
 
    iRet = bus_dbapi_get_json_data((const char *)repmsg->data, &msgdata, &type);
    if (0 == iRet) {
        printf("======>> protomsg: %s\n", msgdata);
        bRet = true;
    }
 
    *pptr_str = msgdata;
 
    free(msgdata);
    free_reply_msg(repmsg);
 
    return bRet;
}
 
/*****************************************************************************
 函 数 名  : get_value_by_key
 功能描述  : 获取json字符串中字段为key值的value,pptr_data内存需要用户申请和释放                 
 输入参数  : char *json        
             const char *key   
             char **pptr_data  
 输出参数  : 无
 返 回 值  : 
 调用函数  : 
 被调函数  : 
 
 修改历史      :
  1.日    期   : 2023年1月13日
    作    者   : cheliequan
    修改内容   : 新生成函数
 
*****************************************************************************/
int get_value_by_key(char *json, const char *key, char **pptr_data) {
    size_t data_len = 0;
 
    // Read JSON and get root
    yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
    yyjson_val *root = yyjson_doc_get_root(doc);
 
    // Get root[key]
    yyjson_val *val = yyjson_obj_get(root, key);
 
    char *val_str = (char *)yyjson_get_str(val);
    printf("val: %s\n", val_str);
    if (NULL == val_str) {
        yyjson_doc_free(doc);
        return -1;
    }
 
    char *ptr_value = yyjson_val_write(val, YYJSON_WRITE_NOFLAG, &data_len);
    printf("data: %s\n", ptr_value);
    printf("len: %lu\n", data_len);
 
    *pptr_data = (char *)malloc(data_len);
    memset(*pptr_data, 0, data_len);
    memcpy(*pptr_data, ptr_value, data_len);
 
    // Free the doc
    yyjson_doc_free(doc);
 
    return 0;
}