From e9c80f1b1c06911d70f961d7430435c58dc18148 Mon Sep 17 00:00:00 2001 From: lixiumei Date: Thu, 31 Mar 2022 08:45:26 +0000 Subject: [PATCH] Replace tag_id with tag_name in the read api --- ft/Keyword/Neuron.py | 16 +-- ft/common.resource | 16 +-- ft/modbus_tcp_test.robot | 196 +++++++++++++++--------------- include/json/neu_json_rw.h | 2 +- plugins/mqtt/command/command.c | 9 +- plugins/mqtt/command/command.h | 4 +- plugins/mqtt/command/read_write.c | 38 ++++-- plugins/mqtt/command/read_write.h | 4 +- plugins/mqtt/mqtt.c | 12 +- plugins/restful/rw_handle.c | 25 ++-- src/parser/neu_json_rw.c | 6 +- tests/json_rw_test.cc | 13 +- 12 files changed, 185 insertions(+), 156 deletions(-) diff --git a/ft/Keyword/Neuron.py b/ft/Keyword/Neuron.py index 7535f757..4354ae85 100644 --- a/ft/Keyword/Neuron.py +++ b/ft/Keyword/Neuron.py @@ -172,10 +172,10 @@ class Read(object): def __init__(self): pass - def Compare_Tag_Value_Bool(self, tags, id, value): + def Compare_Tag_Value_Bool(self, tags, name, value): ret = -1 for tag in tags: - if tag['id'] == int(id): + if tag['name'] == name: if isinstance(value, str): if (tag['value'] == False and value.lower() == "false" or tag['value'] == True and value.lower() == "true"): @@ -185,28 +185,28 @@ class Read(object): break return ret - def Compare_Tag_Value_Int(self, tags, id, value): + def Compare_Tag_Value_Int(self, tags, name, value): ret = -1 for tag in tags: - if tag['id'] == int(id): + if tag['name'] == name: if int(tag['value']) == int(value): ret = 0 break return ret - def Compare_Tag_Value_Float(self, tags, id, value): + def Compare_Tag_Value_Float(self, tags, name, value): ret = -1 for tag in tags: - if tag['id'] == int(id): + if tag['name'] == name: if float(tag['value']) <= float(value) + 0.1 and float(tag['value']) >= float(value) - 0.1: ret = 0 break return ret - def Compare_Tag_Value_String(self, tags, id, value): + def Compare_Tag_Value_String(self, tags, name, value): ret = -1 for tag in tags: - if tag['id'] == int(id): + if tag['name'] == name: if tag['value'] == str(value): ret = 0 break diff --git a/ft/common.resource b/ft/common.resource index 752e99e1..84946e8d 100644 --- a/ft/common.resource +++ b/ft/common.resource @@ -114,30 +114,30 @@ Neuron Ready Sleep 3 Compare Tag Value As Bool - [Arguments] ${tags} ${id} ${value} + [Arguments] ${tags} ${name} ${value} - ${ret} = Compare Tag Value Bool ${tags} ${id} ${value} + ${ret} = Compare Tag Value Bool ${tags} ${name} ${value} Should Be Equal As Integers ${ret} 0 Compare Tag Value As Int - [Arguments] ${tags} ${id} ${value} + [Arguments] ${tags} ${name} ${value} - ${ret} = Compare Tag Value Int ${tags} ${id} ${value} + ${ret} = Compare Tag Value Int ${tags} ${name} ${value} Should Be Equal As Integers ${ret} 0 Compare Tag Value As Float - [Arguments] ${tags} ${id} ${value} + [Arguments] ${tags} ${name} ${value} - ${ret} = Compare Tag Value Float ${tags} ${id} ${value} + ${ret} = Compare Tag Value Float ${tags} ${name} ${value} Should Be Equal As Integers ${ret} 0 Compare Tag Value As String - [Arguments] ${tags} ${id} ${value} + [Arguments] ${tags} ${name} ${value} - ${ret} = Compare Tag Value String ${tags} ${id} ${value} + ${ret} = Compare Tag Value String ${tags} ${name} ${value} Should Be Equal As Integers ${ret} 0 diff --git a/ft/modbus_tcp_test.robot b/ft/modbus_tcp_test.robot index c4a54f2a..6b63cf76 100644 --- a/ft/modbus_tcp_test.robot +++ b/ft/modbus_tcp_test.robot @@ -81,14 +81,14 @@ Read multiple points in the coil area, the point address includes continuous and Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 0 + Compare Tag Value As Int ${res}[tags] tag1 1 + Compare Tag Value As Int ${res}[tags] tag2 0 + Compare Tag Value As Int ${res}[tags] tag3 0 + Compare Tag Value As Int ${res}[tags] tag4 0 + Compare Tag Value As Int ${res}[tags] tag5 0 + Compare Tag Value As Int ${res}[tags] tag6 0 + Compare Tag Value As Int ${res}[tags] tag7 0 + Compare Tag Value As Int ${res}[tags] tag8 0 Write Tags ${test_node_id} ${group} {"id": ${tag2_id}, "value": 1} Write Tags ${test_node_id} ${group} {"id": ${tag3_id}, "value": 1} @@ -99,14 +99,14 @@ Read multiple points in the coil area, the point address includes continuous and Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 0 + Compare Tag Value As Int ${res}[tags] tag1 1 + Compare Tag Value As Int ${res}[tags] tag2 1 + Compare Tag Value As Int ${res}[tags] tag3 1 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 1 + Compare Tag Value As Int ${res}[tags] tag6 0 + Compare Tag Value As Int ${res}[tags] tag7 1 + Compare Tag Value As Int ${res}[tags] tag8 0 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id} @@ -132,14 +132,14 @@ Read multiple points in the input area, the point address includes continuous an Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 1 + Compare Tag Value As Int ${res}[tags] tag1 0 + Compare Tag Value As Int ${res}[tags] tag2 1 + Compare Tag Value As Int ${res}[tags] tag3 1 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 0 + Compare Tag Value As Int ${res}[tags] tag6 1 + Compare Tag Value As Int ${res}[tags] tag7 1 + Compare Tag Value As Int ${res}[tags] tag8 1 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id} @@ -166,14 +166,14 @@ Read multiple points in the input reg area, the point address includes continuou Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 9 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 11 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 6946923 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 7143534 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 66651130 - Compare Tag Value As Float ${res}[tags] ${tag8_id} 1.475336887045722e-36 + Compare Tag Value As Int ${res}[tags] tag1 0 + Compare Tag Value As Int ${res}[tags] tag2 1 + Compare Tag Value As Int ${res}[tags] tag3 9 + Compare Tag Value As Int ${res}[tags] tag4 11 + Compare Tag Value As Int ${res}[tags] tag5 6946923 + Compare Tag Value As Int ${res}[tags] tag6 7143534 + Compare Tag Value As Int ${res}[tags] tag7 66651130 + Compare Tag Value As Float ${res}[tags] tag8 1.475336887045722e-36 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id} @@ -199,14 +199,14 @@ Read multiple points in the hold reg area, the point address includes continuous Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 123 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 456 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 0 - Compare Tag Value As Float ${res}[tags] ${tag8_id} 0.0 + Compare Tag Value As Int ${res}[tags] tag1 123 + Compare Tag Value As Int ${res}[tags] tag2 456 + Compare Tag Value As Int ${res}[tags] tag3 0 + Compare Tag Value As Int ${res}[tags] tag4 0 + Compare Tag Value As Int ${res}[tags] tag5 0 + Compare Tag Value As Int ${res}[tags] tag6 0 + Compare Tag Value As Int ${res}[tags] tag7 0 + Compare Tag Value As Float ${res}[tags] tag8 0.0 Write Tags ${test_node_id} ${group} {"id": ${tag1_id}, "value": 1} Write Tags ${test_node_id} ${group} {"id": ${tag2_id}, "value": 2} @@ -220,14 +220,14 @@ Read multiple points in the hold reg area, the point address includes continuous Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 2 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 10 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 12 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 6946924 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 7143536 - Compare Tag Value As Int ${res}[tags] ${tag7_id} 66651131 - Compare Tag Value As Float ${res}[tags] ${tag8_id} 11.234 + Compare Tag Value As Int ${res}[tags] tag1 1 + Compare Tag Value As Int ${res}[tags] tag2 2 + Compare Tag Value As Int ${res}[tags] tag3 10 + Compare Tag Value As Int ${res}[tags] tag4 12 + Compare Tag Value As Int ${res}[tags] tag5 6946924 + Compare Tag Value As Int ${res}[tags] tag6 7143536 + Compare Tag Value As Int ${res}[tags] tag7 66651131 + Compare Tag Value As Float ${res}[tags] tag8 11.234 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id} @@ -257,16 +257,16 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 7 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 524297 - Compare Tag Value As Float ${res}[tags] ${tag7_id} 1.010207273319475e-39 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag9_id} 0 - Compare Tag Value As Float ${res}[tags] ${tag10_id} 0.0 + Compare Tag Value As Int ${res}[tags] tag1 0 + Compare Tag Value As Int ${res}[tags] tag2 0 + Compare Tag Value As Int ${res}[tags] tag3 0 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 7 + Compare Tag Value As Int ${res}[tags] tag6 524297 + Compare Tag Value As Float ${res}[tags] tag7 1.010207273319475e-39 + Compare Tag Value As Int ${res}[tags] tag8 0 + Compare Tag Value As Int ${res}[tags] tag9 0 + Compare Tag Value As Float ${res}[tags] tag10 0.0 Write Tags ${test_node_id} ${group} {"id": ${tag1_id}, "value": 1} Write Tags ${test_node_id} ${group} {"id": ${tag2_id}, "value": 1} @@ -278,16 +278,16 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 7 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 524297 - Compare Tag Value As Float ${res}[tags] ${tag7_id} 1.010207273319475e-39 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 62225 - Compare Tag Value As Int ${res}[tags] ${tag9_id} 66651134 - Compare Tag Value As Float ${res}[tags] ${tag10_id} 11.123 + Compare Tag Value As Int ${res}[tags] tag1 1 + Compare Tag Value As Int ${res}[tags] tag2 1 + Compare Tag Value As Int ${res}[tags] tag3 1 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 7 + Compare Tag Value As Int ${res}[tags] tag6 524297 + Compare Tag Value As Float ${res}[tags] tag7 1.010207273319475e-39 + Compare Tag Value As Int ${res}[tags] tag8 62225 + Compare Tag Value As Int ${res}[tags] tag9 66651134 + Compare Tag Value As Float ${res}[tags] tag10 11.123 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id},${tag9_id},${tag10_id} @@ -317,16 +317,16 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 17 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 1114130 - Compare Tag Value As Float ${res}[tags] ${tag7_id} 1.928576247884031e-39 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 0 - Compare Tag Value As Int ${res}[tags] ${tag9_id} 0 - Compare Tag Value As Float ${res}[tags] ${tag10_id} 0.0 + Compare Tag Value As Int ${res}[tags] tag1 0 + Compare Tag Value As Int ${res}[tags] tag2 0 + Compare Tag Value As Int ${res}[tags] tag3 0 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 17 + Compare Tag Value As Int ${res}[tags] tag6 1114130 + Compare Tag Value As Float ${res}[tags] tag7 1.928576247884031e-39 + Compare Tag Value As Int ${res}[tags] tag8 0 + Compare Tag Value As Int ${res}[tags] tag9 0 + Compare Tag Value As Float ${res}[tags] tag10 0.0 Write Tags ${test_node_id} ${group} {"id": ${tag1_id}, "value": 1} Write Tags ${test_node_id} ${group} {"id": ${tag2_id}, "value": 1} @@ -338,16 +338,16 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Sleep 1s 500ms ${res} = Read Tags ${test_node_id} ${group} - Compare Tag Value As Int ${res}[tags] ${tag1_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag2_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag3_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag4_id} 1 - Compare Tag Value As Int ${res}[tags] ${tag5_id} 17 - Compare Tag Value As Int ${res}[tags] ${tag6_id} 1114130 - Compare Tag Value As Float ${res}[tags] ${tag7_id} 1.928576247884031e-39 - Compare Tag Value As Int ${res}[tags] ${tag8_id} 62226 - Compare Tag Value As Int ${res}[tags] ${tag9_id} 66651136 - Compare Tag Value As Float ${res}[tags] ${tag10_id} 11.789 + Compare Tag Value As Int ${res}[tags] tag1 1 + Compare Tag Value As Int ${res}[tags] tag2 1 + Compare Tag Value As Int ${res}[tags] tag3 1 + Compare Tag Value As Int ${res}[tags] tag4 1 + Compare Tag Value As Int ${res}[tags] tag5 17 + Compare Tag Value As Int ${res}[tags] tag6 1114130 + Compare Tag Value As Float ${res}[tags] tag7 1.928576247884031e-39 + Compare Tag Value As Int ${res}[tags] tag8 62226 + Compare Tag Value As Int ${res}[tags] tag9 66651136 + Compare Tag Value As Float ${res}[tags] tag10 11.789 [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id},${tag9_id},${tag10_id} @@ -360,15 +360,15 @@ Neuron Context Ready LOGIN - Add Node type=${${NODE_DRIVER}} name=modbus-tcp-adapter plugin_name=modbus-tcp - ${id} = Get Node ID ${NODE_DRIVER} modbus-tcp-adapter - ${mqtt_id} = Get Node ID ${NODE_MQTT} mqtt-adapter + Add Node type=${${NODE_DRIVER}} name=modbus-tcp-adapter plugin_name=modbus-tcp + ${id} = Get Node ID ${NODE_DRIVER} modbus-tcp-adapter + ${mqtt_id} = Get Node ID ${NODE_MQTT} mqtt-adapter Set Global Variable ${test_node_id} ${id} - Node Setting ${test_node_id} ${MODBUS_TCP_CONFIG} - Add Group Config ${test_node_id} ${group} 300 - Subscribe Group ${test_node_id} ${mqtt_id} ${group} + Node Setting ${test_node_id} ${MODBUS_TCP_CONFIG} + Add Group Config ${test_node_id} ${group} 300 + Subscribe Group ${test_node_id} ${mqtt_id} ${group} Stop All Processes LOGOUT @@ -395,7 +395,7 @@ Read Write A Point In The hold/coil Reg Area Should Success ${res} = Read Tags ${node_id} ${group} Check Response Status ${res} 200 #Check Error Code ${res} ${ERR_SUCCESS} - Run Keyword ${cmp} ${res}[tags] ${tag_id} 0 + Run Keyword ${cmp} ${res}[tags] ${res}[tags][0][name] 0 ${res} = Write Tags ${node_id} ${group} {"id": ${tag_id}, "value": ${value}} @@ -403,7 +403,7 @@ Read Write A Point In The hold/coil Reg Area Should Success ${res} = Read Tags ${node_id} ${group} Check Response Status ${res} 200 #Check Error Code ${res} ${ERR_SUCCESS} - Run Keyword ${cmp} ${res}[tags] ${tag_id} ${value} + Run Keyword ${cmp} ${res}[tags] ${res}[tags][0][name] ${value} [Teardown] Del Tags ${node_id} ${group} ${tag_id} @@ -430,8 +430,8 @@ Read A Point In The Input Or Input Reg Area Should Return Success Check Response Status ${res} 200 #Check Error Code ${res} ${ERR_SUCCESS} - Run Keyword ${cmp} ${res}[tags] ${tag1_id} ${value1} - Run Keyword ${cmp} ${res}[tags] ${tag2_id} ${value2} + Run Keyword ${cmp} ${res}[tags] ${res}[tags][0][name] ${value1} + Run Keyword ${cmp} ${res}[tags] ${res}[tags][1][name] ${value2} [Teardown] Del Tags ${node_id} ${group} ${tag1_id},${tag2_id} diff --git a/include/json/neu_json_rw.h b/include/json/neu_json_rw.h index c2b74ae9..b8dc144c 100644 --- a/include/json/neu_json_rw.h +++ b/include/json/neu_json_rw.h @@ -40,7 +40,7 @@ typedef struct { typedef struct { int64_t error; - int64_t id; + char * name; enum neu_json_type t; union neu_json_value value; } neu_json_read_resp_tag_t; diff --git a/plugins/mqtt/command/command.c b/plugins/mqtt/command/command.c index df10e111..fb9c705c 100644 --- a/plugins/mqtt/command/command.c +++ b/plugins/mqtt/command/command.c @@ -550,11 +550,12 @@ void command_response_handle(mqtt_response_t *response) neu_json_decode_mqtt_req_free(mqtt); } -char *command_read_once_response(neu_plugin_t * plugin, +char *command_read_once_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val) { - return command_rw_read_once_response(plugin, parse_header, resp_val); + return command_rw_read_once_response(plugin, node_id, parse_header, + resp_val); } char *command_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, @@ -566,9 +567,9 @@ char *command_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, resp_val); } -char *command_write_response(neu_plugin_t * plugin, +char *command_write_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val) { - return command_rw_write_response(plugin, parse_header, resp_val); + return command_rw_write_response(plugin, node_id, parse_header, resp_val); } \ No newline at end of file diff --git a/plugins/mqtt/command/command.h b/plugins/mqtt/command/command.h index 103e956a..ebcef617 100644 --- a/plugins/mqtt/command/command.h +++ b/plugins/mqtt/command/command.h @@ -61,14 +61,14 @@ typedef struct { } mqtt_response_t; void command_response_handle(mqtt_response_t *response); -char *command_read_once_response(neu_plugin_t * plugin, +char *command_read_once_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val); char *command_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, const char * node_name, neu_taggrp_config_t *config, neu_data_val_t * resp_val); -char *command_write_response(neu_plugin_t * plugin, +char *command_write_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val); diff --git a/plugins/mqtt/command/read_write.c b/plugins/mqtt/command/read_write.c index bd77ec25..ca138b55 100644 --- a/plugins/mqtt/command/read_write.c +++ b/plugins/mqtt/command/read_write.c @@ -48,10 +48,14 @@ int command_rw_read_once_request(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, } static int wrap_read_response_json_object(neu_fixed_array_t * array, - neu_json_read_resp_t *json) + neu_json_read_resp_t *json, + neu_plugin_t * plugin, + uint32_t node_id) { - neu_int_val_t * int_val; - neu_data_val_t *val; + neu_int_val_t * int_val; + neu_data_val_t * val; + neu_datatag_table_t *datatag_table = + neu_system_get_datatags_table(plugin, node_id); json->n_tag = array->length; if (0 == json->n_tag) { @@ -66,7 +70,9 @@ static int wrap_read_response_json_object(neu_fixed_array_t * array, val = int_val->val; neu_dtype_e type = neu_dvalue_get_value_type(val); - json->tags[i].id = int_val->key; + neu_datatag_t *tag = neu_datatag_tbl_get(datatag_table, int_val->key); + + json->tags[i].name = tag->name; json->tags[i].error = NEU_ERR_SUCCESS; switch (type) { @@ -197,7 +203,7 @@ static void clean_read_response_json_object(neu_json_read_resp_t *json) free(json->tags); } -char *command_rw_read_once_response(neu_plugin_t * plugin, +char *command_rw_read_once_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val) { @@ -218,7 +224,7 @@ char *command_rw_read_once_response(neu_plugin_t * plugin, neu_json_read_resp_t json; memset(&json, 0, sizeof(neu_json_read_resp_t)); - rc = wrap_read_response_json_object(array, &json); + rc = wrap_read_response_json_object(array, &json, plugin, node_id); if (0 != rc) { return NULL; } @@ -264,7 +270,7 @@ char *command_rw_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, header.timestamp); neu_json_read_resp_t json = { 0 }; - wrap_read_response_json_object(array, &json); + wrap_read_response_json_object(array, &json, plugin, (uint32_t) sender); neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp, &header, neu_json_encode_read_periodic_resp, &json_str); clean_read_response_json_object(&json); @@ -355,10 +361,14 @@ int command_rw_write_request(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, } static int wrap_write_response_json_object(neu_fixed_array_t * array, - neu_json_read_resp_t *json) + neu_json_read_resp_t *json, + neu_plugin_t * plugin, + uint32_t node_id) { - neu_int_val_t * int_val; - neu_data_val_t *val; + neu_int_val_t * int_val; + neu_data_val_t * val; + neu_datatag_table_t *datatag_table = + neu_system_get_datatags_table(plugin, node_id); json->n_tag = array->length; json->tags = (neu_json_read_resp_tag_t *) calloc( @@ -368,7 +378,9 @@ static int wrap_write_response_json_object(neu_fixed_array_t * array, int_val = neu_fixed_array_get(array, i); val = int_val->val; - json->tags[i].id = int_val->key; + neu_datatag_t *tag = neu_datatag_tbl_get(datatag_table, int_val->key); + + json->tags[i].name = tag->name; int32_t value; neu_dvalue_get_errorcode(val, &value); @@ -392,7 +404,7 @@ static void clean_write_response_json_object(neu_json_read_resp_t *json) free(json->tags); } -char *command_rw_write_response(neu_plugin_t * plugin, +char *command_rw_write_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val) { @@ -402,7 +414,7 @@ char *command_rw_write_response(neu_plugin_t * plugin, neu_dvalue_get_ref_array(resp_val, &array); neu_json_read_resp_t json; - wrap_write_response_json_object(array, &json); + wrap_write_response_json_object(array, &json, plugin, node_id); char *json_str = NULL; neu_json_encode_with_mqtt(&json, neu_json_encode_read_resp, parse_header, diff --git a/plugins/mqtt/command/read_write.h b/plugins/mqtt/command/read_write.h index 3d3e4742..7100446e 100644 --- a/plugins/mqtt/command/read_write.h +++ b/plugins/mqtt/command/read_write.h @@ -30,7 +30,7 @@ extern "C" { int command_rw_read_once_request(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_read_req_t *req, uint32_t req_id); -char *command_rw_read_once_response(neu_plugin_t * plugin, +char *command_rw_read_once_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val); char *command_rw_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, @@ -39,7 +39,7 @@ char *command_rw_read_periodic_response(neu_plugin_t *plugin, uint64_t sender, neu_data_val_t * resp_val); int command_rw_write_request(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_write_req_t *write_req, uint32_t req_id); -char *command_rw_write_response(neu_plugin_t * plugin, +char *command_rw_write_response(neu_plugin_t *plugin, uint32_t node_id, neu_json_mqtt_t *parse_header, neu_data_val_t * resp_val); diff --git a/plugins/mqtt/mqtt.c b/plugins/mqtt/mqtt.c index c2b50529..97b743ac 100644 --- a/plugins/mqtt/mqtt.c +++ b/plugins/mqtt/mqtt.c @@ -595,10 +595,11 @@ static int mqtt_plugin_request(neu_plugin_t *plugin, neu_request_t *req) struct context *ctx = NULL; ctx = context_list_find(plugin, req->req_id); if (NULL != ctx) { - char *json_str = command_read_once_response( - plugin, &ctx->parse_header, read_resp->data_val); - ctx->result = json_str; - ctx->ready = true; + char *json_str = command_read_once_response(plugin, req->sender_id, + &ctx->parse_header, + read_resp->data_val); + ctx->result = json_str; + ctx->ready = true; } pthread_mutex_unlock(&plugin->list_mutex); break; @@ -611,7 +612,8 @@ static int mqtt_plugin_request(neu_plugin_t *plugin, neu_request_t *req) struct context *ctx = NULL; ctx = context_list_find(plugin, req->req_id); if (NULL != ctx) { - char *json_str = command_write_response(plugin, &ctx->parse_header, + char *json_str = command_write_response(plugin, req->sender_id, + &ctx->parse_header, write_resp->data_val); ctx->result = json_str; ctx->ready = true; diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index d5b5521c..1daba397 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -13,8 +13,10 @@ #include "rw_handle.h" struct cmd_ctx { - uint32_t event_id; - nng_aio *aio; + uint32_t event_id; + nng_aio * aio; + neu_plugin_t *plugin; + uint32_t node_id; TAILQ_ENTRY(cmd_ctx) node; }; @@ -24,8 +26,9 @@ TAILQ_HEAD(, cmd_ctx) write_cmd_ctxs; static pthread_mutex_t read_ctx_mtx; static pthread_mutex_t write_ctx_mtx; -static void read_add_ctx(uint32_t event_id, nng_aio *aio); -static void write_add_ctx(uint32_t event_id, nng_aio *aio); +static void read_add_ctx(uint32_t event_id, nng_aio *aio, neu_plugin_t *plugin, + neu_node_id_t node_id); +static void write_add_ctx(uint32_t event_id, nng_aio *aio); static struct cmd_ctx *read_find_ctx(uint32_t event_id); static struct cmd_ctx *write_find_ctx(uint32_t event_id); @@ -66,7 +69,8 @@ void handle_rw_uninit() pthread_mutex_destroy(&write_ctx_mtx); } -static void read_add_ctx(uint32_t event_id, nng_aio *aio) +static void read_add_ctx(uint32_t event_id, nng_aio *aio, neu_plugin_t *plugin, + neu_node_id_t node_id) { struct cmd_ctx *ctx = calloc(1, sizeof(struct cmd_ctx)); @@ -74,6 +78,8 @@ static void read_add_ctx(uint32_t event_id, nng_aio *aio) ctx->aio = aio; ctx->event_id = event_id; + ctx->node_id = node_id; + ctx->plugin = plugin; TAILQ_INSERT_TAIL(&read_cmd_ctxs, ctx, node); @@ -144,7 +150,7 @@ void handle_read(nng_aio *aio) neu_taggrp_config_t *config = neu_system_find_group_config( plugin, req->node_id, req->group_config_name); uint32_t event_id = neu_plugin_get_event_id(plugin); - read_add_ctx(event_id, aio); + read_add_ctx(event_id, aio, plugin, req->node_id); neu_plugin_send_read_cmd(plugin, event_id, req->node_id, config); }) } @@ -174,6 +180,8 @@ void handle_read_resp(void *cmd_resp) neu_json_read_resp_t api_res = { 0 }; char * result = NULL; neu_int_val_t * iv = NULL; + neu_datatag_table_t * datatag_table = + neu_system_get_datatags_table(ctx->plugin, ctx->node_id); log_info("read resp id: %d, ctx: %p", req->req_id, ctx); assert(ctx != NULL); @@ -186,7 +194,10 @@ void handle_read_resp(void *cmd_resp) for (size_t i = 0; i < array->length; i++) { iv = (neu_int_val_t *) neu_fixed_array_get(array, i); - api_res.tags[i].id = iv->key; + neu_datatag_t *tag = + neu_datatag_tbl_get(datatag_table, (datatag_id_t) iv->key); + + api_res.tags[i].name = tag->name; api_res.tags[i].error = NEU_ERR_SUCCESS; switch (neu_dvalue_get_value_type(iv->val)) { diff --git a/src/parser/neu_json_rw.c b/src/parser/neu_json_rw.c index 90b5dfb6..4011269a 100644 --- a/src/parser/neu_json_rw.c +++ b/src/parser/neu_json_rw.c @@ -43,9 +43,9 @@ int neu_json_encode_read_resp(void *json_object, void *param) .v = p_tag->value, }, { - .name = "id", - .t = NEU_JSON_INT, - .v.val_int = p_tag->id, + .name = "name", + .t = NEU_JSON_STR, + .v.val_str = p_tag->name, }, { .name = "error", diff --git a/tests/json_rw_test.cc b/tests/json_rw_test.cc index 0ce66df3..5c6a02f3 100644 --- a/tests/json_rw_test.cc +++ b/tests/json_rw_test.cc @@ -28,9 +28,10 @@ TEST(JsonAPITest, ReadReqDecode) TEST(JsonAPITest, ReadResEncode) { - char *buf = - (char *) "{\"tags\": [{\"value\": 123, \"id\": 1, \"error\": 0}, " - "{\"value\": 11.123456789, \"id\": 2, \"error\": 0}]}"; + char *buf = (char *) "{\"tags\": [{\"value\": 123, \"name\": \"data1\", " + "\"error\": 0}, " + "{\"value\": 11.123456789, \"name\": \"data2\", " + "\"error\": 0}]}"; char * result = NULL; neu_json_read_resp_t res = { .n_tag = 2, @@ -38,11 +39,11 @@ TEST(JsonAPITest, ReadResEncode) res.tags = (neu_json_read_resp_tag_t *) calloc( 2, sizeof(neu_json_read_resp_tag_t)); - res.tags[0].id = 1; + res.tags[0].name = strdup((char *) "data1"); res.tags[0].t = NEU_JSON_INT; res.tags[0].value.val_int = 123; - res.tags[1].id = 2; + res.tags[1].name = strdup((char *) "data2"); res.tags[1].t = NEU_JSON_DOUBLE; res.tags[1].value.val_double = 11.123456789; @@ -50,6 +51,8 @@ TEST(JsonAPITest, ReadResEncode) neu_json_encode_by_fn(&res, neu_json_encode_read_resp, &result)); EXPECT_STREQ(buf, result); + free(res.tags[0].name); + free(res.tags[1].name); free(res.tags); free(result); }