Skip to content

Commit 2054b8f

Browse files
authored
Merge pull request #2340 from hxy7yx/main-1
mqtt: add logs for ecp-format
2 parents 38c44c4 + 7dad962 commit 2054b8f

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

include/neuron/json/neu_json_fn.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ int neu_json_encode_by_fn(void *param, neu_json_encode_fn fn, char **result);
3636
int neu_json_encode_with_mqtt(void *param, neu_json_encode_fn fn,
3737
void *mqtt_param, neu_json_encode_fn mqtt_fn,
3838
char **result);
39+
int neu_json_encode_with_mqtt_ecp(void *param, neu_json_encode_fn fn,
40+
void *mqtt_param, neu_json_encode_fn mqtt_fn,
41+
char **result);
3942

4043
#define NEU_JSON_RESPONSE_ERROR(err, func) \
4144
{ \

plugins/mqtt/mqtt.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
}
8080
},
8181
"upload_err": {
82-
"name": "Upload Tag Error Code ",
82+
"name": "Upload Tag Error Code",
8383
"name_zh": "上报点位错误码",
8484
"description": "When data tag collection reports an error, report the tag error code.",
8585
"description_zh": "点位采集报错时,上报点位错误码。",

plugins/mqtt/mqtt_handle.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ char *generate_upload_json(neu_plugin_t *plugin, neu_reqresp_trans_data_t *data,
108108
return NULL;
109109
}
110110

111+
int ret;
112+
111113
switch (format) {
112114
case MQTT_UPLOAD_FORMAT_VALUES:
113115
neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp1, &header,
@@ -120,9 +122,14 @@ char *generate_upload_json(neu_plugin_t *plugin, neu_reqresp_trans_data_t *data,
120122
&json_str);
121123
break;
122124
case MQTT_UPLOAD_FORMAT_ECP:
123-
neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp_ecp, &header,
124-
neu_json_encode_read_periodic_resp,
125-
&json_str);
125+
ret = neu_json_encode_with_mqtt_ecp(
126+
&json, neu_json_encode_read_resp_ecp, &header,
127+
neu_json_encode_read_periodic_resp, &json_str);
128+
if (ret == -2) {
129+
*skip = true;
130+
plog_warn(plugin, "driver:%s group:%s, no valid tags", data->driver,
131+
data->group);
132+
}
126133
break;
127134
default:
128135
plog_warn(plugin, "invalid upload format: %d", format);

src/parser/neu_json_fn.c

+30
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ int neu_json_encode_with_mqtt(void *param, neu_json_encode_fn fn,
7171
return ret;
7272
}
7373

74+
int neu_json_encode_with_mqtt_ecp(void *param, neu_json_encode_fn fn,
75+
void *mqtt_param, neu_json_encode_fn mqtt_fn,
76+
char **result)
77+
{
78+
void *object = neu_json_encode_new();
79+
80+
if (mqtt_fn != NULL) {
81+
if (mqtt_fn(object, mqtt_param) != 0) {
82+
neu_json_encode_free(object);
83+
return -1;
84+
}
85+
}
86+
87+
if (fn != NULL) {
88+
int encode_ret = fn(object, param);
89+
if (encode_ret == -2) {
90+
neu_json_encode_free(object);
91+
return encode_ret;
92+
}
93+
if (encode_ret != 0) {
94+
neu_json_encode_free(object);
95+
return -1;
96+
}
97+
}
98+
99+
int ret = neu_json_encode(object, result);
100+
neu_json_decode_free(object);
101+
return ret;
102+
}
103+
74104
int neu_parse_param(const char *buf, char **err_param, int n,
75105
neu_json_elem_t *ele, ...)
76106
{

src/parser/neu_json_rw.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ int neu_json_encode_read_resp2(void *json_object, void *param)
347347

348348
int neu_json_encode_read_resp_ecp(void *json_object, void *param)
349349
{
350-
int ret = 0;
351-
neu_json_read_resp_t *resp = (neu_json_read_resp_t *) param;
350+
int ret = 0;
351+
int has_data = 0;
352+
neu_json_read_resp_t *resp = (neu_json_read_resp_t *) param;
352353

353354
void * tag_array = neu_json_array();
354355
neu_json_read_resp_tag_t *p_tag = resp->tags;
@@ -380,6 +381,7 @@ int neu_json_encode_read_resp_ecp(void *json_object, void *param)
380381

381382
tag_array =
382383
neu_json_encode_array(tag_array, tag_elems, 3 + p_tag->n_meta);
384+
has_data = 1;
383385
p_tag++;
384386
}
385387

@@ -391,6 +393,10 @@ int neu_json_encode_read_resp_ecp(void *json_object, void *param)
391393
ret = neu_json_encode_field(json_object, resp_elems,
392394
NEU_JSON_ELEM_SIZE(resp_elems));
393395

396+
if (!has_data) {
397+
return -2;
398+
}
399+
394400
return ret;
395401
}
396402

0 commit comments

Comments
 (0)