Skip to content

Commit

Permalink
Merge pull request #2317 from hxy7yx/v2.9-2
Browse files Browse the repository at this point in the history
[v2.9]fix(mqtt):separate MQTT and API for JSON(read resp)
  • Loading branch information
fengzeroz authored Nov 19, 2024
2 parents 71d9375 + ac9b57f commit 0125fcf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/neuron/json/neu_json_rw.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef struct {

int neu_json_encode_read_resp(void *json_object, void *param);
int neu_json_encode_read_resp1(void *json_object, void *param);
int neu_json_encode_read_resp2(void *json_object, void *param);
int neu_json_encode_read_paginate_resp(void *json_object, void *param);

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion plugins/mqtt/mqtt_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ char *generate_upload_json(neu_plugin_t *plugin, neu_reqresp_trans_data_t *data,
neu_json_encode_read_periodic_resp,
&json_str);
} else if (MQTT_UPLOAD_FORMAT_TAGS == format) { // tags
neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp, &header,
neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp2, &header,
neu_json_encode_read_periodic_resp,
&json_str);
} else {
Expand Down
47 changes: 47 additions & 0 deletions src/parser/neu_json_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,53 @@ int neu_json_encode_read_resp1(void *json_object, void *param)
return ret;
}

int neu_json_encode_read_resp2(void *json_object, void *param)
{
int ret = 0;
neu_json_read_resp_t *resp = (neu_json_read_resp_t *) param;

void * tag_array = neu_json_array();
neu_json_read_resp_tag_t *p_tag = resp->tags;
for (int i = 0; i < resp->n_tag; i++) {
neu_json_elem_t tag_elems[2 + NEU_TAG_META_SIZE] = { 0 };

tag_elems[0].name = "name";
tag_elems[0].t = NEU_JSON_STR;
tag_elems[0].v.val_str = p_tag->name;

if (p_tag->error != 0) {
tag_elems[1].name = "error";
tag_elems[1].t = NEU_JSON_INT;
tag_elems[1].v.val_int = p_tag->error;
} else {
tag_elems[1].name = "value";
tag_elems[1].t = p_tag->t;
tag_elems[1].v = p_tag->value;
tag_elems[1].precision = p_tag->precision;
}

for (int k = 0; k < p_tag->n_meta; k++) {
tag_elems[2 + k].name = p_tag->metas[k].name;
tag_elems[2 + k].t = p_tag->metas[k].t;
tag_elems[2 + k].v = p_tag->metas[k].value;
}

tag_array =
neu_json_encode_array(tag_array, tag_elems, 2 + p_tag->n_meta);
p_tag++;
}

neu_json_elem_t resp_elems[] = { {
.name = "tags",
.t = NEU_JSON_OBJECT,
.v.val_object = tag_array,
} };
ret = neu_json_encode_field(json_object, resp_elems,
NEU_JSON_ELEM_SIZE(resp_elems));

return ret;
}

int neu_json_decode_write_req(char *buf, neu_json_write_req_t **result)
{
void *json_obj = neu_json_decode_new(buf);
Expand Down

0 comments on commit 0125fcf

Please sign in to comment.